Skip to content

Commit b80f629

Browse files
committed
Merge branch 'jk/war-on-git-path'
While handy, "git_path()" is a dangerous function to use as a callsite that uses it safely one day can be broken by changes to other code that calls it. Reduction of its use continues. * jk/war-on-git-path: am: drop "dir" parameter from am_state_init replace strbuf_addstr(git_path()) with git_path_buf() replace xstrdup(git_path(...)) with git_pathdup(...) use git_path_* helper functions branch: add edit_description() helper bisect: add git_path_bisect_terms helper
2 parents 6cbc478 + 16d2676 commit b80f629

File tree

10 files changed

+28
-30
lines changed

10 files changed

+28
-30
lines changed

bisect.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@ static int read_bisect_refs(void)
432432

433433
static GIT_PATH_FUNC(git_path_bisect_names, "BISECT_NAMES")
434434
static GIT_PATH_FUNC(git_path_bisect_expected_rev, "BISECT_EXPECTED_REV")
435+
static GIT_PATH_FUNC(git_path_bisect_terms, "BISECT_TERMS")
435436

436437
static void read_bisect_paths(struct argv_array *array)
437438
{
@@ -906,7 +907,7 @@ static void show_diff_tree(const char *prefix, struct commit *commit)
906907
void read_bisect_terms(const char **read_bad, const char **read_good)
907908
{
908909
struct strbuf str = STRBUF_INIT;
909-
const char *filename = git_path("BISECT_TERMS");
910+
const char *filename = git_path_bisect_terms();
910911
FILE *fp = fopen(filename, "r");
911912

912913
if (!fp) {

builtin/am.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,17 +134,15 @@ struct am_state {
134134
};
135135

136136
/**
137-
* Initializes am_state with the default values. The state directory is set to
138-
* dir.
137+
* Initializes am_state with the default values.
139138
*/
140-
static void am_state_init(struct am_state *state, const char *dir)
139+
static void am_state_init(struct am_state *state)
141140
{
142141
int gpgsign;
143142

144143
memset(state, 0, sizeof(*state));
145144

146-
assert(dir);
147-
state->dir = xstrdup(dir);
145+
state->dir = git_pathdup("rebase-apply");
148146

149147
state->prec = 4;
150148

@@ -2323,7 +2321,7 @@ int cmd_am(int argc, const char **argv, const char *prefix)
23232321

23242322
git_config(git_am_config, NULL);
23252323

2326-
am_state_init(&state, git_path("rebase-apply"));
2324+
am_state_init(&state);
23272325

23282326
in_progress = am_in_progress(&state);
23292327
if (in_progress)

builtin/branch.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ static void rename_branch(const char *oldname, const char *newname, int force)
504504
strbuf_release(&newsection);
505505
}
506506

507-
static const char edit_description[] = "BRANCH_DESCRIPTION";
507+
static GIT_PATH_FUNC(edit_description, "EDIT_DESCRIPTION")
508508

509509
static int edit_branch_description(const char *branch_name)
510510
{
@@ -519,9 +519,9 @@ static int edit_branch_description(const char *branch_name)
519519
" %s\n"
520520
"Lines starting with '%c' will be stripped.\n"),
521521
branch_name, comment_line_char);
522-
write_file_buf(git_path(edit_description), buf.buf, buf.len);
522+
write_file_buf(edit_description(), buf.buf, buf.len);
523523
strbuf_reset(&buf);
524-
if (launch_editor(git_path(edit_description), &buf, NULL)) {
524+
if (launch_editor(edit_description(), &buf, NULL)) {
525525
strbuf_release(&buf);
526526
return -1;
527527
}

builtin/commit.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -821,9 +821,9 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
821821
"If this is not correct, please remove the file\n"
822822
" %s\n"
823823
"and try again.\n"),
824-
git_path(whence == FROM_MERGE
825-
? "MERGE_HEAD"
826-
: "CHERRY_PICK_HEAD"));
824+
whence == FROM_MERGE ?
825+
git_path_merge_head() :
826+
git_path_cherry_pick_head());
827827
}
828828

829829
fprintf(s->fp, "\n");

builtin/config.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -600,8 +600,9 @@ int cmd_config(int argc, const char **argv, const char *prefix)
600600
if (given_config_source.blob)
601601
die("editing blobs is not supported");
602602
git_config(git_default_config, NULL);
603-
config_file = xstrdup(given_config_source.file ?
604-
given_config_source.file : git_path("config"));
603+
config_file = given_config_source.file ?
604+
xstrdup(given_config_source.file) :
605+
git_pathdup("config");
605606
if (use_global_config) {
606607
int fd = open(config_file, O_CREAT | O_EXCL | O_WRONLY, 0666);
607608
if (fd >= 0) {

builtin/pull.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ static int git_pull_config(const char *var, const char *value, void *cb)
332332
*/
333333
static void get_merge_heads(struct oid_array *merge_heads)
334334
{
335-
const char *filename = git_path("FETCH_HEAD");
335+
const char *filename = git_path_fetch_head();
336336
FILE *fp;
337337
struct strbuf sb = STRBUF_INIT;
338338
struct object_id oid;
@@ -791,7 +791,7 @@ int cmd_pull(int argc, const char **argv, const char *prefix)
791791
if (read_cache_unmerged())
792792
die_resolve_conflict("pull");
793793

794-
if (file_exists(git_path("MERGE_HEAD")))
794+
if (file_exists(git_path_merge_head()))
795795
die_conclude_merge();
796796

797797
if (get_oid("HEAD", &orig_head))

builtin/worktree.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,7 @@ static void prune_worktrees(void)
106106
printf("%s\n", reason.buf);
107107
if (show_only)
108108
continue;
109-
strbuf_reset(&path);
110-
strbuf_addstr(&path, git_path("worktrees/%s", d->d_name));
109+
git_path_buf(&path, "worktrees/%s", d->d_name);
111110
ret = remove_dir_recursively(&path, 0);
112111
if (ret < 0 && errno == ENOTDIR)
113112
ret = unlink(path.buf);
@@ -215,8 +214,7 @@ static int add_worktree(const char *path, const char *refname,
215214
}
216215

217216
name = worktree_basename(path, &len);
218-
strbuf_addstr(&sb_repo,
219-
git_path("worktrees/%.*s", (int)(path + len - name), name));
217+
git_path_buf(&sb_repo, "worktrees/%.*s", (int)(path + len - name), name);
220218
len = sb_repo.len;
221219
if (safe_create_leading_directories_const(sb_repo.buf))
222220
die_errno(_("could not create leading directories of '%s'"),

fast-import.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3202,7 +3202,7 @@ static char* make_fast_import_path(const char *path)
32023202
{
32033203
if (!relative_marks_paths || is_absolute_path(path))
32043204
return xstrdup(path);
3205-
return xstrdup(git_path("info/fast-import/%s", path));
3205+
return git_pathdup("info/fast-import/%s", path);
32063206
}
32073207

32083208
static void option_import_marks(const char *marks,

notes-merge.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@ int notes_merge_commit(struct notes_merge_options *o,
676676
const char *msg = strstr(buffer, "\n\n");
677677
int baselen;
678678

679-
strbuf_addstr(&path, git_path(NOTES_MERGE_WORKTREE));
679+
git_path_buf(&path, NOTES_MERGE_WORKTREE);
680680
if (o->verbosity >= 3)
681681
printf("Committing notes in notes merge worktree at %s\n",
682682
path.buf);
@@ -741,7 +741,7 @@ int notes_merge_abort(struct notes_merge_options *o)
741741
struct strbuf buf = STRBUF_INIT;
742742
int ret;
743743

744-
strbuf_addstr(&buf, git_path(NOTES_MERGE_WORKTREE));
744+
git_path_buf(&buf, NOTES_MERGE_WORKTREE);
745745
if (o->verbosity >= 3)
746746
printf("Removing notes merge worktree at %s/*\n", buf.buf);
747747
ret = remove_dir_recursively(&buf, REMOVE_DIR_KEEP_TOPLEVEL);

sequencer.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,12 +1065,12 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,
10651065
flags |= CLEANUP_MSG;
10661066
msg_file = rebase_path_fixup_msg();
10671067
} else {
1068-
const char *dest = git_path("SQUASH_MSG");
1068+
const char *dest = git_path_squash_msg();
10691069
unlink(dest);
10701070
if (copy_file(dest, rebase_path_squash_msg(), 0666))
10711071
return error(_("could not rename '%s' to '%s'"),
10721072
rebase_path_squash_msg(), dest);
1073-
unlink(git_path("MERGE_MSG"));
1073+
unlink(git_path_merge_msg());
10741074
msg_file = dest;
10751075
flags |= EDIT_MSG;
10761076
}
@@ -1820,10 +1820,10 @@ static int error_failed_squash(struct commit *commit,
18201820
return error(_("could not rename '%s' to '%s'"),
18211821
rebase_path_squash_msg(), rebase_path_message());
18221822
unlink(rebase_path_fixup_msg());
1823-
unlink(git_path("MERGE_MSG"));
1824-
if (copy_file(git_path("MERGE_MSG"), rebase_path_message(), 0666))
1823+
unlink(git_path_merge_msg());
1824+
if (copy_file(git_path_merge_msg(), rebase_path_message(), 0666))
18251825
return error(_("could not copy '%s' to '%s'"),
1826-
rebase_path_message(), git_path("MERGE_MSG"));
1826+
rebase_path_message(), git_path_merge_msg());
18271827
return error_with_patch(commit, subject, subject_len, opts, 1, 0);
18281828
}
18291829

@@ -2167,7 +2167,7 @@ static int commit_staged_changes(struct replay_opts *opts)
21672167
if (has_unstaged_changes(1))
21682168
return error(_("cannot rebase: You have unstaged changes."));
21692169
if (!has_uncommitted_changes(0)) {
2170-
const char *cherry_pick_head = git_path("CHERRY_PICK_HEAD");
2170+
const char *cherry_pick_head = git_path_cherry_pick_head();
21712171

21722172
if (file_exists(cherry_pick_head) && unlink(cherry_pick_head))
21732173
return error(_("could not remove CHERRY_PICK_HEAD"));

0 commit comments

Comments
 (0)