# 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
#!/usr/bin/env python
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Change the upstream of the current branch."""





def main(args):
  root_ref = root()

  parser = argparse.ArgumentParser()
  g = parser.add_mutually_exclusive_group()
  g.add_argument('new_parent', nargs='?',
                 help='New parent branch (or tag) to reparent to.')
  g.add_argument('--root', action='store_true',
                 help='Reparent to the configured root branch (%s).' % root_ref)
  g.add_argument('--lkgr', action='store_true',
                 help='Reparent to the lkgr tag.')
  opts = parser.parse_args(args)

  # TODO(iannucci): Allow specification of the branch-to-reparent


# 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

#!/usr/bin/env python
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Change the upstream of the current branch."""





def main(args):
  root_ref = root()

  parser = argparse.ArgumentParser()
  g = parser.add_mutually_exclusive_group()
  g.add_argument('new_parent', nargs='?',
                 help='New parent branch (or tag) to reparent to.')
  g.add_argument('--root', action='store_true',
                 help='Reparent to the configured root branch (%s).' % root_ref)
  g.add_argument('--lkgr', action='store_true',
                 help='Reparent to the lkgr tag.')
  opts = parser.parse_args(args)

  # TODO(iannucci): Allow specification of the branch-to-reparent


  branch = current_branch()