Skip to content

Commit d3cc5f4

Browse files
peffgitster
authored andcommitted
show-branch: use skip_prefix to drop magic numbers
We make several starts_with() calls, only to advance pointers. This is exactly what skip_prefix() is for, which lets us avoid manually-counted magic numbers. Helped-by: Pranit Bauva <[email protected]> Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d9e557a commit d3cc5f4

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

builtin/show-branch.c

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,7 @@ static void show_one_commit(struct commit *commit, int no_name)
275275
pp_commit_easy(CMIT_FMT_ONELINE, commit, &pretty);
276276
pretty_str = pretty.buf;
277277
}
278-
if (starts_with(pretty_str, "[PATCH] "))
279-
pretty_str += 8;
278+
skip_prefix(pretty_str, "[PATCH] ", &pretty_str);
280279

281280
if (!no_name) {
282281
if (name && name->head_name) {
@@ -470,17 +469,14 @@ static void snarf_refs(int head, int remotes)
470469
}
471470
}
472471

473-
static int rev_is_head(char *head, char *name,
472+
static int rev_is_head(const char *head, const char *name,
474473
unsigned char *head_sha1, unsigned char *sha1)
475474
{
476475
if (!head || (head_sha1 && sha1 && hashcmp(head_sha1, sha1)))
477476
return 0;
478-
if (starts_with(head, "refs/heads/"))
479-
head += 11;
480-
if (starts_with(name, "refs/heads/"))
481-
name += 11;
482-
else if (starts_with(name, "heads/"))
483-
name += 6;
477+
skip_prefix(head, "refs/heads/", &head);
478+
if (!skip_prefix(name, "refs/heads/", &name))
479+
skip_prefix(name, "heads/", &name);
484480
return !strcmp(head, name);
485481
}
486482

@@ -799,8 +795,9 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
799795
has_head++;
800796
}
801797
if (!has_head) {
802-
int offset = starts_with(head, "refs/heads/") ? 11 : 0;
803-
append_one_rev(head + offset);
798+
const char *name = head;
799+
skip_prefix(name, "refs/heads/", &name);
800+
append_one_rev(name);
804801
}
805802
}
806803

0 commit comments

Comments
 (0)