clone.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # vim: set fileencoding=utf-8 :
  2. #
  3. # (C) 2009, 2010, 2015 Guido Guenther <agx@sigxcpu.org>
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, please see
  16. # <http://www.gnu.org/licenses/>
  17. #
  18. # inspired by dom-git-checkout
  19. #
  20. """Clone a Git repository and set it up for gbp"""
  21. from six.moves import configparser
  22. import sys
  23. import os, os.path
  24. from gbp.config import (GbpOptionParser, GbpOptionGroup)
  25. from gbp.deb.git import DebianGitRepository
  26. from gbp.git import (GitRepository, GitRepositoryError)
  27. from gbp.errors import GbpError
  28. import gbp.log
  29. def build_parser(name):
  30. try:
  31. parser = GbpOptionParser(command=os.path.basename(name), prefix='',
  32. usage='%prog [options] repository - clone a remote repository')
  33. except configparser.ParsingError as err:
  34. gbp.log.err(err)
  35. return None
  36. branch_group = GbpOptionGroup(parser, "branch options", "branch tracking and layout options")
  37. parser.add_option_group(branch_group)
  38. branch_group.add_option("--all", action="store_true", dest="all", default=False,
  39. help="track all branches, not only debian and upstream")
  40. branch_group.add_config_file_option(option_name="upstream-branch", dest="upstream_branch")
  41. branch_group.add_config_file_option(option_name="debian-branch", dest="debian_branch")
  42. branch_group.add_boolean_config_file_option(option_name="pristine-tar", dest="pristine_tar")
  43. branch_group.add_option("--depth", action="store", dest="depth", default=0,
  44. help="git history depth (for creating shallow clones)")
  45. branch_group.add_option("--reference", action="store", dest="reference", default=None,
  46. help="git reference repository (use local copies where possible)")
  47. parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False,
  48. help="verbose command execution")
  49. parser.add_config_file_option(option_name="color", dest="color", type='tristate')
  50. parser.add_config_file_option(option_name="color-scheme",
  51. dest="color_scheme")
  52. return parser
  53. def parse_args (argv):
  54. parser = build_parser(argv[0])
  55. if not parser:
  56. return None, None
  57. (options, args) = parser.parse_args(argv)
  58. gbp.log.setup(options.color, options.verbose, options.color_scheme)
  59. return (options, args)
  60. def main(argv):
  61. retval = 0
  62. (options, args) = parse_args(argv)
  63. if not options:
  64. return 1
  65. if len(args) < 2:
  66. gbp.log.err("Need a repository to clone.")
  67. return 1
  68. else:
  69. source = args[1]
  70. clone_to, auto_name = (os.path.curdir, True) if len(args) < 3 else (args[2], False)
  71. try:
  72. GitRepository(clone_to)
  73. gbp.log.err("Can't run inside a git repository.")
  74. return 1
  75. except GitRepositoryError:
  76. pass
  77. try:
  78. repo = DebianGitRepository.clone(clone_to, source, options.depth,
  79. auto_name=auto_name,reference=options.reference)
  80. os.chdir(repo.path)
  81. # Reparse the config files of the cloned repository so we pick up the
  82. # branch information from there:
  83. (options, args) = parse_args(argv)
  84. # Track all branches:
  85. if options.all:
  86. remotes = repo.get_remote_branches()
  87. for remote in remotes:
  88. local = remote.replace("origin/", "", 1)
  89. if not repo.has_branch(local) and \
  90. local != "HEAD":
  91. repo.create_branch(local, remote)
  92. else: # only track gbp's default branches
  93. branches = [ options.debian_branch, options.upstream_branch ]
  94. if options.pristine_tar:
  95. branches += [ repo.pristine_tar_branch ]
  96. gbp.log.debug('Will track branches: %s' % branches)
  97. for branch in branches:
  98. remote = 'origin/%s' % branch
  99. if repo.has_branch(remote, remote=True) and \
  100. not repo.has_branch(branch):
  101. repo.create_branch(branch, remote)
  102. repo.set_branch(options.debian_branch)
  103. except GitRepositoryError as err:
  104. gbp.log.err("Git command failed: %s" % err)
  105. retval = 1
  106. except GbpError as err:
  107. if str(err):
  108. gbp.log.err(err)
  109. retval = 1
  110. return retval
  111. if __name__ == '__main__':
  112. sys.exit(main(sys.argv))
  113. # vim:et:ts=4:sw=4:et:sts=4:ai:set list listchars=tab\:»·,trail\:·: