wt-status: use skip_prefix() to get rid of magic string length constants
Signed-off-by: Rene Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe committed
Oct 31, 2015 at 18:37 UTC
c72b49dfab8381abfd947db53c1c9e2da9593ab6
1 file changed
+15
-21
wt-status.c
+15
-21
@@ -897,15 +897,15 @@ static void wt_status_print_verbose(struct wt_status *s)
897
static void wt_status_print_tracking(struct wt_status *s)
898
{
899
struct strbuf sb = STRBUF_INIT;
900
- const char *cp, *ep;
900
+ const char *cp, *ep, *branch_name;
901
struct branch *branch;
902
char comment_line_string[3];
903
int i;
904
905
assert(s->branch && !s->is_initial);
906
- if (!starts_with(s->branch, "refs/heads/"))
906
+ if (!skip_prefix(s->branch, "refs/heads/", &branch_name))
907
return;
908
- branch = branch_get(s->branch + 11);
908
+ branch = branch_get(branch_name);
909
if (!format_tracking_info(branch, &sb))
910
return;
911
@@ -1154,6 +1154,7 @@ static char *read_and_strip_branch(const char *path)
1154
{
1155
struct strbuf sb = STRBUF_INIT;
1156
unsigned char sha1[20];
1157
+ const char *branch_name;
1158
1159
if (strbuf_read_file(&sb, git_path("%s", path), 0) <= 0)
1160
goto got_nothing;
@@ -1162,8 +1163,8 @@ static char *read_and_strip_branch(const char *path)
1163
strbuf_setlen(&sb, sb.len - 1);
1164
if (!sb.len)
1165
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);
1168
else if (starts_with(sb.buf, "refs/"))
1169
;
1170
else if (!get_sha1_hex(sb.buf, sha1)) {
@@ -1194,9 +1195,8 @@ static int grab_1st_switch(unsigned char *osha1, unsigned char *nsha1,
1195
struct grab_1st_switch_cbdata *cb = cb_data;
1196
const char *target = NULL, *end;
1197
1197
- if (!starts_with(message, "checkout: moving from "))
1198
+ if (!skip_prefix(message, "checkout: moving from ", &message))
1199
return 0;
1199
- message += strlen("checkout: moving from ");
1200
target = strstr(message, " to ");
1201
if (!target)
1202
return 0;
@@ -1228,14 +1228,10 @@ static void wt_status_get_detached_from(struct wt_status_state *state)
1228
/* perhaps sha1 is a tag, try to dereference to a commit */
1229
((commit = lookup_commit_reference_gently(sha1, 1)) != NULL &&
1230
!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);
1235
} else
1236
state->detached_from =
1237
xstrdup(find_unique_abbrev(cb.nsha1, DEFAULT_ABBREV));
@@ -1322,9 +1318,7 @@ void wt_status_print(struct wt_status *s)
1318
if (s->branch) {
1319
const char *on_what = _("On branch ");
1320
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")) {
1322
branch_status_color = color(WT_STATUS_NOBRANCH, s);
1323
if (state.rebase_in_progress || state.rebase_interactive_in_progress) {
1324
on_what = _("rebase in progress; onto ");
@@ -1339,7 +1333,8 @@ void wt_status_print(struct wt_status *s)
1333
branch_name = "";
1334
on_what = _("Not currently on any branch.");
1335
}
1342
- }
1336
+ } else
1337
+ skip_prefix(branch_name, "refs/heads/", &branch_name);
1338
status_printf(s, color(WT_STATUS_HEADER, s), "%s", "");
1339
status_printf_more(s, branch_status_color, "%s", on_what);
1340
status_printf_more(s, branch_color, "%s\n", branch_name);
@@ -1530,8 +1525,7 @@ static void wt_shortstatus_print_tracking(struct wt_status *s)
1525
goto conclude;
1526
}
1527
1533
- if (starts_with(branch_name, "refs/heads/"))
1534
- branch_name += 11;
1528
+ skip_prefix(branch_name, "refs/heads/", &branch_name);
1529
1530
branch = branch_get(branch_name);
1531