Skip to content

Commit 90d31ff

Browse files
pcloudsgitster
authored andcommitted
files-backend.c: reduce duplication in add_per_worktree_entries_to_dir()
This function is duplicated to handle refs/bisect/ and refs/worktree/ and a third prefix is coming. Time to clean up. This also fixes incorrect "refs/worktrees/" length in this code. The correct length is 14 not 11. The test in the next patch will also cover this. Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 09e6564 commit 90d31ff

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

refs/files-backend.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -220,22 +220,22 @@ static void files_ref_path(struct files_ref_store *refs,
220220
*/
221221
static void add_per_worktree_entries_to_dir(struct ref_dir *dir, const char *dirname)
222222
{
223-
int pos;
223+
const char *prefixes[] = { "refs/bisect/", "refs/worktree/" };
224+
int ip;
224225

225226
if (strcmp(dirname, "refs/"))
226227
return;
227228

228-
pos = search_ref_dir(dir, "refs/bisect/", 12);
229-
if (pos < 0) {
230-
struct ref_entry *child_entry =
231-
create_dir_entry(dir->cache, "refs/bisect/", 12, 1);
232-
add_entry_to_dir(dir, child_entry);
233-
}
229+
for (ip = 0; ip < ARRAY_SIZE(prefixes); ip++) {
230+
const char *prefix = prefixes[ip];
231+
int prefix_len = strlen(prefix);
232+
struct ref_entry *child_entry;
233+
int pos;
234234

235-
pos = search_ref_dir(dir, "refs/worktree/", 11);
236-
if (pos < 0) {
237-
struct ref_entry *child_entry =
238-
create_dir_entry(dir->cache, "refs/worktree/", 11, 1);
235+
pos = search_ref_dir(dir, prefix, prefix_len);
236+
if (pos >= 0)
237+
continue;
238+
child_entry = create_dir_entry(dir->cache, prefix, prefix_len, 1);
239239
add_entry_to_dir(dir, child_entry);
240240
}
241241
}

0 commit comments

Comments
 (0)