summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Keating <jkeating@redhat.com>2010-07-23 18:07:28 -0700
committerJesse Keating <jkeating@redhat.com>2010-07-23 18:07:28 -0700
commit26b2ed80839c521a6b79fafda008220918ac05b5 (patch)
tree7e35084d5d26b4141454643787b3ea84e407fa76
parent35eb92bea31c3cd8ce26d3e94b6d784ae5faa53f (diff)
downloadfedora-packager-26b2ed80839c521a6b79fafda008220918ac05b5.tar.gz
fedora-packager-26b2ed80839c521a6b79fafda008220918ac05b5.tar.xz
fedora-packager-26b2ed80839c521a6b79fafda008220918ac05b5.zip
Rework how we deal with branches
Create a new list_branches function to discover the local and remote branches. Allow creating a new branch if the requested local branch doesn't already exist.
-rwxr-xr-xsrc/fedpkg.py20
-rw-r--r--src/pyfedpkg/__init__.py73
2 files changed, 66 insertions, 27 deletions
diff --git a/src/fedpkg.py b/src/fedpkg.py
index bf3c517..f9db3c1 100755
--- a/src/fedpkg.py
+++ b/src/fedpkg.py
@@ -586,16 +586,25 @@ def srpm(args):
sys.exit(1)
def switch_branch(args):
+ try:
+ mymodule = pyfedpkg.PackageModule(args.path)
+ except pyfedpkg.FedpkgError, e:
+ log.error('Could not init the module')
+ sys.exit(1)
if args.branch:
try:
- pyfedpkg.switch_branch(args.branch)
+ mymodule.switch_branch(args.branch)
except pyfedpkg.FedpkgError, e:
- log.debug('Unable to switch to another branch: %s' % e)
+ log.error('Unable to switch to another branch: %s' % e)
+ sys.exit(1)
else:
try:
- pyfedpkg.switch_branch(list=1)
+ (locals, remotes) = mymodule.list_branches()
except pyfedpkg.FedpkgError, e:
- log.debug('Unable to list branches: %s' % e)
+ log.error('Unable to list branches: %s' % e)
+ sys.exit(1)
+ print('Locals:\n %s\nRemotes:\n %s' %
+ ('\n '.join(locals), '\n '.join(remotes)))
def tagrequest(args):
# not implimented
@@ -874,7 +883,8 @@ packages will be built sequentially.
# switch branches
parser_switchbranch = subparsers.add_parser('switch-branch',
help = 'Work with branches')
- parser_switchbranch.add_argument('branch', nargs = '?')
+ parser_switchbranch.add_argument('branch', nargs = '?',
+ help = 'Switch to or create branch')
parser_switchbranch.add_argument('-l', '--list',
help = 'List both remote-tracking branches and local branches',
action = 'store_true')
diff --git a/src/pyfedpkg/__init__.py b/src/pyfedpkg/__init__.py
index 1a48b81..07b3d60 100644
--- a/src/pyfedpkg/__init__.py
+++ b/src/pyfedpkg/__init__.py
@@ -253,28 +253,6 @@ def clean(dry=False, useignore=True):
log.error(error)
return proc.returncode
-def switch_branch(branch=None, list=None):
- """Work with different branches.
-
- branch is the name of the branch to switch to
-
- Logs output and returns nothing.
- """
- if list:
- cmd = ['git', 'branch']
- log.info('Listing branches')
- cmd.extend(['-a'])
- _run_command(cmd)
- else:
- cmd = ['git', 'checkout']
- cmd.extend([branch])
- log.debug('Switching to branch %s' % branch)
- try:
- _run_command(cmd)
- except FedpkgError:
- sys.exit(1)
- return
-
def clone(module, user, path=os.getcwd(), branch=None, bare_dir=None):
"""Clone a repo, optionally check out a specific branch.
@@ -1107,6 +1085,26 @@ class PackageModule:
_run_command(cmd, shell=True)
return
+ def list_branches(self):
+ """Returns a tuple of local and remote branch names"""
+
+ log.debug('Listing refs')
+ refs = self.repo.refs
+ # Sort into local and remote branches
+ remotes = []
+ locals = []
+ for ref in refs:
+ if type(ref) == git.Head:
+ log.debug('Found local branch %s' % ref.name)
+ locals.append(ref.name)
+ elif type(ref) == git.RemoteReference:
+ if ref.name == 'origin/HEAD':
+ log.debug('Skipping remote branch alias origin/HEAD')
+ continue # Not useful in this context
+ log.debug('Found remote branch %s' % ref.name)
+ remotes.append(ref.name)
+ return (locals, remotes)
+
def local(self, arch=None, hashtype='sha256'):
"""rpmbuild locally for given arch.
@@ -1331,6 +1329,37 @@ class PackageModule:
_run_command(cmd, shell=True)
return
+ def switch_branch(self, branch):
+ """Switch the working branch
+
+ Will create a local branch if one doesn't already exist,
+ based on origin/<branch>/master
+
+ Logs output and returns nothing.
+ """
+
+ # Get our list of branches
+ (locals, remotes) = self.list_branches()
+
+ if not branch in locals:
+ # We need to create a branch
+ log.debug('No local branch found, creating a new one')
+ if not 'origin/%s/master' % branch in remotes:
+ raise FedpkgError('Unknown remote branch %s' % branch)
+ try:
+ log.info(self.repo.git.checkout('-b', branch, '--track',
+ 'origin/%s/master' % branch))
+ except: # this needs to be finer grained I think...
+ raise FedpkgError('Could not create branch %s' % branch)
+ else:
+ try:
+ output = self.repo.git.checkout(branch)
+ # The above shoudl have no output, but stash it anyway
+ log.info("Switched to branch '%s'" % branch)
+ except: # This needs to be finer grained I think...
+ raise FedpkgError('Could not check out %s' % branch)
+ return
+
def unused_patches(self):
"""Discover patches checked into source control that are not used