@matheusbsbs / matheus-bagliano / commits / 72972eb374
Fork 0

builtin-remote.c: fix earlier "skip_prefix()" conversion

The original code relied on an insane definition of skip_prefix() that returned an empty string for a NULL input and returned the original if the given "prefix" is not a prefix at all (it would have been justifiable if it were called "come_up_with_a_short_name_to_report_ref()" or something, though). In any case, when we replaced it with a more saner definition of the function whose behaviour is true to its name, its callers needed to be adjusted but the conversion missed one call site. This introduces a helper function "abbrev_ref()" whose purpose is to get a full refname and its possible prefix and to strip the prefix part if it matches, or refname itself in full if it doesn't. This makes the callers easier to read again. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Jul 17, 2008 at 21:30 UTC 72972eb3749daecf328badbc1c1952cb513f34f9
1 file changed +19 -32
builtin-remote.c
+19 -32
@@ -147,6 +147,15 @@ struct branch_info {
147
148 static struct path_list branch_list;
149
150 +static const char *abbrev_ref(const char *name, const char *prefix)
151 +{
152 + const char *abbrev = skip_prefix(name, prefix);
153 + if (abbrev)
154 + return abbrev;
155 + return name;
156 +}
157 +#define abbrev_branch(name) abbrev_ref((name), "refs/heads/")
158 +
159 static int config_read_branches(const char *key, const char *value, void *cb)
160 {
161 if (!prefixcmp(key, "branch.")) {
@@ -176,18 +185,12 @@ static int config_read_branches(const char *key, const char *value, void *cb)
185 info->remote = xstrdup(value);
186 } else {
187 char *space = strchr(value, ' ');
179 - const char *ptr = skip_prefix(value, "refs/heads/");
180 - if (ptr)
181 - value = ptr;
188 + value = abbrev_branch(value);
189 while (space) {
190 char *merge;
191 merge = xstrndup(value, space - value);
192 path_list_append(merge, &info->merge);
186 - ptr = skip_prefix(space + 1, "refs/heads/");
187 - if (ptr)
188 - value = ptr;
189 - else
190 - value = space + 1;
193 + value = abbrev_branch(space + 1);
194 space = strchr(value, ' ');
195 }
196 path_list_append(xstrdup(value), &info->merge);
@@ -219,12 +222,7 @@ static int handle_one_branch(const char *refname,
222 refspec.dst = (char *)refname;
223 if (!remote_find_tracking(states->remote, &refspec)) {
224 struct path_list_item *item;
222 - const char *name, *ptr;
223 - ptr = skip_prefix(refspec.src, "refs/heads/");
224 - if (ptr)
225 - name = ptr;
226 - else
227 - name = refspec.src;
225 + const char *name = abbrev_branch(refspec.src);
226 /* symbolic refs pointing nowhere were handled already */
227 if ((flags & REF_ISSYMREF) ||
228 unsorted_path_list_has_path(&states->tracked,
@@ -253,7 +251,6 @@ static int get_ref_states(const struct ref *ref, struct ref_states *states)
251 struct path_list *target = &states->tracked;
252 unsigned char sha1[20];
253 void *util = NULL;
256 - const char *ptr;
254
255 if (!ref->peer_ref || read_ref(ref->peer_ref->name, sha1))
256 target = &states->new;
@@ -262,10 +259,7 @@ static int get_ref_states(const struct ref *ref, struct ref_states *states)
259 if (hashcmp(sha1, ref->new_sha1))
260 util = &states;
261 }
265 - ptr = skip_prefix(ref->name, "refs/heads/");
266 - if (!ptr)
267 - ptr = ref->name;
268 - path_list_append(ptr, target)->util = util;
262 + path_list_append(abbrev_branch(ref->name), target)->util = util;
263 }
264 free_refs(fetch_map);
265
@@ -460,10 +454,8 @@ static int append_ref_to_tracked_list(const char *refname,
454
455 memset(&refspec, 0, sizeof(refspec));
456 refspec.dst = (char *)refname;
463 - if (!remote_find_tracking(states->remote, &refspec)) {
464 - path_list_append(skip_prefix(refspec.src, "refs/heads/"),
465 - &states->tracked);
466 - }
457 + if (!remote_find_tracking(states->remote, &refspec))
458 + path_list_append(abbrev_branch(refspec.src), &states->tracked);
459
460 return 0;
461 }
@@ -530,15 +522,10 @@ static int show(int argc, const char **argv)
522 "es" : "");
523 for (i = 0; i < states.remote->push_refspec_nr; i++) {
524 struct refspec *spec = states.remote->push + i;
533 - const char *p = "", *q = "";
534 - if (spec->src)
535 - p = skip_prefix(spec->src, "refs/heads/");
536 - if (spec->dst)
537 - q = skip_prefix(spec->dst, "refs/heads/");
525 printf(" %s%s%s%s", spec->force ? "+" : "",
539 - p ? p : spec->src,
540 - spec->dst ? ":" : "",
541 - q ? q : spec->dst);
526 + abbrev_branch(spec->src),
527 + spec->dst ? ":" : "",
528 + spec->dst ? abbrev_branch(spec->dst) : "");
529 }
530 printf("\n");
531 }
@@ -588,7 +575,7 @@ static int prune(int argc, const char **argv)
575 result |= delete_ref(refname, NULL);
576
577 printf(" * [%s] %s\n", dry_run ? "would prune" : "pruned",
591 - skip_prefix(refname, "refs/remotes/"));
578 + abbrev_ref(refname, "refs/remotes/"));
579 }
580
581 /* NEEDSWORK: free remote */