Skip to contentSkip to conversation

fetch: 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 fetch: use skip_prefix() instead of starts_with()

Get rid of magic numbers by letting skip_prefix() set the pointer "what".

---
 builtin/fetch.c | 12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)
builtin/fetch.c +3 -9
index 863c858fde..02e8619618 100644
@@ -954,18 +954,12 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
 				kind = "";
 				what = "";
 			}
-			else if (starts_with(rm->name, "refs/heads/")) {
+			else if (skip_prefix(rm->name, "refs/heads/", &what))
 				kind = "branch";
-				what = rm->name + 11;
-			}
-			else if (starts_with(rm->name, "refs/tags/")) {
+			else if (skip_prefix(rm->name, "refs/tags/", &what))
 				kind = "tag";
-				what = rm->name + 10;
-			}
-			else if (starts_with(rm->name, "refs/remotes/")) {
+			else if (skip_prefix(rm->name, "refs/remotes/", &what))
 				kind = "remote-tracking branch";
-				what = rm->name + 13;
-			}
 			else {
 				kind = "";
 				what = rm->name;

--
2.24.0

#1 lore raw
Quoted 14 lines · René Scharfe
builtin/fetch.c +1 -4
index 863c858fde..02e8619618 100644
@@ -954,18 +954,12 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
 				kind = "";
 				what = "";
 			}
-			else if (starts_with(rm->name, "refs/heads/")) {
+			else if (skip_prefix(rm->name, "refs/heads/", &what))
 				kind = "branch";
-				what = rm->name + 11;
-			}
-			else if (starts_with(rm->name, "refs/tags/")) {

builtin/fetch.c static int store_updated_refs(const char *raw_url, const char *remote_name, · line 963

Quoted 10 lines
builtin/fetch.c +2 -5
+			else if (skip_prefix(rm->name, "refs/tags/", &what))
 				kind = "tag";
-				what = rm->name + 10;
-			}
-			else if (starts_with(rm->name, "refs/remotes/")) {
+			else if (skip_prefix(rm->name, "refs/remotes/", &what))
 				kind = "remote-tracking branch";
-				what = rm->name + 13;
-			}
 			else {

Yep, this one looks obviously correct. We were already mutating "what", so this just does it inside skip_prefix(). Much nicer.

-Peff