From: Catalin Marinas <catalin.marinas@gmail.com>
To: git@vger.kernel.org
Cc: kha@treskal.com
Subject: [StGIT PATCH 3/5] Create a git.Branch class as ancestor of stack.Stack
Date: Wed, 04 Jun 2008 22:13:35 +0100 [thread overview]
Message-ID: <20080604211334.32531.74258.stgit@localhost.localdomain> (raw)
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]
next prev parent reply other threads:[~2008-06-04 21:29 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-06-04 21:13 [StGIT PATCH 0/5] Various updates to the new infrastructure Catalin Marinas
2008-06-04 21:13 ` [StGIT PATCH 1/5] Allow stack.patchorder.all to return hidden patches Catalin Marinas
2008-06-05 6:41 ` Karl Hasselström
2008-06-05 11:46 ` Catalin Marinas
2008-06-04 21:13 ` [StGIT PATCH 2/5] Rename Repository.head to Repository.head_ref Catalin Marinas
2008-06-05 6:46 ` Karl Hasselström
2008-06-05 11:49 ` Catalin Marinas
2008-06-05 11:58 ` Karl Hasselström
2008-06-05 12:06 ` Catalin Marinas
2008-06-05 12:46 ` Karl Hasselström
2008-06-04 21:13 ` Catalin Marinas [this message]
2008-06-05 7:01 ` [StGIT PATCH 3/5] Create a git.Branch class as ancestor of stack.Stack Karl Hasselström
2008-06-05 12:03 ` Catalin Marinas
2008-06-05 13:04 ` Karl Hasselström
2008-06-06 8:44 ` Catalin Marinas
2008-06-07 9:06 ` Karl Hasselström
2008-06-08 22:16 ` Catalin Marinas
2008-06-09 0:07 ` David Aguilar
2008-06-09 0:46 ` Sverre Rabbelier
2008-06-09 7:07 ` Karl Hasselström
2008-06-04 21:13 ` [StGIT PATCH 4/5] Add stack creation and initialisation support to lib.Stack Catalin Marinas
2008-06-05 7:28 ` Karl Hasselström
2008-06-05 12:42 ` Catalin Marinas
2008-06-07 8:59 ` Karl Hasselström
2008-06-04 21:13 ` [StGIT PATCH 5/5] Add stack creation and deletion support to the new infrastructure Catalin Marinas
2008-06-05 7:34 ` Karl Hasselström
2008-06-05 9:43 ` Catalin Marinas
2008-06-05 7:38 ` [StGIT PATCH 0/5] Various updates " 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=20080604211334.32531.74258.stgit@localhost.localdomain \
--to=catalin.marinas@gmail.com \
--cc=git@vger.kernel.org \
--cc=kha@treskal.com \
/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).