[PATCH 1/3] Add and use a function skip_prefix_if_present()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Most of the calls to skip_prefix_defval() had equal first and third
arguments, with the effect of skipping the prefix if present, but
otherwise returning the original string.  So define a new function
that does exactly that.

Signed-off-by: Michael Haggerty <mhagger@xxxxxxxxxxxx>
---
 builtin/checkout.c    | 4 ++--
 builtin/fast-export.c | 2 +-
 builtin/merge.c       | 2 +-
 builtin/show-branch.c | 6 +++---
 git-compat-util.h     | 5 +++++
 git.c                 | 2 +-
 notes.c               | 4 ++--
 refs.c                | 2 +-
 wt-status.c           | 4 ++--
 9 files changed, 18 insertions(+), 13 deletions(-)

diff --git a/builtin/checkout.c b/builtin/checkout.c
index 6531ed4..84682f1 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -1151,8 +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"));
-		argv0 = skip_prefix_defval(argv0, "refs/", argv0);
-		argv0 = skip_prefix_defval(argv0, "remotes/", argv0);
+		argv0 = skip_prefix_if_present(argv0, "refs/");
+		argv0 = skip_prefix_if_present(argv0, "remotes/");
 		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 cd0a302..c87c7ea 100644
--- a/builtin/fast-export.c
+++ b/builtin/fast-export.c
@@ -476,7 +476,7 @@ static void handle_tag(const char *name, struct tag *tag)
 		}
 	}
 
-	name = skip_prefix_defval(name, "refs/tags/", name);
+	name = skip_prefix_if_present(name, "refs/tags/");
 	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 603f80a..7b01dcf 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -1109,7 +1109,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
 	 */
 	branch = branch_to_free = resolve_refdup("HEAD", head_sha1, 0, &flag);
 	if (branch)
-		branch = skip_prefix_defval(branch, "refs/heads/", branch);
+		branch = skip_prefix_if_present(branch, "refs/heads/");
 	if (!branch || is_null_sha1(head_sha1))
 		head_commit = NULL;
 	else
diff --git a/builtin/show-branch.c b/builtin/show-branch.c
index 6078132..f2c3b19 100644
--- a/builtin/show-branch.c
+++ b/builtin/show-branch.c
@@ -284,7 +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;
 	}
-	pretty_str = skip_prefix_defval(pretty_str, "[PATCH] ", pretty_str);
+	pretty_str = skip_prefix_if_present(pretty_str, "[PATCH] ");
 
 	if (!no_name) {
 		if (name && name->head_name) {
@@ -478,7 +478,7 @@ static int rev_is_head(const char *head, int headlen, const char *name,
 	if ((!head[0]) ||
 	    (head_sha1 && sha1 && hashcmp(head_sha1, sha1)))
 		return 0;
-	head = skip_prefix_defval(head, "refs/heads/", head);
+	head = skip_prefix_if_present(head, "refs/heads/");
 	if (starts_with(name, "refs/heads/"))
 		name += 11;
 	else if (starts_with(name, "heads/"))
@@ -810,7 +810,7 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
 				has_head++;
 		}
 		if (!has_head)
-			append_one_rev(skip_prefix_defval(head, "refs/heads/", head));
+			append_one_rev(skip_prefix_if_present(head, "refs/heads/"));
 	}
 
 	if (!ref_name_cnt) {
diff --git a/git-compat-util.h b/git-compat-util.h
index 59265af..cff946c 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -368,6 +368,11 @@ static inline const char *skip_prefix(const char *str, const char *prefix)
 	return skip_prefix_defval(str, prefix, NULL);
 }
 
+static inline const char *skip_prefix_if_present(const char *str, const char *prefix)
+{
+	return skip_prefix_defval(str, prefix, str);
+}
+
 static inline int starts_with(const char *str, const char *prefix)
 {
 	return skip_prefix(str, prefix) != NULL;
diff --git a/git.c b/git.c
index 321ae81..f3357d8 100644
--- a/git.c
+++ b/git.c
@@ -579,7 +579,7 @@ int main(int argc, char **av)
 	argc--;
 	handle_options(&argv, &argc, NULL);
 	if (argc > 0) {
-		argv[0] = skip_prefix_defval(argv[0], "--", argv[0]);
+		argv[0] = skip_prefix_if_present(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 31f513b..15c49d8 100644
--- a/notes.c
+++ b/notes.c
@@ -1243,8 +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 {
-			ref = skip_prefix_defval(ref, "refs/", ref);
-			ref = skip_prefix_defval(ref, "notes/", ref);
+			ref = skip_prefix_if_present(ref, "refs/");
+			ref = skip_prefix_if_present(ref, "notes/");
 			strbuf_addf(sb, "\nNotes (%s):\n", ref);
 		}
 	}
diff --git a/refs.c b/refs.c
index 217093f..808985d 100644
--- a/refs.c
+++ b/refs.c
@@ -2318,7 +2318,7 @@ static void try_remove_empty_parents(char *name)
 /* make sure nobody touched the ref, and unlink */
 static void prune_ref(struct ref_to_prune *r)
 {
-	const char *name = skip_prefix_defval(r->name, "refs/", r->name);
+	const char *name = skip_prefix_if_present(r->name, "refs/");
 	struct ref_lock *lock = lock_ref_sha1(name, r->sha1);
 
 	if (lock) {
diff --git a/wt-status.c b/wt-status.c
index 185fa81..e7fab5c 100644
--- a/wt-status.c
+++ b/wt-status.c
@@ -1179,8 +1179,8 @@ static void wt_status_get_detached_from(struct wt_status_state *state)
 	     ((commit = lookup_commit_reference_gently(sha1, 1)) != NULL &&
 	      !hashcmp(cb.nsha1, commit->object.sha1)))) {
 		const char *p;
-		if ((p = skip_prefix_defval(ref, "refs/tags/", ref)) == ref)
-			p = skip_prefix_defval(ref, "refs/remotes/", ref);
+		if (!(p = skip_prefix(ref, "refs/tags/")))
+			p = skip_prefix_if_present(ref, "refs/remotes/");
 		state->detached_from = xstrdup(p);
 	} else
 		state->detached_from =
-- 
1.8.5.3

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]