def list_change_branches(): """Lists all temporary change branches. Lists the branches and prompts user with a menu to check one of them out. """ branches = get_change_branches() if not branches: print "You have no change branches to list" return print "Change branches:\n" i = 0 for branch in branches: i += 1 output = git.run_command("git log --oneline -1 %s --" % branch, trap_stdout=True) sys.stdout.write("{0:>2}. {1} {2}".format(i, branch, output)) try: selection = raw_input("\nSelect a branch number to check out, " "or hit enter to exit: ") except (EOFError, KeyboardInterrupt): # User pressed or Ctrl-D or Ctrl-C. return if selection.isdigit() and int(selection) <= len(branches): git.run_command_or_die("git checkout %s" % branches[int(selection) - 1]) elif selection: print "Not a valid selection" else: pass # User hit enter; just exit.
def main(argv): if FLAGS['help-summary'].value: usage(include_flags=False) sys.exit() # Fail gracefully if run outside a git repository. git.run_command_or_die('git status') configure() argc = len(argv) if argc > 2: usage(include_flags=False) sys.exit(1) elif argc == 2: subcommand = argv[1] else: subcommand = 'create' # default subcommand if subcommand == 'create': create_change() elif subcommand == 'update': update_change() elif subcommand == 'rebase': rebase() elif subcommand == 'list': list_change_branches() elif subcommand == 'submit': submit_change() elif subcommand == 'gc': garbage_collect() elif subcommand == 'print': print_push_command() else: exit_error('Unknown subcommand: %s.' % subcommand)
def rebase(): """Rebases the target and temporary change branches. Rebases the target branch (the branch from which the temporary change branch was created) and then rebases the temporary change branch. This can be used to pull upstream changes down to both branches to resolve a failed Gerrit submission due to a path conflict. If there are conflicts with either rebase operation, the process terminates and it is up to the user to resolve the conflicts. """ change_id = check_for_change_branch() change = get_change(change_id) target_branch = change["branch"] change_branch = git.get_branch() git.run_command_or_die("git checkout %s" % target_branch) try: git.run_command("git pull --rebase", output_on_error=False) except git.CalledProcessError, e: print ( "Rebase failed for branch %s. After resolving merge failure(s),\n" 'check out the change branch (%s) and run "git change rebase" again.\n' 'See "git help rebase" for help on resolving merge conflicts.' % (target_branch, change_branch) ) sys.exit(e.returncode)
def rebase(): """Rebases the target and temporary change branches. Rebases the target branch (the branch from which the temporary change branch was created) and then rebases the temporary change branch. This can be used to pull upstream changes down to both branches to resolve a failed Gerrit submission due to a path conflict. If there are conflicts with either rebase operation, the process terminates and it is up to the user to resolve the conflicts. """ check_for_change_branch() target_branch = get_target_branch() change_branch = git.get_current_branch() git.run_command_or_die('git checkout %s' % target_branch) try: git.run_command('git pull --rebase', output_on_error=False) except git.CalledProcessError, e: print ('Rebase failed for branch %s. After resolving merge failure(s),\n' 'check out the change branch (%s) and run "git change rebase" again.\n' 'See "git help rebase" for help on resolving merge conflicts.' % (target_branch, change_branch)) sys.exit(e.returncode)
def main(argv): if FLAGS["help-summary"].value: usage(include_flags=False) sys.exit() # Get remote from command-line flag or config option, otherwise # fall back to flag default. if not FLAGS["remote"].present: remote = git.get_config_option("git-change.remote") if remote is not None: FLAGS.remote = remote # Get Gerrit ssh host from command-line flag or config option, # otherwise exit with an error. gerrit_ssh_host = FLAGS["gerrit-ssh-host"] if not gerrit_ssh_host.present: gerrit_ssh_host.value = git.get_config_option("git-change.gerrit-ssh-host") if gerrit_ssh_host.value is None: exit_error('Please define git config option "git-change.gerrit-ssh-host" ' "or pass --gerrit-ssh-host.") # --merge-commit implies --use-head-commit. if FLAGS["merge-commit"].value: FLAGS["use-head-commit"].value = True # Fail gracefully if run outside a git repository. git.run_command_or_die("git status") argc = len(argv) if argc > 2 and argv[1] == "download": subcommand = argv[1] elif argc > 2 or argc == 2 and argv[1] == "download": usage(include_flags=False) sys.exit(1) elif argc == 2: subcommand = argv[1] else: subcommand = "create" # default subcommand if subcommand == "create": create_change() elif subcommand == "update": update_change() elif subcommand == "rebase": rebase() elif subcommand == "list": list_change_branches() elif subcommand == "submit": submit_change() elif subcommand == "gc": garbage_collect() elif subcommand == "print": print_push_command() elif subcommand == "download": download_review(argv[2]) else: exit_error("Unknown subcommand: %s." % subcommand)
def submit_change(): """Submits the existing change to Gerrit.""" change_id = check_for_change_branch() change = get_change(change_id) if not change['open']: exit_error('Change %s is no longer open.' % change_id) commit = git.run_command('git rev-parse --verify HEAD', trap_stdout=True) project = change['project'] git.run_command_or_die('ssh %s gerrit review --project %s --submit %s' % (FLAGS['gerrit-ssh-host'].value, project, commit))
def submit_change(): """Submits the existing change to Gerrit.""" change_id = check_for_change_branch() change = get_change(change_id) if not change["open"]: exit_error("Change %s is no longer open." % change_id) commit = git.run_command("git rev-parse --verify HEAD", trap_stdout=True) project = change["project"] ssh_host, _, ssh_port = FLAGS["gerrit-ssh-host"].value.partition(":") ssh_port = (ssh_port and "-p %s " % ssh_port) or "" git.run_command_or_die("ssh %s%s gerrit review --project %s --submit %s" % (ssh_port, ssh_host, project, commit))
def list_change_branches(): """Lists all temporary change branches. Lists the branches and prompts user with a menu to check one of them out. """ branches = get_change_branches() if not branches: print 'You have no change branches to list' return not_merged_branches = git.run_command('git branch --no-merged', trap_stdout=True).strip().split('\n') not_merged_branches = [line.strip()[BRANCH_SHORT_LENGTH:] for line in not_merged_branches] print 'Change branches:\n' i = 0 for branch in branches: i += 1 output = git.run_command('git log --oneline -1 %s --' % branch, trap_stdout=True) change_id = output.split(' ')[0] description = ' '.join(output.split(' ')[1:]) short_branch = branch[0:16] change_branch = branch.split('-')[1] # handle colors here use_color = git.get_config_option('git-change.color') use_color = (use_color != 'false') # auto or yes or anything else count as True cid_url = git.get_config_option('git-change.cid-url') or '' if use_color and change_branch not in not_merged_branches: # not not == is merged sys.stdout.write(COLOR_OBSOLETE) if use_color and change_branch == get_change_id_from_branch(): sys.stdout.write(COLOR_CURRENT) sys.stdout.write('{i:>2}: {branch_id} {href}{cid} {name}'.format( i=i, branch_id=short_branch, href=cid_url, cid=change_id, name=description)) if use_color: sys.stdout.write(COLOR_CLEAR) try: selection = raw_input('\nSelect a branch number to check out, ' 'or hit enter to exit: ') except (EOFError, KeyboardInterrupt): # User pressed or Ctrl-D or Ctrl-C. return if selection.isdigit() and int(selection) <= len(branches): git.run_command_or_die('git checkout %s' % branches[int(selection) - 1]) elif selection: print 'Not a valid selection' else: pass # User hit enter; just exit.
# working tree and index are clean and thus 'git reset --hard' # is safe to run. git.run_command("git checkout %s" % original_branch) git.run_command("git reset --hard HEAD^") print "Removed HEAD commit from branch %s" % original_branch if FLAGS.switch or FLAGS.chain: git.run_command("git checkout %s" % new_branch) return # Switch back to the original branch, but not if --chain is true # as the user may be want to make multiple commits in the # temporary change branch. if FLAGS.switch or FLAGS.chain: pass # switch to (stay on) temporary change branch else: git.run_command_or_die("git checkout %s" % original_branch) def rebase(): """Rebases the target and temporary change branches. Rebases the target branch (the branch from which the temporary change branch was created) and then rebases the temporary change branch. This can be used to pull upstream changes down to both branches to resolve a failed Gerrit submission due to a path conflict. If there are conflicts with either rebase operation, the process terminates and it is up to the user to resolve the conflicts. """ change_id = check_for_change_branch()
# working tree and index are clean and thus 'git reset --hard' # is safe to run. git.run_command('git checkout %s' % original_branch) git.run_command('git reset --hard HEAD^') print 'Removed HEAD commit from branch %s' % original_branch if FLAGS.switch or FLAGS.chain: git.run_command('git checkout %s' % new_branch) return # Switch back to the original branch, but not if --chain is true # as the user may be want to make multiple commits in the # temporary change branch. if FLAGS.switch or FLAGS.chain: pass # switch to (stay on) temporary change branch else: git.run_command_or_die('git checkout %s' % original_branch) def rebase(): """Rebases the target and temporary change branches. Rebases the target branch (the branch from which the temporary change branch was created) and then rebases the temporary change branch. This can be used to pull upstream changes down to both branches to resolve a failed Gerrit submission due to a path conflict. If there are conflicts with either rebase operation, the process terminates and it is up to the user to resolve the conflicts. """ check_for_change_branch()