From 09e65645e3b092e4d93bb8513b14474c68df23d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Thu, 7 Mar 2019 19:29:15 +0700 Subject: files-backend.c: factor out per-worktree code in loose_fill_ref_dir() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is the first step for further cleaning up and extending this function. Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- refs/files-backend.c | 50 ++++++++++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/refs/files-backend.c b/refs/files-backend.c index 9183875dad..06ba49f47a 100644 --- a/refs/files-backend.c +++ b/refs/files-backend.c @@ -213,6 +213,33 @@ static void files_ref_path(struct files_ref_store *refs, } } +/* + * Manually add refs/bisect and refs/worktree, which, being + * per-worktree, might not appear in the directory listing for + * refs/ in the main repo. + */ +static void add_per_worktree_entries_to_dir(struct ref_dir *dir, const char *dirname) +{ + int pos; + + if (strcmp(dirname, "refs/")) + return; + + pos = search_ref_dir(dir, "refs/bisect/", 12); + if (pos < 0) { + struct ref_entry *child_entry = + create_dir_entry(dir->cache, "refs/bisect/", 12, 1); + add_entry_to_dir(dir, child_entry); + } + + pos = search_ref_dir(dir, "refs/worktree/", 11); + if (pos < 0) { + struct ref_entry *child_entry = + create_dir_entry(dir->cache, "refs/worktree/", 11, 1); + add_entry_to_dir(dir, child_entry); + } +} + /* * Read the loose references from the namespace dirname into dir * (without recursing). dirname must end with '/'. dir must be the @@ -296,28 +323,7 @@ static void loose_fill_ref_dir(struct ref_store *ref_store, strbuf_release(&path); closedir(d); - /* - * Manually add refs/bisect and refs/worktree, which, being - * per-worktree, might not appear in the directory listing for - * refs/ in the main repo. - */ - if (!strcmp(dirname, "refs/")) { - int pos = search_ref_dir(dir, "refs/bisect/", 12); - - if (pos < 0) { - struct ref_entry *child_entry = create_dir_entry( - dir->cache, "refs/bisect/", 12, 1); - add_entry_to_dir(dir, child_entry); - } - - pos = search_ref_dir(dir, "refs/worktree/", 11); - - if (pos < 0) { - struct ref_entry *child_entry = create_dir_entry( - dir->cache, "refs/worktree/", 11, 1); - add_entry_to_dir(dir, child_entry); - } - } + add_per_worktree_entries_to_dir(dir, dirname); } static struct ref_cache *get_loose_ref_cache(struct files_ref_store *refs) -- cgit v1.2.3 From 90d31ff5d470286a8480311384429ebee8bbc939 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Thu, 7 Mar 2019 19:29:16 +0700 Subject: files-backend.c: reduce duplication in add_per_worktree_entries_to_dir() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Signed-off-by: Junio C Hamano --- refs/files-backend.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/refs/files-backend.c b/refs/files-backend.c index 06ba49f47a..cb92677151 100644 --- a/refs/files-backend.c +++ b/refs/files-backend.c @@ -220,22 +220,22 @@ static void files_ref_path(struct files_ref_store *refs, */ static void add_per_worktree_entries_to_dir(struct ref_dir *dir, const char *dirname) { - int pos; + const char *prefixes[] = { "refs/bisect/", "refs/worktree/" }; + int ip; if (strcmp(dirname, "refs/")) return; - pos = search_ref_dir(dir, "refs/bisect/", 12); - if (pos < 0) { - struct ref_entry *child_entry = - create_dir_entry(dir->cache, "refs/bisect/", 12, 1); - add_entry_to_dir(dir, child_entry); - } + for (ip = 0; ip < ARRAY_SIZE(prefixes); ip++) { + const char *prefix = prefixes[ip]; + int prefix_len = strlen(prefix); + struct ref_entry *child_entry; + int pos; - pos = search_ref_dir(dir, "refs/worktree/", 11); - if (pos < 0) { - struct ref_entry *child_entry = - create_dir_entry(dir->cache, "refs/worktree/", 11, 1); + pos = search_ref_dir(dir, prefix, prefix_len); + if (pos >= 0) + continue; + child_entry = create_dir_entry(dir->cache, prefix, prefix_len, 1); add_entry_to_dir(dir, child_entry); } } -- cgit v1.2.3 From b9317d55a37f93c47d48f12a7b3e45a71434d0e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Thu, 7 Mar 2019 19:29:17 +0700 Subject: Make sure refs/rewritten/ is per-worktree MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit a9be29c981 (sequencer: make refs generated by the `label` command worktree-local, 2018-04-25) adds refs/rewritten/ as per-worktree reference space. Unfortunately (my bad) there are a couple places that need update to make sure it's really per-worktree. - add_per_worktree_entries_to_dir() is updated to make sure ref listing look at per-worktree refs/rewritten/ instead of per-repo one [1] - common_list[] is updated so that git_path() returns the correct location. This includes "rev-parse --git-path". This mess is created by me. I started trying to fix it with the introduction of refs/worktree, where all refs will be per-worktree without special treatments. Unfortunate refs/rewritten came before refs/worktree so this is all we can do. This also fixes logs/refs/worktree not being per-worktree. [1] note that ref listing still works sometimes. For example, if you have .git/worktrees/foo/refs/rewritten/bar AND the directory .git/worktrees/refs/rewritten, refs/rewritten/bar will show up. add_per_worktree_entries_to_dir() is only needed when the directory .git/worktrees/refs/rewritten is missing. Reported-by: Phillip Wood Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- path.c | 3 +++ refs/files-backend.c | 4 ++-- t/t1415-worktree-refs.sh | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/path.c b/path.c index dc3294c71e..a29f131c44 100644 --- a/path.c +++ b/path.c @@ -115,10 +115,13 @@ static struct common_dir common_list[] = { { 1, 1, 0, "logs" }, { 1, 1, 1, "logs/HEAD" }, { 0, 1, 1, "logs/refs/bisect" }, + { 0, 1, 1, "logs/refs/rewritten" }, + { 0, 1, 1, "logs/refs/worktree" }, { 0, 1, 0, "lost-found" }, { 0, 1, 0, "objects" }, { 0, 1, 0, "refs" }, { 0, 1, 1, "refs/bisect" }, + { 0, 1, 1, "refs/rewritten" }, { 0, 1, 1, "refs/worktree" }, { 0, 1, 0, "remotes" }, { 0, 1, 0, "worktrees" }, diff --git a/refs/files-backend.c b/refs/files-backend.c index cb92677151..3daf8e6dd0 100644 --- a/refs/files-backend.c +++ b/refs/files-backend.c @@ -214,13 +214,13 @@ static void files_ref_path(struct files_ref_store *refs, } /* - * Manually add refs/bisect and refs/worktree, which, being + * Manually add refs/bisect, refs/rewritten and refs/worktree, which, being * per-worktree, might not appear in the directory listing for * refs/ in the main repo. */ static void add_per_worktree_entries_to_dir(struct ref_dir *dir, const char *dirname) { - const char *prefixes[] = { "refs/bisect/", "refs/worktree/" }; + const char *prefixes[] = { "refs/bisect/", "refs/worktree/", "refs/rewritten/" }; int ip; if (strcmp(dirname, "refs/")) diff --git a/t/t1415-worktree-refs.sh b/t/t1415-worktree-refs.sh index b664e51250..bb2c7572a3 100755 --- a/t/t1415-worktree-refs.sh +++ b/t/t1415-worktree-refs.sh @@ -76,4 +76,39 @@ test_expect_success 'reflog of worktrees/xx/HEAD' ' test_cmp expected actual.wt2 ' +test_expect_success 'for-each-ref from main repo' ' + mkdir fer1 && + git -C fer1 init repo && + test_commit -C fer1/repo initial && + git -C fer1/repo worktree add ../second && + git -C fer1/repo update-ref refs/bisect/main HEAD && + git -C fer1/repo update-ref refs/rewritten/main HEAD && + git -C fer1/repo update-ref refs/worktree/main HEAD && + git -C fer1/repo for-each-ref --format="%(refname)" | grep main >actual && + cat >expected <<-\EOF && + refs/bisect/main + refs/rewritten/main + refs/worktree/main + EOF + test_cmp expected actual +' + +test_expect_success 'for-each-ref from linked repo' ' + mkdir fer2 && + git -C fer2 init repo && + test_commit -C fer2/repo initial && + git -C fer2/repo worktree add ../second && + git -C fer2/second update-ref refs/bisect/second HEAD && + git -C fer2/second update-ref refs/rewritten/second HEAD && + git -C fer2/second update-ref refs/worktree/second HEAD && + git -C fer2/second for-each-ref --format="%(refname)" | grep second >actual && + cat >expected <<-\EOF && + refs/bisect/second + refs/heads/second + refs/rewritten/second + refs/worktree/second + EOF + test_cmp expected actual +' + test_done -- cgit v1.2.3