Subject: [PATCH 03/12] Add and use skip_prefix_defval()

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Wed, 18 Dec 2013 21:53:48 +0700

This is a variant of skip_prefix() that returns a specied pointer
instead of NULL if no prefix is found. It's helpful to simplify

  if (starts_with(foo, "bar"))
    foo += 3;

into

  foo = skip_prefix_gently(foo, "bar", foo);

Signed-off-by: Nguyễn Thái Ngọc Duy <[email-masked]>
---
 builtin/checkout.c    |  6 ++----
 builtin/fast-export.c |  3 +--
 builtin/merge.c       |  4 ++--
 builtin/show-branch.c | 14 +++++---------
 git-compat-util.h     |  9 +++++++--
 git.c                 |  3 +--
 notes.c               |  6 ++----
 wt-status.c           | 12 ++++--------
 8 files changed, 24 insertions(+), 33 deletions(-)

diff --git a/builtin/checkout.c b/builtin/checkout.c
index 5df3837..6531ed4 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -1151,10 +1151,8 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
 		const char *argv0 = argv[0];
 		if (!argc || !strcmp(argv0, "--"))
 			die (_("--track needs a branch name"));
-		if (starts_with(argv0, "refs/"))
-			argv0 += 5;
-		if (starts_with(argv0, "remotes/"))
-			argv0 += 8;
+		argv0 = skip_prefix_defval(argv0, "refs/", argv0);
+		argv0 = skip_prefix_defval(argv0, "remotes/", argv0);
 		argv0 = strchr(argv0, '/');
 		if (!argv0 || !argv0[1])
 			die (_("Missing branch name; try -b"));
diff --git a/builtin/fast-export.c b/builtin/fast-export.c
index b8d8a3a..cd0a302 100644
--- a/builtin/fast-export.c
+++ b/builtin/fast-export.c
@@ -476,8 +476,7 @@ static void handle_tag(const char *name, struct tag *tag)
 		}
 	}
 
-	if (starts_with(name, "refs/tags/"))
-		name += 10;
+	name = skip_prefix_defval(name, "refs/tags/", name);
 	printf("tag %s\nfrom :%d\n%.*s%sdata %d\n%.*s\n",
 	       name, tagged_mark,
 	       (int)(tagger_end - tagger), tagger,
diff --git a/builtin/merge.c b/builtin/merge.c
index 4941a6c..590d907 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -1106,8 +1106,8 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
 	 * current branch.
 	 */
 	branch = branch_to_free = resolve_refdup("HEAD", head_sha1, 0, &flag);
-	if (branch && starts_with(branch, "refs/heads/"))
-		branch += 11;
+	if (branch)
+		branch = skip_prefix_defval(branch, "refs/heads/", branch);
 	if (!branch || is_null_sha1(head_sha1))
 		head_commit = NULL;
 	else
diff --git a/builtin/show-branch.c b/builtin/show-branch.c
index d9217ce..6078132 100644
--- a/builtin/show-branch.c
+++ b/builtin/show-branch.c
@@ -284,8 +284,7 @@ static void show_one_commit(struct commit *commit, int no_name)
 		pp_commit_easy(CMIT_FMT_ONELINE, commit, &pretty);
 		pretty_str = pretty.buf;
 	}
-	if (starts_with(pretty_str, "[PATCH] "))
-		pretty_str += 8;
+	pretty_str = skip_prefix_defval(pretty_str, "[PATCH] ", pretty_str);
 
 	if (!no_name) {
 		if (name && name->head_name) {
@@ -473,14 +472,13 @@ static void snarf_refs(int head, int remotes)
 	}
 }
 
-static int rev_is_head(char *head, int headlen, char *name,
+static int rev_is_head(const char *head, int headlen, const char *name,
 		       unsigned char *head_sha1, unsigned char *sha1)
 {
 	if ((!head[0]) ||
 	    (head_sha1 && sha1 && hashcmp(head_sha1, sha1)))
 		return 0;
-	if (starts_with(head, "refs/heads/"))
-		head += 11;
+	head = skip_prefix_defval(head, "refs/heads/", head);
 	if (starts_with(name, "refs/heads/"))
 		name += 11;
 	else if (starts_with(name, "heads/"))
@@ -811,10 +809,8 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
 					head_sha1, NULL))
 				has_head++;
 		}
-		if (!has_head) {
-			int offset = starts_with(head, "refs/heads/") ? 11 : 0;
-			append_one_rev(head + offset);
-		}
+		if (!has_head)
+			append_one_rev(skip_prefix_defval(head, "refs/heads/", head));
 	}
 
 	if (!ref_name_cnt) {
diff --git a/git-compat-util.h b/git-compat-util.h
index 84f1078..b72a80d 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -354,10 +354,15 @@ extern int prefixcmp(const char *str, const char *prefix);
 extern int ends_with(const char *str, const char *suffix);
 extern int suffixcmp(const char *str, const char *suffix);
 
-static inline const char *skip_prefix(const char *str, const char *prefix)
+static inline const char *skip_prefix_defval(const char *str, const char *prefix, const char *defval)
 {
 	size_t len = strlen(prefix);
-	return strncmp(str, prefix, len) ? NULL : str + len;
+	return strncmp(str, prefix, len) ? defval : str + len;
+}
+
+static inline const char *skip_prefix(const char *str, const char *prefix)
+{
+	return skip_prefix_defval(str, prefix, NULL);
 }
 
 static inline int starts_with(const char *str, const char *prefix)
diff --git a/git.c b/git.c
index 35fda7e..321ae81 100644
--- a/git.c
+++ b/git.c
@@ -579,8 +579,7 @@ int main(int argc, char **av)
 	argc--;
 	handle_options(&argv, &argc, NULL);
 	if (argc > 0) {
-		if (starts_with(argv[0], "--"))
-			argv[0] += 2;
+		argv[0] = skip_prefix_defval(argv[0], "--", argv[0]);
 	} else {
 		/* The user didn't specify a command; give them help */
 		commit_pager_choice();
diff --git a/notes.c b/notes.c
index 5f07c0b..31f513b 100644
--- a/notes.c
+++ b/notes.c
@@ -1243,10 +1243,8 @@ static void format_note(struct notes_tree *t, const unsigned char *object_sha1,
 		if (!ref || !strcmp(ref, GIT_NOTES_DEFAULT_REF)) {
 			strbuf_addstr(sb, "\nNotes:\n");
 		} else {
-			if (starts_with(ref, "refs/"))
-				ref += 5;
-			if (starts_with(ref, "notes/"))
-				ref += 6;
+			ref = skip_prefix_defval(ref, "refs/", ref);
+			ref = skip_prefix_defval(ref, "notes/", ref);
 			strbuf_addf(sb, "\nNotes (%s):\n", ref);
 		}
 	}
diff --git a/wt-status.c b/wt-status.c
index 4e55810..1f65039 100644
--- a/wt-status.c
+++ b/wt-status.c
@@ -1179,14 +1179,10 @@ static void wt_status_get_detached_from(struct wt_status_state *state)
 	     /* perhaps sha1 is a tag, try to dereference to a commit */
 	     ((commit = lookup_commit_reference_gently(sha1, 1)) != NULL &&
 	      !hashcmp(cb.nsha1, commit->object.sha1)))) {
-		int ofs;
-		if (starts_with(ref, "refs/tags/"))
-			ofs = strlen("refs/tags/");
-		else if (starts_with(ref, "refs/remotes/"))
-			ofs = strlen("refs/remotes/");
-		else
-			ofs = 0;
-		state->detached_from = xstrdup(ref + ofs);
+		const char *p;
+		if ((p = skip_prefix_defval(ref, "refs/tags/", ref)) == ref)
+			p = skip_prefix_defval(ref, "refs/remotes/", ref);
+		state->detached_from = xstrdup(p);
 	} else
 		state->detached_from =
 			xstrdup(find_unique_abbrev(cb.nsha1, DEFAULT_ABBREV));
-- 
1.8.5.1.208.g019362e