git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/4] Extract function trim_url and optimize calls of it.

From
Vasyl' Vavrychuk <vvavrychuk@gmail.com>
Date
Feb 12, 2011, 22:38 UTC
Message-ID
<1297550311-17723-3-git-send-email-vvavrychuk@gmail.com>
In-Reply-To
<1297550311-17723-1-git-send-email-vvavrychuk@gmail.com>
Extract compact code into trim_url. Dont call it every iteration in the loop since no reason.
Signed-off-by: Vasyl' Vavrychuk <vvavrychuk@gmail.com>
---
 builtin/fetch.c |   29 ++++++++++++++++++-----------
 1 files changed, 18 insertions(+), 11 deletions(-)
diff --git a/builtin/fetch.c b/builtin/fetch.c
index 2b0b11e..46d8fd6 100644
--- a/builtin/fetch.c
+++ b/builtin/fetch.c
@@ -89,6 +89,19 @@ static void unlock_pack_on_signal(int signo)
 	raise(signo);
 }
 
+static void trim_url(char *url)
+{
+	int i, url_len;
+
+	url_len = strlen(url);
+	for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
+		;
+	url_len = i + 1;
+	if (4 < i && !strncmp(".git", url + i - 3, 4))
+		url_len = i - 3;
+	url[url_len] = '\0';
+}
+
 static void add_merge_config(struct ref **head,
 			   const struct ref *remote_refs,
 		           struct branch *branch,
@@ -329,7 +342,7 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
 {
 	FILE *fp;
 	struct commit *commit;
-	int url_len, i, note_len, shown_url = 0, rc = 0;
+	int i, note_len, shown_url = 0, rc = 0;
 	char note[1024];
 	const char *what, *kind;
 	struct ref *rm;
@@ -339,10 +352,12 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
 	if (!fp)
 		return error("cannot open %s: %s\n", filename, strerror(errno));
 
-	if (raw_url)
+	if (raw_url) {
 		url = transport_anonymize_url(raw_url);
-	else
+		trim_url(url);
+	} else {
 		url = xstrdup("foreign");
+	}
 	for (rm = ref_map; rm; rm = rm->next) {
 		struct ref *ref = NULL;
 
@@ -379,14 +394,6 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
 			what = rm->name;
 		}
 
-		url_len = strlen(url);
-		for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
-			;
-		url_len = i + 1;
-		if (4 < i && !strncmp(".git", url + i - 3, 4))
-			url_len = i - 3;
-		url[url_len] = '\0';
-
 		note_len = 0;
 		if (*what) {
 			if (*kind)
-- 
1.7.1
Previous: Junio C HamanoNext: Junio C Hamano
Message 4 of 12 in “clean-up store_updated_refs”
  1. 0/4 clean-up store_updated_refsVasyl' Vavrychuk, Feb 12, 2011
  2. 1/4 Reduce url_len variable scopeVasyl' Vavrychuk, Feb 12, 2011
  3. Junio C HamanoFeb 17, 2011
  4. 2/4 Extract function trim_url and optimize calls of it.Vasyl' Vavrychuk, Feb 12, 2011
  5. Junio C HamanoFeb 17, 2011
  6. 3/4 Dont use the same variable for different thingsVasyl' Vavrychuk, Feb 12, 2011
  7. Junio C HamanoFeb 17, 2011
  8. João P. SampaioFeb 17, 2011
  9. 4/4 Replace pointer arithmetic with strbufVasyl' Vavrychuk, Feb 12, 2011
  10. Sverre RabbelierFeb 13, 2011
  11. Vasyl'Feb 13, 2011
  12. Sverre RabbelierFeb 13, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.