From: Michael Haggerty <mhagger@alum.mit.edu>
To: Junio C Hamano <gitster@pobox.com>
Cc: "Kent R. Spillner" <kspillner@acm.org>,
"Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>,
git@vger.kernel.org, "Michael Haggerty" <mhagger@alum.mit.edu>
Subject: [PATCH 1/3] Add and use a function skip_prefix_if_present()
Date: Wed, 5 Feb 2014 07:25:15 +0100 [thread overview]
Message-ID: <1391581517-8586-2-git-send-email-mhagger@alum.mit.edu> (raw)
In-Reply-To: <1391581517-8586-1-git-send-email-mhagger@alum.mit.edu>
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@alum.mit.edu>
---
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
next prev parent reply other threads:[~2014-02-05 6:33 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-02-05 6:25 [PATCH 0/3] Add a function skip_prefix_if_present() Michael Haggerty
2014-02-05 6:25 ` Michael Haggerty [this message]
2014-02-05 6:25 ` [PATCH 2/3] diff_scoreopt_parse(): use skip_prefix_if_present() Michael Haggerty
2014-02-05 6:25 ` [PATCH 3/3] rev_is_head(): use skip_prefix() Michael Haggerty
2014-02-05 6:55 ` [PATCH 0/3] Add a function skip_prefix_if_present() Michael Haggerty
2014-02-05 17:26 ` Jeff King
2014-02-06 12:19 ` Duy Nguyen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1391581517-8586-2-git-send-email-mhagger@alum.mit.edu \
--to=mhagger@alum.mit.edu \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=kspillner@acm.org \
--cc=pclouds@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).