git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 6/9] Fix a seriously bad interaction between .git caching and repo cloning

From
Yann Dirson <ydirson@altern.org>
Date
Apr 16, 2006, 10:52 UTC
Message-ID
<20060416105239.9884.5212.stgit@gandelf.nowhere.earth>
In-Reply-To
<20060416104144.9884.28167.stgit@gandelf.nowhere.earth>

Testcase 2 exhibits a problem with caching the location of .git while cloning a repository. Since basedir.get() is called before the clone is built, a value may get stored in the cache if we are within a git-controlled tree already. Then when constructing the object for the clone, a bogus .git is used, which can lead, when running tests in t/trash, to corruption of the stgit .git repository.

Testcase 1 does not show any problem by chance, because since we have a ./.git prepared for use by the testsuite, value ".git" get cached, and it happens that this value will be still valid after chdir'ing into the newborn clone.

Clearing the cache at the appropriate place fixes the problem.
Signed-off-by: Yann Dirson <ydirson@altern.org>
---
 stgit/basedir.py        |    6 ++++++
 stgit/commands/clone.py |    3 +++
 t/t1100-clone-under.sh  |   36 ++++++++++++++++++++++++++++++++++++
 3 files changed, 45 insertions(+), 0 deletions(-)
diff --git a/stgit/basedir.py b/stgit/basedir.py
index c394572..81f2b40 100644
--- a/stgit/basedir.py
+++ b/stgit/basedir.py
@@ -42,3 +42,9 @@ def get():
             __base_dir = __output('git-rev-parse --git-dir 2> /dev/null')
 
     return __base_dir
+
+def clear_cache():
+    """Clear the cached location of .git
+    """
+    global __base_dir
+    __base_dir = None
diff --git a/stgit/commands/clone.py b/stgit/commands/clone.py
index 9ad76a6..455dd6e 100644
--- a/stgit/commands/clone.py
+++ b/stgit/commands/clone.py
@@ -51,6 +51,9 @@ def func(parser, options, args):
     os.chdir(local_dir)
     git.checkout(tree_id = 'HEAD')
 
+    # be sure to forget any cached value for .git, since we're going
+    # to work on a brand new repository
+    basedir.clear_cache()
     stack.Series().init()
 
     print 'done'
diff --git a/t/t1100-clone-under.sh b/t/t1100-clone-under.sh
new file mode 100755
index 0000000..c86ef61
--- /dev/null
+++ b/t/t1100-clone-under.sh
@@ -0,0 +1,36 @@
+#!/bin/sh
+#
+# Copyright (c) 2006 Yann Dirson
+#
+
+test_description='Check cloning in a repo subdir
+
+Check that "stg clone" works in a subdir of a git tree.
+This ensures (to some point) that a clone within a tree does
+not corrupt the enclosing repo.
+
+This test must be run before any tests making use of clone.
+'
+
+. ./test-lib.sh
+
+# Here we are in a repo, we have a ./.git
+# Do not get rid of it, or a bug may bite out stgit repo hard
+
+# Need a repo to clone
+test_create_repo foo
+
+test_expect_success \
+    'stg clone right inside a git tree' \
+    "stg clone foo bar"
+
+# now work in a subdir
+mkdir sub
+mv foo sub
+cd sub
+
+test_expect_success \
+    'stg clone deeper under a git tree' \
+    "stg clone foo bar"
+
+test_done
Previous: Yann DirsonNext: Yann Dirson
Message 7 of 10 in “Add a testsuite to stgit (take 3), and more”
  1. 0/9 Add a testsuite to stgit (take 3), and moreYann Dirson, Apr 16, 2006
  2. 1/9 Add a testsuite framework copied from git-coreYann Dirson, Apr 16, 2006
  3. 2/9 Add list of bugs to TODOYann Dirson, Apr 16, 2006
  4. 3/9 Add a couple of safety checks to series creationYann Dirson, Apr 16, 2006
  5. 4/9 Make branch creation atomicYann Dirson, Apr 16, 2006
  6. 5/9 Correctly handle refs/patches on series renameYann Dirson, Apr 16, 2006
  7. 6/9 Fix a seriously bad interaction between .git caching and repo cloningYann Dirson, Apr 16, 2006
  8. 7/9 Test that pulls a patch creating a file that got modified afterwardsYann Dirson, Apr 16, 2006
  9. 8/9 Exercise "stg pull" on patches just appending lines.Yann Dirson, Apr 16, 2006
  10. 9/9 Look for templates in ~/.stgit/templates as wellYann Dirson, Apr 16, 2006

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.