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

[PATCH v4 2/4] cache-tree: replace "for" loops in update_one with "while" loops

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Dec 16, 2012, 04:15 UTC
Message-ID
<1355631328-26678-3-git-send-email-pclouds@gmail.com>
In-Reply-To
<1355631328-26678-1-git-send-email-pclouds@gmail.com>

The loops in update_one can be increased in two different ways: step by one for files and by <n> for directories. "for" loop is not suitable for this as it always steps by one and special handling is required for directories. Replace them with "while" loops for clarity.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 cache-tree.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
diff --git a/cache-tree.c b/cache-tree.c
index e2beab5..44eed28 100644
--- a/cache-tree.c
+++ b/cache-tree.c
@@ -259,7 +259,8 @@ static int update_one(struct cache_tree *it,
 	/*
 	 * Find the subtrees and update them.
 	 */
-	for (i = 0; i < entries; i++) {
+	i = 0;
+	while (i < entries) {
 		struct cache_entry *ce = cache[i];
 		struct cache_tree_sub *sub;
 		const char *path, *slash;
@@ -271,8 +272,10 @@ static int update_one(struct cache_tree *it,
 			break; /* at the end of this level */
 
 		slash = strchr(path + baselen, '/');
-		if (!slash)
+		if (!slash) {
+			i++;
 			continue;
+		}
 		/*
 		 * a/bbb/c (base = a/, slash = /c)
 		 * ==>
@@ -289,7 +292,7 @@ static int update_one(struct cache_tree *it,
 				    flags);
 		if (subcnt < 0)
 			return subcnt;
-		i += subcnt - 1;
+		i += subcnt;
 		sub->used = 1;
 	}
 
@@ -300,7 +303,8 @@ static int update_one(struct cache_tree *it,
 	 */
 	strbuf_init(&buffer, 8192);
 
-	for (i = 0; i < entries; i++) {
+	i = 0;
+	while (i < entries) {
 		struct cache_entry *ce = cache[i];
 		struct cache_tree_sub *sub;
 		const char *path, *slash;
@@ -320,7 +324,7 @@ static int update_one(struct cache_tree *it,
 			if (!sub)
 				die("cache-tree.c: '%.*s' in '%s' not found",
 				    entlen, path + baselen, path);
-			i += sub->cache_tree->entry_count - 1;
+			i += sub->cache_tree->entry_count;
 			sha1 = sub->cache_tree->sha1;
 			mode = S_IFDIR;
 		}
@@ -328,6 +332,7 @@ static int update_one(struct cache_tree *it,
 			sha1 = ce->sha1;
 			mode = ce->ce_mode;
 			entlen = pathlen - baselen;
+			i++;
 		}
 		if (mode != S_IFGITLINK && !missing_ok && !has_sha1_file(sha1)) {
 			strbuf_release(&buffer);
-- 
1.8.0.rc2.23.g1fb49df
Previous: Nguyễn Thái Ngọc DuyNext: Nguyễn Thái Ngọc Duy
Message 3 of 8 in “nd/invalidate-i-t-a-cache-tree”
  1. 0/4 nd/invalidate-i-t-a-cache-treeNguyễn Thái Ngọc Duy, Dec 16, 2012
  2. 1/4 cache-tree: remove dead i-t-a code in verify_cache()Nguyễn Thái Ngọc Duy, Dec 16, 2012
  3. 2/4 cache-tree: replace "for" loops in update_one with "while" loopsNguyễn Thái Ngọc Duy, Dec 16, 2012
  4. 3/4 cache-tree: fix writing cache-tree when CE_REMOVE is presentNguyễn Thái Ngọc Duy, Dec 16, 2012
  5. Junio C HamanoDec 16, 2012
  6. Nguyen Thai Ngoc DuyDec 16, 2012
  7. Junio C HamanoDec 16, 2012
  8. 4/4 cache-tree: invalidate i-t-a paths after generating treesNguyễn Thái Ngọc Duy, Dec 16, 2012

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.