]> git.ipfire.org Git - thirdparty/git.git/blobdiff - setup.c
Merge branch 'ep/maint-equals-null-cocci' for maint-2.35
[thirdparty/git.git] / setup.c
diff --git a/setup.c b/setup.c
index ac1ef9d97649ae5cc051bdaf32bb16cb6e207dbf..a47ef58df5305811edb4f120ebac8538b5385629 100644 (file)
--- a/setup.c
+++ b/setup.c
@@ -13,6 +13,7 @@ static int work_tree_config_is_bogus;
 
 static struct startup_info the_startup_info;
 struct startup_info *startup_info = &the_startup_info;
+const char *tmp_original_cwd;
 
 /*
  * The input parameter must contain an absolute path, and it must already be
@@ -433,6 +434,69 @@ void setup_work_tree(void)
        initialized = 1;
 }
 
+static void setup_original_cwd(void)
+{
+       struct strbuf tmp = STRBUF_INIT;
+       const char *worktree = NULL;
+       int offset = -1;
+
+       if (!tmp_original_cwd)
+               return;
+
+       /*
+        * startup_info->original_cwd points to the current working
+        * directory we inherited from our parent process, which is a
+        * directory we want to avoid removing.
+        *
+        * For convience, we would like to have the path relative to the
+        * worktree instead of an absolute path.
+        *
+        * Yes, startup_info->original_cwd is usually the same as 'prefix',
+        * but differs in two ways:
+        *   - prefix has a trailing '/'
+        *   - if the user passes '-C' to git, that modifies the prefix but
+        *     not startup_info->original_cwd.
+        */
+
+       /* Normalize the directory */
+       strbuf_realpath(&tmp, tmp_original_cwd, 1);
+       free((char*)tmp_original_cwd);
+       tmp_original_cwd = NULL;
+       startup_info->original_cwd = strbuf_detach(&tmp, NULL);
+
+       /*
+        * Get our worktree; we only protect the current working directory
+        * if it's in the worktree.
+        */
+       worktree = get_git_work_tree();
+       if (!worktree)
+               goto no_prevention_needed;
+
+       offset = dir_inside_of(startup_info->original_cwd, worktree);
+       if (offset >= 0) {
+               /*
+                * If startup_info->original_cwd == worktree, that is already
+                * protected and we don't need original_cwd as a secondary
+                * protection measure.
+                */
+               if (!*(startup_info->original_cwd + offset))
+                       goto no_prevention_needed;
+
+               /*
+                * original_cwd was inside worktree; precompose it just as
+                * we do prefix so that built up paths will match
+                */
+               startup_info->original_cwd = \
+                       precompose_string_if_needed(startup_info->original_cwd
+                                                   + offset);
+               return;
+       }
+
+no_prevention_needed:
+       free((char*)startup_info->original_cwd);
+       startup_info->original_cwd = NULL;
+}
+
 static int read_worktree_config(const char *var, const char *value, void *vdata)
 {
        struct repository_format *data = vdata;
@@ -1391,6 +1455,7 @@ const char *setup_git_directory_gently(int *nongit_ok)
                setenv(GIT_PREFIX_ENVIRONMENT, "", 1);
        }
 
+       setup_original_cwd();
 
        strbuf_release(&dir);
        strbuf_release(&gitdir);