From 9d5b9577f7c3a60a830714a93644a936369382a2 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sun, 8 Sep 2024 14:32:42 -0400 Subject: Change default behavior in git repository to be searching through repo files when no argument are specified. -G is now just for `git ls-files` patterns --- bp.c | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/bp.c b/bp.c index 8da3036..f4243e1 100644 --- a/bp.c +++ b/bp.c @@ -36,21 +36,22 @@ static const char *usage = ( "Usage:\n" " "BP_NAME" [flags] [...]\n\n" "Flags:\n" - " -h --help print the usage and quit\n" - " -v --verbose print verbose debugging info\n" - " -e --explain explain the matches\n" + " -A --context-after set number of lines of context to print after the match\n" + " -B --context-before set number of lines of context to print before the match\n" + " -C --context set number of lines of context to print before and after the match\n" + " -G --git in a git repository, treat filenames as patterns for `git ls-files`\n" + " -I --inplace modify a file in-place\n" " -c --case use case sensitivity\n" + " -e --explain explain the matches\n" + " -f --format fancy|plain|bare|file:line set the output format\n" + " -g --grammar use the specified file as a grammar\n" + " -h --help print the usage and quit\n" " -i --ignore-case preform matching case-insensitively\n" - " -I --inplace modify a file in-place\n" " -l --list-files list filenames only\n" - " -w --word find words matching the given string pattern\n" " -r --replace replace the input pattern with the given replacement\n" " -s --skip skip over the given pattern when looking for matches\n" - " -B --context-before set number of lines of context to print before the match\n" - " -A --context-after set number of lines of context to print after the match\n" - " -C --context set number of lines of context to print before and after the match\n" - " -f --format fancy|plain|bare|file:line set the output format\n" - " -g --grammar use the specified file as a grammar"); + " -v --verbose print verbose debugging info\n" + " -w --word find words matching the given string pattern\n"); // Used as a heuristic to check if a file is binary or text: #define CHECK_FIRST_N_BYTES 256 @@ -563,7 +564,7 @@ int main(int argc, char *argv[]) ++argv; break; } else if (BOOLFLAG("-h") || BOOLFLAG("--help")) { - printf("%s\n\n%s\n", description, usage); + printf("%s\n\n%s", description, usage); exit(EXIT_SUCCESS); } else if (BOOLFLAG("-v") || BOOLFLAG("--verbose")) { options.verbose = true; @@ -666,6 +667,11 @@ int main(int argc, char *argv[]) if (options.verbose) printf("Matching pattern: %P\n", pattern); + // Default to git mode if there's a .git directory and no files were specified: + struct stat gitdir; + if (argc == 0 && stat(".git", &gitdir) == 0 && S_ISDIR(gitdir.st_mode)) + options.git_mode = true; + int found = 0; if (options.git_mode) { // Get the list of files from `git --ls-files ...` found = process_git_files(pattern, defs, argc, argv); -- cgit v1.2.3