ani: add functions to locate .pijul

laumann
May 15, 2023, 1:30 PM
ZPNA2D42RFGGC45PLDYDGQUDPC7D4NBEH2HD4Y2L5P2CK3R52FAQC

Dependencies

  • [2] 2U7P5SFQ Change struct names "struct foo -> typedef struct Foo"
  • [3] XJ2PEH74 add meson.build
  • [4] FB67XX5E add argument parsing setup
  • [5] AHIXA5ZE Get rid of all warnings, rework init code. How do I do multiline messages?
  • [6] QMTJBCUJ ani: remove comment
  • [7] XTKRT6OQ format the codebase
  • [8] Q7TKZCJP Add initial support for reading the offsets from a (fixed) change
  • [9] B3XLVPNC Add ani.c and initial Makefile
  • [*] Y26WT3ZF change: decode message, description and timestamp
  • [*] PEUS54XQ

Change contents

  • edit in scaffold.h at line 7
    [11.164]
    [11.164]
    char *xstrdup(const char *str);
  • edit in scaffold.c at line 55
    [11.522]
    char *
    xstrdup(const char *str)
    {
    char *ret = strdup(str);
    if (!ret)
    die("out of memory, strdup failed");
    return ret;
    }
  • edit in meson.build at line 27
    [3.318]
    [3.318]
    'dir.c',
  • replacement in init.c at line 32
    [4.51][4.51:89]()
    cmd_init(int argc, const char **argv)
    [4.51]
    [4.701]
    cmd_init(int argc, const char **argv, const char *_ignore)
  • file addition: dir.h (----------)
    [12.1]
    #ifndef ANI_DIR_H
    #define ANI_DIR_H
    /* Routines for working with directories */
    int has_dotpijul(const char *); /* check if given directory has a .pijul */
    /**
    * Search for a .pijul folder. Starts from the current working
    * directory, and walks up the parent directories until a directory is
    * found. If found, returns the absolute path (without "/.pijul" as an
    * allocated string. Otherwise, returns NULL
    */
    char *find_dotpijul(void);
    #endif
  • file addition: dir.c (----------)
    [12.1]
    #include <unistd.h>
    #include <stdio.h>
    #include <sys/types.h>
    #include <dirent.h>
    #include <string.h>
    #include "scaffold.h"
    #include "dir.h"
    int
    has_dotpijul(const char *path)
    {
    struct dirent *d;
    DIR *dir;
    int ret;
    ret = 0;
    if (!(dir = opendir(path)))
    return ret;
    while ((d = readdir(dir)) != NULL) {
    if (!strncmp(d->d_name, ".pijul", 6)) {
    ret = 1;
    break;
    }
    }
    closedir(dir);
    return ret;
    }
    #ifndef PATH_MAX
    #define PATH_MAX 4096
    #endif
    char *
    find_dotpijul()
    {
    /**
    * idea is this: set a string to current directory, check if
    * ".pijul" is present - if not, cd("..") and look again.
    */
    char path[PATH_MAX] = { 0 };
    char *s = NULL;
    int found = 0;
    if (!getcwd(path, PATH_MAX))
    die("unable to get current working directory");
    while (!(found = has_dotpijul(path)) && path != s) {
    s = strrchr(path, '/');
    *s = '\0';
    }
    if (!found)
    return NULL;
    s = xstrdup(path);
    return s;
    }
  • replacement in change.c at line 654
    [4.2236][4.2236:2270]()
    cmd_change(int argc, char **argv)
    [4.2236]
    [4.3043]
    cmd_change(int argc, char **argv, const char *pijul_dir)
  • edit in ani.c at line 7
    [4.1150]
    [4.1150]
    #include "dir.h"
  • edit in ani.c at line 10
    [4.51]
    [4.51]
    #define NEED_WORK_TREE (1 << 0)
  • replacement in ani.c at line 22
    [4.1205][4.1205:1237]()
    int (*fn)(int, const char **);
    [4.1205]
    [2.4549]
    int (*fn)(int, const char **, const char *);
    unsigned int flags;
  • replacement in ani.c at line 27
    [2.4583][4.1274:1297](),[4.1274][4.1274:1297](),[4.1297][4.3227:3254]()
    { "init", cmd_init },
    { "change", cmd_change },
    [2.4583]
    [4.1297]
    { "init", cmd_init, 0 },
    { "change", cmd_change, NEED_WORK_TREE },
  • edit in ani.c at line 58
    [4.350]
    [2.4608]
    char *pijul_dir = NULL;
  • replacement in ani.c at line 71
    [4.1788][4.1788:1823]()
    return (command->fn)(argc, argv);
    [4.1779]
    [4.1823]
    if (command->flags & NEED_WORK_TREE) {
    pijul_dir = find_dotpijul();
    if (!pijul_dir)
    die("no Pijul repository found");
    }
    return (command->fn)(argc, argv, pijul_dir);