Skip to content

Commit 8c9155e

Browse files
committed
Merge branch 'jk/git-path'
git_path() and mkpath() are handy helper functions but it is easy to misuse, as the callers need to be careful to keep the number of active results below 4. Their uses have been reduced. * jk/git-path: memoize common git-path "constant" files get_repo_path: refactor path-allocation find_hook: keep our own static buffer refs.c: remove_empty_directories can take a strbuf refs.c: avoid git_path assignment in lock_ref_sha1_basic refs.c: avoid repeated git_path calls in rename_tmp_log refs.c: simplify strbufs in reflog setup and writing path.c: drop git_path_submodule refs.c: remove extra git_path calls from read_loose_refs remote.c: drop extraneous local variable from migrate_file prefer mkpathdup to mkpath in assignments prefer git_pathdup to git_path in some possibly-dangerous cases add_to_alternates_file: don't add duplicate entries t5700: modernize style cache.h: complete set of git_path_submodule helpers cache.h: clarify documentation for git_path, et al
2 parents 51a22ce + f932729 commit 8c9155e

30 files changed

+465
-363
lines changed

attr.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,8 @@ static int git_attr_system(void)
490490
return !git_env_bool("GIT_ATTR_NOSYSTEM", 0);
491491
}
492492

493+
static GIT_PATH_FUNC(git_path_info_attributes, INFOATTRIBUTES_FILE)
494+
493495
static void bootstrap_attr_stack(void)
494496
{
495497
struct attr_stack *elem;
@@ -531,7 +533,7 @@ static void bootstrap_attr_stack(void)
531533
debug_push(elem);
532534
}
533535

534-
elem = read_attr_from_file(git_path(INFOATTRIBUTES_FILE), 1);
536+
elem = read_attr_from_file(git_path_info_attributes(), 1);
535537
if (!elem)
536538
elem = xcalloc(1, sizeof(*elem));
537539
elem->origin = NULL;

bisect.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,10 +429,13 @@ static int read_bisect_refs(void)
429429
return for_each_ref_in("refs/bisect/", register_ref, NULL);
430430
}
431431

432+
static GIT_PATH_FUNC(git_path_bisect_names, "BISECT_NAMES")
433+
static GIT_PATH_FUNC(git_path_bisect_expected_rev, "BISECT_EXPECTED_REV")
434+
432435
static void read_bisect_paths(struct argv_array *array)
433436
{
434437
struct strbuf str = STRBUF_INIT;
435-
const char *filename = git_path("BISECT_NAMES");
438+
const char *filename = git_path_bisect_names();
436439
FILE *fp = fopen(filename, "r");
437440

438441
if (!fp)
@@ -653,7 +656,7 @@ static void exit_if_skipped_commits(struct commit_list *tried,
653656

654657
static int is_expected_rev(const struct object_id *oid)
655658
{
656-
const char *filename = git_path("BISECT_EXPECTED_REV");
659+
const char *filename = git_path_bisect_expected_rev();
657660
struct stat st;
658661
struct strbuf str = STRBUF_INIT;
659662
FILE *fp;

branch.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -302,13 +302,13 @@ void create_branch(const char *head,
302302

303303
void remove_branch_state(void)
304304
{
305-
unlink(git_path("CHERRY_PICK_HEAD"));
306-
unlink(git_path("REVERT_HEAD"));
307-
unlink(git_path("MERGE_HEAD"));
308-
unlink(git_path("MERGE_RR"));
309-
unlink(git_path("MERGE_MSG"));
310-
unlink(git_path("MERGE_MODE"));
311-
unlink(git_path("SQUASH_MSG"));
305+
unlink(git_path_cherry_pick_head());
306+
unlink(git_path_revert_head());
307+
unlink(git_path_merge_head());
308+
unlink(git_path_merge_rr());
309+
unlink(git_path_merge_msg());
310+
unlink(git_path_merge_mode());
311+
unlink(git_path_squash_msg());
312312
}
313313

314314
static void check_linked_checkout(const char *branch, const char *id)

builtin/blame.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2227,20 +2227,19 @@ static struct commit_list **append_parent(struct commit_list **tail, const unsig
22272227
static void append_merge_parents(struct commit_list **tail)
22282228
{
22292229
int merge_head;
2230-
const char *merge_head_file = git_path("MERGE_HEAD");
22312230
struct strbuf line = STRBUF_INIT;
22322231

2233-
merge_head = open(merge_head_file, O_RDONLY);
2232+
merge_head = open(git_path_merge_head(), O_RDONLY);
22342233
if (merge_head < 0) {
22352234
if (errno == ENOENT)
22362235
return;
2237-
die("cannot open '%s' for reading", merge_head_file);
2236+
die("cannot open '%s' for reading", git_path_merge_head());
22382237
}
22392238

22402239
while (!strbuf_getwholeline_fd(&line, merge_head, '\n')) {
22412240
unsigned char sha1[20];
22422241
if (line.len < 40 || get_sha1_hex(line.buf, sha1))
2243-
die("unknown line in '%s': %s", merge_head_file, line.buf);
2242+
die("unknown line in '%s': %s", git_path_merge_head(), line.buf);
22442243
tail = append_parent(tail, sha1);
22452244
}
22462245
close(merge_head);

builtin/clone.c

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -99,51 +99,66 @@ static const char *argv_submodule[] = {
9999
"submodule", "update", "--init", "--recursive", NULL
100100
};
101101

102-
static char *get_repo_path(const char *repo, int *is_bundle)
102+
static const char *get_repo_path_1(struct strbuf *path, int *is_bundle)
103103
{
104104
static char *suffix[] = { "/.git", "", ".git/.git", ".git" };
105105
static char *bundle_suffix[] = { ".bundle", "" };
106+
size_t baselen = path->len;
106107
struct stat st;
107108
int i;
108109

109110
for (i = 0; i < ARRAY_SIZE(suffix); i++) {
110-
const char *path;
111-
path = mkpath("%s%s", repo, suffix[i]);
112-
if (stat(path, &st))
111+
strbuf_setlen(path, baselen);
112+
strbuf_addstr(path, suffix[i]);
113+
if (stat(path->buf, &st))
113114
continue;
114-
if (S_ISDIR(st.st_mode) && is_git_directory(path)) {
115+
if (S_ISDIR(st.st_mode) && is_git_directory(path->buf)) {
115116
*is_bundle = 0;
116-
return xstrdup(absolute_path(path));
117+
return path->buf;
117118
} else if (S_ISREG(st.st_mode) && st.st_size > 8) {
118119
/* Is it a "gitfile"? */
119120
char signature[8];
120-
int len, fd = open(path, O_RDONLY);
121+
const char *dst;
122+
int len, fd = open(path->buf, O_RDONLY);
121123
if (fd < 0)
122124
continue;
123125
len = read_in_full(fd, signature, 8);
124126
close(fd);
125127
if (len != 8 || strncmp(signature, "gitdir: ", 8))
126128
continue;
127-
path = read_gitfile(path);
128-
if (path) {
129+
dst = read_gitfile(path->buf);
130+
if (dst) {
129131
*is_bundle = 0;
130-
return xstrdup(absolute_path(path));
132+
return dst;
131133
}
132134
}
133135
}
134136

135137
for (i = 0; i < ARRAY_SIZE(bundle_suffix); i++) {
136-
const char *path;
137-
path = mkpath("%s%s", repo, bundle_suffix[i]);
138-
if (!stat(path, &st) && S_ISREG(st.st_mode)) {
138+
strbuf_setlen(path, baselen);
139+
strbuf_addstr(path, bundle_suffix[i]);
140+
if (!stat(path->buf, &st) && S_ISREG(st.st_mode)) {
139141
*is_bundle = 1;
140-
return xstrdup(absolute_path(path));
142+
return path->buf;
141143
}
142144
}
143145

144146
return NULL;
145147
}
146148

149+
static char *get_repo_path(const char *repo, int *is_bundle)
150+
{
151+
struct strbuf path = STRBUF_INIT;
152+
const char *raw;
153+
char *canon;
154+
155+
strbuf_addstr(&path, repo);
156+
raw = get_repo_path_1(&path, is_bundle);
157+
canon = raw ? xstrdup(absolute_path(raw)) : NULL;
158+
strbuf_release(&path);
159+
return canon;
160+
}
161+
147162
static char *guess_dir_name(const char *repo, int is_bundle, int is_bare)
148163
{
149164
const char *end = repo + strlen(repo), *start, *ptr;

builtin/commit.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,9 @@ static int opt_parse_m(const struct option *opt, const char *arg, int unset)
166166

167167
static void determine_whence(struct wt_status *s)
168168
{
169-
if (file_exists(git_path("MERGE_HEAD")))
169+
if (file_exists(git_path_merge_head()))
170170
whence = FROM_MERGE;
171-
else if (file_exists(git_path("CHERRY_PICK_HEAD"))) {
171+
else if (file_exists(git_path_cherry_pick_head())) {
172172
whence = FROM_CHERRY_PICK;
173173
if (file_exists(git_path(SEQ_DIR)))
174174
sequencer_in_use = 1;
@@ -725,12 +725,12 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
725725
format_commit_message(commit, "fixup! %s\n\n",
726726
&sb, &ctx);
727727
hook_arg1 = "message";
728-
} else if (!stat(git_path("MERGE_MSG"), &statbuf)) {
729-
if (strbuf_read_file(&sb, git_path("MERGE_MSG"), 0) < 0)
728+
} else if (!stat(git_path_merge_msg(), &statbuf)) {
729+
if (strbuf_read_file(&sb, git_path_merge_msg(), 0) < 0)
730730
die_errno(_("could not read MERGE_MSG"));
731731
hook_arg1 = "merge";
732-
} else if (!stat(git_path("SQUASH_MSG"), &statbuf)) {
733-
if (strbuf_read_file(&sb, git_path("SQUASH_MSG"), 0) < 0)
732+
} else if (!stat(git_path_squash_msg(), &statbuf)) {
733+
if (strbuf_read_file(&sb, git_path_squash_msg(), 0) < 0)
734734
die_errno(_("could not read SQUASH_MSG"));
735735
hook_arg1 = "squash";
736736
} else if (template_file) {
@@ -1684,10 +1684,10 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
16841684
if (!reflog_msg)
16851685
reflog_msg = "commit (merge)";
16861686
pptr = &commit_list_insert(current_head, pptr)->next;
1687-
fp = fopen(git_path("MERGE_HEAD"), "r");
1687+
fp = fopen(git_path_merge_head(), "r");
16881688
if (fp == NULL)
16891689
die_errno(_("could not open '%s' for reading"),
1690-
git_path("MERGE_HEAD"));
1690+
git_path_merge_head());
16911691
while (strbuf_getline(&m, fp, '\n') != EOF) {
16921692
struct commit *parent;
16931693

@@ -1698,8 +1698,8 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
16981698
}
16991699
fclose(fp);
17001700
strbuf_release(&m);
1701-
if (!stat(git_path("MERGE_MODE"), &statbuf)) {
1702-
if (strbuf_read_file(&sb, git_path("MERGE_MODE"), 0) < 0)
1701+
if (!stat(git_path_merge_mode(), &statbuf)) {
1702+
if (strbuf_read_file(&sb, git_path_merge_mode(), 0) < 0)
17031703
die_errno(_("could not read MERGE_MODE"));
17041704
if (!strcmp(sb.buf, "no-ff"))
17051705
allow_fast_forward = 0;
@@ -1775,12 +1775,12 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
17751775
}
17761776
ref_transaction_free(transaction);
17771777

1778-
unlink(git_path("CHERRY_PICK_HEAD"));
1779-
unlink(git_path("REVERT_HEAD"));
1780-
unlink(git_path("MERGE_HEAD"));
1781-
unlink(git_path("MERGE_MSG"));
1782-
unlink(git_path("MERGE_MODE"));
1783-
unlink(git_path("SQUASH_MSG"));
1778+
unlink(git_path_cherry_pick_head());
1779+
unlink(git_path_revert_head());
1780+
unlink(git_path_merge_head());
1781+
unlink(git_path_merge_msg());
1782+
unlink(git_path_merge_mode());
1783+
unlink(git_path_squash_msg());
17841784

17851785
if (commit_index_files())
17861786
die (_("Repository has been updated, but unable to write\n"

builtin/fetch.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
591591
const char *what, *kind;
592592
struct ref *rm;
593593
char *url;
594-
const char *filename = dry_run ? "/dev/null" : git_path("FETCH_HEAD");
594+
const char *filename = dry_run ? "/dev/null" : git_path_fetch_head();
595595
int want_status;
596596

597597
fp = fopen(filename, "a");
@@ -834,7 +834,7 @@ static void check_not_current_branch(struct ref *ref_map)
834834

835835
static int truncate_fetch_head(void)
836836
{
837-
const char *filename = git_path("FETCH_HEAD");
837+
const char *filename = git_path_fetch_head();
838838
FILE *fp = fopen(filename, "w");
839839

840840
if (!fp)

builtin/fsck.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,13 +243,14 @@ static void check_unreachable_object(struct object *obj)
243243
printf("dangling %s %s\n", typename(obj->type),
244244
sha1_to_hex(obj->sha1));
245245
if (write_lost_and_found) {
246-
const char *filename = git_path("lost-found/%s/%s",
246+
char *filename = git_pathdup("lost-found/%s/%s",
247247
obj->type == OBJ_COMMIT ? "commit" : "other",
248248
sha1_to_hex(obj->sha1));
249249
FILE *f;
250250

251251
if (safe_create_leading_directories_const(filename)) {
252252
error("Could not create lost-found");
253+
free(filename);
253254
return;
254255
}
255256
if (!(f = fopen(filename, "w")))
@@ -262,6 +263,7 @@ static void check_unreachable_object(struct object *obj)
262263
if (fclose(f))
263264
die_errno("Could not finish '%s'",
264265
filename);
266+
free(filename);
265267
}
266268
return;
267269
}

builtin/merge.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,9 @@ static struct option builtin_merge_options[] = {
231231
/* Cleans up metadata that is uninteresting after a succeeded merge. */
232232
static void drop_save(void)
233233
{
234-
unlink(git_path("MERGE_HEAD"));
235-
unlink(git_path("MERGE_MSG"));
236-
unlink(git_path("MERGE_MODE"));
234+
unlink(git_path_merge_head());
235+
unlink(git_path_merge_msg());
236+
unlink(git_path_merge_mode());
237237
}
238238

239239
static int save_state(unsigned char *stash)
@@ -338,7 +338,7 @@ static void squash_message(struct commit *commit, struct commit_list *remotehead
338338
struct pretty_print_context ctx = {0};
339339

340340
printf(_("Squash commit -- not updating HEAD\n"));
341-
filename = git_path("SQUASH_MSG");
341+
filename = git_path_squash_msg();
342342
fd = open(filename, O_WRONLY | O_CREAT, 0666);
343343
if (fd < 0)
344344
die_errno(_("Could not write to '%s'"), filename);
@@ -754,7 +754,7 @@ static void add_strategies(const char *string, unsigned attr)
754754

755755
static void write_merge_msg(struct strbuf *msg)
756756
{
757-
const char *filename = git_path("MERGE_MSG");
757+
const char *filename = git_path_merge_msg();
758758
int fd = open(filename, O_WRONLY | O_CREAT, 0666);
759759
if (fd < 0)
760760
die_errno(_("Could not open '%s' for writing"),
@@ -766,7 +766,7 @@ static void write_merge_msg(struct strbuf *msg)
766766

767767
static void read_merge_msg(struct strbuf *msg)
768768
{
769-
const char *filename = git_path("MERGE_MSG");
769+
const char *filename = git_path_merge_msg();
770770
strbuf_reset(msg);
771771
if (strbuf_read_file(msg, filename, 0) < 0)
772772
die_errno(_("Could not read from '%s'"), filename);
@@ -799,10 +799,10 @@ static void prepare_to_commit(struct commit_list *remoteheads)
799799
strbuf_commented_addf(&msg, _(merge_editor_comment), comment_line_char);
800800
write_merge_msg(&msg);
801801
if (run_commit_hook(0 < option_edit, get_index_file(), "prepare-commit-msg",
802-
git_path("MERGE_MSG"), "merge", NULL))
802+
git_path_merge_msg(), "merge", NULL))
803803
abort_commit(remoteheads, NULL);
804804
if (0 < option_edit) {
805-
if (launch_editor(git_path("MERGE_MSG"), NULL, NULL))
805+
if (launch_editor(git_path_merge_msg(), NULL, NULL))
806806
abort_commit(remoteheads, NULL);
807807
}
808808
read_merge_msg(&msg);
@@ -865,7 +865,7 @@ static int suggest_conflicts(void)
865865
FILE *fp;
866866
struct strbuf msgbuf = STRBUF_INIT;
867867

868-
filename = git_path("MERGE_MSG");
868+
filename = git_path_merge_msg();
869869
fp = fopen(filename, "a");
870870
if (!fp)
871871
die_errno(_("Could not open '%s' for writing"), filename);
@@ -967,7 +967,7 @@ static void write_merge_state(struct commit_list *remoteheads)
967967
}
968968
strbuf_addf(&buf, "%s\n", sha1_to_hex(sha1));
969969
}
970-
filename = git_path("MERGE_HEAD");
970+
filename = git_path_merge_head();
971971
fd = open(filename, O_WRONLY | O_CREAT, 0666);
972972
if (fd < 0)
973973
die_errno(_("Could not open '%s' for writing"), filename);
@@ -977,7 +977,7 @@ static void write_merge_state(struct commit_list *remoteheads)
977977
strbuf_addch(&merge_msg, '\n');
978978
write_merge_msg(&merge_msg);
979979

980-
filename = git_path("MERGE_MODE");
980+
filename = git_path_merge_mode();
981981
fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
982982
if (fd < 0)
983983
die_errno(_("Could not open '%s' for writing"), filename);
@@ -1070,7 +1070,7 @@ static void handle_fetch_head(struct commit_list **remotes, struct strbuf *merge
10701070
if (!merge_names)
10711071
merge_names = &fetch_head_file;
10721072

1073-
filename = git_path("FETCH_HEAD");
1073+
filename = git_path_fetch_head();
10741074
fd = open(filename, O_RDONLY);
10751075
if (fd < 0)
10761076
die_errno(_("could not open '%s' for reading"), filename);
@@ -1204,7 +1204,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
12041204
int nargc = 2;
12051205
const char *nargv[] = {"reset", "--merge", NULL};
12061206

1207-
if (!file_exists(git_path("MERGE_HEAD")))
1207+
if (!file_exists(git_path_merge_head()))
12081208
die(_("There is no merge to abort (MERGE_HEAD missing)."));
12091209

12101210
/* Invoke 'git reset --merge' */
@@ -1215,7 +1215,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
12151215
if (read_cache_unmerged())
12161216
die_resolve_conflict("merge");
12171217

1218-
if (file_exists(git_path("MERGE_HEAD"))) {
1218+
if (file_exists(git_path_merge_head())) {
12191219
/*
12201220
* There is no unmerged entry, don't advise 'git
12211221
* add/rm <file>', just 'git commit'.
@@ -1226,7 +1226,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
12261226
else
12271227
die(_("You have not concluded your merge (MERGE_HEAD exists)."));
12281228
}
1229-
if (file_exists(git_path("CHERRY_PICK_HEAD"))) {
1229+
if (file_exists(git_path_cherry_pick_head())) {
12301230
if (advice_resolve_conflict)
12311231
die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists).\n"
12321232
"Please, commit your changes before you merge."));

0 commit comments

Comments
 (0)