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 <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Mar 7, 2019 at 19:29 UTC 90d31ff5d470286a8480311384429ebee8bbc939
1 file changed +11 -11
refs/files-backend.c
+11 -11
@@ -220,22 +220,22 @@ static void files_ref_path(struct files_ref_store *refs,
220 */
221 static void add_per_worktree_entries_to_dir(struct ref_dir *dir, const char *dirname)
222 {
223 - int pos;
223 + const char *prefixes[] = { "refs/bisect/", "refs/worktree/" };
224 + int ip;
225
226 if (strcmp(dirname, "refs/"))
227 return;
228
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;
234
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);
239 add_entry_to_dir(dir, child_entry);
240 }
241 }