{"thread":{"id":"43729","subject":"Re: [RFC/PATCH] Change \"refs/\" references to symbolic constants","startedAt":"2006-12-14T16:44:05Z","lastAt":"2006-12-17T09:00:18Z","messageCount":3,"participants":["Junio C Hamano","Andy Parkins"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"297451","messageId":"200612141644.06167.andyparkins@gmail.com","threadId":"43729","inReplyTo":null,"subject":"[RFC/PATCH] Change \"refs/\" references to symbolic constants","fromName":"Andy Parkins","fromEmail":"andyparkins@gmail.com","sentAt":"2006-12-14T16:44:05Z","receivedAt":"2006-12-14T16:44:05Z","isPatch":true,"sender":{"key":"andyparkins@gmail.com","avatar":null},"body":"Changed repeated use of the same constants for the ref paths to be\nsymbolic constants.  I've defined them in refs.h\n\n  refs/ is now PATH_REFS\n  refs/heads/ is now PATH_REFS_HEADS\n  refs/tags/ is now PATH_REFS_TAGS\n  refs/remotes/ is now PATH_REFS_REMOTES\n\nI've changed all references to them and made constants for the string\nlengths as well.  This has clarified the code in some places; for\nexample:\n\n - len = strlen(refs[i]) + 11;\n + len = strlen(refs[i]) + STRLEN_PATH_REFS_TAGS + 1;\n\nIn this case 11 isn't STRLEN_PATH_REFS_HEADS, as it is in most other\ncases, it's TAGS + 1.  With the change to symbolic constants it's much\nclearer what is happening.\n\nSigned-off-by: Andy Parkins <andyparkins@gmail.com>\n---\n\nI chose to add the constants to refs.h; unfortunately that meant adding\nsome #include \"refs.h\" around the place.\n\nIs that wise?  Would I be better putting the constants in cache.h?\n\nI'm tempted to add a constant for \"objects/\" as well, to complete the set.\nWould there be any objections?\n\n builtin-branch.c        |   24 ++++++++++++------------\n builtin-fmt-merge-msg.c |    5 +++--\n builtin-init-db.c       |   15 ++++++++-------\n builtin-name-rev.c      |   10 +++++-----\n builtin-pack-refs.c     |    2 +-\n builtin-push.c          |    8 ++++----\n builtin-show-branch.c   |   28 ++++++++++++++--------------\n builtin-show-ref.c      |    4 ++--\n connect.c               |   18 +++++++++---------\n describe.c              |    2 +-\n fetch-pack.c            |    4 ++--\n fsck-objects.c          |    2 +-\n http-fetch.c            |    5 +++--\n http-push.c             |    4 ++--\n local-fetch.c           |    3 ++-\n path.c                  |    5 +++--\n receive-pack.c          |    4 ++--\n refs.c                  |   16 ++++++++--------\n refs.h                  |   15 +++++++++++++++\n setup.c                 |    5 +++--\n sha1_name.c             |   10 +++++-----\n wt-status.c             |    3 ++-\n 22 files changed, 107 insertions(+), 85 deletions(-)\n\ndiff --git a/builtin-branch.c b/builtin-branch.c\nindex 560309c..1f4b6f0 100644\n--- a/builtin-branch.c\n+++ b/builtin-branch.c\n@@ -105,7 +105,7 @@ static void delete_branches(int argc, const char **argv, \nint force)\n \t\tif (!strcmp(head, argv[i]))\n \t\t\tdie(\"Cannot delete the branch you are currently on.\");\n \n-\t\tname = xstrdup(mkpath(\"refs/heads/%s\", argv[i]));\n+\t\tname = xstrdup(mkpath(PATH_REFS_HEADS \"%s\", argv[i]));\n \t\tif (!resolve_ref(name, sha1, 1, NULL))\n \t\t\tdie(\"Branch '%s' not found.\", argv[i]);\n \n@@ -161,15 +161,15 @@ static int append_ref(const char *refname, const \nunsigned char *sha1, int flags,\n \tint len;\n \n \t/* Detect kind */\n-\tif (!strncmp(refname, \"refs/heads/\", 11)) {\n+\tif (!strncmp(refname, PATH_REFS_HEADS, STRLEN_PATH_REFS_HEADS)) {\n \t\tkind = REF_LOCAL_BRANCH;\n-\t\trefname += 11;\n-\t} else if (!strncmp(refname, \"refs/remotes/\", 13)) {\n+\t\trefname += STRLEN_PATH_REFS_HEADS;\n+\t} else if (!strncmp(refname, PATH_REFS_REMOTES, STRLEN_PATH_REFS_REMOTES)) {\n \t\tkind = REF_REMOTE_BRANCH;\n-\t\trefname += 13;\n-\t} else if (!strncmp(refname, \"refs/tags/\", 10)) {\n+\t\trefname += STRLEN_PATH_REFS_REMOTES;\n+\t} else if (!strncmp(refname, PATH_REFS_TAGS, STRLEN_PATH_REFS_TAGS)) {\n \t\tkind = REF_TAG;\n-\t\trefname += 10;\n+\t\trefname += STRLEN_PATH_REFS_TAGS;\n \t}\n \n \t/* Don't add types the caller doesn't want */\n@@ -290,7 +290,7 @@ static void create_branch(const char *name, const char \n*start,\n \tunsigned char sha1[20];\n \tchar ref[PATH_MAX], msg[PATH_MAX + 20];\n \n-\tsnprintf(ref, sizeof ref, \"refs/heads/%s\", name);\n+\tsnprintf(ref, sizeof ref, PATH_REFS_HEADS \"%s\", name);\n \tif (check_ref_format(ref))\n \t\tdie(\"'%s' is not a valid branch name.\", name);\n \n@@ -324,13 +324,13 @@ static void rename_branch(const char *oldname, const \nchar *newname, int force)\n \tchar oldref[PATH_MAX], newref[PATH_MAX], logmsg[PATH_MAX*2 + 100];\n \tunsigned char sha1[20];\n \n-\tif (snprintf(oldref, sizeof(oldref), \"refs/heads/%s\", oldname) > \nsizeof(oldref))\n+\tif (snprintf(oldref, sizeof(oldref), PATH_REFS_HEADS \"%s\", oldname) > \nsizeof(oldref))\n \t\tdie(\"Old branchname too long\");\n \n \tif (check_ref_format(oldref))\n \t\tdie(\"Invalid branch name: %s\", oldref);\n \n-\tif (snprintf(newref, sizeof(newref), \"refs/heads/%s\", newname) > \nsizeof(newref))\n+\tif (snprintf(newref, sizeof(newref), PATH_REFS_HEADS \"%s\", newname) > \nsizeof(newref))\n \t\tdie(\"New branchname too long\");\n \n \tif (check_ref_format(newref))\n@@ -430,8 +430,8 @@ int cmd_branch(int argc, const char **argv, const char \n*prefix)\n \thead = xstrdup(resolve_ref(\"HEAD\", head_sha1, 0, NULL));\n \tif (!head)\n \t\tdie(\"Failed to resolve HEAD as a valid ref.\");\n-\tif (strncmp(head, \"refs/heads/\", 11))\n-\t\tdie(\"HEAD not found below refs/heads!\");\n+\tif (strncmp(head, PATH_REFS_HEADS, STRLEN_PATH_REFS_HEADS))\n+\t\tdie(\"HEAD not found below \" PATH_REFS_HEADS \"!\");\n \thead += 11;\n \n \tif (delete)\ndiff --git a/builtin-fmt-merge-msg.c b/builtin-fmt-merge-msg.c\nindex 87d3d63..d0615b5 100644\n--- a/builtin-fmt-merge-msg.c\n+++ b/builtin-fmt-merge-msg.c\n@@ -4,6 +4,7 @@\n #include \"diff.h\"\n #include \"revision.h\"\n #include \"tag.h\"\n+#include \"refs.h\"\n \n static const char *fmt_merge_msg_usage =\n \t\"git-fmt-merge-msg [--summary] [--no-summary] [--file <file>]\";\n@@ -280,8 +281,8 @@ int cmd_fmt_merge_msg(int argc, const char **argv, const \nchar *prefix)\n \tcurrent_branch = resolve_ref(\"HEAD\", head_sha1, 1, NULL);\n \tif (!current_branch)\n \t\tdie(\"No current branch\");\n-\tif (!strncmp(current_branch, \"refs/heads/\", 11))\n-\t\tcurrent_branch += 11;\n+\tif (!strncmp(current_branch, PATH_REFS_HEADS, STRLEN_PATH_REFS_HEADS))\n+\t\tcurrent_branch += STRLEN_PATH_REFS_HEADS;\n \n \twhile (fgets(line, sizeof(line), in)) {\n \t\ti++;\ndiff --git a/builtin-init-db.c b/builtin-init-db.c\nindex 235a0ee..df343ea 100644\n--- a/builtin-init-db.c\n+++ b/builtin-init-db.c\n@@ -5,6 +5,7 @@\n  */\n #include \"cache.h\"\n #include \"builtin.h\"\n+#include \"refs.h\"\n \n #ifndef DEFAULT_GIT_TEMPLATE_DIR\n #define DEFAULT_GIT_TEMPLATE_DIR \"/usr/share/git-core/templates/\"\n@@ -182,11 +183,11 @@ static void create_default_files(const char *git_dir, \nconst char *template_path)\n \t/*\n \t * Create .git/refs/{heads,tags}\n \t */\n-\tstrcpy(path + len, \"refs\");\n+\tstrcpy(path + len, PATH_REFS);\n \tsafe_create_dir(path, 1);\n-\tstrcpy(path + len, \"refs/heads\");\n+\tstrcpy(path + len, PATH_REFS_HEADS);\n \tsafe_create_dir(path, 1);\n-\tstrcpy(path + len, \"refs/tags\");\n+\tstrcpy(path + len, PATH_REFS_TAGS);\n \tsafe_create_dir(path, 1);\n \n \t/* First copy the templates -- we might have the default\n@@ -205,11 +206,11 @@ static void create_default_files(const char *git_dir, \nconst char *template_path)\n \tif (shared_repository) {\n \t\tpath[len] = 0;\n \t\tadjust_shared_perm(path);\n-\t\tstrcpy(path + len, \"refs\");\n+\t\tstrcpy(path + len, PATH_REFS);\n \t\tadjust_shared_perm(path);\n-\t\tstrcpy(path + len, \"refs/heads\");\n+\t\tstrcpy(path + len, PATH_REFS_HEADS);\n \t\tadjust_shared_perm(path);\n-\t\tstrcpy(path + len, \"refs/tags\");\n+\t\tstrcpy(path + len, PATH_REFS_TAGS);\n \t\tadjust_shared_perm(path);\n \t}\n \n@@ -219,7 +220,7 @@ static void create_default_files(const char *git_dir, \nconst char *template_path)\n \t */\n \tstrcpy(path + len, \"HEAD\");\n \tif (read_ref(\"HEAD\", sha1) < 0) {\n-\t\tif (create_symref(\"HEAD\", \"refs/heads/master\") < 0)\n+\t\tif (create_symref(\"HEAD\", PATH_REFS_HEADS \"master\") < 0)\n \t\t\texit(1);\n \t}\n \ndiff --git a/builtin-name-rev.c b/builtin-name-rev.c\nindex 618aa31..ef66b1f 100644\n--- a/builtin-name-rev.c\n+++ b/builtin-name-rev.c\n@@ -81,7 +81,7 @@ static int name_ref(const char *path, const unsigned char \n*sha1, int flags, void\n \tint tags_only = *(int*)cb_data;\n \tint deref = 0;\n \n-\tif (tags_only && strncmp(path, \"refs/tags/\", 10))\n+\tif (tags_only && strncmp(path, PATH_REFS_TAGS, STRLEN_PATH_REFS_TAGS))\n \t\treturn 0;\n \n \twhile (o && o->type == OBJ_TAG) {\n@@ -94,10 +94,10 @@ static int name_ref(const char *path, const unsigned char \n*sha1, int flags, void\n \tif (o && o->type == OBJ_COMMIT) {\n \t\tstruct commit *commit = (struct commit *)o;\n \n-\t\tif (!strncmp(path, \"refs/heads/\", 11))\n-\t\t\tpath = path + 11;\n-\t\telse if (!strncmp(path, \"refs/\", 5))\n-\t\t\tpath = path + 5;\n+\t\tif (!strncmp(path, PATH_REFS_HEADS, STRLEN_PATH_REFS_HEADS))\n+\t\t\tpath = path + STRLEN_PATH_REFS_HEADS;\n+\t\telse if (!strncmp(path, PATH_REFS, STRLEN_PATH_REFS))\n+\t\t\tpath = path + STRLEN_PATH_REFS;\n \n \t\tname_rev(commit, xstrdup(path), 0, 0, deref);\n \t}\ndiff --git a/builtin-pack-refs.c b/builtin-pack-refs.c\nindex 8dc5b9e..c15f629 100644\n--- a/builtin-pack-refs.c\n+++ b/builtin-pack-refs.c\n@@ -36,7 +36,7 @@ static int handle_one_ref(const char *path, const unsigned \nchar *sha1,\n \t/* Do not pack the symbolic refs */\n \tif ((flags & REF_ISSYMREF))\n \t\treturn 0;\n-\tis_tag_ref = !strncmp(path, \"refs/tags/\", 10);\n+\tis_tag_ref = !strncmp(path, PATH_REFS_TAGS, STRLEN_PATH_REFS_TAGS);\n \tif (!cb->all && !is_tag_ref)\n \t\treturn 0;\n \ndiff --git a/builtin-push.c b/builtin-push.c\nindex b7412e8..de3eb46 100644\n--- a/builtin-push.c\n+++ b/builtin-push.c\n@@ -30,9 +30,9 @@ static void add_refspec(const char *ref)\n static int expand_one_ref(const char *ref, const unsigned char *sha1, int \nflag, void *cb_data)\n {\n \t/* Ignore the \"refs/\" at the beginning of the refname */\n-\tref += 5;\n+\tref += STRLEN_PATH_REFS;\n \n-\tif (!strncmp(ref, \"tags/\", 5))\n+\tif (!strncmp(ref, PATH_TAGS, STRLEN_PATH_TAGS))\n \t\tadd_refspec(xstrdup(ref));\n \treturn 0;\n }\n@@ -68,9 +68,9 @@ static void set_refspecs(const char **refs, int nr)\n \t\t\t\t\tif (nr <= ++i)\n \t\t\t\t\t\tdie(\"tag <tag> shorthand without <tag>\");\n \t\t\t\t\tif (pass) {\n-\t\t\t\t\t\tlen = strlen(refs[i]) + 11;\n+\t\t\t\t\t\tlen = strlen(refs[i]) + STRLEN_PATH_REFS_TAGS + 1;\n \t\t\t\t\t\ttag = xmalloc(len);\n-\t\t\t\t\t\tstrcpy(tag, \"refs/tags/\");\n+\t\t\t\t\t\tstrcpy(tag, PATH_REFS_TAGS);\n \t\t\t\t\t\tstrcat(tag, refs[i]);\n \t\t\t\t\t\trefspec[cnt] = tag;\n \t\t\t\t\t}\ndiff --git a/builtin-show-branch.c b/builtin-show-branch.c\nindex fb1a400..d598d2d 100644\n--- a/builtin-show-branch.c\n+++ b/builtin-show-branch.c\n@@ -372,22 +372,22 @@ static int append_ref(const char *refname, const \nunsigned char *sha1, int flag,\n static int append_head_ref(const char *refname, const unsigned char *sha1, \nint flag, void *cb_data)\n {\n \tunsigned char tmp[20];\n-\tint ofs = 11;\n-\tif (strncmp(refname, \"refs/heads/\", ofs))\n+\tint ofs = STRLEN_PATH_REFS_HEADS;\n+\tif (strncmp(refname, PATH_REFS_HEADS, ofs))\n \t\treturn 0;\n \t/* If both heads/foo and tags/foo exists, get_sha1 would\n \t * get confused.\n \t */\n \tif (get_sha1(refname + ofs, tmp) || hashcmp(tmp, sha1))\n-\t\tofs = 5;\n+\t\tofs = STRLEN_PATH_REFS;\n \treturn append_ref(refname + ofs, sha1, flag, cb_data);\n }\n \n static int append_tag_ref(const char *refname, const unsigned char *sha1, int \nflag, void *cb_data)\n {\n-\tif (strncmp(refname, \"refs/tags/\", 10))\n+\tif (strncmp(refname, PATH_REFS_TAGS, STRLEN_PATH_REFS_TAGS))\n \t\treturn 0;\n-\treturn append_ref(refname + 5, sha1, flag, cb_data);\n+\treturn append_ref(refname + STRLEN_PATH_REFS, sha1, flag, cb_data);\n }\n \n static const char *match_ref_pattern = NULL;\n@@ -416,9 +416,9 @@ static int append_matching_ref(const char *refname, const \nunsigned char *sha1, i\n \t\treturn 0;\n \tif (fnmatch(match_ref_pattern, tail, 0))\n \t\treturn 0;\n-\tif (!strncmp(\"refs/heads/\", refname, 11))\n+\tif (!strncmp(PATH_REFS_HEADS, refname, STRLEN_PATH_REFS_HEADS))\n \t\treturn append_head_ref(refname, sha1, flag, cb_data);\n-\tif (!strncmp(\"refs/tags/\", refname, 10))\n+\tif (!strncmp(PATH_REFS_TAGS, refname, STRLEN_PATH_REFS_TAGS))\n \t\treturn append_tag_ref(refname, sha1, flag, cb_data);\n \treturn append_ref(refname, sha1, flag, cb_data);\n }\n@@ -443,12 +443,12 @@ static int rev_is_head(char *head, int headlen, char \n*name,\n \tif ((!head[0]) ||\n \t    (head_sha1 && sha1 && hashcmp(head_sha1, sha1)))\n \t\treturn 0;\n-\tif (!strncmp(head, \"refs/heads/\", 11))\n-\t\thead += 11;\n-\tif (!strncmp(name, \"refs/heads/\", 11))\n-\t\tname += 11;\n-\telse if (!strncmp(name, \"heads/\", 6))\n-\t\tname += 6;\n+\tif (!strncmp(head, PATH_REFS_HEADS, STRLEN_PATH_REFS_HEADS))\n+\t\thead += STRLEN_PATH_REFS_HEADS;\n+\tif (!strncmp(name, PATH_REFS_HEADS, STRLEN_PATH_REFS_HEADS))\n+\t\tname += STRLEN_PATH_REFS_HEADS;\n+\telse if (!strncmp(name, PATH_HEADS, STRLEN_PATH_HEADS))\n+\t\tname += STRLEN_PATH_HEADS;\n \treturn !strcmp(head, name);\n }\n \n@@ -659,7 +659,7 @@ int cmd_show_branch(int ac, const char **av, const char \n*prefix)\n \t\t\t\thas_head++;\n \t\t}\n \t\tif (!has_head) {\n-\t\t\tint pfxlen = strlen(\"refs/heads/\");\n+\t\t\tint pfxlen = STRLEN_PATH_REFS_HEADS;\n \t\t\tappend_one_rev(head + pfxlen);\n \t\t}\n \t}\ndiff --git a/builtin-show-ref.c b/builtin-show-ref.c\nindex 0739798..29ec05c 100644\n--- a/builtin-show-ref.c\n+++ b/builtin-show-ref.c\n@@ -18,8 +18,8 @@ static int show_ref(const char *refname, const unsigned char \n*sha1, int flag, vo\n \tif (tags_only || heads_only) {\n \t\tint match;\n \n-\t\tmatch = heads_only && !strncmp(refname, \"refs/heads/\", 11);\n-\t\tmatch |= tags_only && !strncmp(refname, \"refs/tags/\", 10);\n+\t\tmatch = heads_only && !strncmp(refname, PATH_REFS_HEADS, \nSTRLEN_PATH_REFS_HEADS);\n+\t\tmatch |= tags_only && !strncmp(refname, PATH_REFS_TAGS, \nSTRLEN_PATH_REFS_TAGS);\n \t\tif (!match)\n \t\t\treturn 0;\n \t}\ndiff --git a/connect.c b/connect.c\nindex f7edba8..49bfa37 100644\n--- a/connect.c\n+++ b/connect.c\n@@ -17,23 +17,23 @@ static int check_ref(const char *name, int len, unsigned \nint flags)\n \tif (!flags)\n \t\treturn 1;\n \n-\tif (len < 5 || memcmp(name, \"refs/\", 5))\n+\tif (len < STRLEN_PATH_REFS || memcmp(name, PATH_REFS, STRLEN_PATH_REFS))\n \t\treturn 0;\n \n \t/* Skip the \"refs/\" part */\n-\tname += 5;\n-\tlen -= 5;\n+\tname += STRLEN_PATH_REFS;\n+\tlen -= STRLEN_PATH_REFS;\n \n \t/* REF_NORMAL means that we don't want the magic fake tag refs */\n \tif ((flags & REF_NORMAL) && check_ref_format(name) < 0)\n \t\treturn 0;\n \n \t/* REF_HEADS means that we want regular branch heads */\n-\tif ((flags & REF_HEADS) && !memcmp(name, \"heads/\", 6))\n+\tif ((flags & REF_HEADS) && !memcmp(name, PATH_HEADS, STRLEN_PATH_HEADS))\n \t\treturn 1;\n \n \t/* REF_TAGS means that we want tags */\n-\tif ((flags & REF_TAGS) && !memcmp(name, \"tags/\", 5))\n+\tif ((flags & REF_TAGS) && !memcmp(name, PATH_TAGS, STRLEN_PATH_TAGS))\n \t\treturn 1;\n \n \t/* All type bits clear means that we are ok with anything */\n@@ -202,8 +202,8 @@ static int count_refspec_match(const char *pattern,\n \t\t */\n \t\tif (namelen != patlen &&\n \t\t    patlen != namelen - 5 &&\n-\t\t    strncmp(name, \"refs/heads/\", 11) &&\n-\t\t    strncmp(name, \"refs/tags/\", 10)) {\n+\t\t    strncmp(name, PATH_REFS_HEADS, STRLEN_PATH_REFS_HEADS) &&\n+\t\t    strncmp(name, PATH_REFS_TAGS, STRLEN_PATH_REFS_TAGS)) {\n \t\t\t/* We want to catch the case where only weak\n \t\t\t * matches are found and there are multiple\n \t\t\t * matches, and where more than one strong\n@@ -290,7 +290,7 @@ static int match_explicit_refs(struct ref *src, struct ref \n*dst,\n \t\tcase 1:\n \t\t\tbreak;\n \t\tcase 0:\n-\t\t\tif (!memcmp(rs[i].dst, \"refs/\", 5)) {\n+\t\t\tif (!memcmp(rs[i].dst, PATH_REFS, STRLEN_PATH_REFS)) {\n \t\t\t\tint len = strlen(rs[i].dst) + 1;\n \t\t\t\tmatched_dst = xcalloc(1, sizeof(*dst) + len);\n \t\t\t\tmemcpy(matched_dst->name, rs[i].dst, len);\n@@ -311,7 +311,7 @@ static int match_explicit_refs(struct ref *src, struct ref \n*dst,\n \t\t\t\terrs = 1;\n \t\t\t\terror(\"dst refspec %s does not match any \"\n \t\t\t\t      \"existing ref on the remote and does \"\n-\t\t\t\t      \"not start with refs/.\", rs[i].dst);\n+\t\t\t\t      \"not start with \" PATH_REFS \".\", rs[i].dst);\n \t\t\t}\n \t\t\tbreak;\n \t\tdefault:\ndiff --git a/describe.c b/describe.c\nindex f4029ee..e3b0baa 100644\n--- a/describe.c\n+++ b/describe.c\n@@ -66,7 +66,7 @@ static int get_name(const char *path, const unsigned char \n*sha1, int flag, void\n \t * If --tags, then any tags are used.\n \t * Otherwise only annotated tags are used.\n \t */\n-\tif (!strncmp(path, \"refs/tags/\", 10)) {\n+\tif (!strncmp(path, PATH_REFS_TAGS, STRLEN_PATH_REFS_TAGS)) {\n \t\tif (object->type == OBJ_TAG)\n \t\t\tprio = 2;\n \t\telse\ndiff --git a/fetch-pack.c b/fetch-pack.c\nindex 743eab7..e67b2fe 100644\n--- a/fetch-pack.c\n+++ b/fetch-pack.c\n@@ -307,8 +307,8 @@ static void filter_refs(struct ref **refs, int nr_match, \nchar **match)\n \n \tfor (ref = *refs; ref; ref = next) {\n \t\tnext = ref->next;\n-\t\tif (!memcmp(ref->name, \"refs/\", 5) &&\n-\t\t    check_ref_format(ref->name + 5))\n+\t\tif (!memcmp(ref->name, PATH_REFS, STRLEN_PATH_REFS) &&\n+\t\t    check_ref_format(ref->name + STRLEN_PATH_REFS))\n \t\t\t; /* trash */\n \t\telse if (fetch_all) {\n \t\t\t*newtail = ref;\ndiff --git a/fsck-objects.c b/fsck-objects.c\nindex 46b628c..f9b1218 100644\n--- a/fsck-objects.c\n+++ b/fsck-objects.c\n@@ -463,7 +463,7 @@ static int fsck_head_link(void)\n \n \tif (!head_points_at || !(flag & REF_ISSYMREF))\n \t\treturn error(\"HEAD is not a symbolic ref\");\n-\tif (strncmp(head_points_at, \"refs/heads/\", 11))\n+\tif (strncmp(head_points_at, PATH_REFS_HEADS, STRLEN_PATH_REFS_HEADS))\n \t\treturn error(\"HEAD points to something strange (%s)\",\n \t\t\t     head_points_at);\n \tif (is_null_sha1(sha1))\ndiff --git a/http-fetch.c b/http-fetch.c\nindex 396552d..2c02690 100644\n--- a/http-fetch.c\n+++ b/http-fetch.c\n@@ -3,6 +3,7 @@\n #include \"pack.h\"\n #include \"fetch.h\"\n #include \"http.h\"\n+#include \"refs.h\"\n \n #define PREV_BUF_SIZE 4096\n #define RANGE_HEADER_SIZE 30\n@@ -943,8 +944,8 @@ static char *quote_ref_url(const char *base, const char \n*ref)\n \t\t\tlen += 2; /* extra two hex plus replacement % */\n \tqref = xmalloc(len);\n \tmemcpy(qref, base, baselen);\n-\tmemcpy(qref + baselen, \"refs/\", 5);\n-\tfor (cp = ref, dp = qref + baselen + 5; (ch = *cp) != 0; cp++) {\n+\tmemcpy(qref + baselen, PATH_REFS, STRLEN_PATH_REFS);\n+\tfor (cp = ref, dp = qref + baselen + STRLEN_PATH_REFS; (ch = *cp) != 0; \ncp++) {\n \t\tif (needs_quote(ch)) {\n \t\t\t*dp++ = '%';\n \t\t\t*dp++ = hex((ch >> 4) & 0xF);\ndiff --git a/http-push.c b/http-push.c\nindex ecefdfd..6f17acf 100644\n--- a/http-push.c\n+++ b/http-push.c\n@@ -1919,7 +1919,7 @@ static void get_local_heads(void)\n static void get_dav_remote_heads(void)\n {\n \tremote_tail = &remote_refs;\n-\tremote_ls(\"refs/\", (PROCESS_FILES | PROCESS_DIRS | RECURSIVE), \nprocess_ls_ref, NULL);\n+\tremote_ls(PATH_REFS, (PROCESS_FILES | PROCESS_DIRS | RECURSIVE), \nprocess_ls_ref, NULL);\n }\n \n static int is_zero_sha1(const unsigned char *sha1)\n@@ -2063,7 +2063,7 @@ static void update_remote_info_refs(struct remote_lock \n*lock)\n \tbuffer.buffer = xcalloc(1, 4096);\n \tbuffer.size = 4096;\n \tbuffer.posn = 0;\n-\tremote_ls(\"refs/\", (PROCESS_FILES | RECURSIVE),\n+\tremote_ls(PATH_REFS, (PROCESS_FILES | RECURSIVE),\n \t\t  add_remote_info_ref, &buffer);\n \tif (!aborted) {\n \t\tif_header = xmalloc(strlen(lock->token) + 25);\ndiff --git a/local-fetch.c b/local-fetch.c\nindex 7b6875c..99c5cb2 100644\n--- a/local-fetch.c\n+++ b/local-fetch.c\n@@ -4,6 +4,7 @@\n #include \"cache.h\"\n #include \"commit.h\"\n #include \"fetch.h\"\n+#include \"refs.h\"\n \n static int use_link;\n static int use_symlink;\n@@ -174,7 +175,7 @@ int fetch_ref(char *ref, unsigned char *sha1)\n \tint ifd;\n \n \tif (ref_name_start < 0) {\n-\t\tsprintf(filename, \"%s/refs/\", path);\n+\t\tsprintf(filename, \"%s/\" PATH_REFS, path);\n \t\tref_name_start = strlen(filename);\n \t}\n \tstrcpy(filename + ref_name_start, ref);\ndiff --git a/path.c b/path.c\nindex d2c076d..10be22c 100644\n--- a/path.c\n+++ b/path.c\n@@ -11,6 +11,7 @@\n  * which is what it's designed for.\n  */\n #include \"cache.h\"\n+#include \"refs.h\"\n #include <pwd.h>\n \n static char bad_path[] = \"/bad-path/\";\n@@ -103,7 +104,7 @@ int validate_symref(const char *path)\n \t/* Make sure it is a \"refs/..\" symlink */\n \tif (S_ISLNK(st.st_mode)) {\n \t\tlen = readlink(path, buffer, sizeof(buffer)-1);\n-\t\tif (len >= 5 && !memcmp(\"refs/\", buffer, 5))\n+\t\tif (len >= 5 && !memcmp(PATH_REFS, buffer, STRLEN_PATH_REFS))\n \t\t\treturn 0;\n \t\treturn -1;\n \t}\n@@ -126,7 +127,7 @@ int validate_symref(const char *path)\n \tlen -= 4;\n \twhile (len && isspace(*buf))\n \t\tbuf++, len--;\n-\tif (len >= 5 && !memcmp(\"refs/\", buf, 5))\n+\tif (len >= STRLEN_PATH_REFS && !memcmp(PATH_REFS, buf, STRLEN_PATH_REFS))\n \t\treturn 0;\n \treturn -1;\n }\ndiff --git a/receive-pack.c b/receive-pack.c\nindex e76d9ae..0d22120 100644\n--- a/receive-pack.c\n+++ b/receive-pack.c\n@@ -105,7 +105,7 @@ static int update(struct command *cmd)\n \tstruct ref_lock *lock;\n \n \tcmd->error_string = NULL;\n-\tif (!strncmp(name, \"refs/\", 5) && check_ref_format(name + 5)) {\n+\tif (!strncmp(name, PATH_REFS, STRLEN_PATH_REFS) && check_ref_format(name + \nSTRLEN_PATH_REFS)) {\n \t\tcmd->error_string = \"funny refname\";\n \t\treturn error(\"refusing to create funny ref '%s' locally\",\n \t\t\t     name);\n@@ -121,7 +121,7 @@ static int update(struct command *cmd)\n \t}\n \tif (deny_non_fast_forwards && !is_null_sha1(new_sha1) &&\n \t    !is_null_sha1(old_sha1) &&\n-\t    !strncmp(name, \"refs/heads/\", 11)) {\n+\t    !strncmp(name, PATH_REFS_HEADS, STRLEN_PATH_REFS_HEADS)) {\n \t\tstruct commit *old_commit, *new_commit;\n \t\tstruct commit_list *bases, *ent;\n \ndiff --git a/refs.c b/refs.c\nindex a02957c..6fb81a7 100644\n--- a/refs.c\n+++ b/refs.c\n@@ -263,7 +263,7 @@ const char *resolve_ref(const char *ref, unsigned char \n*sha1, int reading, int *\n \t\t/* Follow \"normalized\" - ie \"refs/..\" symlinks by hand */\n \t\tif (S_ISLNK(st.st_mode)) {\n \t\t\tlen = readlink(path, buffer, sizeof(buffer)-1);\n-\t\t\tif (len >= 5 && !memcmp(\"refs/\", buffer, 5)) {\n+\t\t\tif (len >= STRLEN_PATH_REFS && !memcmp(PATH_REFS, buffer, \nSTRLEN_PATH_REFS)) {\n \t\t\t\tbuffer[len] = 0;\n \t\t\t\tstrcpy(ref_buffer, buffer);\n \t\t\t\tref = ref_buffer;\n@@ -458,22 +458,22 @@ int head_ref(each_ref_fn fn, void *cb_data)\n \n int for_each_ref(each_ref_fn fn, void *cb_data)\n {\n-\treturn do_for_each_ref(\"refs/\", fn, 0, cb_data);\n+\treturn do_for_each_ref(PATH_REFS, fn, 0, cb_data);\n }\n \n int for_each_tag_ref(each_ref_fn fn, void *cb_data)\n {\n-\treturn do_for_each_ref(\"refs/tags/\", fn, 10, cb_data);\n+\treturn do_for_each_ref(PATH_REFS_TAGS, fn, STRLEN_PATH_REFS_TAGS, cb_data);\n }\n \n int for_each_branch_ref(each_ref_fn fn, void *cb_data)\n {\n-\treturn do_for_each_ref(\"refs/heads/\", fn, 11, cb_data);\n+\treturn do_for_each_ref(PATH_REFS_HEADS, fn, STRLEN_PATH_REFS_HEADS, \ncb_data);\n }\n \n int for_each_remote_ref(each_ref_fn fn, void *cb_data)\n {\n-\treturn do_for_each_ref(\"refs/remotes/\", fn, 13, cb_data);\n+\treturn do_for_each_ref(PATH_REFS_REMOTES, fn, STRLEN_PATH_REFS_REMOTES, \ncb_data);\n }\n \n /* NEEDSWORK: This is only used by ssh-upload and it should go; the\n@@ -485,7 +485,7 @@ int get_ref_sha1(const char *ref, unsigned char *sha1)\n {\n \tif (check_ref_format(ref))\n \t\treturn -1;\n-\treturn read_ref(mkpath(\"refs/%s\", ref), sha1);\n+\treturn read_ref(mkpath(PATH_REFS \"%s\", ref), sha1);\n }\n \n /*\n@@ -702,7 +702,7 @@ struct ref_lock *lock_ref_sha1(const char *ref, const \nunsigned char *old_sha1)\n \tchar refpath[PATH_MAX];\n \tif (check_ref_format(ref))\n \t\treturn NULL;\n-\tstrcpy(refpath, mkpath(\"refs/%s\", ref));\n+\tstrcpy(refpath, mkpath(PATH_REFS \"%s\", ref));\n \treturn lock_ref_sha1_basic(refpath, old_sha1, NULL);\n }\n \n@@ -917,7 +917,7 @@ static int log_ref_write(struct ref_lock *lock,\n \tconst char *committer;\n \n \tif (log_all_ref_updates &&\n-\t    !strncmp(lock->ref_name, \"refs/heads/\", 11)) {\n+\t    !strncmp(lock->ref_name, PATH_REFS_HEADS, STRLEN_PATH_REFS_HEADS)) {\n \t\tif (safe_create_leading_directories(lock->log_file) < 0)\n \t\t\treturn error(\"unable to create directory for %s\",\n \t\t\t\tlock->log_file);\ndiff --git a/refs.h b/refs.h\nindex 51aab1e..cd7c428 100644\n--- a/refs.h\n+++ b/refs.h\n@@ -13,6 +13,21 @@ struct ref_lock {\n #define REF_ISSYMREF 01\n #define REF_ISPACKED 02\n \n+#define PATH_REFS                \"refs/\"\n+#define STRLEN_PATH_REFS         5\n+#define PATH_HEADS               \"heads/\"\n+#define STRLEN_PATH_HEADS        6\n+#define PATH_TAGS                \"tags/\"\n+#define STRLEN_PATH_TAGS         5\n+#define PATH_REMOTES             \"remotes/\"\n+#define STRLEN_PATH_REMOTES      8\n+#define PATH_REFS_HEADS          PATH_REFS PATH_HEADS\n+#define STRLEN_PATH_REFS_HEADS   (STRLEN_PATH_REFS+STRLEN_PATH_HEADS)\n+#define PATH_REFS_TAGS           PATH_REFS PATH_TAGS\n+#define STRLEN_PATH_REFS_TAGS    (STRLEN_PATH_REFS+STRLEN_PATH_TAGS)\n+#define PATH_REFS_REMOTES        PATH_REFS PATH_REMOTES\n+#define STRLEN_PATH_REFS_REMOTES (STRLEN_PATH_REFS+STRLEN_PATH_REMOTES)\n+\n /*\n  * Calls the specified function for each ref file until it returns nonzero,\n  * and returns the value\ndiff --git a/setup.c b/setup.c\nindex 2afdba4..ea2c4b7 100644\n--- a/setup.c\n+++ b/setup.c\n@@ -1,4 +1,5 @@\n #include \"cache.h\"\n+#include \"refs.h\"\n \n const char *prefix_path(const char *prefix, int len, const char *path)\n {\n@@ -142,7 +143,7 @@ const char **get_pathspec(const char *prefix, const char \n**pathspec)\n  */\n static int is_toplevel_directory(void)\n {\n-\tif (access(\".git/refs/\", X_OK) ||\n+\tif (access(\".git/\" PATH_REFS, X_OK) ||\n \t    access(getenv(DB_ENVIRONMENT) ?\n \t\t   getenv(DB_ENVIRONMENT) : \".git/objects/\", X_OK) ||\n \t    validate_symref(\".git/HEAD\"))\n@@ -167,7 +168,7 @@ const char *setup_git_directory_gently(int *nongit_ok)\n \t\t\tdie(\"'$%s' too big\", GIT_DIR_ENVIRONMENT);\n \t\tmemcpy(path, getenv(GIT_DIR_ENVIRONMENT), len);\n \t\t\n-\t\tstrcpy(path + len, \"/refs\");\n+\t\tstrcpy(path + len, PATH_REFS);\n \t\tif (access(path, X_OK))\n \t\t\tgoto bad_dir_environ;\n \t\tstrcpy(path + len, \"/HEAD\");\ndiff --git a/sha1_name.c b/sha1_name.c\nindex 6d7cd78..b81c7fc 100644\n--- a/sha1_name.c\n+++ b/sha1_name.c\n@@ -239,11 +239,11 @@ static int get_sha1_basic(const char *str, int len, \nunsigned char *sha1)\n {\n \tstatic const char *fmt[] = {\n \t\t\"%.*s\",\n-\t\t\"refs/%.*s\",\n-\t\t\"refs/tags/%.*s\",\n-\t\t\"refs/heads/%.*s\",\n-\t\t\"refs/remotes/%.*s\",\n-\t\t\"refs/remotes/%.*s/HEAD\",\n+\t\tPATH_REFS \"%.*s\",\n+\t\tPATH_REFS_TAGS \"%.*s\",\n+\t\tPATH_REFS_HEADS \"%.*s\",\n+\t\tPATH_REFS_REMOTES \"%.*s\",\n+\t\tPATH_REFS_REMOTES \"%.*s/HEAD\",\n \t\tNULL\n \t};\n \tstatic const char *warning = \"warning: refname '%.*s' is ambiguous.\\n\";\ndiff --git a/wt-status.c b/wt-status.c\nindex 6e9414d..bac8634 100644\n--- a/wt-status.c\n+++ b/wt-status.c\n@@ -7,6 +7,7 @@\n #include \"diff.h\"\n #include \"revision.h\"\n #include \"diffcore.h\"\n+#include \"refs.h\"\n \n int wt_status_use_color = 0;\n static char wt_status_colors[][COLOR_MAXLEN] = {\n@@ -271,7 +272,7 @@ static void wt_status_print_verbose(struct wt_status *s)\n \n void wt_status_print(struct wt_status *s)\n {\n-\tif (s->branch && strcmp(s->branch, \"refs/heads/master\"))\n+\tif (s->branch && strcmp(s->branch, PATH_REFS_HEADS \"master\"))\n \t\tcolor_printf_ln(color(WT_STATUS_HEADER),\n \t\t\t\"# On branch %s\", s->branch);\n \n-- \n1.4.4.1.g3ece-dirty\n"},{"id":"297431","messageId":"7vslffcy80.fsf@assigned-by-dhcp.cox.net","threadId":"43729","inReplyTo":"200612141644.06167.andyparkins@gmail.com","subject":"Re: [RFC/PATCH] Change \"refs/\" references to symbolic constants","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2006-12-16T21:44:31Z","receivedAt":"2006-12-16T21:44:31Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Andy Parkins <andyparkins@gmail.com> writes:\n\n> Changed repeated use of the same constants for the ref paths to be\n> symbolic constants.\n\nThis is not rejected nor forgotten but has not been applied yet,\nand may not be immediately.\n\nI would love to have this; it would reduce mistakes (spelling\n\"refs/head/\" would silently compile fine and result in a broken\ncommand, while spelling PATH_REFS_HEAD would be caught by the\ncompiler).\n\nA sad thing is this needs to be done when things are relatively\nquiescent.\n\nSo I would apply this before v1.5.0 (or a replacement, if you\nhave updates), but it might have to wait a bit until I I can say\n\"ok, no big patches to C part is pending right now and it is a\ngood time to do this clean-up\".\n\n"},{"id":"298690","messageId":"200612170900.19856.andyparkins@gmail.com","threadId":"43729","inReplyTo":"7vslffcy80.fsf@assigned-by-dhcp.cox.net","subject":"Re: [RFC/PATCH] Change \"refs/\" references to symbolic constants","fromName":"Andy Parkins","fromEmail":"andyparkins@gmail.com","sentAt":"2006-12-17T09:00:18Z","receivedAt":"2006-12-17T09:00:18Z","isPatch":true,"sender":{"key":"andyparkins@gmail.com","avatar":null},"body":"On Saturday 2006, December 16 21:44, Junio C Hamano wrote:\n\n> This is not rejected nor forgotten but has not been applied yet,\n> and may not be immediately.\n\nOh good.\n\n> I would love to have this; it would reduce mistakes (spelling\n> \"refs/head/\" would silently compile fine and result in a broken\n> command, while spelling PATH_REFS_HEAD would be caught by the\n> compiler).\n\nI'd not even considered that; I liked it because of a natural disinclination \ntowards repeated use semanitcally identical literals.\n\n> A sad thing is this needs to be done when things are relatively\n> quiescent.\n\nWhile true - is there ever such a time?  :-)\n\nOn the other hand, this is (hopefully) a zero-impact change, and should leave \nthe object the same...  I might even test it by comparing objects.\n\n> So I would apply this before v1.5.0 (or a replacement, if you\n> have updates), but it might have to wait a bit until I I can say\n> \"ok, no big patches to C part is pending right now and it is a\n> good time to do this clean-up\".\n\nIf I know it's wanted I will happily maintain it indefinitely.  It's part of \nmy rebase on master set anyway, so it's no pain to do so.  I'll keep an eye \nout for quiet times, or if you prod me when you want it I'll make you a \nrebased version instantly.\n\nWhile we're on this subject, does the same apply to my hashsize literals patch \nwhich swapped 20 for HASH_WIDTH?\n\nAndy\n-- \nDr Andrew Parkins, M Eng (Hons), AMIEE\n"}]}