name-ref: factor out name shortening logic from name_ref()

The logic will be used in a new codepath for showing exact matches. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Jul 7, 2013 at 14:13 UTC 9608a190c0344a55b1d106c1cca17b17fe276c86
1 file changed +12 -7
builtin/name-rev.c
+12 -7
@@ -96,6 +96,17 @@ static int subpath_matches(const char *path, const char *filter)
96 return -1;
97 }
98
99 +static const char *name_ref_abbrev(const char *refname, int shorten_unambiguous)
100 +{
101 + if (shorten_unambiguous)
102 + refname = shorten_unambiguous_ref(refname, 0);
103 + else if (!prefixcmp(refname, "refs/heads/"))
104 + refname = refname + 11;
105 + else if (!prefixcmp(refname, "refs/"))
106 + refname = refname + 5;
107 + return refname;
108 +}
109 +
110 struct name_ref_data {
111 int tags_only;
112 int name_only;
@@ -134,13 +145,7 @@ static int name_ref(const char *path, const unsigned char *sha1, int flags, void
145 if (o && o->type == OBJ_COMMIT) {
146 struct commit *commit = (struct commit *)o;
147
137 - if (can_abbreviate_output)
138 - path = shorten_unambiguous_ref(path, 0);
139 - else if (!prefixcmp(path, "refs/heads/"))
140 - path = path + 11;
141 - else if (!prefixcmp(path, "refs/"))
142 - path = path + 5;
143 -
148 + path = name_ref_abbrev(path, can_abbreviate_output);
149 name_rev(commit, xstrdup(path), 0, 0, deref);
150 }
151 return 0;