]> git.ipfire.org Git - thirdparty/git.git/commitdiff
refs: use chdir_notify to update cached relative paths
authorJeff King <peff@peff.net>
Fri, 30 Mar 2018 18:35:12 +0000 (14:35 -0400)
committerJunio C Hamano <gitster@pobox.com>
Fri, 30 Mar 2018 19:50:03 +0000 (12:50 -0700)
Commit f57f37e2e1 (files-backend: remove the use of
git_path(), 2017-03-26) introduced a regression when a
relative $GIT_DIR is used in a working tree:

  - when we initialize the ref backend, we make a copy of
    get_git_dir(), which may be relative

  - later, we may call setup_work_tree() and chdir to the
    root of the working tree

  - further calls to the ref code will use the stored git
    directory, but relative paths will now point to the
    wrong place

The new test in t1501 demonstrates one such instance (the
bug causes us to write the ref update to the nonsense
"relative/relative/.git").

Since setup_work_tree() now uses chdir_notify, we can just
ask it update our relative paths when necessary.

Reported-by: Rafael Ascensao <rafa.almas@gmail.com>
Helped-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
refs/files-backend.c
refs/packed-backend.c
t/t1501-work-tree.sh

index bec8e30e9e3e2995739dfff7cc971f8de623610d..a92a2aa82137e811e12d1e9d67eb3b4fd85f19fd 100644 (file)
@@ -9,6 +9,7 @@
 #include "../lockfile.h"
 #include "../object.h"
 #include "../dir.h"
+#include "../chdir-notify.h"
 
 /*
  * This backend uses the following flags in `ref_update::flags` for
@@ -106,6 +107,11 @@ static struct ref_store *files_ref_store_create(const char *gitdir,
        refs->packed_ref_store = packed_ref_store_create(sb.buf, flags);
        strbuf_release(&sb);
 
+       chdir_notify_reparent("files-backend $GIT_DIR",
+                             &refs->gitdir);
+       chdir_notify_reparent("files-backend $GIT_COMMONDIR",
+                             &refs->gitcommondir);
+
        return ref_store;
 }
 
index 65288c647278aa27790b13c0360f756686dadf7a..369c34f886fc5532de2afbd1590823eed0758a33 100644 (file)
@@ -5,6 +5,7 @@
 #include "packed-backend.h"
 #include "../iterator.h"
 #include "../lockfile.h"
+#include "../chdir-notify.h"
 
 enum mmap_strategy {
        /*
@@ -202,6 +203,8 @@ struct ref_store *packed_ref_store_create(const char *path,
        refs->store_flags = store_flags;
 
        refs->path = xstrdup(path);
+       chdir_notify_reparent("packed-refs", &refs->path);
+
        return ref_store;
 }
 
index b06210ec5e8ffa30b59314b1c79ebc9bdb8f6476..a5db53337baee2db00ec8ac7b476026796665594 100755 (executable)
@@ -431,4 +431,16 @@ test_expect_success 'error out gracefully on invalid $GIT_WORK_TREE' '
        )
 '
 
+test_expect_success 'refs work with relative gitdir and work tree' '
+       git init relative &&
+       git -C relative commit --allow-empty -m one &&
+       git -C relative commit --allow-empty -m two &&
+
+       GIT_DIR=relative/.git GIT_WORK_TREE=relative git reset HEAD^ &&
+
+       git -C relative log -1 --format=%s >actual &&
+       echo one >expect &&
+       test_cmp expect actual
+'
+
 test_done