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

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

From
CMCatalin Marinas <catalin.marinas@gmail.com>
Date
Jun 4, 2008, 21:13 UTC
Message-ID
<20080604211334.32531.74258.stgit@localhost.localdomain>
In-Reply-To
<20080604210655.32531.82580.stgit@localhost.localdomain>

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.

Signed-off-by: Catalin Marinas <catalin.marinas@gmail.com>
---
 stgit/lib/git.py   |   51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 stgit/lib/stack.py |   25 ++++---------------------
 2 files changed, 55 insertions(+), 21 deletions(-)
diff --git a/stgit/lib/git.py b/stgit/lib/git.py
index fd66f6d..6393af2 100644
--- a/stgit/lib/git.py
+++ b/stgit/lib/git.py
@@ -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,47 @@ 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 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)
+        self._repository.run(['git', 'branch', create_at.sha1]).discard_output()
diff --git a/stgit/lib/stack.py b/stgit/lib/stack.py
index bdd21b1..aca7a36 100644
--- a/stgit/lib/stack.py
+++ b/stgit/lib/stack.py
@@ -130,34 +130,20 @@ 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)."""
     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 +163,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)
-    @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]
Previous: Karl HasselströmNext: Karl Hasselström
Message 11 of 28 in “Various updates to the new infrastructure”
  1. 0/5 Various updates to the new infrastructureCatalin Marinas, Jun 4, 2008
  2. 1/5 Allow stack.patchorder.all to return hidden patchesCatalin Marinas, Jun 4, 2008
  3. Karl HasselströmJun 5, 2008
  4. Catalin MarinasJun 5, 2008
  5. 2/5 Rename Repository.head to Repository.head_refCatalin Marinas, Jun 4, 2008
  6. Karl HasselströmJun 5, 2008
  7. Catalin MarinasJun 5, 2008
  8. Karl HasselströmJun 5, 2008
  9. Catalin MarinasJun 5, 2008
  10. Karl HasselströmJun 5, 2008
  11. 3/5 Create a git.Branch class as ancestor of stack.StackCatalin Marinas, Jun 4, 2008
  12. Karl HasselströmJun 5, 2008
  13. Catalin MarinasJun 5, 2008
  14. Karl HasselströmJun 5, 2008
  15. Catalin MarinasJun 6, 2008
  16. Karl HasselströmJun 7, 2008
  17. Catalin MarinasJun 8, 2008
  18. David AguilarJun 9, 2008
  19. Sverre RabbelierJun 9, 2008
  20. Karl HasselströmJun 9, 2008
  21. 4/5 Add stack creation and initialisation support to lib.StackCatalin Marinas, Jun 4, 2008
  22. Karl HasselströmJun 5, 2008
  23. Catalin MarinasJun 5, 2008
  24. Karl HasselströmJun 7, 2008
  25. 5/5 Add stack creation and deletion support to the new infrastructureCatalin Marinas, Jun 4, 2008
  26. Karl HasselströmJun 5, 2008
  27. Catalin MarinasJun 5, 2008
  28. Karl HasselströmJun 5, 2008

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.