From: Duy Nguyen <pclo...@gmail.com>

When $CWD is moved, relative path must be updated to be relative to
the new $CWD. This function helps do that. The _in_place version is
just for convenient (and we will use it quite often in subsequent
patches).

Signed-off-by: Nguyễn Thái Ngọc Duy <pclo...@gmail.com>
---
 cache.h |  3 +++
 setup.c | 20 ++++++++++++++++++++
 2 files changed, 23 insertions(+)

diff --git a/cache.h b/cache.h
index bbaf5c349a..05f32c9659 100644
--- a/cache.h
+++ b/cache.h
@@ -522,6 +522,9 @@ extern void set_git_work_tree(const char *tree);
 
 #define ALTERNATE_DB_ENVIRONMENT "GIT_ALTERNATE_OBJECT_DIRECTORIES"
 
+extern void setup_adjust_path(const char *name, char **path,
+                             const char *old_cwd,
+                             const char *new_cwd);
 extern void setup_work_tree(void);
 /*
  * Find the commondir and gitdir of the repository that contains the current
diff --git a/setup.c b/setup.c
index 664453fcef..e26f44185e 100644
--- a/setup.c
+++ b/setup.c
@@ -376,6 +376,26 @@ int is_inside_work_tree(void)
        return inside_work_tree;
 }
 
+void setup_adjust_path(const char *name, char **path,
+                      const char *old_cwd,
+                      const char *new_cwd)
+{
+       char *old_path = *path;
+       struct strbuf sb = STRBUF_INIT;
+
+       if (!old_path || is_absolute_path(old_path))
+               return;
+
+       strbuf_addstr(&sb, old_cwd);
+       strbuf_ensure_trailing_dir_sep(&sb);
+       strbuf_addstr(&sb, old_path);
+       *path = xstrdup(remove_leading_path(sb.buf, new_cwd));
+       trace_printf_key(&trace_setup_key, "setup: adjust '%s' to %s",
+                        name, *path);
+       strbuf_release(&sb);
+       free(old_path);
+}
+
 void setup_work_tree(void)
 {
        const char *work_tree, *git_dir;
-- 
2.17.0.rc1.439.gca064e2955

Reply via email to