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 <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Nguyễn Thái Ngọc Duy committed
Dec 16, 2012 at 11:15 UTC
386cc8b031611497186a8c89be0f980a54786525
1 file changed
+10
-5
cache-tree.c
+10
-5
@@ -259,7 +259,8 @@ static int update_one(struct cache_tree *it,
259
/*
260
* Find the subtrees and update them.
261
*/
262
- for (i = 0; i < entries; i++) {
262
+ i = 0;
263
+ while (i < entries) {
264
struct cache_entry *ce = cache[i];
265
struct cache_tree_sub *sub;
266
const char *path, *slash;
@@ -271,8 +272,10 @@ static int update_one(struct cache_tree *it,
272
break; /* at the end of this level */
273
274
slash = strchr(path + baselen, '/');
274
- if (!slash)
275
+ if (!slash) {
276
+ i++;
277
continue;
278
+ }
279
/*
280
* a/bbb/c (base = a/, slash = /c)
281
* ==>
@@ -289,7 +292,7 @@ static int update_one(struct cache_tree *it,
292
flags);
293
if (subcnt < 0)
294
return subcnt;
292
- i += subcnt - 1;
295
+ i += subcnt;
296
sub->used = 1;
297
}
298
@@ -300,7 +303,8 @@ static int update_one(struct cache_tree *it,
303
*/
304
strbuf_init(&buffer, 8192);
305
303
- for (i = 0; i < entries; i++) {
306
+ i = 0;
307
+ while (i < entries) {
308
struct cache_entry *ce = cache[i];
309
struct cache_tree_sub *sub;
310
const char *path, *slash;
@@ -320,7 +324,7 @@ static int update_one(struct cache_tree *it,
324
if (!sub)
325
die("cache-tree.c: '%.*s' in '%s' not found",
326
entlen, path + baselen, path);
323
- i += sub->cache_tree->entry_count - 1;
327
+ i += sub->cache_tree->entry_count;
328
sha1 = sub->cache_tree->sha1;
329
mode = S_IFDIR;
330
}
@@ -328,6 +332,7 @@ static int update_one(struct cache_tree *it,
332
sha1 = ce->sha1;
333
mode = ce->ce_mode;
334
entlen = pathlen - baselen;
335
+ i++;
336
}
337
if (mode != S_IFGITLINK && !missing_ok && !has_sha1_file(sha1)) {
338
strbuf_release(&buffer);