memoize common git-path "constant" files

One of the most common uses of git_path() is to pass a constant, like git_path("MERGE_MSG"). This has two drawbacks: 1. The return value is a static buffer, and the lifetime is dependent on other calls to git_path, etc. 2. There's no compile-time checking of the pathname. This is OK for a one-off (after all, we have to spell it correctly at least once), but many of these constant strings appear throughout the code. This patch introduces a series of functions to "memoize" these strings, which are essentially globals for the lifetime of the program. We compute the value once, take ownership of the buffer, and return the cached value for subsequent calls. cache.h provides a helper macro for defining these functions as one-liners, and defines a few common ones for global use. Using a macro is a little bit gross, but it does nicely document the purpose of the functions. If we need to touch them all later (e.g., because we learned how to change the git_dir variable at runtime, and need to invalidate all of the stored values), it will be much easier to have the complete list. Note that the shared-global functions have separate, manual declarations. We could do something clever with the macros (e.g., expand it to a declaration in some places, and a declaration _and_ a definition in path.c). But there aren't that many, and it's probably better to stay away from too-magical macros. Likewise, if we abandon the C preprocessor in favor of generating these with a script, we could get much fancier. E.g., normalizing "FOO/BAR-BAZ" into "git_path_foo_bar_baz". But the small amount of saved typing is probably not worth the resulting confusion to readers who want to grep for the function's definition. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Aug 10, 2015 at 05:38 UTC f932729cc7707390f4d6739be1573e93ceb9df22
17 files changed +151 -119
attr.c
+3 -1
@@ -490,6 +490,8 @@ static int git_attr_system(void)
490 return !git_env_bool("GIT_ATTR_NOSYSTEM", 0);
491 }
492
493 +static GIT_PATH_FUNC(git_path_info_attributes, INFOATTRIBUTES_FILE)
494 +
495 static void bootstrap_attr_stack(void)
496 {
497 struct attr_stack *elem;
@@ -531,7 +533,7 @@ static void bootstrap_attr_stack(void)
533 debug_push(elem);
534 }
535
534 - elem = read_attr_from_file(git_path(INFOATTRIBUTES_FILE), 1);
536 + elem = read_attr_from_file(git_path_info_attributes(), 1);
537 if (!elem)
538 elem = xcalloc(1, sizeof(*elem));
539 elem->origin = NULL;
bisect.c
+5 -2
@@ -420,10 +420,13 @@ static int read_bisect_refs(void)
420 return for_each_ref_in("refs/bisect/", register_ref, NULL);
421 }
422
423 +static GIT_PATH_FUNC(git_path_bisect_names, "BISECT_NAMES")
424 +static GIT_PATH_FUNC(git_path_bisect_expected_rev, "BISECT_EXPECTED_REV")
425 +
426 static void read_bisect_paths(struct argv_array *array)
427 {
428 struct strbuf str = STRBUF_INIT;
426 - const char *filename = git_path("BISECT_NAMES");
429 + const char *filename = git_path_bisect_names();
430 FILE *fp = fopen(filename, "r");
431
432 if (!fp)
@@ -644,7 +647,7 @@ static void exit_if_skipped_commits(struct commit_list *tried,
647
648 static int is_expected_rev(const struct object_id *oid)
649 {
647 - const char *filename = git_path("BISECT_EXPECTED_REV");
650 + const char *filename = git_path_bisect_expected_rev();
651 struct stat st;
652 struct strbuf str = STRBUF_INIT;
653 FILE *fp;
branch.c
+7 -7
@@ -302,11 +302,11 @@ void create_branch(const char *head,
302
303 void remove_branch_state(void)
304 {
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());
312 }
builtin/blame.c
+3 -4
@@ -2227,20 +2227,19 @@ static struct commit_list **append_parent(struct commit_list **tail, const unsig
2227 static void append_merge_parents(struct commit_list **tail)
2228 {
2229 int merge_head;
2230 - const char *merge_head_file = git_path("MERGE_HEAD");
2230 struct strbuf line = STRBUF_INIT;
2231
2233 - merge_head = open(merge_head_file, O_RDONLY);
2232 + merge_head = open(git_path_merge_head(), O_RDONLY);
2233 if (merge_head < 0) {
2234 if (errno == ENOENT)
2235 return;
2237 - die("cannot open '%s' for reading", merge_head_file);
2236 + die("cannot open '%s' for reading", git_path_merge_head());
2237 }
2238
2239 while (!strbuf_getwholeline_fd(&line, merge_head, '\n')) {
2240 unsigned char sha1[20];
2241 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);
2243 tail = append_parent(tail, sha1);
2244 }
2245 close(merge_head);
builtin/commit.c
+16 -16
@@ -166,9 +166,9 @@ static int opt_parse_m(const struct option *opt, const char *arg, int unset)
166
167 static void determine_whence(struct wt_status *s)
168 {
169 - if (file_exists(git_path("MERGE_HEAD")))
169 + if (file_exists(git_path_merge_head()))
170 whence = FROM_MERGE;
171 - else if (file_exists(git_path("CHERRY_PICK_HEAD"))) {
171 + else if (file_exists(git_path_cherry_pick_head())) {
172 whence = FROM_CHERRY_PICK;
173 if (file_exists(git_path(SEQ_DIR)))
174 sequencer_in_use = 1;
@@ -725,12 +725,12 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
725 format_commit_message(commit, "fixup! %s\n\n",
726 &sb, &ctx);
727 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)
730 die_errno(_("could not read MERGE_MSG"));
731 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)
734 die_errno(_("could not read SQUASH_MSG"));
735 hook_arg1 = "squash";
736 } else if (template_file) {
@@ -1684,10 +1684,10 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1684 if (!reflog_msg)
1685 reflog_msg = "commit (merge)";
1686 pptr = &commit_list_insert(current_head, pptr)->next;
1687 - fp = fopen(git_path("MERGE_HEAD"), "r");
1687 + fp = fopen(git_path_merge_head(), "r");
1688 if (fp == NULL)
1689 die_errno(_("could not open '%s' for reading"),
1690 - git_path("MERGE_HEAD"));
1690 + git_path_merge_head());
1691 while (strbuf_getline(&m, fp, '\n') != EOF) {
1692 struct commit *parent;
1693
@@ -1698,8 +1698,8 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1698 }
1699 fclose(fp);
1700 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)
1703 die_errno(_("could not read MERGE_MODE"));
1704 if (!strcmp(sb.buf, "no-ff"))
1705 allow_fast_forward = 0;
@@ -1775,12 +1775,12 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1775 }
1776 ref_transaction_free(transaction);
1777
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());
1784
1785 if (commit_index_files())
1786 die (_("Repository has been updated, but unable to write\n"
builtin/fetch.c
+2 -2
@@ -591,7 +591,7 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
591 const char *what, *kind;
592 struct ref *rm;
593 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();
595 int want_status;
596
597 fp = fopen(filename, "a");
@@ -834,7 +834,7 @@ static void check_not_current_branch(struct ref *ref_map)
834
835 static int truncate_fetch_head(void)
836 {
837 - const char *filename = git_path("FETCH_HEAD");
837 + const char *filename = git_path_fetch_head();
838 FILE *fp = fopen(filename, "w");
839
840 if (!fp)
builtin/merge.c
+15 -15
@@ -231,9 +231,9 @@ static struct option builtin_merge_options[] = {
231 /* Cleans up metadata that is uninteresting after a succeeded merge. */
232 static void drop_save(void)
233 {
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());
237 }
238
239 static int save_state(unsigned char *stash)
@@ -338,7 +338,7 @@ static void squash_message(struct commit *commit, struct commit_list *remotehead
338 struct pretty_print_context ctx = {0};
339
340 printf(_("Squash commit -- not updating HEAD\n"));
341 - filename = git_path("SQUASH_MSG");
341 + filename = git_path_squash_msg();
342 fd = open(filename, O_WRONLY | O_CREAT, 0666);
343 if (fd < 0)
344 die_errno(_("Could not write to '%s'"), filename);
@@ -754,7 +754,7 @@ static void add_strategies(const char *string, unsigned attr)
754
755 static void write_merge_msg(struct strbuf *msg)
756 {
757 - const char *filename = git_path("MERGE_MSG");
757 + const char *filename = git_path_merge_msg();
758 int fd = open(filename, O_WRONLY | O_CREAT, 0666);
759 if (fd < 0)
760 die_errno(_("Could not open '%s' for writing"),
@@ -766,7 +766,7 @@ static void write_merge_msg(struct strbuf *msg)
766
767 static void read_merge_msg(struct strbuf *msg)
768 {
769 - const char *filename = git_path("MERGE_MSG");
769 + const char *filename = git_path_merge_msg();
770 strbuf_reset(msg);
771 if (strbuf_read_file(msg, filename, 0) < 0)
772 die_errno(_("Could not read from '%s'"), filename);
@@ -799,10 +799,10 @@ static void prepare_to_commit(struct commit_list *remoteheads)
799 strbuf_commented_addf(&msg, _(merge_editor_comment), comment_line_char);
800 write_merge_msg(&msg);
801 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))
803 abort_commit(remoteheads, NULL);
804 if (0 < option_edit) {
805 - if (launch_editor(git_path("MERGE_MSG"), NULL, NULL))
805 + if (launch_editor(git_path_merge_msg(), NULL, NULL))
806 abort_commit(remoteheads, NULL);
807 }
808 read_merge_msg(&msg);
@@ -865,7 +865,7 @@ static int suggest_conflicts(void)
865 FILE *fp;
866 struct strbuf msgbuf = STRBUF_INIT;
867
868 - filename = git_path("MERGE_MSG");
868 + filename = git_path_merge_msg();
869 fp = fopen(filename, "a");
870 if (!fp)
871 die_errno(_("Could not open '%s' for writing"), filename);
@@ -967,7 +967,7 @@ static void write_merge_state(struct commit_list *remoteheads)
967 }
968 strbuf_addf(&buf, "%s\n", sha1_to_hex(sha1));
969 }
970 - filename = git_path("MERGE_HEAD");
970 + filename = git_path_merge_head();
971 fd = open(filename, O_WRONLY | O_CREAT, 0666);
972 if (fd < 0)
973 die_errno(_("Could not open '%s' for writing"), filename);
@@ -977,7 +977,7 @@ static void write_merge_state(struct commit_list *remoteheads)
977 strbuf_addch(&merge_msg, '\n');
978 write_merge_msg(&merge_msg);
979
980 - filename = git_path("MERGE_MODE");
980 + filename = git_path_merge_mode();
981 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
982 if (fd < 0)
983 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
1070 if (!merge_names)
1071 merge_names = &fetch_head_file;
1072
1073 - filename = git_path("FETCH_HEAD");
1073 + filename = git_path_fetch_head();
1074 fd = open(filename, O_RDONLY);
1075 if (fd < 0)
1076 die_errno(_("could not open '%s' for reading"), filename);
@@ -1204,7 +1204,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1204 int nargc = 2;
1205 const char *nargv[] = {"reset", "--merge", NULL};
1206
1207 - if (!file_exists(git_path("MERGE_HEAD")))
1207 + if (!file_exists(git_path_merge_head()))
1208 die(_("There is no merge to abort (MERGE_HEAD missing)."));
1209
1210 /* Invoke 'git reset --merge' */
@@ -1215,7 +1215,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1215 if (read_cache_unmerged())
1216 die_resolve_conflict("merge");
1217
1218 - if (file_exists(git_path("MERGE_HEAD"))) {
1218 + if (file_exists(git_path_merge_head())) {
1219 /*
1220 * There is no unmerged entry, don't advise 'git
1221 * add/rm <file>', just 'git commit'.
@@ -1226,7 +1226,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1226 else
1227 die(_("You have not concluded your merge (MERGE_HEAD exists)."));
1228 }
1229 - if (file_exists(git_path("CHERRY_PICK_HEAD"))) {
1229 + if (file_exists(git_path_cherry_pick_head())) {
1230 if (advice_resolve_conflict)
1231 die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists).\n"
1232 "Please, commit your changes before you merge."));
builtin/reset.c
+1 -1
@@ -36,7 +36,7 @@ static const char *reset_type_names[] = {
36
37 static inline int is_merge(void)
38 {
39 - return !access(git_path("MERGE_HEAD"), F_OK);
39 + return !access(git_path_merge_head(), F_OK);
40 }
41
42 static int reset_index(const unsigned char *sha1, int reset_type, int quiet)
cache.h
+26
@@ -735,6 +735,32 @@ extern char *git_pathdup_submodule(const char *path, const char *fmt, ...)
735
736 extern void report_linked_checkout_garbage(void);
737
738 +/*
739 + * You can define a static memoized git path like:
740 + *
741 + * static GIT_PATH_FUNC(git_path_foo, "FOO");
742 + *
743 + * or use one of the global ones below.
744 + */
745 +#define GIT_PATH_FUNC(func, filename) \
746 + const char *func(void) \
747 + { \
748 + static char *ret; \
749 + if (!ret) \
750 + ret = git_pathdup(filename); \
751 + return ret; \
752 + }
753 +
754 +const char *git_path_cherry_pick_head(void);
755 +const char *git_path_revert_head(void);
756 +const char *git_path_squash_msg(void);
757 +const char *git_path_merge_msg(void);
758 +const char *git_path_merge_rr(void);
759 +const char *git_path_merge_mode(void);
760 +const char *git_path_merge_head(void);
761 +const char *git_path_fetch_head(void);
762 +const char *git_path_shallow(void);
763 +
764 /*
765 * Return the name of the file in the local object database that would
766 * be used to store a loose object with the specified sha1. The
contrib/examples/builtin-fetch--tool.c
+2 -2
@@ -516,7 +516,7 @@ int cmd_fetch__tool(int argc, const char **argv, const char *prefix)
516
517 if (argc != 8)
518 return error("append-fetch-head takes 6 args");
519 - filename = git_path("FETCH_HEAD");
519 + filename = git_path_fetch_head();
520 fp = fopen(filename, "a");
521 if (!fp)
522 return error("cannot open %s: %s", filename, strerror(errno));
@@ -534,7 +534,7 @@ int cmd_fetch__tool(int argc, const char **argv, const char *prefix)
534
535 if (argc != 5)
536 return error("fetch-native-store takes 3 args");
537 - filename = git_path("FETCH_HEAD");
537 + filename = git_path_fetch_head();
538 fp = fopen(filename, "a");
539 if (!fp)
540 return error("cannot open %s: %s", filename, strerror(errno));
dir.c
+3 -1
@@ -2185,6 +2185,8 @@ int remove_dir_recursively(struct strbuf *path, int flag)
2185 return remove_dir_recurse(path, flag, NULL);
2186 }
2187
2188 +static GIT_PATH_FUNC(git_path_info_exclude, "info/exclude")
2189 +
2190 void setup_standard_excludes(struct dir_struct *dir)
2191 {
2192 const char *path;
@@ -2199,7 +2201,7 @@ void setup_standard_excludes(struct dir_struct *dir)
2201 dir->untracked ? &dir->ss_excludes_file : NULL);
2202
2203 /* per repository user preference */
2202 - path = git_path("info/exclude");
2204 + path = git_path_info_exclude();
2205 if (!access_or_warn(path, R_OK, 0))
2206 add_excludes_from_file_1(dir, path,
2207 dir->untracked ? &dir->ss_info_exclude : NULL);
fetch-pack.c
+1 -1
@@ -948,7 +948,7 @@ static void update_shallow(struct fetch_pack_args *args,
948
949 if (args->depth > 0 && alternate_shallow_file) {
950 if (*alternate_shallow_file == '\0') { /* --unshallow */
951 - unlink_or_warn(git_path("shallow"));
951 + unlink_or_warn(git_path_shallow());
952 rollback_lock_file(&shallow_lock);
953 } else
954 commit_lock_file(&shallow_lock);
path.c
+10
@@ -933,3 +933,13 @@ char *xdg_config_home(const char *filename)
933 return mkpathdup("%s/.config/git/%s", home, filename);
934 return NULL;
935 }
936 +
937 +GIT_PATH_FUNC(git_path_cherry_pick_head, "CHERRY_PICK_HEAD")
938 +GIT_PATH_FUNC(git_path_revert_head, "REVERT_HEAD")
939 +GIT_PATH_FUNC(git_path_squash_msg, "SQUASH_MSG")
940 +GIT_PATH_FUNC(git_path_merge_msg, "MERGE_MSG")
941 +GIT_PATH_FUNC(git_path_merge_rr, "MERGE_RR")
942 +GIT_PATH_FUNC(git_path_merge_mode, "MERGE_MODE")
943 +GIT_PATH_FUNC(git_path_merge_head, "MERGE_HEAD")
944 +GIT_PATH_FUNC(git_path_fetch_head, "FETCH_HEAD")
945 +GIT_PATH_FUNC(git_path_shallow, "shallow")
rerere.c
+8 -11
@@ -20,8 +20,6 @@ static int rerere_enabled = -1;
20 /* automatically update cleanly resolved paths to the index */
21 static int rerere_autoupdate;
22
23 -static char *merge_rr_path;
24 -
23 const char *rerere_path(const char *hex, const char *file)
24 {
25 return git_path("rr-cache/%s/%s", hex, file);
@@ -37,7 +35,7 @@ static void read_rr(struct string_list *rr)
35 {
36 unsigned char sha1[20];
37 char buf[PATH_MAX];
40 - FILE *in = fopen(merge_rr_path, "r");
38 + FILE *in = fopen(git_path_merge_rr(), "r");
39 if (!in)
40 return;
41 while (fread(buf, 40, 1, in) == 1) {
@@ -577,21 +575,21 @@ static void git_rerere_config(void)
575 git_config(git_default_config, NULL);
576 }
577
578 +static GIT_PATH_FUNC(git_path_rr_cache, "rr-cache")
579 +
580 static int is_rerere_enabled(void)
581 {
582 - const char *rr_cache;
582 int rr_cache_exists;
583
584 if (!rerere_enabled)
585 return 0;
586
588 - rr_cache = git_path("rr-cache");
589 - rr_cache_exists = is_directory(rr_cache);
587 + rr_cache_exists = is_directory(git_path_rr_cache());
588 if (rerere_enabled < 0)
589 return rr_cache_exists;
590
593 - if (!rr_cache_exists && mkdir_in_gitdir(rr_cache))
594 - die("Could not create directory %s", rr_cache);
591 + if (!rr_cache_exists && mkdir_in_gitdir(git_path_rr_cache()))
592 + die("Could not create directory %s", git_path_rr_cache());
593 return 1;
594 }
595
@@ -605,8 +603,7 @@ int setup_rerere(struct string_list *merge_rr, int flags)
603
604 if (flags & (RERERE_AUTOUPDATE|RERERE_NOAUTOUPDATE))
605 rerere_autoupdate = !!(flags & RERERE_AUTOUPDATE);
608 - merge_rr_path = git_pathdup("MERGE_RR");
609 - fd = hold_lock_file_for_update(&write_lock, merge_rr_path,
606 + fd = hold_lock_file_for_update(&write_lock, git_path_merge_rr(),
607 LOCK_DIE_ON_ERROR);
608 read_rr(merge_rr);
609 return fd;
@@ -741,5 +738,5 @@ void rerere_clear(struct string_list *merge_rr)
738 if (!has_rerere_resolution(name))
739 unlink_rr_item(name);
740 }
744 - unlink_or_warn(git_path("MERGE_RR"));
741 + unlink_or_warn(git_path_merge_rr());
742 }
sequencer.c
+40 -47
@@ -21,6 +21,11 @@
21 const char sign_off_header[] = "Signed-off-by: ";
22 static const char cherry_picked_prefix[] = "(cherry picked from commit ";
23
24 +static GIT_PATH_FUNC(git_path_todo_file, SEQ_TODO_FILE)
25 +static GIT_PATH_FUNC(git_path_opts_file, SEQ_OPTS_FILE)
26 +static GIT_PATH_FUNC(git_path_seq_dir, SEQ_DIR)
27 +static GIT_PATH_FUNC(git_path_head_file, SEQ_HEAD_FILE)
28 +
29 static int is_rfc2822_line(const char *buf, int len)
30 {
31 int i;
@@ -186,7 +191,7 @@ static void print_advice(int show_hint, struct replay_opts *opts)
191 * (typically rebase --interactive) wants to take care
192 * of the commit itself so remove CHERRY_PICK_HEAD
193 */
189 - unlink(git_path("CHERRY_PICK_HEAD"));
194 + unlink(git_path_cherry_pick_head());
195 return;
196 }
197
@@ -467,7 +472,6 @@ static int do_pick_commit(struct commit *commit, struct replay_opts *opts)
472 struct commit *base, *next, *parent;
473 const char *base_label, *next_label;
474 struct commit_message msg = { NULL, NULL, NULL, NULL };
470 - char *defmsg = NULL;
475 struct strbuf msgbuf = STRBUF_INIT;
476 int res, unborn = 0, allow;
477
@@ -537,8 +541,6 @@ static int do_pick_commit(struct commit *commit, struct replay_opts *opts)
541 * reverse of it if we are revert.
542 */
543
540 - defmsg = git_pathdup("MERGE_MSG");
541 -
544 if (opts->action == REPLAY_REVERT) {
545 base = commit;
546 base_label = msg.label;
@@ -585,12 +587,12 @@ static int do_pick_commit(struct commit *commit, struct replay_opts *opts)
587 if (!opts->strategy || !strcmp(opts->strategy, "recursive") || opts->action == REPLAY_REVERT) {
588 res = do_recursive_merge(base, next, base_label, next_label,
589 head, &msgbuf, opts);
588 - write_message(&msgbuf, defmsg);
590 + write_message(&msgbuf, git_path_merge_msg());
591 } else {
592 struct commit_list *common = NULL;
593 struct commit_list *remotes = NULL;
594
593 - write_message(&msgbuf, defmsg);
595 + write_message(&msgbuf, git_path_merge_msg());
596
597 commit_list_insert(base, &common);
598 commit_list_insert(next, &remotes);
@@ -628,11 +630,10 @@ static int do_pick_commit(struct commit *commit, struct replay_opts *opts)
630 goto leave;
631 }
632 if (!opts->no_commit)
631 - res = run_git_commit(defmsg, opts, allow);
633 + res = run_git_commit(git_path_merge_msg(), opts, allow);
634
635 leave:
636 free_message(commit, &msg);
635 - free(defmsg);
637
638 return res;
639 }
@@ -756,24 +757,23 @@ static int parse_insn_buffer(char *buf, struct commit_list **todo_list,
757 static void read_populate_todo(struct commit_list **todo_list,
758 struct replay_opts *opts)
759 {
759 - const char *todo_file = git_path(SEQ_TODO_FILE);
760 struct strbuf buf = STRBUF_INIT;
761 int fd, res;
762
763 - fd = open(todo_file, O_RDONLY);
763 + fd = open(git_path_todo_file(), O_RDONLY);
764 if (fd < 0)
765 - die_errno(_("Could not open %s"), todo_file);
765 + die_errno(_("Could not open %s"), git_path_todo_file());
766 if (strbuf_read(&buf, fd, 0) < 0) {
767 close(fd);
768 strbuf_release(&buf);
769 - die(_("Could not read %s."), todo_file);
769 + die(_("Could not read %s."), git_path_todo_file());
770 }
771 close(fd);
772
773 res = parse_insn_buffer(buf.buf, todo_list, opts);
774 strbuf_release(&buf);
775 if (res)
776 - die(_("Unusable instruction sheet: %s"), todo_file);
776 + die(_("Unusable instruction sheet: %s"), git_path_todo_file());
777 }
778
779 static int populate_opts_cb(const char *key, const char *value, void *data)
@@ -813,12 +813,10 @@ static int populate_opts_cb(const char *key, const char *value, void *data)
813
814 static void read_populate_opts(struct replay_opts **opts_ptr)
815 {
816 - const char *opts_file = git_path(SEQ_OPTS_FILE);
817 -
818 - if (!file_exists(opts_file))
816 + if (!file_exists(git_path_opts_file()))
817 return;
820 - if (git_config_from_file(populate_opts_cb, opts_file, *opts_ptr) < 0)
821 - die(_("Malformed options sheet: %s"), opts_file);
818 + if (git_config_from_file(populate_opts_cb, git_path_opts_file(), *opts_ptr) < 0)
819 + die(_("Malformed options sheet: %s"), git_path_opts_file());
820 }
821
822 static void walk_revs_populate_todo(struct commit_list **todo_list,
@@ -836,31 +834,29 @@ static void walk_revs_populate_todo(struct commit_list **todo_list,
834
835 static int create_seq_dir(void)
836 {
839 - const char *seq_dir = git_path(SEQ_DIR);
840 -
841 - if (file_exists(seq_dir)) {
837 + if (file_exists(git_path_seq_dir())) {
838 error(_("a cherry-pick or revert is already in progress"));
839 advise(_("try \"git cherry-pick (--continue | --quit | --abort)\""));
840 return -1;
841 }
846 - else if (mkdir(seq_dir, 0777) < 0)
847 - die_errno(_("Could not create sequencer directory %s"), seq_dir);
842 + else if (mkdir(git_path_seq_dir(), 0777) < 0)
843 + die_errno(_("Could not create sequencer directory %s"),
844 + git_path_seq_dir());
845 return 0;
846 }
847
848 static void save_head(const char *head)
849 {
853 - const char *head_file = git_path(SEQ_HEAD_FILE);
850 static struct lock_file head_lock;
851 struct strbuf buf = STRBUF_INIT;
852 int fd;
853
858 - fd = hold_lock_file_for_update(&head_lock, head_file, LOCK_DIE_ON_ERROR);
854 + fd = hold_lock_file_for_update(&head_lock, git_path_head_file(), LOCK_DIE_ON_ERROR);
855 strbuf_addf(&buf, "%s\n", head);
856 if (write_in_full(fd, buf.buf, buf.len) < 0)
861 - die_errno(_("Could not write to %s"), head_file);
857 + die_errno(_("Could not write to %s"), git_path_head_file());
858 if (commit_lock_file(&head_lock) < 0)
863 - die(_("Error wrapping up %s."), head_file);
859 + die(_("Error wrapping up %s."), git_path_head_file());
860 }
861
862 static int reset_for_rollback(const unsigned char *sha1)
@@ -877,8 +873,8 @@ static int rollback_single_pick(void)
873 {
874 unsigned char head_sha1[20];
875
880 - if (!file_exists(git_path("CHERRY_PICK_HEAD")) &&
881 - !file_exists(git_path("REVERT_HEAD")))
876 + if (!file_exists(git_path_cherry_pick_head()) &&
877 + !file_exists(git_path_revert_head()))
878 return error(_("no cherry-pick or revert in progress"));
879 if (read_ref_full("HEAD", 0, head_sha1, NULL))
880 return error(_("cannot resolve HEAD"));
@@ -889,13 +885,11 @@ static int rollback_single_pick(void)
885
886 static int sequencer_rollback(struct replay_opts *opts)
887 {
892 - const char *filename;
888 FILE *f;
889 unsigned char sha1[20];
890 struct strbuf buf = STRBUF_INIT;
891
897 - filename = git_path(SEQ_HEAD_FILE);
898 - f = fopen(filename, "r");
892 + f = fopen(git_path_head_file(), "r");
893 if (!f && errno == ENOENT) {
894 /*
895 * There is no multiple-cherry-pick in progress.
@@ -905,18 +899,18 @@ static int sequencer_rollback(struct replay_opts *opts)
899 return rollback_single_pick();
900 }
901 if (!f)
908 - return error(_("cannot open %s: %s"), filename,
902 + return error(_("cannot open %s: %s"), git_path_head_file(),
903 strerror(errno));
904 if (strbuf_getline(&buf, f, '\n')) {
911 - error(_("cannot read %s: %s"), filename, ferror(f) ?
912 - strerror(errno) : _("unexpected end of file"));
905 + error(_("cannot read %s: %s"), git_path_head_file(),
906 + ferror(f) ? strerror(errno) : _("unexpected end of file"));
907 fclose(f);
908 goto fail;
909 }
910 fclose(f);
911 if (get_sha1_hex(buf.buf, sha1) || buf.buf[40] != '\0') {
912 error(_("stored pre-cherry-pick HEAD file '%s' is corrupt"),
919 - filename);
913 + git_path_head_file());
914 goto fail;
915 }
916 if (reset_for_rollback(sha1))
@@ -931,28 +925,27 @@ fail:
925
926 static void save_todo(struct commit_list *todo_list, struct replay_opts *opts)
927 {
934 - const char *todo_file = git_path(SEQ_TODO_FILE);
928 static struct lock_file todo_lock;
929 struct strbuf buf = STRBUF_INIT;
930 int fd;
931
939 - fd = hold_lock_file_for_update(&todo_lock, todo_file, LOCK_DIE_ON_ERROR);
932 + fd = hold_lock_file_for_update(&todo_lock, git_path_todo_file(), LOCK_DIE_ON_ERROR);
933 if (format_todo(&buf, todo_list, opts) < 0)
941 - die(_("Could not format %s."), todo_file);
934 + die(_("Could not format %s."), git_path_todo_file());
935 if (write_in_full(fd, buf.buf, buf.len) < 0) {
936 strbuf_release(&buf);
944 - die_errno(_("Could not write to %s"), todo_file);
937 + die_errno(_("Could not write to %s"), git_path_todo_file());
938 }
939 if (commit_lock_file(&todo_lock) < 0) {
940 strbuf_release(&buf);
948 - die(_("Error wrapping up %s."), todo_file);
941 + die(_("Error wrapping up %s."), git_path_todo_file());
942 }
943 strbuf_release(&buf);
944 }
945
946 static void save_opts(struct replay_opts *opts)
947 {
955 - const char *opts_file = git_path(SEQ_OPTS_FILE);
948 + const char *opts_file = git_path_opts_file();
949
950 if (opts->no_commit)
951 git_config_set_in_file(opts_file, "options.no-commit", "true");
@@ -1013,8 +1006,8 @@ static int continue_single_pick(void)
1006 {
1007 const char *argv[] = { "commit", NULL };
1008
1016 - if (!file_exists(git_path("CHERRY_PICK_HEAD")) &&
1017 - !file_exists(git_path("REVERT_HEAD")))
1009 + if (!file_exists(git_path_cherry_pick_head()) &&
1010 + !file_exists(git_path_revert_head()))
1011 return error(_("no cherry-pick or revert in progress"));
1012 return run_command_v_opt(argv, RUN_GIT_CMD);
1013 }
@@ -1023,14 +1016,14 @@ static int sequencer_continue(struct replay_opts *opts)
1016 {
1017 struct commit_list *todo_list = NULL;
1018
1026 - if (!file_exists(git_path(SEQ_TODO_FILE)))
1019 + if (!file_exists(git_path_todo_file()))
1020 return continue_single_pick();
1021 read_populate_opts(&opts);
1022 read_populate_todo(&todo_list, opts);
1023
1024 /* Verify that the conflict has been resolved */
1032 - if (file_exists(git_path("CHERRY_PICK_HEAD")) ||
1033 - file_exists(git_path("REVERT_HEAD"))) {
1025 + if (file_exists(git_path_cherry_pick_head()) ||
1026 + file_exists(git_path_revert_head())) {
1027 int ret = continue_single_pick();
1028 if (ret)
1029 return ret;
shallow.c
+5 -5
@@ -48,7 +48,7 @@ int is_repository_shallow(void)
48 return is_shallow;
49
50 if (!path)
51 - path = git_path("shallow");
51 + path = git_path_shallow();
52 /*
53 * fetch-pack sets '--shallow-file ""' as an indicator that no
54 * shallow file should be used. We could just open it and it
@@ -142,7 +142,7 @@ static void check_shallow_file_for_update(void)
142 if (is_shallow == -1)
143 die("BUG: shallow must be initialized by now");
144
145 - if (!stat_validity_check(&shallow_stat, git_path("shallow")))
145 + if (!stat_validity_check(&shallow_stat, git_path_shallow()))
146 die("shallow file has changed since we read it");
147 }
148
@@ -261,7 +261,7 @@ void setup_alternate_shallow(struct lock_file *shallow_lock,
261 struct strbuf sb = STRBUF_INIT;
262 int fd;
263
264 - fd = hold_lock_file_for_update(shallow_lock, git_path("shallow"),
264 + fd = hold_lock_file_for_update(shallow_lock, git_path_shallow(),
265 LOCK_DIE_ON_ERROR);
266 check_shallow_file_for_update();
267 if (write_shallow_commits(&sb, 0, extra)) {
@@ -308,7 +308,7 @@ void prune_shallow(int show_only)
308 strbuf_release(&sb);
309 return;
310 }
311 - fd = hold_lock_file_for_update(&shallow_lock, git_path("shallow"),
311 + fd = hold_lock_file_for_update(&shallow_lock, git_path_shallow(),
312 LOCK_DIE_ON_ERROR);
313 check_shallow_file_for_update();
314 if (write_shallow_commits_1(&sb, 0, NULL, SEEN_ONLY)) {
@@ -317,7 +317,7 @@ void prune_shallow(int show_only)
317 shallow_lock.filename.buf);
318 commit_lock_file(&shallow_lock);
319 } else {
320 - unlink(git_path("shallow"));
320 + unlink(git_path_shallow());
321 rollback_lock_file(&shallow_lock);
322 }
323 strbuf_release(&sb);
wt-status.c
+4 -4
@@ -1171,7 +1171,7 @@ static void show_rebase_in_progress(struct wt_status *s,
1171 status_printf_ln(s, color,
1172 _(" (use \"git rebase --abort\" to check out the original branch)"));
1173 }
1174 - } else if (state->rebase_in_progress || !stat(git_path("MERGE_MSG"), &st)) {
1174 + } else if (state->rebase_in_progress || !stat(git_path_merge_msg(), &st)) {
1175 print_rebase_state(s, state, color);
1176 if (s->hints)
1177 status_printf_ln(s, color,
@@ -1368,7 +1368,7 @@ void wt_status_get_state(struct wt_status_state *state,
1368 struct stat st;
1369 unsigned char sha1[20];
1370
1371 - if (!stat(git_path("MERGE_HEAD"), &st)) {
1371 + if (!stat(git_path_merge_head(), &st)) {
1372 state->merge_in_progress = 1;
1373 } else if (!stat(git_path("rebase-apply"), &st)) {
1374 if (!stat(git_path("rebase-apply/applying"), &st)) {
@@ -1387,7 +1387,7 @@ void wt_status_get_state(struct wt_status_state *state,
1387 state->rebase_in_progress = 1;
1388 state->branch = read_and_strip_branch("rebase-merge/head-name");
1389 state->onto = read_and_strip_branch("rebase-merge/onto");
1390 - } else if (!stat(git_path("CHERRY_PICK_HEAD"), &st) &&
1390 + } else if (!stat(git_path_cherry_pick_head(), &st) &&
1391 !get_sha1("CHERRY_PICK_HEAD", sha1)) {
1392 state->cherry_pick_in_progress = 1;
1393 hashcpy(state->cherry_pick_head_sha1, sha1);
@@ -1396,7 +1396,7 @@ void wt_status_get_state(struct wt_status_state *state,
1396 state->bisect_in_progress = 1;
1397 state->branch = read_and_strip_branch("BISECT_START");
1398 }
1399 - if (!stat(git_path("REVERT_HEAD"), &st) &&
1399 + if (!stat(git_path_revert_head(), &st) &&
1400 !get_sha1("REVERT_HEAD", sha1)) {
1401 state->revert_in_progress = 1;
1402 hashcpy(state->revert_head_sha1, sha1);