From: Karl Hasselström Date: Wed, 19 Dec 2007 21:12:20 +0000 (+0100) Subject: Make "stg goto" subdirectory safe X-Git-Tag: v0.15-rc1~303 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~mdw/git/stgit/commitdiff_plain/8d96d568f4e73e104ae8409fe552b3f47d944043 Make "stg goto" subdirectory safe This is not specific to "stg goto" -- it affects all commands that use the new infrastructure. (But of those, only goto and coalesce were subdirectory unsafe.) Signed-off-by: Karl Hasselström --- diff --git a/stgit/lib/git.py b/stgit/lib/git.py index 6aba966..118c9b2 100644 --- a/stgit/lib/git.py +++ b/stgit/lib/git.py @@ -344,6 +344,7 @@ class Worktree(object): def __init__(self, directory): self.__directory = directory env = property(lambda self: { 'GIT_WORK_TREE': self.__directory }) + directory = property(lambda self: self.__directory) class CheckoutException(exception.StgException): pass @@ -364,7 +365,7 @@ class IndexAndWorktree(RunWithEnv): self.run(['git', 'read-tree', '-u', '-m', '--exclude-per-directory=.gitignore', old_tree.sha1, new_tree.sha1] - ).discard_output() + ).cwd(self.__worktree.directory).discard_output() except run.RunException: raise CheckoutException('Index/workdir dirty') def merge(self, base, ours, theirs): @@ -377,7 +378,7 @@ class IndexAndWorktree(RunWithEnv): env = { 'GITHEAD_%s' % base.sha1: 'ancestor', 'GITHEAD_%s' % ours.sha1: 'current', 'GITHEAD_%s' % theirs.sha1: 'patched'} - ).discard_output() + ).cwd(self.__worktree.directory).discard_output() except run.RunException, e: raise MergeException('Index/worktree dirty') def changed_files(self): diff --git a/stgit/run.py b/stgit/run.py index 78537db..77f2e65 100644 --- a/stgit/run.py +++ b/stgit/run.py @@ -42,7 +42,7 @@ class Run: if type(c) != str: raise Exception, 'Bad command: %r' % (cmd,) self.__good_retvals = [0] - self.__env = None + self.__env = self.__cwd = None self.__indata = None self.__discard_stderr = False def __log_start(self): @@ -67,7 +67,7 @@ class Run: """Run with captured IO.""" self.__log_start() try: - p = subprocess.Popen(self.__cmd, env = self.__env, + p = subprocess.Popen(self.__cmd, env = self.__env, cwd = self.__cwd, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE) @@ -85,7 +85,7 @@ class Run: assert self.__indata == None self.__log_start() try: - p = subprocess.Popen(self.__cmd, env = self.__env) + p = subprocess.Popen(self.__cmd, env = self.__env, cwd = self.__cwd) self.exitcode = p.wait() except OSError, e: raise self.exc('%s failed: %s' % (self.__cmd[0], e)) @@ -104,6 +104,9 @@ class Run: self.__env = dict(os.environ) self.__env.update(env) return self + def cwd(self, cwd): + self.__cwd = cwd + return self def raw_input(self, indata): self.__indata = indata return self diff --git a/t/t2800-goto-subdir.sh b/t/t2800-goto-subdir.sh index 9f3ab26..fcad7da 100755 --- a/t/t2800-goto-subdir.sh +++ b/t/t2800-goto-subdir.sh @@ -24,7 +24,7 @@ EOF cat > expected2.txt < actual.txt && diff -u expected1.txt actual.txt && @@ -47,7 +47,7 @@ EOF cat > expected2.txt < actual.txt &&