Skip to content

Commit 386cc8b

Browse files
pcloudsgitster
authored andcommitted
cache-tree: replace "for" loops in update_one with "while" loops
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent dbc3904 commit 386cc8b

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

cache-tree.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,8 @@ static int update_one(struct cache_tree *it,
259259
/*
260260
* Find the subtrees and update them.
261261
*/
262-
for (i = 0; i < entries; i++) {
262+
i = 0;
263+
while (i < entries) {
263264
struct cache_entry *ce = cache[i];
264265
struct cache_tree_sub *sub;
265266
const char *path, *slash;
@@ -271,8 +272,10 @@ static int update_one(struct cache_tree *it,
271272
break; /* at the end of this level */
272273

273274
slash = strchr(path + baselen, '/');
274-
if (!slash)
275+
if (!slash) {
276+
i++;
275277
continue;
278+
}
276279
/*
277280
* a/bbb/c (base = a/, slash = /c)
278281
* ==>
@@ -289,7 +292,7 @@ static int update_one(struct cache_tree *it,
289292
flags);
290293
if (subcnt < 0)
291294
return subcnt;
292-
i += subcnt - 1;
295+
i += subcnt;
293296
sub->used = 1;
294297
}
295298

@@ -300,7 +303,8 @@ static int update_one(struct cache_tree *it,
300303
*/
301304
strbuf_init(&buffer, 8192);
302305

303-
for (i = 0; i < entries; i++) {
306+
i = 0;
307+
while (i < entries) {
304308
struct cache_entry *ce = cache[i];
305309
struct cache_tree_sub *sub;
306310
const char *path, *slash;
@@ -320,14 +324,15 @@ static int update_one(struct cache_tree *it,
320324
if (!sub)
321325
die("cache-tree.c: '%.*s' in '%s' not found",
322326
entlen, path + baselen, path);
323-
i += sub->cache_tree->entry_count - 1;
327+
i += sub->cache_tree->entry_count;
324328
sha1 = sub->cache_tree->sha1;
325329
mode = S_IFDIR;
326330
}
327331
else {
328332
sha1 = ce->sha1;
329333
mode = ce->ce_mode;
330334
entlen = pathlen - baselen;
335+
i++;
331336
}
332337
if (mode != S_IFGITLINK && !missing_ok && !has_sha1_file(sha1)) {
333338
strbuf_release(&buffer);

0 commit comments

Comments
 (0)