Skip to contentSkip to conversation

Implement a new patch identification scheme and id command

PATCH In review Catalin Marinas 2 participants 6 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Implement a new patch identification scheme and id command

The new scheme allows '[<branch>:]<patch>' and '[<branch>:]{base}' (the latter showing the base of a stack). The former format allows symbols like ^, ^{...} etc.

---

 stgit/commands/common.py   |   32 ++++++++++++++++++++++++++++++++
 stgit/commands/id.py       |   25 +++++++++++--------------
 stgit/lib/git.py           |    4 ++--
 t/t0001-subdir-branches.sh |   26 +++++++++++---------------
 t/t1200-push-modified.sh   |    2 +-
 t/t1201-pull-trailing.sh   |    2 +-
 t/t2200-rebase.sh          |    2 +-
 7 files changed, 59 insertions(+), 34 deletions(-)
stgit/commands/common.py +32 -0
index 029ec65..d1545e4 100644
@@ -28,6 +28,7 @@ from stgit.run import *
 from stgit import stack, git, basedir
 from stgit.config import config, file_extensions
 from stgit.lib import stack as libstack
+from stgit.lib import git as libgit
 
 # Command exception class
 class CmdException(StgException):
@@ -116,6 +117,37 @@ def git_id(crt_series, rev):
 
     raise CmdException, 'Unknown patch or revision: %s' % rev
 
+def git_sha1(repository, branch, name):
+    """Return the SHA1 value if 'name' is a patch name or Git commit.
+    The patch names allowed are in the form '<branch>:<patch>' and can be
+    followed by standard symbols used by git-rev-parse. If <patch> is '{base}',
+    it represents the bottom of the stack.
+    """
+    # Try a Git commit first
+    try:
+        return repository.rev_parse(name, discard_stderr = True).sha1
+    except libgit.RepositoryException:
+        pass
+
+    # Git didn't recognise it, try a [branch:]patch name
+    try:
+        branch, patch = name.split(':', 1)
+    except ValueError:
+        patch = name
+    if not branch:
+        branch = repository.current_branch_name
+
+    # the stack base
+    if patch == '{base}':
+        return repository.get_stack(branch).base.sha1
+
+    # any other combination of branch and patch
+    try:
+        return repository.rev_parse('patches/%s/%s' % (branch, patch),
+                                    discard_stderr = True).sha1
+    except libgit.RepositoryException:
+        raise CmdException('%s: Unknown patch or revision name' % name)
+
 def check_local_changes():
     if git.local_changes():
         raise CmdException('local changes in the tree. Use "refresh" or'
stgit/commands/id.py +11 -14
index 94b0229..50f6e8a 100644
@@ -15,29 +15,26 @@ along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 """
 
-import sys, os
 from optparse import OptionParser, make_option
 
-from stgit.commands.common import *
-from stgit.utils import *
-from stgit.out import *
-from stgit import stack, git
-
+from stgit.out import out
+from stgit.commands import common
+from stgit.lib import stack
 
 help = 'print the GIT hash value of a StGIT reference'
 usage = """%prog [options] [id]
 
-Print the hash value of a GIT id (defaulting to HEAD). In addition to
-the standard GIT id's like heads and tags, this command also accepts
-'base[@<branch>]' and '[<patch>[@<branch>]][//[bottom | top]]'. If no
-'top' or 'bottom' are passed and <patch> is a valid patch name, 'top'
-will be used by default."""
+Print the SHA1 value of a Git id (defaulting to HEAD). In addition to
+the standard Git id's like heads and tags, this command also accepts
+'[<branch>:]<patch>]' and '[<branch>:]{base}]' showing the id of a patch
+or the base of the stack. If no branch is specified, it defaults to the
+current one. The bottom of a patch is accessible with the
+'[<branch>:]<patch>]^' format."""
 
-directory = DirectoryHasRepository()
+directory = common.DirectoryHasRepositoryLib()
 options = [make_option('-b', '--branch',
                        help = 'use BRANCH instead of the default one')]
 
-
 def func(parser, options, args):
     """Show the applied patches
     """
@@ -48,4 +45,4 @@ def func(parser, options, args):
     else:
         parser.error('incorrect number of arguments')
 
-    out.stdout(git_id(crt_series, id_str))
+    out.stdout(common.git_sha1(directory.repository, options.branch, id_str))
stgit/lib/git.py +2 -2
index 0e0cccb..379fb52 100644
@@ -422,11 +422,11 @@ class Repository(RunWithEnv):
     refs = property(lambda self: self.__refs)
     def cat_object(self, sha1):
         return self.run(['git', 'cat-file', '-p', sha1]).raw_output()
-    def rev_parse(self, rev):
+    def rev_parse(self, rev, discard_stderr = False):
         try:
             return self.get_commit(self.run(
                     ['git', 'rev-parse', '%s^{commit}' % rev]
-                    ).output_one_line())
+                    ).discard_stderr(discard_stderr).output_one_line())
         except run.RunException:
             raise RepositoryException('%s: No such revision' % rev)
     def get_tree(self, sha1):
t/t0001-subdir-branches.sh +11 -15
index 0eed3a4..cf2059a 100755
@@ -1,6 +1,6 @@
 #!/bin/sh
 #
-# Copyright (c) 2006 Karl Hasselstr�m
+# Copyright (c) 2006 Karl Hasselstr�m
 #
 
 test_description='Branch names containing slashes
@@ -18,25 +18,21 @@ test_expect_success 'Create a patch' \
    stg new foo -m "Add foo.txt" &&
    stg refresh'
 
-test_expect_success 'Old and new id with non-slashy branch' \
-  'stg id foo &&
-   stg id foo// &&
-   stg id foo/ &&
-   stg id foo//top &&
-   stg id foo/top &&
-   stg id foo@master &&
-   stg id foo@master//top &&
-   stg id foo@master/top'
+test_expect_success 'Try id with non-slashy branch' \
+  'stg id &&
+   stg id foo &&
+   stg id foo^ &&
+   stg id master:foo &&
+   stg id master:foo^'
 
 test_expect_success 'Clone branch to slashier name' \
   'stg branch --clone x/y/z'
 
-test_expect_success 'Try new form of id with slashy branch' \
+test_expect_success 'Try new id with slashy branch' \
   'stg id foo &&
-   stg id foo// &&
-   stg id foo//top &&
-   stg id foo@x/y/z &&
-   stg id foo@x/y/z//top'
+   stg id foo^ &&
+   stg id x/y/z:foo &&
+   stg id x/y/z:foo^'
 
 test_expect_success 'Try old id with slashy branch' '
    ! stg id foo/ &&
t/t1200-push-modified.sh +1 -1
index ba4f70c..e3c6425 100755
@@ -36,7 +36,7 @@ test_expect_success \
     (
         cd foo &&
         GIT_DIR=../bar/.git git-format-patch --stdout \
-          $(cd ../bar && stg id base@master)..HEAD | git-am -3 -k
+          $(cd ../bar && stg id master:{base})..HEAD | git-am -3 -k
     )
 '
 
t/t1201-pull-trailing.sh +1 -1
index 9d70fe0..8a74873 100755
@@ -30,7 +30,7 @@ test_expect_success \
     'Port those patches to orig tree' \
     '(cd foo &&
       GIT_DIR=../bar/.git git-format-patch --stdout \
-          $(cd ../bar && stg id base@master)..HEAD |
+          $(cd ../bar && stg id master:{base})..HEAD |
       git-am -3 -k
      )
     '
t/t2200-rebase.sh +1 -1
index ec2a104..cd43c41 100755
@@ -27,7 +27,7 @@ test_expect_success \
 	'Rebase to previous commit' \
 	'
 	stg rebase master~1 &&
-	test `stg id base@stack` = `git rev-parse master~1` &&
+	test `stg id stack:{base}` = `git rev-parse master~1` &&
 	test `stg applied | wc -l` = 1
 	'
 
#1 lore raw

2008/6/14 Catalin Marinas <[email protected]>:

t/t0001-subdir-branches.sh line 3

Quoted 7 lines
t/t0001-subdir-branches.sh +1 -1
@@ -1,6 +1,6 @@
 #!/bin/sh
 #
-# Copyright (c) 2006 Karl Hasselström
+# Copyright (c) 2006 Karl Hasselstr�m

I'll remove this hunk :-) (not sure how it got here).

Signature · 1 line
Catalin
#2 lore raw
In reply to Catalin Marinas
Quoted 3 lines · Catalin Marinas

The new scheme allows '[<branch>:]<patch>' and '[<branch>:]{base}' (the latter showing the base of a stack). The former format allows symbols like ^, ^{...} etc.

I like your choices.

stgit/commands/common.py def git_id(crt_series, rev): · line 125

Quoted 6 lines
stgit/commands/common.py +6 -0
+def git_sha1(repository, branch, name):
+    """Return the SHA1 value if 'name' is a patch name or Git commit.
+    The patch names allowed are in the form '<branch>:<patch>' and can be
+    followed by standard symbols used by git-rev-parse. If <patch> is '{base}',
+    it represents the bottom of the stack.
+    """

Why not return the Commit directly, and let the caller extract its sha1 if that's what it wants?

You don't remove the old parse_rev() and git_id(), and particularly the latter has a lot of callers. Meaning that the rest of StGit still speaks the old syntax.

stgit/commands/common.py def git_id(crt_series, rev): · line 130

Quoted 5 lines
stgit/commands/common.py +5 -0
+    # Try a Git commit first
+    try:
+        return repository.rev_parse(name, discard_stderr = True).sha1
+    except libgit.RepositoryException:
+        pass

What if you have a branch or tag with the same name as a patch? This will prefer the branch, which might not be the best choice.

Quoted 2 lines

+current one. The bottom of a patch is accessible with the +'[<branch>:]<patch>]^' format."""

You have an extra ] here.

stgit/commands/id.py along with this program; if not, write to the Free Software · line 36

Quoted 4 lines
stgit/commands/id.py +1 -1
-directory = DirectoryHasRepository()
+directory = common.DirectoryHasRepositoryLib()
 options = [make_option('-b', '--branch',
                        help = 'use BRANCH instead of the default one')]

Couldn't we kill this option? (And in the process, the branch argument to git_sha1.)

t/t0001-subdir-branches.sh test_expect_success 'Create a patch' \ · line 31

Quoted 2 lines
t/t0001-subdir-branches.sh +1 -1
-test_expect_success 'Try new form of id with slashy branch' \
+test_expect_success 'Try new id with slashy branch' \

Strictly speaking, this isn't so new anymore.

Signature · 2 lines
Karl Hasselström, [email protected]
      www.treskal.com/kalle
#3 lore raw

2008/6/14 Karl Hasselström <[email protected]>:

Quoted 7 lines · Catalin Marinas
Quoted 4 lines

The new scheme allows '[<branch>:]<patch>' and '[<branch>:]{base}' (the latter showing the base of a stack). The former format allows symbols like ^, ^{...} etc.

I like your choices.

I think the initial idea belongs to Yann.

Quoted 9 lines

stgit/commands/common.py def git_id(crt_series, rev): · line 125

Quoted 6 lines
stgit/commands/common.py +6 -0
+def git_sha1(repository, branch, name):
+    """Return the SHA1 value if 'name' is a patch name or Git commit.
+    The patch names allowed are in the form '<branch>:<patch>' and can be
+    followed by standard symbols used by git-rev-parse. If <patch> is '{base}',
+    it represents the bottom of the stack.
+    """

Why not return the Commit directly, and let the caller extract its sha1 if that's what it wants?

OK, I was thinking about that. I'll convert it to git_commit(...) and return a commit since I think it's only "stg id" that needs the SHA1 value.

Quoted 3 lines

You don't remove the old parse_rev() and git_id(), and particularly the latter has a lot of callers. Meaning that the rest of StGit still speaks the old syntax.

I thought about removing them when we convert the commands to the new infrastructure. In the meantime, I can rewrite git_id to use git_commit directly. The parse_rev is only used by the 'pick' command (and git_id). I'll have a look at these functions.

Quoted 8 lines

stgit/commands/common.py def git_id(crt_series, rev): · line 130

Quoted 5 lines
stgit/commands/common.py +5 -0
+    # Try a Git commit first
+    try:
+        return repository.rev_parse(name, discard_stderr = True).sha1
+    except libgit.RepositoryException:
+        pass

What if you have a branch or tag with the same name as a patch? This will prefer the branch, which might not be the best choice.

I can swap them and have the patch as preferred. People can be more precise if they want the branch or tag by passing heads/... or tags/...

Quoted 7 lines

stgit/commands/id.py along with this program; if not, write to the Free Software · line 36

Quoted 4 lines
stgit/commands/id.py +1 -1
-directory = DirectoryHasRepository()
+directory = common.DirectoryHasRepositoryLib()
 options = [make_option('-b', '--branch',
                        help = 'use BRANCH instead of the default one')]

Couldn't we kill this option? (And in the process, the branch argument to git_sha1.)

No problem with the option but I would like to keep the branch argument to git_sha1. There might be cases where it is used like picking multiple patches and I only specify one -B option to 'pick'. I don't want to build the qualified patch name for every patch. I can change the prototype of git_sha1 though (to git_commit):

def git_commit(name, repository, branch = None):

and branch needs to be passed explicitly and used instead of the default one (when the patch name doesn't contain any).

Quoted 4 lines

t/t0001-subdir-branches.sh test_expect_success 'Create a patch' \ · line 31

Quoted 2 lines
t/t0001-subdir-branches.sh +1 -1
-test_expect_success 'Try new form of id with slashy branch' \
+test_expect_success 'Try new id with slashy branch' \

Strictly speaking, this isn't so new anymore.

The id format is new.

Signature · 1 line
Catalin
#4 lore raw

2008/6/16 Catalin Marinas <[email protected]>:

Quoted 10 lines

2008/6/14 Karl Hasselström <[email protected]>:

Quoted 4 lines · Catalin Marinas

You don't remove the old parse_rev() and git_id(), and particularly the latter has a lot of callers. Meaning that the rest of StGit still speaks the old syntax.

I thought about removing them when we convert the commands to the new infrastructure. In the meantime, I can rewrite git_id to use git_commit directly. The parse_rev is only used by the 'pick' command (and git_id). I'll have a look at these functions.

Done that - git_id/parse_rev now use the new id mechanism. Eventually we'll drop git_id once all the commands are converted.

Quoted 11 lines
Quoted 7 lines

stgit/commands/id.py along with this program; if not, write to the Free Software · line 36

Quoted 4 lines
stgit/commands/id.py +1 -1
-directory = DirectoryHasRepository()
+directory = common.DirectoryHasRepositoryLib()
 options = [make_option('-b', '--branch',
                        help = 'use BRANCH instead of the default one')]

Couldn't we kill this option? (And in the process, the branch argument to git_sha1.)

No problem with the option but I would like to keep the branch argument to git_sha1. There might be cases where it is used like picking multiple patches and I only specify one -B option to 'pick'.

What about supporting patch ranges with the new id format, something like:

branch:patch1..patch2,patch3

or

branch:patch1..patch2 branch:patch3

The following can be valid as well but patch3 may be on a different branch:

branch:patch1..patch2 patch3

This way we could get rid of many --branch options.

Signature · 1 line
Catalin
#5 lore raw
Quoted 8 lines · Catalin Marinas

What about supporting patch ranges with the new id format, something like:

branch:patch1..patch2,patch3

or

branch:patch1..patch2 branch:patch3

Yes, that's a good idea; the endpoints of a range have to be on the same branch no matter what, so having the branch: prefix apply to both of the endpoint patches in "branch:patch1..patch2" is a good idea.

I'm not sure if the comma notation is worth it. And if it turns out to have been useful, we can just advise users to write

  $ stg foo branch:{p1..p2,p3}

which the shell will expand to

  $ stg foo branch:p1..p2 branch:p3
Quoted 1 line

This way we could get rid of many --branch options.

Indeed.

Signature · 2 lines
Karl Hasselström, [email protected]
      www.treskal.com/kalle