Beispiel #1
0
def main(argv):
  assert 2 <= len(argv) < 3, "Must supply (<newname>) or (<oldname> <newname>)"
  if len(argv) == 2:
    old_name = current_branch()
    new_name = argv[1]
  else:
    old_name = argv[1]
    new_name = argv[2]
  assert old_name in branches(), "<oldname> must exist"
  assert new_name not in branches(), "<newname> must not exist"

  run_git('branch', '-m', old_name, new_name)

  matcher = re.compile(r'^branch\.(.*)\.merge$')
  branches_to_fix = []
  for line in run_git('config', '-l').splitlines():
    key, value = line.split('=', 1)
    if value == 'refs/heads/' + old_name:
      m = matcher.match(key)
      if m:
        branch = m.group(1)
        remote = run_git('config', '--get', 'branch.%s.remote' % branch)
        if remote == '.':
          branches_to_fix.append(branch)
  for b in branches_to_fix:
    run_git('config', 'branch.%s.merge' % b, 'refs/heads/' + new_name)
Beispiel #2
0
def bclean():
  merged = list(branches('--merged', 'origin/master'))

  if VERBOSE:
    print merged

  upstreams = {}
  downstreams = collections.defaultdict(list)
  for branch in branches():
    try:
      parent = upstream(branch)
      upstreams[branch] = parent
      downstreams[parent].append(branch)
    except CalledProcessError:
      pass

  if VERBOSE:
    print upstreams
    print downstreams

  if current_branch() in merged:
    run_git('checkout', 'origin/master')
  for branch in merged:
    for down in downstreams[branch]:
      if down not in merged:
        run_git('branch', '--set-upstream-to', upstreams[branch], down)
        print ('Reparented %s to track %s (was tracking %s)'
               % (down, upstreams[branch], branch))
    print run_git('branch', '-d', branch)

  return 0
Beispiel #3
0
def main(argv):
    assert len(argv) == 1, "No arguments expected"
    upfn = upstream
    cur = current_branch()
    if cur == "HEAD":
        upfn = lambda b: git_hash(upstream(b))
        cur = git_hash(cur)
    downstreams = [b for b in branches() if upfn(b) == cur]
    if not downstreams:
        return "No downstream branches"
    elif len(downstreams) == 1:
        run_git("checkout", downstreams[0])
    else:
        high = len(downstreams) - 1
        print
        while True:
            print "Please select a downstream branch"
            for i, b in enumerate(downstreams):
                print "  %d. %s" % (i, b)
            r = raw_input("Selection (0-%d)[0]: " % high).strip() or "0"
            if not r.isdigit() or (0 > int(r) > high):
                print "Invalid choice."
            else:
                run_git("checkout", downstreams[int(r)])
                break
Beispiel #4
0
def main():
  if '--clean' in sys.argv:
    clean_refs()
    return 0

  orig_branch = current_branch()
  if orig_branch == 'HEAD':
    orig_branch = git_hash('HEAD')

  if 'origin' in run_git('remote').splitlines():
    run_git('fetch', 'origin', stderr=None)
  else:
    run_git('svn', 'fetch', stderr=None)
  branch_tree = {}
  for branch in branches():
    parent = upstream(branch)
    if not parent:
      print 'Skipping %s: No upstream specified' % branch
      continue
    branch_tree[branch] = parent

  starting_refs = {}
  for branch, parent in branch_tree.iteritems():
    starting_refs[branch] = get_or_create_merge_base_tag(branch, parent)

  if VERBOSE:
    pprint(branch_tree)
    pprint(starting_refs)

  # XXX There is a more efficient way to do this, but for now...
  while branch_tree:
    this_pass = [i for i in branch_tree.items() if i[1] not in branch_tree]
    for branch, parent in this_pass:
      clean_branch(branch, parent, starting_refs[branch])
      del branch_tree[branch]

  clean_refs()

  bclean()

  if orig_branch in branches():
    run_git('checkout', orig_branch)
  else:
    run_git('checkout', 'origin/master')

  return 0
Beispiel #5
0
def main():
  BRIGHT = '\x1b[1m'
  CYAN = '\x1b[36m'
  GREEN = '\x1b[32m'
  BLUEBAK = '\x1b[44m'
  MAGENTA = '\x1b[35m'
  RED = '\x1b[31m'
  RESET = '\x1b[m'
  current = current_branch()
  all_branches = set(branches())
  if current in all_branches:
    all_branches.remove(current)
  all_tags = set(tags())
  try:
    for line in sys.stdin.xreadlines():
      start = line.find(GREEN+' (')
      end   = line.find(')', start)
      if start != -1 and end != -1:
        start += len(GREEN) + 2
        branch_list = line[start:end].split(', ')
        branches_str = ''
        if branch_list:
          colored_branches = []
          head_marker = ''
          for b in branch_list:
            if b == "HEAD":
              head_marker = BLUEBAK+BRIGHT+'*'
              continue
            if b == current:
              colored_branches.append(CYAN+BRIGHT+b+RESET)
              current = None
            elif b in all_branches:
              colored_branches.append(GREEN+BRIGHT+b+RESET)
              all_branches.remove(b)
            elif b in all_tags:
              colored_branches.append(MAGENTA+BRIGHT+b+RESET)
              all_tags.remove(b)
            else:
              colored_branches.append(RED+b)
            branches_str = '(%s) ' % ((GREEN+", ").join(colored_branches)+GREEN)
          line = "%s%s%s" % (line[:start-1], branches_str, line[end+5:])
          if head_marker:
            line = line.replace('*', head_marker, 1)
      sys.stdout.write(line)
  except:
    pass
  return 0
Beispiel #6
0
def main(argv):
  assert len(argv) == 1, "No arguments expected"
  branch_map = {}
  par_map = collections.defaultdict(list)
  for branch in branches():
    par = upstream(branch)
    branch_map[branch] = par
    par_map[par].append(branch)

  current = current_branch()
  hashes = git_hashes(current, *branch_map.keys())
  current_hash = hashes[0]
  par_hashes = {k: hashes[i+1] for i, k in enumerate(branch_map.iterkeys())}
  while par_map:
    for parent in par_map:
      if parent not in branch_map:
        if parent not in par_hashes:
          par_hashes[parent] = git_hash(parent)
        print_branch(current, current_hash, parent, par_hashes, par_map,
                     branch_map)
        break