Skip to content

Commit

Permalink
cache-tree: replace "for" loops in update_one with "while" loops
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
pclouds authored and gitster committed Dec 16, 2012
1 parent dbc3904 commit 386cc8b
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions cache-tree.c
Expand Up @@ -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;
Expand All @@ -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)
* ==>
Expand All @@ -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;
}

Expand All @@ -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;
Expand All @@ -320,14 +324,15 @@ 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;
}
else {
sha1 = ce->sha1;
mode = ce->ce_mode;
entlen = pathlen - baselen;
i++;
}
if (mode != S_IFGITLINK && !missing_ok && !has_sha1_file(sha1)) {
strbuf_release(&buffer);
Expand Down

0 comments on commit 386cc8b

Please sign in to comment.