Skip to contentSkip to conversation

push: use skip_prefix() instead of starts_with()

PATCH In review René Scharfe 2 participants 2 messages Nov 26, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 push: use skip_prefix() instead of starts_with()

Get rid of a magic number by using skip_prefix().

---
 builtin/push.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
builtin/push.c +3 -2
index 843f5b22a2..6dbf0f0bb7 100644
@@ -64,6 +64,7 @@ static struct string_list push_options_config = STRING_LIST_INIT_DUP;
 static const char *map_refspec(const char *ref,
 			       struct remote *remote, struct ref *local_refs)
 {
+	const char *branch_name;
 	struct ref *matched = NULL;
 
 	/* Does "ref" uniquely name our ref? */
@@ -84,8 +85,8 @@ static const char *map_refspec(const char *ref,
 	}
 
 	if (push_default == PUSH_DEFAULT_UPSTREAM &&
-	    starts_with(matched->name, "refs/heads/")) {
-		struct branch *branch = branch_get(matched->name + 11);
+	    skip_prefix(matched->name, "refs/heads/", &branch_name)) {
+		struct branch *branch = branch_get(branch_name);
 		if (branch->merge_nr == 1 && branch->merge[0]->src) {
 			struct strbuf buf = STRBUF_INIT;
 			strbuf_addf(&buf, "%s:%s",

--
2.24.0

#1 lore raw
Quoted 13 lines · René Scharfe
builtin/push.c +1 -0
index 843f5b22a2..6dbf0f0bb7 100644
@@ -64,6 +64,7 @@ static struct string_list push_options_config = STRING_LIST_INIT_DUP;
 static const char *map_refspec(const char *ref,
 			       struct remote *remote, struct ref *local_refs)
 {
+	const char *branch_name;
 	struct ref *matched = NULL;
 
 	/* Does "ref" uniquely name our ref? */

builtin/push.c static const char *map_refspec(const char *ref, · line 89

Quoted 7 lines
builtin/push.c +2 -2
@@ -84,8 +85,8 @@ static const char *map_refspec(const char *ref,
 	}
 
 	if (push_default == PUSH_DEFAULT_UPSTREAM &&
-	    starts_with(matched->name, "refs/heads/")) {
-		struct branch *branch = branch_get(matched->name + 11);
+	    skip_prefix(matched->name, "refs/heads/", &branch_name)) {
+		struct branch *branch = branch_get(branch_name);

Yep, obviously correct because you introduce a new variable instead of mutating an existing one. Looks good.

-Peff