# Path: git_common.py
# def upstream(branch):
#   try:
#     return run('rev-parse', '--abbrev-ref', '--symbolic-full-name',
#                branch+'@{upstream}')
#   except subprocess2.CalledProcessError:
#     return None
# 
# def current_branch():
#   try:
#     return run('rev-parse', '--abbrev-ref', 'HEAD')
#   except subprocess2.CalledProcessError:
#     return None
# 
# def run(*cmd, **kwargs):
#   """The same as run_with_stderr, except it only returns stdout."""
#   return run_with_stderr(*cmd, **kwargs)[0]
# 
# def tags(*args):
#   return run('tag', *args).splitlines()
# 
# def set_branch_config(branch, option, value, scope='local'):
#   set_config('branch.%s.%s' % (branch, option), value, scope=scope)
# 
# Path: git_common.py
# def get_or_create_merge_base(branch, parent=None):
#   """Finds the configured merge base for branch.
# 
#   If parent is supplied, it's used instead of calling upstream(branch).
#   """
#   base = branch_config(branch, 'base')
#   base_upstream = branch_config(branch, 'base-upstream')
#   parent = parent or upstream(branch)
#   if not parent:
#     return None
#   actual_merge_base = run('merge-base', parent, branch)
# 
#   if base_upstream != parent:
#     base = None
#     base_upstream = None
# 
#   def is_ancestor(a, b):
#     return run_with_retcode('merge-base', '--is-ancestor', a, b) == 0
# 
#   if base:
#     if not is_ancestor(base, branch):
#       logging.debug('Found WRONG pre-set merge-base for %s: %s', branch, base)
#       base = None
#     elif is_ancestor(base, actual_merge_base):
#       logging.debug('Found OLD pre-set merge-base for %s: %s', branch, base)
#       base = None
#     else:
#       logging.debug('Found pre-set merge-base for %s: %s', branch, base)
# 
#   if not base:
#     base = actual_merge_base
#     manual_merge_base(branch, base, parent)
# 
#   return base
# 
# def root():
#   return config('depot-tools.upstream', 'origin/master')
# 
# def manual_merge_base(branch, base, parent):
#   set_branch_config(branch, 'base', base)
#   set_branch_config(branch, 'base-upstream', parent)


import argparse
import sys
import subprocess2
import git_rebase_update
from git_common import upstream, current_branch, run, tags, set_branch_config
from git_common import get_or_create_merge_base, root, manual_merge_base
    new_parent = opts.new_parent
  cur_parent = upstream(branch)

  if branch == 'HEAD' or not branch:
    parser.error('Must be on the branch you want to reparent')
  if new_parent == cur_parent:
    parser.error('Cannot reparent a branch to its existing parent')

  mbase = get_or_create_merge_base(branch, cur_parent)

  all_tags = tags()
  if cur_parent in all_tags:
    cur_parent += ' [tag]'

  try:
    run('show-ref', new_parent)
  except subprocess2.CalledProcessError:
    print >> sys.stderr, 'fatal: invalid reference: %s' % new_parent
    return 1

  if new_parent in all_tags:
    print ("Reparenting %s to track %s [tag] (was %s)"
           % (branch, new_parent, cur_parent))
    set_branch_config(branch, 'remote', '.')
    set_branch_config(branch, 'merge', new_parent)
  else:
    print ("Reparenting %s to track %s (was %s)"
           % (branch, new_parent, cur_parent))
    run('branch', '--set-upstream-to', new_parent, branch)


# Path: git_common.py
# def upstream(branch):
#   try:
#     return run('rev-parse', '--abbrev-ref', '--symbolic-full-name',
#                branch+'@{upstream}')
#   except subprocess2.CalledProcessError:
#     return None
# 
# def current_branch():
#   try:
#     return run('rev-parse', '--abbrev-ref', 'HEAD')
#   except subprocess2.CalledProcessError:
#     return None
# 
# def run(*cmd, **kwargs):
#   """The same as run_with_stderr, except it only returns stdout."""
#   return run_with_stderr(*cmd, **kwargs)[0]
# 
# def tags(*args):
#   return run('tag', *args).splitlines()
# 
# def set_branch_config(branch, option, value, scope='local'):
#   set_config('branch.%s.%s' % (branch, option), value, scope=scope)
# 
# Path: git_common.py
# def get_or_create_merge_base(branch, parent=None):
#   """Finds the configured merge base for branch.
# 
#   If parent is supplied, it's used instead of calling upstream(branch).
#   """
#   base = branch_config(branch, 'base')
#   base_upstream = branch_config(branch, 'base-upstream')
#   parent = parent or upstream(branch)
#   if not parent:
#     return None
#   actual_merge_base = run('merge-base', parent, branch)
# 
#   if base_upstream != parent:
#     base = None
#     base_upstream = None
# 
#   def is_ancestor(a, b):
#     return run_with_retcode('merge-base', '--is-ancestor', a, b) == 0
# 
#   if base:
#     if not is_ancestor(base, branch):
#       logging.debug('Found WRONG pre-set merge-base for %s: %s', branch, base)
#       base = None
#     elif is_ancestor(base, actual_merge_base):
#       logging.debug('Found OLD pre-set merge-base for %s: %s', branch, base)
#       base = None
#     else:
#       logging.debug('Found pre-set merge-base for %s: %s', branch, base)
# 
#   if not base:
#     base = actual_merge_base
#     manual_merge_base(branch, base, parent)
# 
#   return base
# 
# def root():
#   return config('depot-tools.upstream', 'origin/master')
# 
# def manual_merge_base(branch, base, parent):
#   set_branch_config(branch, 'base', base)
#   set_branch_config(branch, 'base-upstream', parent)


# Path: git_reparent_branch.py
import argparse
import sys
import subprocess2
import git_rebase_update
from git_common import upstream, current_branch, run, tags, set_branch_config
from git_common import get_or_create_merge_base, root, manual_merge_base

    new_parent = opts.new_parent
  cur_parent = upstream(branch)

  if branch == 'HEAD' or not branch:
    parser.error('Must be on the branch you want to reparent')
  if new_parent == cur_parent:
    parser.error('Cannot reparent a branch to its existing parent')

  mbase = get_or_create_merge_base(branch, cur_parent)

  all_tags = tags()
  if cur_parent in all_tags:
    cur_parent += ' [tag]'

  try:
    run('show-ref', new_parent)
  except subprocess2.CalledProcessError:
    print >> sys.stderr, 'fatal: invalid reference: %s' % new_parent
    return 1

  if new_parent in all_tags:
    print ("Reparenting %s to track %s [tag] (was %s)"
           % (branch, new_parent, cur_parent))
    set_branch_config(branch, 'remote', '.')
    set_branch_config(branch, 'merge', new_parent)
  else:
    print ("Reparenting %s to track %s (was %s)"
           % (branch, new_parent, cur_parent))
    run('branch', '--set-upstream-to', new_parent, branch)


  manual_merge_base(branch, mbase, new_parent)