git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Karl Hasselström" <kha@treskal.com>
To: Catalin Marinas <catalin.marinas@gmail.com>
Cc: git@vger.kernel.org, "David Kågedal" <davidk@lysator.liu.se>
Subject: [StGit PATCH 2/2] Make "stg goto" subdirectory safe
Date: Wed, 19 Dec 2007 23:24:49 +0100	[thread overview]
Message-ID: <20071219221957.29455.27315.stgit@yoghurt> (raw)
In-Reply-To: <20071219221848.29455.50676.stgit@yoghurt>

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 <kha@treskal.com>

---

On 2007-12-18 10:24:19 +0100, Karl Hasselström wrote:

> On 2007-12-18 09:11:00 +0000, Catalin Marinas wrote:
> 
> > On 17/12/2007, Karl Hasselström <kha@treskal.com> wrote:
> >
> > > Be careful about merging past the "goto" patch -- I'm pretty
> > > sure it breaks when called from a subdirectory, and I don't have
> > > time to fix that right now. (It should be a clean fix, though --
> > > just adjust the cwd for precisely those git subprocesses that
> > > need it, which is very few. I think.)
> >
> > Why not just change the cwd when the command starts and it should
> > be safe for all the git subprocesses.
> 
> It doesn't feel very clean to require the caller of some unspecified
> subset of git calls to remember to set the correct piece of global
> state.
> 
> And the correct solution should really be very simple, since it's
> precisely the worktree operations (specifically, the subset thereof
> that have to operate on the whole tree) that require setting cwd.
> And those are very clearly separated out from the rest in the new
> infrastructure.

And here it is! Note that not counting the cwd support in run.py and
changing the test to expect success, I had to change just three lines
to fix this problem for _all_ "new infrastructure" commands. And no
unintended side effects, since the cwd is changed only where it's
necessary.

 stgit/lib/git.py       |    5 +++--
 stgit/run.py           |    9 ++++++---
 t/t2800-goto-subdir.sh |    4 ++--
 3 files changed, 11 insertions(+), 7 deletions(-)


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 <<EOF
 bar
 EOF
-test_expect_failure 'Goto in subdirectory (just pop)' '
+test_expect_success 'Goto in subdirectory (just pop)' '
     (cd foo && stg goto p1) &&
     cat foo/bar > actual.txt &&
     diff -u expected1.txt actual.txt &&
@@ -47,7 +47,7 @@ EOF
 cat > expected2.txt <<EOF
 bar
 EOF
-test_expect_failure 'Goto in subdirectory (conflicting push)' '
+test_expect_success 'Goto in subdirectory (conflicting push)' '
     (cd foo && stg goto p3) ;
     [ $? -eq 3 ] &&
     cat foo/bar > actual.txt &&

  parent reply	other threads:[~2007-12-19 22:25 UTC|newest]

Thread overview: 57+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-12-14 10:55 [StGit PATCH 00/17] Series short description David Kågedal
2007-12-14 10:55 ` [StGit PATCH 01/17] Add an StGit mode for emacs David Kågedal
2007-12-14 10:56 ` [StGit PATCH 02/17] Emacs mode: Show keybindings when user presses "h" or "?" David Kågedal
2007-12-14 10:56 ` [StGit PATCH 03/17] Emacs mode: Add an explanatory header David Kågedal
2007-12-14 10:57 ` [StGit PATCH 04/17] Emacs mode: Makefile for building stgit.el David Kågedal
2007-12-14 10:57 ` [StGit PATCH 05/17] Emacs mode: push/pop next patch, not patch at point David Kågedal
2007-12-14 10:57 ` [StGit PATCH 06/17] Emacs mode: Let "P" push or pop " David Kågedal
2007-12-14 10:57 ` [StGit PATCH 07/17] Emacs mode: Bind "G" to "stg goto" David Kågedal
2007-12-14 10:57 ` [StGit PATCH 08/17] Emacs mode: show patches' short description David Kågedal
2007-12-14 10:58 ` [StGit PATCH 09/17] Emacs mode: Improve the output buffer state David Kågedal
2007-12-14 10:58 ` [StGit PATCH 10/17] Emacs mode: Bind n and p David Kågedal
2007-12-14 10:58 ` [StGit PATCH 11/17] Emacs mode: add stgit-repair David Kågedal
2007-12-14 10:58 ` [StGit PATCH 12/17] Emacs mode: added stgit-commit and stgit-uncommit David Kågedal
2007-12-14 10:59 ` [StGit PATCH 13/17] Emacs mode: Add stgit-edit command David Kågedal
2007-12-14 10:59 ` [StGit PATCH 14/17] Emacs mode: added fontification David Kågedal
2007-12-14 10:59 ` [StGit PATCH 15/17] Emacs mode: Added stgit-new David Kågedal
2007-12-14 10:59 ` [StGit PATCH 16/17] Emacs mode: Add mark command David Kågedal
2007-12-14 10:59 ` [StGit PATCH 17/17] Emacs mode: coalesce command David Kågedal
2007-12-17 11:09 ` [StGit PATCH 00/17] Series short description Catalin Marinas
2007-12-17 22:48   ` Karl Hasselström
2007-12-18  5:21     ` kha/safe and kha/experimental updated Karl Hasselström
2007-12-18 16:09       ` Catalin Marinas
2007-12-18 16:39         ` Jakub Narebski
2007-12-18 16:52           ` Catalin Marinas
2007-12-19  9:41             ` David Kågedal
2007-12-19  9:50               ` David Kågedal
2007-12-19 10:19               ` Catalin Marinas
2007-12-19  9:38           ` Karl Hasselström
2007-12-19 10:44             ` Jakub Narebski
2007-12-19 11:40               ` Karl Hasselström
2007-12-19 11:47                 ` Catalin Marinas
2007-12-19 16:23                 ` Jakub Narebski
2007-12-19 17:02                   ` Catalin Marinas
2007-12-19 17:14                     ` David Kågedal
2007-12-19 17:14                   ` Karl Hasselström
2007-12-19  9:34         ` Karl Hasselström
2007-12-19 10:09           ` Catalin Marinas
2007-12-19 11:20             ` Karl Hasselström
2007-12-19 11:40               ` Catalin Marinas
2007-12-19 12:10                 ` Karl Hasselström
2007-12-19 15:38         ` Catalin Marinas
2007-12-19 14:59       ` Catalin Marinas
2007-12-19 15:39         ` David Kågedal
2007-12-18  9:11     ` [StGit PATCH 00/17] Series short description Catalin Marinas
2007-12-18  9:20       ` David Kågedal
2007-12-18  9:24       ` Karl Hasselström
2007-12-19 22:19         ` [StGit PATCH 0/2] Make new infrastructure subdirectory safe Karl Hasselström
2007-12-19 22:19           ` [StGit PATCH 1/2] Test that "stg goto" can be called from a subdirectory Karl Hasselström
2007-12-19 22:24           ` Karl Hasselström [this message]
2007-12-19 22:46             ` kha/safe updated Karl Hasselström
2007-12-19 23:17               ` Catalin Marinas
2007-12-19 23:26                 ` Karl Hasselström
2007-12-19 23:29                   ` Catalin Marinas
2007-12-20  6:39                     ` Karl Hasselström
2007-12-19 23:24               ` David Kågedal
2007-12-20  6:38                 ` Karl Hasselström
2007-12-19 22:28           ` [StGit PATCH 0/2] Make new infrastructure subdirectory safe Karl Hasselström

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20071219221957.29455.27315.stgit@yoghurt \
    --to=kha@treskal.com \
    --cc=catalin.marinas@gmail.com \
    --cc=davidk@lysator.liu.se \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).