From bb36535f1a932c7749e58f2abb1cc88364200258 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Sat, 13 Jan 2024 01:06:03 +0100 Subject: Support glob arguments to git-grab --- grab.1 | 26 ++++++++++++++++++++++---- grab.c | 25 +++++++++++++++++++------ 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/grab.1 b/grab.1 index 8ee5b28..8bd2e9e 100644 --- a/grab.1 +++ b/grab.1 @@ -15,6 +15,7 @@ .Nm "git grab" .Op Fl nz .Ar pattern +.Op Ar glob ... .Nm "git grab" .Fl h .Sh DESCRIPTION @@ -39,10 +40,17 @@ The utility is identical to the .Nm utility in all ways except for two exceptions. -The first is that files cannot be passed as command-line arguments, -.Nm git-grab -instead will read all the files specified by an invocation of -.Xr git-ls-files 1 . +The first is that if no files +.Pq globs in this case to be precise +are specified, +input is not read from the standard-input but instead all files returned +by an invocation of +.Xr git-ls-files 1 +are processed. +If the user provides one or more globs, +only the files returned by +.Xr git-ls-files 1 +that match one or more of the given globs will be processed. Secondly, the .Fl f option is not available; @@ -156,6 +164,16 @@ List all your systems CPU flags, sorted and without duplicates: Search for a pattern in multiple files without printing filenames: .Pp .Dl $ cat file1 file2 file3 | grab 'x/pattern/' +.Pp +Search for usages of an +.Ql +Vue component — +but only those which are being passed a +.Ql placeholder +property — +searching all files in the current git-repository: +.Pp +.Dl $ git grab 'x/]+>/ g/\ebplaceholder\eb/' '*.vue' .Sh SEE ALSO .Xr git-ls-files 1 , .Xr grep 1 diff --git a/grab.c b/grab.c index 88a7a4a..96b5d9f 100644 --- a/grab.c +++ b/grab.c @@ -64,7 +64,7 @@ static regex_t mkregex(char *, size_t); static struct ops comppat(char *); static char *env_or_default(const char *, const char *); #if GIT_GRAB -static FILE *getfstream(void); +static FILE *getfstream(int n, char *v[n]); #endif static bool xisspace(char); @@ -96,7 +96,7 @@ usage(const char *s) { fprintf(stderr, #if GIT_GRAB - "Usage: %s [-nz] pattern\n" + "Usage: %s [-nz] pattern [glob ...]\n" #else "Usage: %s [-fnz] pattern [file ...]\n" #endif @@ -164,7 +164,7 @@ main(int argc, char **argv) ops = comppat(argv[0]); #if GIT_GRAB - if ((flist = getfstream()) == nullptr) + if ((flist = getfstream(argc - 1, argv + 1)) == nullptr) die("getfstream"); while ((nr = getdelim(&entry, &len, '\0', flist)) > 0) { FILE *fp; @@ -425,7 +425,7 @@ mkregex(char *s, size_t n) #if GIT_GRAB FILE * -getfstream(void) +getfstream(int argc, char *argv[argc]) { pid_t pid; int fds[2]; @@ -440,11 +440,24 @@ getfstream(void) switch (pid = fork()) { case -1: die("fork"); - case 0: + case 0:; + size_t len = argc + 5; + char **args; + close(fds[FD_R]); if (dup2(fds[FD_W], STDOUT_FILENO) == -1) die("dup2"); - execlp("git", "git", "ls-files", "-z", nullptr); + + if (!(args = malloc(len * sizeof(char *)))) + die("malloc"); + args[0] = "git"; + args[1] = "ls-files"; + args[2] = "-z"; + args[3] = "--"; + memcpy(args + 4, argv, argc * sizeof(char *)); + args[len - 1] = nullptr; + + execvp("git", args); die("execvp: git ls-files -z"); } -- cgit v1.2.3