Skip to content

Commit 102de88

Browse files
stefanbellergitster
authored andcommitted
path.c: migrate global git_path_* to take a repository argument
Migrate all git_path_* functions that are defined in path.c to take a repository argument. Unlike other patches in this series, do not use the #define trick, as we rewrite the whole function, which is rather small. This doesn't migrate all the functions, as other builtins have their own local path functions defined using GIT_PATH_FUNC. So keep that macro around to serve the other locations. Signed-off-by: Stefan Beller <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0437a2e commit 102de88

File tree

15 files changed

+135
-101
lines changed

15 files changed

+135
-101
lines changed

blame.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,17 +112,19 @@ static void append_merge_parents(struct commit_list **tail)
112112
int merge_head;
113113
struct strbuf line = STRBUF_INIT;
114114

115-
merge_head = open(git_path_merge_head(), O_RDONLY);
115+
merge_head = open(git_path_merge_head(the_repository), O_RDONLY);
116116
if (merge_head < 0) {
117117
if (errno == ENOENT)
118118
return;
119-
die("cannot open '%s' for reading", git_path_merge_head());
119+
die("cannot open '%s' for reading",
120+
git_path_merge_head(the_repository));
120121
}
121122

122123
while (!strbuf_getwholeline_fd(&line, merge_head, '\n')) {
123124
struct object_id oid;
124125
if (line.len < GIT_SHA1_HEXSZ || get_oid_hex(line.buf, &oid))
125-
die("unknown line in '%s': %s", git_path_merge_head(), line.buf);
126+
die("unknown line in '%s': %s",
127+
git_path_merge_head(the_repository), line.buf);
126128
tail = append_parent(tail, &oid);
127129
}
128130
close(merge_head);

branch.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -339,13 +339,13 @@ void create_branch(const char *name, const char *start_name,
339339

340340
void remove_branch_state(void)
341341
{
342-
unlink(git_path_cherry_pick_head());
343-
unlink(git_path_revert_head());
344-
unlink(git_path_merge_head());
345-
unlink(git_path_merge_rr());
346-
unlink(git_path_merge_msg());
347-
unlink(git_path_merge_mode());
348-
unlink(git_path_squash_msg());
342+
unlink(git_path_cherry_pick_head(the_repository));
343+
unlink(git_path_revert_head(the_repository));
344+
unlink(git_path_merge_head(the_repository));
345+
unlink(git_path_merge_rr(the_repository));
346+
unlink(git_path_merge_msg(the_repository));
347+
unlink(git_path_merge_mode(the_repository));
348+
unlink(git_path_squash_msg(the_repository));
349349
}
350350

351351
void die_if_checked_out(const char *branch, int ignore_current_worktree)

builtin/commit.c

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

146146
static void determine_whence(struct wt_status *s)
147147
{
148-
if (file_exists(git_path_merge_head()))
148+
if (file_exists(git_path_merge_head(the_repository)))
149149
whence = FROM_MERGE;
150-
else if (file_exists(git_path_cherry_pick_head())) {
150+
else if (file_exists(git_path_cherry_pick_head(the_repository))) {
151151
whence = FROM_CHERRY_PICK;
152152
if (file_exists(git_path_seq_dir()))
153153
sequencer_in_use = 1;
@@ -696,21 +696,21 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
696696
if (have_option_m)
697697
strbuf_addbuf(&sb, &message);
698698
hook_arg1 = "message";
699-
} else if (!stat(git_path_merge_msg(), &statbuf)) {
699+
} else if (!stat(git_path_merge_msg(the_repository), &statbuf)) {
700700
/*
701701
* prepend SQUASH_MSG here if it exists and a
702702
* "merge --squash" was originally performed
703703
*/
704-
if (!stat(git_path_squash_msg(), &statbuf)) {
705-
if (strbuf_read_file(&sb, git_path_squash_msg(), 0) < 0)
704+
if (!stat(git_path_squash_msg(the_repository), &statbuf)) {
705+
if (strbuf_read_file(&sb, git_path_squash_msg(the_repository), 0) < 0)
706706
die_errno(_("could not read SQUASH_MSG"));
707707
hook_arg1 = "squash";
708708
} else
709709
hook_arg1 = "merge";
710-
if (strbuf_read_file(&sb, git_path_merge_msg(), 0) < 0)
710+
if (strbuf_read_file(&sb, git_path_merge_msg(the_repository), 0) < 0)
711711
die_errno(_("could not read MERGE_MSG"));
712-
} else if (!stat(git_path_squash_msg(), &statbuf)) {
713-
if (strbuf_read_file(&sb, git_path_squash_msg(), 0) < 0)
712+
} else if (!stat(git_path_squash_msg(the_repository), &statbuf)) {
713+
if (strbuf_read_file(&sb, git_path_squash_msg(the_repository), 0) < 0)
714714
die_errno(_("could not read SQUASH_MSG"));
715715
hook_arg1 = "squash";
716716
} else if (template_file) {
@@ -791,8 +791,8 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
791791
" %s\n"
792792
"and try again.\n"),
793793
whence == FROM_MERGE ?
794-
git_path_merge_head() :
795-
git_path_cherry_pick_head());
794+
git_path_merge_head(the_repository) :
795+
git_path_cherry_pick_head(the_repository));
796796
}
797797

798798
fprintf(s->fp, "\n");
@@ -1523,7 +1523,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
15231523
if (!reflog_msg)
15241524
reflog_msg = "commit (merge)";
15251525
pptr = commit_list_append(current_head, pptr);
1526-
fp = xfopen(git_path_merge_head(), "r");
1526+
fp = xfopen(git_path_merge_head(the_repository), "r");
15271527
while (strbuf_getline_lf(&m, fp) != EOF) {
15281528
struct commit *parent;
15291529

@@ -1534,8 +1534,8 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
15341534
}
15351535
fclose(fp);
15361536
strbuf_release(&m);
1537-
if (!stat(git_path_merge_mode(), &statbuf)) {
1538-
if (strbuf_read_file(&sb, git_path_merge_mode(), 0) < 0)
1537+
if (!stat(git_path_merge_mode(the_repository), &statbuf)) {
1538+
if (strbuf_read_file(&sb, git_path_merge_mode(the_repository), 0) < 0)
15391539
die_errno(_("could not read MERGE_MODE"));
15401540
if (!strcmp(sb.buf, "no-ff"))
15411541
allow_fast_forward = 0;
@@ -1598,12 +1598,12 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
15981598
die("%s", err.buf);
15991599
}
16001600

1601-
unlink(git_path_cherry_pick_head());
1602-
unlink(git_path_revert_head());
1603-
unlink(git_path_merge_head());
1604-
unlink(git_path_merge_msg());
1605-
unlink(git_path_merge_mode());
1606-
unlink(git_path_squash_msg());
1601+
unlink(git_path_cherry_pick_head(the_repository));
1602+
unlink(git_path_revert_head(the_repository));
1603+
unlink(git_path_merge_head(the_repository));
1604+
unlink(git_path_merge_msg(the_repository));
1605+
unlink(git_path_merge_mode(the_repository));
1606+
unlink(git_path_squash_msg(the_repository));
16071607

16081608
if (commit_index_files())
16091609
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
@@ -767,7 +767,7 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
767767
const char *what, *kind;
768768
struct ref *rm;
769769
char *url;
770-
const char *filename = dry_run ? "/dev/null" : git_path_fetch_head();
770+
const char *filename = dry_run ? "/dev/null" : git_path_fetch_head(the_repository);
771771
int want_status;
772772
int summary_width = transport_summary_width(ref_map);
773773

@@ -1019,7 +1019,7 @@ static void check_not_current_branch(struct ref *ref_map)
10191019

10201020
static int truncate_fetch_head(void)
10211021
{
1022-
const char *filename = git_path_fetch_head();
1022+
const char *filename = git_path_fetch_head(the_repository);
10231023
FILE *fp = fopen_for_writing(filename);
10241024

10251025
if (!fp)

builtin/merge.c

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,9 @@ static struct option builtin_merge_options[] = {
245245
/* Cleans up metadata that is uninteresting after a succeeded merge. */
246246
static void drop_save(void)
247247
{
248-
unlink(git_path_merge_head());
249-
unlink(git_path_merge_msg());
250-
unlink(git_path_merge_mode());
248+
unlink(git_path_merge_head(the_repository));
249+
unlink(git_path_merge_msg(the_repository));
250+
unlink(git_path_merge_mode(the_repository));
251251
}
252252

253253
static int save_state(struct object_id *stash)
@@ -380,7 +380,7 @@ static void squash_message(struct commit *commit, struct commit_list *remotehead
380380
oid_to_hex(&commit->object.oid));
381381
pretty_print_commit(&ctx, commit, &out);
382382
}
383-
write_file_buf(git_path_squash_msg(), out.buf, out.len);
383+
write_file_buf(git_path_squash_msg(the_repository), out.buf, out.len);
384384
strbuf_release(&out);
385385
}
386386

@@ -741,7 +741,7 @@ static void add_strategies(const char *string, unsigned attr)
741741

742742
static void read_merge_msg(struct strbuf *msg)
743743
{
744-
const char *filename = git_path_merge_msg();
744+
const char *filename = git_path_merge_msg(the_repository);
745745
strbuf_reset(msg);
746746
if (strbuf_read_file(msg, filename, 0) < 0)
747747
die_errno(_("Could not read from '%s'"), filename);
@@ -778,18 +778,18 @@ static void prepare_to_commit(struct commit_list *remoteheads)
778778
if (signoff)
779779
append_signoff(&msg, ignore_non_trailer(msg.buf, msg.len), 0);
780780
write_merge_heads(remoteheads);
781-
write_file_buf(git_path_merge_msg(), msg.buf, msg.len);
781+
write_file_buf(git_path_merge_msg(the_repository), msg.buf, msg.len);
782782
if (run_commit_hook(0 < option_edit, get_index_file(), "prepare-commit-msg",
783-
git_path_merge_msg(), "merge", NULL))
783+
git_path_merge_msg(the_repository), "merge", NULL))
784784
abort_commit(remoteheads, NULL);
785785
if (0 < option_edit) {
786-
if (launch_editor(git_path_merge_msg(), NULL, NULL))
786+
if (launch_editor(git_path_merge_msg(the_repository), NULL, NULL))
787787
abort_commit(remoteheads, NULL);
788788
}
789789

790790
if (verify_msg && run_commit_hook(0 < option_edit, get_index_file(),
791791
"commit-msg",
792-
git_path_merge_msg(), NULL))
792+
git_path_merge_msg(the_repository), NULL))
793793
abort_commit(remoteheads, NULL);
794794

795795
read_merge_msg(&msg);
@@ -859,7 +859,7 @@ static int suggest_conflicts(void)
859859
FILE *fp;
860860
struct strbuf msgbuf = STRBUF_INIT;
861861

862-
filename = git_path_merge_msg();
862+
filename = git_path_merge_msg(the_repository);
863863
fp = xfopen(filename, "a");
864864

865865
append_conflicts_hint(&msgbuf);
@@ -939,20 +939,21 @@ static void write_merge_heads(struct commit_list *remoteheads)
939939
}
940940
strbuf_addf(&buf, "%s\n", oid_to_hex(oid));
941941
}
942-
write_file_buf(git_path_merge_head(), buf.buf, buf.len);
942+
write_file_buf(git_path_merge_head(the_repository), buf.buf, buf.len);
943943

944944
strbuf_reset(&buf);
945945
if (fast_forward == FF_NO)
946946
strbuf_addstr(&buf, "no-ff");
947-
write_file_buf(git_path_merge_mode(), buf.buf, buf.len);
947+
write_file_buf(git_path_merge_mode(the_repository), buf.buf, buf.len);
948948
strbuf_release(&buf);
949949
}
950950

951951
static void write_merge_state(struct commit_list *remoteheads)
952952
{
953953
write_merge_heads(remoteheads);
954954
strbuf_addch(&merge_msg, '\n');
955-
write_file_buf(git_path_merge_msg(), merge_msg.buf, merge_msg.len);
955+
write_file_buf(git_path_merge_msg(the_repository), merge_msg.buf,
956+
merge_msg.len);
956957
}
957958

958959
static int default_edit_option(void)
@@ -1035,7 +1036,7 @@ static void handle_fetch_head(struct commit_list **remotes, struct strbuf *merge
10351036
if (!merge_names)
10361037
merge_names = &fetch_head_file;
10371038

1038-
filename = git_path_fetch_head();
1039+
filename = git_path_fetch_head(the_repository);
10391040
fd = open(filename, O_RDONLY);
10401041
if (fd < 0)
10411042
die_errno(_("could not open '%s' for reading"), filename);
@@ -1209,7 +1210,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
12091210
usage_msg_opt(_("--abort expects no arguments"),
12101211
builtin_merge_usage, builtin_merge_options);
12111212

1212-
if (!file_exists(git_path_merge_head()))
1213+
if (!file_exists(git_path_merge_head(the_repository)))
12131214
die(_("There is no merge to abort (MERGE_HEAD missing)."));
12141215

12151216
/* Invoke 'git reset --merge' */
@@ -1225,7 +1226,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
12251226
usage_msg_opt(_("--continue expects no arguments"),
12261227
builtin_merge_usage, builtin_merge_options);
12271228

1228-
if (!file_exists(git_path_merge_head()))
1229+
if (!file_exists(git_path_merge_head(the_repository)))
12291230
die(_("There is no merge in progress (MERGE_HEAD missing)."));
12301231

12311232
/* Invoke 'git commit' */
@@ -1236,7 +1237,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
12361237
if (read_cache_unmerged())
12371238
die_resolve_conflict("merge");
12381239

1239-
if (file_exists(git_path_merge_head())) {
1240+
if (file_exists(git_path_merge_head(the_repository))) {
12401241
/*
12411242
* There is no unmerged entry, don't advise 'git
12421243
* add/rm <file>', just 'git commit'.
@@ -1247,7 +1248,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
12471248
else
12481249
die(_("You have not concluded your merge (MERGE_HEAD exists)."));
12491250
}
1250-
if (file_exists(git_path_cherry_pick_head())) {
1251+
if (file_exists(git_path_cherry_pick_head(the_repository))) {
12511252
if (advice_resolve_conflict)
12521253
die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists).\n"
12531254
"Please, commit your changes before you merge."));

builtin/pull.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ static int git_pull_config(const char *var, const char *value, void *cb)
351351
*/
352352
static void get_merge_heads(struct oid_array *merge_heads)
353353
{
354-
const char *filename = git_path_fetch_head();
354+
const char *filename = git_path_fetch_head(the_repository);
355355
FILE *fp;
356356
struct strbuf sb = STRBUF_INIT;
357357
struct object_id oid;
@@ -857,7 +857,7 @@ int cmd_pull(int argc, const char **argv, const char *prefix)
857857
if (read_cache_unmerged())
858858
die_resolve_conflict("pull");
859859

860-
if (file_exists(git_path_merge_head()))
860+
if (file_exists(git_path_merge_head(the_repository)))
861861
die_conclude_merge();
862862

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

builtin/reset.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ static const char *reset_type_names[] = {
3939

4040
static inline int is_merge(void)
4141
{
42-
return !access(git_path_merge_head(), F_OK);
42+
return !access(git_path_merge_head(the_repository), F_OK);
4343
}
4444

4545
static int reset_index(const struct object_id *oid, int reset_type, int quiet)

fetch-pack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1136,7 +1136,7 @@ static void update_shallow(struct fetch_pack_args *args,
11361136

11371137
if (args->deepen && alternate_shallow_file) {
11381138
if (*alternate_shallow_file == '\0') { /* --unshallow */
1139-
unlink_or_warn(git_path_shallow());
1139+
unlink_or_warn(git_path_shallow(the_repository));
11401140
rollback_lock_file(&shallow_lock);
11411141
} else
11421142
commit_lock_file(&shallow_lock);

path.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,12 +1358,12 @@ char *xdg_cache_home(const char *filename)
13581358
return NULL;
13591359
}
13601360

1361-
GIT_PATH_FUNC(git_path_cherry_pick_head, "CHERRY_PICK_HEAD")
1362-
GIT_PATH_FUNC(git_path_revert_head, "REVERT_HEAD")
1363-
GIT_PATH_FUNC(git_path_squash_msg, "SQUASH_MSG")
1364-
GIT_PATH_FUNC(git_path_merge_msg, "MERGE_MSG")
1365-
GIT_PATH_FUNC(git_path_merge_rr, "MERGE_RR")
1366-
GIT_PATH_FUNC(git_path_merge_mode, "MERGE_MODE")
1367-
GIT_PATH_FUNC(git_path_merge_head, "MERGE_HEAD")
1368-
GIT_PATH_FUNC(git_path_fetch_head, "FETCH_HEAD")
1369-
GIT_PATH_FUNC(git_path_shallow, "shallow")
1361+
REPO_GIT_PATH_FUNC(cherry_pick_head, "CHERRY_PICK_HEAD")
1362+
REPO_GIT_PATH_FUNC(revert_head, "REVERT_HEAD")
1363+
REPO_GIT_PATH_FUNC(squash_msg, "SQUASH_MSG")
1364+
REPO_GIT_PATH_FUNC(merge_msg, "MERGE_MSG")
1365+
REPO_GIT_PATH_FUNC(merge_rr, "MERGE_RR")
1366+
REPO_GIT_PATH_FUNC(merge_mode, "MERGE_MODE")
1367+
REPO_GIT_PATH_FUNC(merge_head, "MERGE_HEAD")
1368+
REPO_GIT_PATH_FUNC(fetch_head, "FETCH_HEAD")
1369+
REPO_GIT_PATH_FUNC(shallow, "shallow")

path.h

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -160,14 +160,36 @@ extern void report_linked_checkout_garbage(void);
160160
return ret; \
161161
}
162162

163-
const char *git_path_cherry_pick_head(void);
164-
const char *git_path_revert_head(void);
165-
const char *git_path_squash_msg(void);
166-
const char *git_path_merge_msg(void);
167-
const char *git_path_merge_rr(void);
168-
const char *git_path_merge_mode(void);
169-
const char *git_path_merge_head(void);
170-
const char *git_path_fetch_head(void);
171-
const char *git_path_shallow(void);
163+
#define REPO_GIT_PATH_FUNC(var, filename) \
164+
const char *git_path_##var(struct repository *r) \
165+
{ \
166+
if (!r->cached_paths.var) \
167+
r->cached_paths.var = git_pathdup(filename); \
168+
return r->cached_paths.var; \
169+
}
170+
171+
struct path_cache {
172+
const char *cherry_pick_head;
173+
const char *revert_head;
174+
const char *squash_msg;
175+
const char *merge_msg;
176+
const char *merge_rr;
177+
const char *merge_mode;
178+
const char *merge_head;
179+
const char *fetch_head;
180+
const char *shallow;
181+
};
182+
183+
#define PATH_CACHE_INIT { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
184+
185+
const char *git_path_cherry_pick_head(struct repository *r);
186+
const char *git_path_revert_head(struct repository *r);
187+
const char *git_path_squash_msg(struct repository *r);
188+
const char *git_path_merge_msg(struct repository *r);
189+
const char *git_path_merge_rr(struct repository *r);
190+
const char *git_path_merge_mode(struct repository *r);
191+
const char *git_path_merge_head(struct repository *r);
192+
const char *git_path_fetch_head(struct repository *r);
193+
const char *git_path_shallow(struct repository *r);
172194

173195
#endif /* PATH_H */

0 commit comments

Comments
 (0)