From 26b2ed80839c521a6b79fafda008220918ac05b5 Mon Sep 17 00:00:00 2001 From: Jesse Keating Date: Fri, 23 Jul 2010 18:07:28 -0700 Subject: 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. --- src/fedpkg.py | 20 +++++++++---- src/pyfedpkg/__init__.py | 73 +++++++++++++++++++++++++++++++++--------------- 2 files changed, 66 insertions(+), 27 deletions(-) (limited to 'src') 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//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 -- cgit