Skip to content

Commit e91ae32

Browse files
committed
Merge branch 'jk/skip-prefix'
* jk/skip-prefix: http-push: refactor parsing of remote object names imap-send: use skip_prefix instead of using magic numbers use skip_prefix to avoid repeated calculations git: avoid magic number with skip_prefix fetch-pack: refactor parsing in get_ack fast-import: refactor parsing of spaces stat_opt: check extra strlen call daemon: use skip_prefix to avoid magic numbers fast-import: use skip_prefix for parsing input use skip_prefix to avoid repeating strings use skip_prefix to avoid magic numbers transport-helper: avoid reading past end-of-string fast-import: fix read of uninitialized argv memory apply: use skip_prefix instead of raw addition refactor skip_prefix to return a boolean avoid using skip_prefix as a boolean daemon: mark some strings as const parse_diff_color_slot: drop ofs parameter
2 parents 72c7794 + 67a31f6 commit e91ae32

40 files changed

+407
-401
lines changed

advice.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,12 @@ void advise(const char *advice, ...)
6161

6262
int git_default_advice_config(const char *var, const char *value)
6363
{
64-
const char *k = skip_prefix(var, "advice.");
64+
const char *k;
6565
int i;
6666

67+
if (!skip_prefix(var, "advice.", &k))
68+
return 0;
69+
6770
for (i = 0; i < ARRAY_SIZE(advice_config); i++) {
6871
if (strcmp(k, advice_config[i].name))
6972
continue;

alias.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ static char *alias_val;
55

66
static int alias_lookup_cb(const char *k, const char *v, void *cb)
77
{
8-
if (starts_with(k, "alias.") && !strcmp(k + 6, alias_key)) {
8+
const char *name;
9+
if (skip_prefix(k, "alias.", &name) && !strcmp(name, alias_key)) {
910
if (!v)
1011
return config_error_nonbool(k);
1112
alias_val = xstrdup(v);

branch.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ static int should_setup_rebase(const char *origin)
5050

5151
void install_branch_config(int flag, const char *local, const char *origin, const char *remote)
5252
{
53-
const char *shortname = skip_prefix(remote, "refs/heads/");
53+
const char *shortname = NULL;
5454
struct strbuf key = STRBUF_INIT;
5555
int rebasing = should_setup_rebase(origin);
5656

57-
if (shortname
57+
if (skip_prefix(remote, "refs/heads/", &shortname)
5858
&& !strcmp(local, shortname)
5959
&& !origin) {
6060
warning(_("Not setting branch %s as its own upstream."),

builtin/apply.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3847,9 +3847,10 @@ static void add_index_file(const char *path, unsigned mode, void *buf, unsigned
38473847
ce->ce_flags = create_ce_flags(0);
38483848
ce->ce_namelen = namelen;
38493849
if (S_ISGITLINK(mode)) {
3850-
const char *s = buf;
3850+
const char *s;
38513851

3852-
if (get_sha1_hex(s + strlen("Subproject commit "), ce->sha1))
3852+
if (!skip_prefix(buf, "Subproject commit ", &s) ||
3853+
get_sha1_hex(s, ce->sha1))
38533854
die(_("corrupt patch for submodule %s"), path);
38543855
} else {
38553856
if (!cached) {

builtin/branch.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,13 +294,13 @@ static char *resolve_symref(const char *src, const char *prefix)
294294
{
295295
unsigned char sha1[20];
296296
int flag;
297-
const char *dst, *cp;
297+
const char *dst;
298298

299299
dst = resolve_ref_unsafe(src, sha1, 0, &flag);
300300
if (!(dst && (flag & REF_ISSYMREF)))
301301
return NULL;
302-
if (prefix && (cp = skip_prefix(dst, prefix)))
303-
dst = cp;
302+
if (prefix)
303+
skip_prefix(dst, prefix, &dst);
304304
return xstrdup(dst);
305305
}
306306

builtin/checkout.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -776,8 +776,8 @@ static int switch_branches(const struct checkout_opts *opts,
776776
if (!(flag & REF_ISSYMREF))
777777
old.path = NULL;
778778

779-
if (old.path && starts_with(old.path, "refs/heads/"))
780-
old.name = old.path + strlen("refs/heads/");
779+
if (old.path)
780+
skip_prefix(old.path, "refs/heads/", &old.name);
781781

782782
if (!new->name) {
783783
new->name = "HEAD";

builtin/clone.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -584,11 +584,11 @@ static void update_remote_refs(const struct ref *refs,
584584
static void update_head(const struct ref *our, const struct ref *remote,
585585
const char *msg)
586586
{
587-
if (our && starts_with(our->name, "refs/heads/")) {
587+
const char *head;
588+
if (our && skip_prefix(our->name, "refs/heads/", &head)) {
588589
/* Local default branch link */
589590
create_symref("HEAD", our->name, NULL);
590591
if (!option_bare) {
591-
const char *head = skip_prefix(our->name, "refs/heads/");
592592
update_ref(msg, "HEAD", our->old_sha1, NULL, 0,
593593
UPDATE_REFS_DIE_ON_ERR);
594594
install_branch_config(0, head, option_origin, our->name);
@@ -703,9 +703,12 @@ static void write_refspec_config(const char* src_ref_prefix,
703703
strbuf_addf(&value, "+%s:%s%s", our_head_points_at->name,
704704
branch_top->buf, option_branch);
705705
} else if (remote_head_points_at) {
706+
const char *head = remote_head_points_at->name;
707+
if (!skip_prefix(head, "refs/heads/", &head))
708+
die("BUG: remote HEAD points at non-head?");
709+
706710
strbuf_addf(&value, "+%s:%s%s", remote_head_points_at->name,
707-
branch_top->buf,
708-
skip_prefix(remote_head_points_at->name, "refs/heads/"));
711+
branch_top->buf, head);
709712
}
710713
/*
711714
* otherwise, the next "git fetch" will

builtin/commit.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,7 +1020,7 @@ static int message_is_empty(struct strbuf *sb)
10201020
static int template_untouched(struct strbuf *sb)
10211021
{
10221022
struct strbuf tmpl = STRBUF_INIT;
1023-
char *start;
1023+
const char *start;
10241024

10251025
if (cleanup_mode == CLEANUP_NONE && sb->len)
10261026
return 0;
@@ -1029,8 +1029,7 @@ static int template_untouched(struct strbuf *sb)
10291029
return 0;
10301030

10311031
stripspace(&tmpl, cleanup_mode == CLEANUP_ALL);
1032-
start = (char *)skip_prefix(sb->buf, tmpl.buf);
1033-
if (!start)
1032+
if (!skip_prefix(sb->buf, tmpl.buf, &start))
10341033
start = sb->buf;
10351034
strbuf_release(&tmpl);
10361035
return rest_is_empty(sb, start - sb->buf);

builtin/fmt-merge-msg.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ static int handle_line(char *line, struct merge_parents *merge_parents)
100100
{
101101
int i, len = strlen(line);
102102
struct origin_data *origin_data;
103-
char *src, *origin;
103+
char *src;
104+
const char *origin;
104105
struct src_data *src_data;
105106
struct string_list_item *item;
106107
int pulling_head = 0;
@@ -164,8 +165,7 @@ static int handle_line(char *line, struct merge_parents *merge_parents)
164165
origin = line;
165166
string_list_append(&src_data->tag, origin + 4);
166167
src_data->head_status |= 2;
167-
} else if (starts_with(line, "remote-tracking branch ")) {
168-
origin = line + strlen("remote-tracking branch ");
168+
} else if (skip_prefix(line, "remote-tracking branch ", &origin)) {
169169
string_list_append(&src_data->r_branch, origin);
170170
src_data->head_status |= 2;
171171
} else {
@@ -300,8 +300,8 @@ static void credit_people(struct strbuf *out,
300300
if (!them->nr ||
301301
(them->nr == 1 &&
302302
me &&
303-
(me = skip_prefix(me, them->items->string)) != NULL &&
304-
skip_prefix(me, " <")))
303+
skip_prefix(me, them->items->string, &me) &&
304+
starts_with(me, " <")))
305305
return;
306306
strbuf_addf(out, "\n%c %s ", comment_line_char, label);
307307
add_people_count(out, them);

builtin/log.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -871,7 +871,7 @@ static char *find_branch_name(struct rev_info *rev)
871871
int i, positive = -1;
872872
unsigned char branch_sha1[20];
873873
const unsigned char *tip_sha1;
874-
const char *ref;
874+
const char *ref, *v;
875875
char *full_ref, *branch = NULL;
876876

877877
for (i = 0; i < rev->cmdline.nr; i++) {
@@ -887,9 +887,9 @@ static char *find_branch_name(struct rev_info *rev)
887887
ref = rev->cmdline.rev[positive].name;
888888
tip_sha1 = rev->cmdline.rev[positive].item->sha1;
889889
if (dwim_ref(ref, strlen(ref), branch_sha1, &full_ref) &&
890-
starts_with(full_ref, "refs/heads/") &&
890+
skip_prefix(full_ref, "refs/heads/", &v) &&
891891
!hashcmp(tip_sha1, branch_sha1))
892-
branch = xstrdup(full_ref + strlen("refs/heads/"));
892+
branch = xstrdup(v);
893893
free(full_ref);
894894
return branch;
895895
}
@@ -1396,10 +1396,10 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
13961396

13971397
if (check_head) {
13981398
unsigned char sha1[20];
1399-
const char *ref;
1399+
const char *ref, *v;
14001400
ref = resolve_ref_unsafe("HEAD", sha1, 1, NULL);
1401-
if (ref && starts_with(ref, "refs/heads/"))
1402-
branch_name = xstrdup(ref + strlen("refs/heads/"));
1401+
if (ref && skip_prefix(ref, "refs/heads/", &v))
1402+
branch_name = xstrdup(v);
14031403
else
14041404
branch_name = xstrdup(""); /* no branch */
14051405
}

0 commit comments

Comments
 (0)