Skip to contentSkip to conversation

Proposed patches

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

lore
Share & tools
lore
#0 lore raw

cover Proposed patches

This series contains the patches pending in my "proposed" branch. They've been updated following comments from Karl.

Catalin Marinas (5):
Add test_patches target to Makefile
Convert "init" to the new StGIT infrastructure Add stack creation and initialisation support to lib.Stack Create a git.Branch class as ancestor of stack.Stack Rename Repository.head to Repository.head_ref

 Makefile               |    9 ++++-
 stgit/commands/init.py |   14 ++-----
 stgit/commands/new.py  |    3 +-
 stgit/lib/git.py       |   58 ++++++++++++++++++++++++++++++-
 stgit/lib/stack.py     |   90 ++++++++++++++++++++++++++++++++++++------------
 5 files changed, 137 insertions(+), 37 deletions(-)
Signature · 1 line
Catalin
#1 lore raw

PATCH 1/5 Rename Repository.head to Repository.head_ref

This is to avoid confusion with the Stack.head function which returns a commit object rather than a file name. The patch also changes the "new" function to use stack.head directly rather than via the Repository.refs... object.

---

 stgit/commands/new.py |    3 +--
 stgit/lib/git.py      |    4 ++--
 stgit/lib/stack.py    |    2 +-
 3 files changed, 4 insertions(+), 5 deletions(-)
stgit/commands/new.py +1 -2
index 15bb2e0..c4ee4e1 100644
@@ -58,9 +58,8 @@ def func(parser, options, args):
     else:
         parser.error('incorrect number of arguments')
 
-    head = directory.repository.refs.get(directory.repository.head)
     cd = gitlib.CommitData(
-        tree = head.data.tree, parents = [head], message = '',
+        tree = stack.head.data.tree, parents = [stack.head], message = '',
         author = gitlib.Person.author(), committer = gitlib.Person.committer())
 
     # Set patch commit message from commandline.
stgit/lib/git.py +2 -2
index c9f01e3..fd66f6d 100644
@@ -443,13 +443,13 @@ class Repository(RunWithEnv):
                                                 ).output_one_line()
         return self.get_commit(sha1)
     @property
-    def head(self):
+    def head_ref(self):
         try:
             return self.run(['git', 'symbolic-ref', '-q', 'HEAD']
                             ).output_one_line()
         except run.RunException:
             raise DetachedHeadException()
-    def set_head(self, ref, msg):
+    def set_head_ref(self, ref, msg):
         self.run(['git', 'symbolic-ref', '-m', msg, 'HEAD', ref]).no_output()
     def simple_merge(self, base, ours, theirs):
         """Given three L{Tree}s, tries to do an in-index merge with a
stgit/lib/stack.py +1 -1
index bdd21b1..b1544a5 100644
@@ -178,7 +178,7 @@ class Repository(git.Repository):
         self.__stacks = {} # name -> Stack
     @property
     def current_branch(self):
-        return utils.strip_leading('refs/heads/', self.head)
+        return utils.strip_leading('refs/heads/', self.head_ref)
     @property
     def current_stack(self):
         return self.get_stack()
Acked-by on v1 1/5
#2 lore raw

Re: [StGIT PATCH 1/5] Rename Repository.head to Repository.head_ref

Quoted 6 lines · Catalin Marinas

This is to avoid confusion with the Stack.head function which returns a commit object rather than a file name. The patch also changes the "new" function to use stack.head directly rather than via the Repository.refs... object.

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

PATCH 2/5 Create a git.Branch class as ancestor of stack.Stack

In reply to Catalin Marinas

This class deals with Git-specific branch commands. The Stack class is a direct child of Branch and some of its functionality was moved to the new class.

---

 stgit/lib/git.py   |   54 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 stgit/lib/stack.py |   27 ++++++--------------------
 2 files changed, 60 insertions(+), 21 deletions(-)
stgit/lib/git.py +54 -0
index fd66f6d..0e0cccb 100644
@@ -28,6 +28,9 @@ class RepositoryException(exception.StgException):
     """Base class for all exceptions due to failed L{Repository}
     operations."""
 
+class BranchException(exception.StgException):
+    """Exception raised by failed L{Branch} operations."""
+
 class DateException(exception.StgException):
     """Exception raised when a date+time string could not be parsed."""
     def __init__(self, string, type):
@@ -379,6 +382,10 @@ class Repository(RunWithEnv):
         except run.RunException:
             raise RepositoryException('Cannot find git repository')
     @property
+    def current_branch_name(self):
+        """Return the name of the current branch."""
+        return utils.strip_leading('refs/heads/', self.head_ref)
+    @property
     def default_index(self):
         """An L{Index} object representing the default index file for the
         repository."""
@@ -619,3 +626,50 @@ class IndexAndWorktree(RunWithEnvCwd):
     def update_index(self, files):
         self.run(['git', 'update-index', '--remove', '-z', '--stdin']
                  ).input_nulterm(files).discard_output()
+
+class Branch(object):
+    """Represents a Git branch."""
+    def __init__(self, repository, name):
+        self.__repository = repository
+        self.__name = name
+        try:
+            self.head
+        except KeyError:
+            raise BranchException('%s: no such branch' % name)
+
+    name = property(lambda self: self.__name)
+    repository = property(lambda self: self.__repository)
+
+    def __ref(self):
+        return 'refs/heads/%s' % self.__name
+    @property
+    def head(self):
+        return self.__repository.refs.get(self.__ref())
+    def set_head(self, commit, msg):
+        self.__repository.refs.set(self.__ref(), commit, msg)
+
+    def set_parent_remote(self, name):
+        value = config.set('branch.%s.remote' % self.__name, name)
+    def set_parent_branch(self, name):
+        if config.get('branch.%s.remote' % self.__name):
+            # Never set merge if remote is not set to avoid
+            # possibly-erroneous lookups into 'origin'
+            config.set('branch.%s.merge' % self.__name, name)
+
+    @classmethod
+    def create(cls, repository, name, create_at = None):
+        """Create a new Git branch and return the corresponding
+        L{Branch} object."""
+        try:
+            branch = cls(repository, name)
+        except BranchException:
+            branch = None
+        if branch:
+            raise BranchException('%s: branch already exists' % name)
+
+        cmd = ['git', 'branch']
+        if create_at:
+            cmd.append(create_at.sha1)
+        repository.run(['git', 'branch', create_at.sha1]).discard_output()
+
+        return cls(repository, name)
stgit/lib/stack.py +6 -21
index b1544a5..030c407 100644
@@ -130,34 +130,22 @@ class Patches(object):
         self.__patches[name] = p
         return p
 
-class Stack(object):
+class Stack(git.Branch):
     """Represents an StGit stack (that is, a git branch with some extra
     metadata)."""
+    __repo_subdir = 'patches'
+
     def __init__(self, repository, name):
-        self.__repository = repository
-        self.__name = name
-        try:
-            self.head
-        except KeyError:
-            raise exception.StgException('%s: no such branch' % name)
+        git.Branch.__init__(self, repository, name)
         self.__patchorder = PatchOrder(self)
         self.__patches = Patches(self)
         if not stackupgrade.update_to_current_format_version(repository, name):
             raise exception.StgException('%s: branch not initialized' % name)
-    name = property(lambda self: self.__name)
-    repository = property(lambda self: self.__repository)
     patchorder = property(lambda self: self.__patchorder)
     patches = property(lambda self: self.__patches)
     @property
     def directory(self):
-        return os.path.join(self.__repository.directory, 'patches', self.__name)
-    def __ref(self):
-        return 'refs/heads/%s' % self.__name
-    @property
-    def head(self):
-        return self.__repository.refs.get(self.__ref())
-    def set_head(self, commit, msg):
-        self.__repository.refs.set(self.__ref(), commit, msg)
+        return os.path.join(self.repository.directory, self.__repo_subdir, self.name)
     @property
     def base(self):
         if self.patchorder.applied:
@@ -177,14 +165,11 @@ class Repository(git.Repository):
         git.Repository.__init__(self, *args, **kwargs)
         self.__stacks = {} # name -> Stack
     @property
-    def current_branch(self):
-        return utils.strip_leading('refs/heads/', self.head_ref)
-    @property
     def current_stack(self):
         return self.get_stack()
     def get_stack(self, name = None):
         if not name:
-            name = self.current_branch
+            name = self.current_branch_name
         if not name in self.__stacks:
             self.__stacks[name] = Stack(self, name)
         return self.__stacks[name]
Acked-by on v1 2/5
#4 lore raw

Re: [StGIT PATCH 2/5] Create a git.Branch class as ancestor of stack.Stack

Quoted 5 lines · Catalin Marinas

This class deals with Git-specific branch commands. The Stack class is a direct child of Branch and some of its functionality was moved to the new class.

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

PATCH 3/5 Add stack creation and initialisation support to lib.Stack

In reply to Catalin Marinas

This patch adds the create and initialise Stack classmethods to handle the initialisation of StGIT patch series on a Git branch.

---

 stgit/lib/stack.py |   63 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 62 insertions(+), 1 deletions(-)
stgit/lib/stack.py +62 -1
index 030c407..9cb3967 100644
@@ -3,6 +3,10 @@
 import os.path
 from stgit import exception, utils
 from stgit.lib import git, stackupgrade
+from stgit.config import config
+
+class StackException(exception.StgException):
+    """Exception raised by L{stack} objects."""
 
 class Patch(object):
     """Represents an StGit patch. This class is mainly concerned with
@@ -105,6 +109,14 @@ class PatchOrder(object):
     all = property(lambda self: self.applied + self.unapplied + self.hidden)
     all_visible = property(lambda self: self.applied + self.unapplied)
 
+    @staticmethod
+    def create(stackdir):
+        """Create the PatchOrder specific files
+        """
+        utils.create_empty_file(os.path.join(stackdir, 'applied'))
+        utils.create_empty_file(os.path.join(stackdir, 'unapplied'))
+        utils.create_empty_file(os.path.join(stackdir, 'hidden'))
+
 class Patches(object):
     """Creates L{Patch} objects. Makes sure there is only one such object
     per patch."""
@@ -140,7 +152,7 @@ class Stack(git.Branch):
         self.__patchorder = PatchOrder(self)
         self.__patches = Patches(self)
         if not stackupgrade.update_to_current_format_version(repository, name):
-            raise exception.StgException('%s: branch not initialized' % name)
+            raise StackException('%s: branch not initialized' % name)
     patchorder = property(lambda self: self.__patchorder)
     patches = property(lambda self: self.__patches)
     @property
@@ -158,6 +170,55 @@ class Stack(git.Branch):
             return True
         return self.head == self.patches.get(self.patchorder.applied[-1]).commit
 
+    def set_parents(self, remote, branch):
+        if remote:
+            self.set_parent_remote(remote)
+        if branch:
+            self.set_parent_branch(branch)
+
+    @classmethod
+    def initialise(cls, repository, name = None):
+        """Initialise a Git branch to handle patch series.
+
+        @param repository: The L{Repository} where the L{Stack} will be created
+        @param name: The name of the L{Stack}
+        """
+        if not name:
+            name = repository.current_branch_name
+        # make sure that the corresponding Git branch exists
+        git.Branch(repository, name)
+
+        dir = os.path.join(repository.directory, cls.__repo_subdir, name)
+        compat_dir = os.path.join(dir, 'patches')
+        if os.path.exists(dir):
+            raise StackException('%s: branch already initialized' % name)
+
+        # create the stack directory and files
+        utils.create_dirs(dir)
+        utils.create_dirs(compat_dir)
+        PatchOrder.create(dir)
+        config.set(stackupgrade.format_version_key(name),
+                   str(stackupgrade.FORMAT_VERSION))
+
+        return repository.get_stack(name)
+
+    @classmethod
+    def create(cls, repository, name,
+               create_at = None, parent_remote = None, parent_branch = None):
+        """Create and initialise a Git branch returning the L{Stack} object.
+
+        @param repository: The L{Repository} where the L{Stack} will be created
+        @param name: The name of the L{Stack}
+        @param create_at: The Git id used as the base for the newly created
+            Git branch
+        @param parent_remote: The name of the remote Git branch
+        @param parent_branch: The name of the parent Git branch
+        """
+        git.Branch.create(repository, name, create_at = create_at)
+        stack = cls.initialise(repository, name)
+        stack.set_parents(parent_remote, parent_branch)
+        return stack
+
 class Repository(git.Repository):
     """A git L{Repository<git.Repository>} with some added StGit-specific
     operations."""
Acked-by on v1 3/5
#6 lore raw

Re: [StGIT PATCH 3/5] Add stack creation and initialisation support to lib.Stack

Quoted 4 lines · Catalin Marinas

This patch adds the create and initialise Stack classmethods to handle the initialisation of StGIT patch series on a Git branch.

I still have some comments, bu the patch looks good as-is.

stgit/lib/stack.py class PatchOrder(object): · line 118

Quoted 7 lines
stgit/lib/stack.py +7 -0
+    @staticmethod
+    def create(stackdir):
+        """Create the PatchOrder specific files
+        """
+        utils.create_empty_file(os.path.join(stackdir, 'applied'))
+        utils.create_empty_file(os.path.join(stackdir, 'unapplied'))
+        utils.create_empty_file(os.path.join(stackdir, 'hidden'))

Maybe s/create/create_files/ or soething to that effect here, as I suggested in another mail a few minutes ago.

stgit/lib/stack.py class Stack(git.Branch): · line 177

Quoted 5 lines
stgit/lib/stack.py +5 -0
+    def set_parents(self, remote, branch):
+        if remote:
+            self.set_parent_remote(remote)
+        if branch:
+            self.set_parent_branch(branch)

Much clearer now. Interesting that the only reason we need this function at all is to make sure that set_parent_remote and set_parent_branch are called in the correct order.

Maybe set_parent_branch should throw an exception instead of silently doing nothing when there is no parent yet? It would force the callers to be more aware of the limitation -- though we might not actually want that. Your call.

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

Re: [StGIT PATCH 3/5] Add stack creation and initialisation support to lib.Stack

Quoted 10 lines · Karl Hasselström

stgit/lib/stack.py class PatchOrder(object): · line 118

Quoted 7 lines · Catalin Marinas
stgit/lib/stack.py +7 -0
+    @staticmethod
+    def create(stackdir):
+        """Create the PatchOrder specific files
+        """
+        utils.create_empty_file(os.path.join(stackdir, 'applied'))
+        utils.create_empty_file(os.path.join(stackdir, 'unapplied'))
+        utils.create_empty_file(os.path.join(stackdir, 'hidden'))
Quoted 2 lines

Maybe s/create/create_files/ or soething to that effect here, as I
suggested in another mail a few minutes ago.

Done, no problem with that.

Quoted 7 lines

stgit/lib/stack.py class Stack(git.Branch): · line 177

Quoted 5 lines
stgit/lib/stack.py +5 -0
+    def set_parents(self, remote, branch):
+        if remote:
+            self.set_parent_remote(remote)
+        if branch:
+            self.set_parent_branch(branch)
Quoted 8 lines

Much clearer now. Interesting that the only reason we need this
function at all is to make sure that set_parent_remote and set_parent_branch are called in the correct order.

Maybe set_parent_branch should throw an exception instead of silently doing nothing when there is no parent yet? It would force the callers to be more aware of the limitation -- though we might not actually want that. Your call.

I can't argue much. I propose to commit the patch as is and modify it afterwards. I haven't touched this code much, it was Yann's implementation and I forgot all the decisions at that time.

Signature · 1 line
Catalin
#8 lore raw

PATCH 4/5 Convert "init" to the new StGIT infrastructure

In reply to Catalin Marinas

---

 stgit/commands/init.py |   14 ++++----------
 1 files changed, 4 insertions(+), 10 deletions(-)
stgit/commands/init.py +4 -10
index 475a4ce..b68acd7 100644
@@ -16,13 +16,8 @@ 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 import stack, git
-
+from stgit.commands import common
+from stgit.lib import stack
 
 help = 'initialise the current branch for use with StGIT'
 usage = """%prog [options]
@@ -31,14 +26,13 @@ Initialise the current GIT branch to be used as an StGIT stack. Note
 that you must already be in a GIT repository and .git/HEAD must point
 to a valid file in refs/heads/."""
 
-directory = DirectoryHasRepository()
+directory = common.DirectoryHasRepositoryLib()
 options = []
 
-
 def func(parser, options, args):
     """Performs the repository initialisation
     """
     if len(args) != 0:
         parser.error('incorrect number of arguments')
 
-    crt_series.init()
+    stack.Stack.initialise(directory.repository)
#10 lore raw

PATCH 5/5 Add test_patches target to Makefile

In reply to Catalin Marinas

This target tests individual patches in the series.

---

 Makefile |    9 ++++++++-
 1 files changed, 8 insertions(+), 1 deletions(-)
Makefile +8 -1
index c890b8e..e5c002a 100644
@@ -2,6 +2,8 @@ PREFIX	?= $(HOME)
 DESTDIR	?= /
 PYTHON	?= python
 
+TEST_PATCHES ?= ..
+
 all:
 	$(PYTHON) setup.py build
 
@@ -14,6 +16,11 @@ doc:
 test:
 	cd t && $(MAKE) all
 
+test_patches:
+	for patch in $$(stg series --noprefix $(TEST_PATCHES)); do \
+		stg goto $$patch && $(MAKE) test || break; \
+	done
+
 clean:
 	for dir in Documentation t; do \
 		(cd $$dir && $(MAKE) clean); \
@@ -26,4 +33,4 @@ clean:
 tags:
 	ctags -e -R stgit/*
 
-.PHONY: all install doc test clean
+.PHONY: all install doc test test_patches clean
#11 lore raw

Re: [StGIT PATCH 5/5] Add test_patches target to Makefile

Quoted 3 lines · Catalin Marinas

This target tests individual patches in the series.

Looks good. It doesn't run the tests with all patches popped, which my invocation did, but I gues we needn't all be paranoid. :-) (And it would be somewhat tricky to do now that you support testing an arbitrary subset of the patches.)

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