Skip to content

Commit 9725c8d

Browse files
avargitster
authored andcommitted
built-ins: trust the "prefix" from run_builtin()
Change code in "builtin/grep.c" and "builtin/ls-tree.c" to trust the "prefix" passed from "run_builtin()". The "prefix" we get from setup.c is either going to be NULL or a string of length >0, never "". So we can drop the "prefix && *prefix" checks added for "builtin/grep.c" in 0d042fe (git-grep: show pathnames relative to the current directory, 2006-08-11), and for "builtin/ls-tree.c" in a69dd58 (ls-tree: chomp leading directories when run from a subdirectory, 2005-12-23). As seen in code in revision.c that was added in cd676a5 (diff --relative: output paths as relative to the current subdirectory, 2008-02-12) we already have existing code that does away with this assertion. This makes it easier to reason about a subsequent change to the "prefix_length" code in grep.c in a subsequent commit, and since we're going to the trouble of doing that let's leave behind an assert() to promise this to any future callers. For "builtin/grep.c" it would be painful to pass the "prefix" down the callchain of: cmd_grep -> grep_tree -> grep_submodule -> grep_cache -> grep_oid -> grep_source_name So for the code that needs it in grep_source_name() let's add a "grep_prefix" variable similar to the existing "ls_tree_prefix". While at it let's move the code in cmd_ls_tree() around so that we assign to the "ls_tree_prefix" right after declaring the variables, and stop assigning to "prefix". We only subsequently used that variable later in the function after clobbering it. Let's just use our own "grep_prefix" instead. Let's also add an assert() in git.c, so that we'll make this promise about the "prefix" to any current and future callers, as well as to any readers of the code. Code history: * The strlen() in "grep.c" hasn't been used since 493b7a0 (grep: accept relative paths outside current working directory, 2009-09-05). When that code was added in 0d042fe (git-grep: show pathnames relative to the current directory, 2006-08-11) we used the length. But since 493b7a0 we haven't used it for anything except a boolean check that we could have done on the "prefix" member itself. Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a5c0ed3 commit 9725c8d

File tree

6 files changed

+13
-13
lines changed

6 files changed

+13
-13
lines changed

builtin/grep.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#include "object-store.h"
2727
#include "packfile.h"
2828

29+
static const char *grep_prefix;
30+
2931
static char const * const grep_usage[] = {
3032
N_("git grep [<options>] [-e] <pattern> [<rev>...] [[--] <path>...]"),
3133
NULL
@@ -315,11 +317,11 @@ static void grep_source_name(struct grep_opt *opt, const char *filename,
315317
strbuf_reset(out);
316318

317319
if (opt->null_following_name) {
318-
if (opt->relative && opt->prefix_length) {
320+
if (opt->relative && grep_prefix) {
319321
struct strbuf rel_buf = STRBUF_INIT;
320322
const char *rel_name =
321323
relative_path(filename + tree_name_len,
322-
opt->prefix, &rel_buf);
324+
grep_prefix, &rel_buf);
323325

324326
if (tree_name_len)
325327
strbuf_add(out, filename, tree_name_len);
@@ -332,8 +334,8 @@ static void grep_source_name(struct grep_opt *opt, const char *filename,
332334
return;
333335
}
334336

335-
if (opt->relative && opt->prefix_length)
336-
quote_path(filename + tree_name_len, opt->prefix, out, 0);
337+
if (opt->relative && grep_prefix)
338+
quote_path(filename + tree_name_len, grep_prefix, out, 0);
337339
else
338340
quote_c_style(filename + tree_name_len, out, NULL, 0);
339341

@@ -962,9 +964,10 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
962964
PARSE_OPT_NOCOMPLETE),
963965
OPT_END()
964966
};
967+
grep_prefix = prefix;
965968

966969
git_config(grep_cmd_config, NULL);
967-
grep_init(&opt, the_repository, prefix);
970+
grep_init(&opt, the_repository);
968971

969972
/*
970973
* If there is no -- then the paths must exist in the working

builtin/ls-tree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ int cmd_ls_tree(int argc, const char **argv, const char *prefix)
150150

151151
git_config(git_default_config, NULL);
152152
ls_tree_prefix = prefix;
153-
if (prefix && *prefix)
153+
if (prefix)
154154
chomp_prefix = strlen(prefix);
155155

156156
argc = parse_options(argc, argv, prefix, ls_tree_options,

git.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
436436
} else {
437437
prefix = NULL;
438438
}
439+
assert(!prefix || *prefix);
439440
precompose_argv_prefix(argc, argv, NULL);
440441
if (use_pager == -1 && run_setup &&
441442
!(p->option & DELAY_PAGER_CONFIG))

grep.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,11 @@ int grep_config(const char *var, const char *value, void *cb)
139139
* default values from the template we read the configuration
140140
* information in an earlier call to git_config(grep_config).
141141
*/
142-
void grep_init(struct grep_opt *opt, struct repository *repo, const char *prefix)
142+
void grep_init(struct grep_opt *opt, struct repository *repo)
143143
{
144144
*opt = grep_defaults;
145145

146146
opt->repo = repo;
147-
opt->prefix = prefix;
148-
opt->prefix_length = (prefix && *prefix) ? strlen(prefix) : 0;
149147
opt->pattern_tail = &opt->pattern_list;
150148
opt->header_tail = &opt->header_list;
151149
}

grep.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,6 @@ struct grep_opt {
134134
*/
135135
struct repository *repo;
136136

137-
const char *prefix;
138-
int prefix_length;
139137
int linenum;
140138
int columnnum;
141139
int invert;
@@ -182,7 +180,7 @@ struct grep_opt {
182180
};
183181

184182
int grep_config(const char *var, const char *value, void *);
185-
void grep_init(struct grep_opt *, struct repository *repo, const char *prefix);
183+
void grep_init(struct grep_opt *, struct repository *repo);
186184
void grep_commit_pattern_type(enum grep_pattern_type, struct grep_opt *opt);
187185

188186
void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen, const char *origin, int no, enum grep_pat_token t);

revision.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1838,7 +1838,7 @@ void repo_init_revisions(struct repository *r,
18381838
revs->commit_format = CMIT_FMT_DEFAULT;
18391839
revs->expand_tabs_in_log_default = 8;
18401840

1841-
grep_init(&revs->grep_filter, revs->repo, prefix);
1841+
grep_init(&revs->grep_filter, revs->repo);
18421842
revs->grep_filter.status_only = 1;
18431843

18441844
repo_diff_setup(revs->repo, &revs->diffopt);

0 commit comments

Comments
 (0)