Skip to content

Commit c72b49d

Browse files
rscharfegitster
authored andcommitted
wt-status: use skip_prefix() to get rid of magic string length constants
Signed-off-by: Rene Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8d8325f commit c72b49d

File tree

1 file changed

+15
-21
lines changed

1 file changed

+15
-21
lines changed

wt-status.c

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -897,15 +897,15 @@ static void wt_status_print_verbose(struct wt_status *s)
897897
static void wt_status_print_tracking(struct wt_status *s)
898898
{
899899
struct strbuf sb = STRBUF_INIT;
900-
const char *cp, *ep;
900+
const char *cp, *ep, *branch_name;
901901
struct branch *branch;
902902
char comment_line_string[3];
903903
int i;
904904

905905
assert(s->branch && !s->is_initial);
906-
if (!starts_with(s->branch, "refs/heads/"))
906+
if (!skip_prefix(s->branch, "refs/heads/", &branch_name))
907907
return;
908-
branch = branch_get(s->branch + 11);
908+
branch = branch_get(branch_name);
909909
if (!format_tracking_info(branch, &sb))
910910
return;
911911

@@ -1154,6 +1154,7 @@ static char *read_and_strip_branch(const char *path)
11541154
{
11551155
struct strbuf sb = STRBUF_INIT;
11561156
unsigned char sha1[20];
1157+
const char *branch_name;
11571158

11581159
if (strbuf_read_file(&sb, git_path("%s", path), 0) <= 0)
11591160
goto got_nothing;
@@ -1162,8 +1163,8 @@ static char *read_and_strip_branch(const char *path)
11621163
strbuf_setlen(&sb, sb.len - 1);
11631164
if (!sb.len)
11641165
goto got_nothing;
1165-
if (starts_with(sb.buf, "refs/heads/"))
1166-
strbuf_remove(&sb,0, strlen("refs/heads/"));
1166+
if (skip_prefix(sb.buf, "refs/heads/", &branch_name))
1167+
strbuf_remove(&sb, 0, branch_name - sb.buf);
11671168
else if (starts_with(sb.buf, "refs/"))
11681169
;
11691170
else if (!get_sha1_hex(sb.buf, sha1)) {
@@ -1194,9 +1195,8 @@ static int grab_1st_switch(unsigned char *osha1, unsigned char *nsha1,
11941195
struct grab_1st_switch_cbdata *cb = cb_data;
11951196
const char *target = NULL, *end;
11961197

1197-
if (!starts_with(message, "checkout: moving from "))
1198+
if (!skip_prefix(message, "checkout: moving from ", &message))
11981199
return 0;
1199-
message += strlen("checkout: moving from ");
12001200
target = strstr(message, " to ");
12011201
if (!target)
12021202
return 0;
@@ -1228,14 +1228,10 @@ static void wt_status_get_detached_from(struct wt_status_state *state)
12281228
/* perhaps sha1 is a tag, try to dereference to a commit */
12291229
((commit = lookup_commit_reference_gently(sha1, 1)) != NULL &&
12301230
!hashcmp(cb.nsha1, commit->object.sha1)))) {
1231-
int ofs;
1232-
if (starts_with(ref, "refs/tags/"))
1233-
ofs = strlen("refs/tags/");
1234-
else if (starts_with(ref, "refs/remotes/"))
1235-
ofs = strlen("refs/remotes/");
1236-
else
1237-
ofs = 0;
1238-
state->detached_from = xstrdup(ref + ofs);
1231+
const char *from = ref;
1232+
if (!skip_prefix(from, "refs/tags/", &from))
1233+
skip_prefix(from, "refs/remotes/", &from);
1234+
state->detached_from = xstrdup(from);
12391235
} else
12401236
state->detached_from =
12411237
xstrdup(find_unique_abbrev(cb.nsha1, DEFAULT_ABBREV));
@@ -1322,9 +1318,7 @@ void wt_status_print(struct wt_status *s)
13221318
if (s->branch) {
13231319
const char *on_what = _("On branch ");
13241320
const char *branch_name = s->branch;
1325-
if (starts_with(branch_name, "refs/heads/"))
1326-
branch_name += 11;
1327-
else if (!strcmp(branch_name, "HEAD")) {
1321+
if (!strcmp(branch_name, "HEAD")) {
13281322
branch_status_color = color(WT_STATUS_NOBRANCH, s);
13291323
if (state.rebase_in_progress || state.rebase_interactive_in_progress) {
13301324
on_what = _("rebase in progress; onto ");
@@ -1339,7 +1333,8 @@ void wt_status_print(struct wt_status *s)
13391333
branch_name = "";
13401334
on_what = _("Not currently on any branch.");
13411335
}
1342-
}
1336+
} else
1337+
skip_prefix(branch_name, "refs/heads/", &branch_name);
13431338
status_printf(s, color(WT_STATUS_HEADER, s), "%s", "");
13441339
status_printf_more(s, branch_status_color, "%s", on_what);
13451340
status_printf_more(s, branch_color, "%s\n", branch_name);
@@ -1530,8 +1525,7 @@ static void wt_shortstatus_print_tracking(struct wt_status *s)
15301525
goto conclude;
15311526
}
15321527

1533-
if (starts_with(branch_name, "refs/heads/"))
1534-
branch_name += 11;
1528+
skip_prefix(branch_name, "refs/heads/", &branch_name);
15351529

15361530
branch = branch_get(branch_name);
15371531

0 commit comments

Comments
 (0)