create_remote_repo.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. # vim: set fileencoding=utf-8 :
  2. #
  3. # (C) 2010, 2012, 2015 Guido Günther <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. # Based on the aa-create-git-repo and dom-new-git-repo shell scripts
  19. """Create a remote Git repository based on the current one"""
  20. from __future__ import print_function
  21. from six.moves import configparser
  22. import sys
  23. import os, os.path
  24. from six.moves import urllib
  25. import subprocess
  26. import tty, termios
  27. import re
  28. import six
  29. from gbp.deb.changelog import ChangeLog, NoChangeLogError
  30. from gbp.command_wrappers import (CommandExecFailed, GitCommand)
  31. from gbp.config import (GbpOptionParserDebian, GbpOptionGroup)
  32. from gbp.errors import GbpError
  33. from gbp.git import GitRepositoryError
  34. from gbp.deb.git import DebianGitRepository
  35. import gbp.log
  36. def print_config(remote, branches):
  37. """
  38. Print out the git config to push to the newly created repo.
  39. >>> print_config({'name': 'name', 'url': 'url'}, ['foo', 'bar'])
  40. [remote "name"]
  41. url = url
  42. fetch = +refs/heads/*:refs/remotes/name/*
  43. push = foo
  44. push = bar
  45. [branch "foo"]
  46. remote = name
  47. merge = refs/heads/foo
  48. [branch "bar"]
  49. remote = name
  50. merge = refs/heads/bar
  51. """
  52. print("""[remote "%(name)s"]
  53. url = %(url)s
  54. fetch = +refs/heads/*:refs/remotes/%(name)s/*""" % remote)
  55. for branch in branches:
  56. print(" push = %s" % branch)
  57. for branch in branches:
  58. print("""[branch "%s"]
  59. remote = %s
  60. merge = refs/heads/%s""" % (branch, remote['name'], branch))
  61. def sort_dict(d):
  62. """Return a sorted list of (key, value) tuples"""
  63. s = []
  64. for key in sorted(six.iterkeys(d)):
  65. s.append((key, d[key]))
  66. return s
  67. def parse_url(remote_url, name, pkg, template_dir=None):
  68. """
  69. Sanity check our remote URL
  70. >>> sort_dict(parse_url("ssh://host/path/%(pkg)s", "origin", "package"))
  71. [('base', ''), ('dir', '/path/package'), ('host', 'host'), ('name', 'origin'), ('pkg', 'package'), ('port', None), ('scheme', 'ssh'), ('template-dir', None), ('url', 'ssh://host/path/package')]
  72. >>> sort_dict(parse_url("ssh://host:22/path/repo.git", "origin", "package"))
  73. [('base', ''), ('dir', '/path/repo.git'), ('host', 'host'), ('name', 'origin'), ('pkg', 'package'), ('port', '22'), ('scheme', 'ssh'), ('template-dir', None), ('url', 'ssh://host:22/path/repo.git')]
  74. >>> sort_dict(parse_url("ssh://host:22/~/path/%(pkg)s.git", "origin", "package"))
  75. [('base', '~/'), ('dir', 'path/package.git'), ('host', 'host'), ('name', 'origin'), ('pkg', 'package'), ('port', '22'), ('scheme', 'ssh'), ('template-dir', None), ('url', 'ssh://host:22/~/path/package.git')]
  76. >>> sort_dict(parse_url("ssh://host:22/~user/path/%(pkg)s.git", "origin", "package", "/doesnot/exist"))
  77. [('base', '~user/'), ('dir', 'path/package.git'), ('host', 'host'), ('name', 'origin'), ('pkg', 'package'), ('port', '22'), ('scheme', 'ssh'), ('template-dir', '/doesnot/exist'), ('url', 'ssh://host:22/~user/path/package.git')]
  78. >>> parse_url("git://host/repo.git", "origin", "package")
  79. Traceback (most recent call last):
  80. ...
  81. GbpError: URL must use ssh protocol.
  82. >>> parse_url("ssh://host/path/repo", "origin", "package")
  83. Traceback (most recent call last):
  84. ...
  85. GbpError: URL needs to contain either a repository name or '%(pkg)s'
  86. >>> parse_url("ssh://host:asdf/path/%(pkg)s.git", "origin", "package")
  87. Traceback (most recent call last):
  88. ...
  89. GbpError: URL contains invalid port.
  90. >>> parse_url("ssh://host/~us er/path/%(pkg)s.git", "origin", "package")
  91. Traceback (most recent call last):
  92. ...
  93. GbpError: URL contains invalid ~username expansion.
  94. """
  95. frags = urllib.parse.urlparse(remote_url)
  96. if frags.scheme in ['ssh', 'git+ssh', '']:
  97. scheme = frags.scheme
  98. else:
  99. raise GbpError("URL must use ssh protocol.")
  100. if not '%(pkg)s' in remote_url and not remote_url.endswith(".git"):
  101. raise GbpError("URL needs to contain either a repository name or '%(pkg)s'")
  102. if ":" in frags.netloc:
  103. (host, port) = frags.netloc.split(":", 1)
  104. if not re.match(r"^[0-9]+$", port):
  105. raise GbpError("URL contains invalid port.")
  106. else:
  107. host = frags.netloc
  108. port = None
  109. if frags.path.startswith("/~"):
  110. m = re.match(r"/(~[a-zA-Z0-9_-]*/)(.*)", frags.path)
  111. if not m:
  112. raise GbpError("URL contains invalid ~username expansion.")
  113. base = m.group(1)
  114. path = m.group(2)
  115. else:
  116. base = ""
  117. path = frags.path
  118. remote = { 'pkg' : pkg,
  119. 'url' : remote_url % { 'pkg': pkg },
  120. 'dir' : path % { 'pkg': pkg },
  121. 'base': base,
  122. 'host': host,
  123. 'port': port,
  124. 'name': name,
  125. 'scheme': scheme,
  126. 'template-dir': template_dir}
  127. return remote
  128. def build_remote_script(remote, branch):
  129. """
  130. Create the script that will be run on the remote side
  131. >>> build_remote_script({'base': 'base', 'dir': 'dir', 'pkg': 'pkg', 'template-dir': None}, 'branch')
  132. '\\nset -e\\numask 002\\nif [ -d base"dir" ]; then\\n echo "Repository at "basedir" already exists - giving up."\\n exit 1\\nfi\\nmkdir -p base"dir"\\ncd base"dir"\\ngit init --bare --shared\\necho "pkg packaging" > description\\necho "ref: refs/heads/branch" > HEAD\\n'
  133. >>> build_remote_script({'base': 'base', 'dir': 'dir', 'pkg': 'pkg', 'template-dir': '/doesnot/exist'}, 'branch')
  134. '\\nset -e\\numask 002\\nif [ -d base"dir" ]; then\\n echo "Repository at "basedir" already exists - giving up."\\n exit 1\\nfi\\nmkdir -p base"dir"\\ncd base"dir"\\ngit init --bare --shared --template=/doesnot/exist\\necho "pkg packaging" > description\\necho "ref: refs/heads/branch" > HEAD\\n'
  135. """
  136. args = remote
  137. args['branch'] = branch
  138. args['git-init-args'] = '--bare --shared'
  139. if args['template-dir']:
  140. args['git-init-args'] += (' --template=%s'
  141. % args['template-dir'])
  142. remote_script_pattern = ['',
  143. 'set -e',
  144. 'umask 002',
  145. 'if [ -d %(base)s"%(dir)s" ]; then',
  146. ' echo "Repository at \"%(base)s%(dir)s\" already exists - giving up."',
  147. ' exit 1',
  148. 'fi',
  149. 'mkdir -p %(base)s"%(dir)s"',
  150. 'cd %(base)s"%(dir)s"',
  151. 'git init %(git-init-args)s',
  152. 'echo "%(pkg)s packaging" > description',
  153. 'echo "ref: refs/heads/%(branch)s" > HEAD',
  154. '' ]
  155. remote_script = '\n'.join(remote_script_pattern) % args
  156. return remote_script
  157. def build_cmd(remote):
  158. """
  159. Build the command we pass the script to
  160. >>> build_cmd({'scheme': ''})
  161. ['sh']
  162. >>> build_cmd({'scheme': 'ssh', 'host': 'host', 'port': 80})
  163. ['ssh', '-p', 80, 'host', 'sh']
  164. """
  165. cmd = []
  166. if remote["scheme"]:
  167. cmd.append('ssh')
  168. if remote["port"]:
  169. cmd.extend(['-p', remote['port']])
  170. cmd.append(remote["host"])
  171. cmd.append('sh')
  172. return cmd
  173. def read_yn():
  174. fd = sys.stdin.fileno()
  175. try:
  176. old_settings = termios.tcgetattr(fd)
  177. except termios.error:
  178. old_settings = None
  179. try:
  180. if old_settings:
  181. tty.setraw(sys.stdin.fileno())
  182. ch = sys.stdin.read(1)
  183. finally:
  184. if old_settings:
  185. termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
  186. if ch in ( 'y', 'Y' ):
  187. return True
  188. else:
  189. return False
  190. def setup_branch_tracking(repo, remote, branches):
  191. repo.add_remote_repo(name=remote['name'], url=remote['url'], fetch=True)
  192. gitTrackRemote = GitCommand('branch', ['--set-upstream-to'])
  193. for branch in branches:
  194. gitTrackRemote(['%s/%s' % (remote['name'], branch), branch])
  195. def push_branches(remote, branches):
  196. gitPush = GitCommand('push')
  197. gitPush([remote['url']] + branches)
  198. gitPush([remote['url'], '--tags'])
  199. def build_parser(name, sections=[]):
  200. try:
  201. parser = GbpOptionParserDebian(command=os.path.basename(name), prefix='',
  202. usage='%prog [options] - '
  203. 'create a remote repository',
  204. sections=sections)
  205. except configparser.ParsingError as err:
  206. gbp.log.err(err)
  207. return None
  208. branch_group = GbpOptionGroup(parser,
  209. "branch options",
  210. "branch layout and tracking options")
  211. branch_group.add_config_file_option(option_name="remote-url-pattern",
  212. dest="remote_url")
  213. parser.add_option_group(branch_group)
  214. branch_group.add_config_file_option(option_name="upstream-branch",
  215. dest="upstream_branch")
  216. branch_group.add_config_file_option(option_name="debian-branch",
  217. dest="debian_branch")
  218. branch_group.add_boolean_config_file_option(option_name="pristine-tar",
  219. dest="pristine_tar")
  220. branch_group.add_boolean_config_file_option(option_name="track",
  221. dest='track')
  222. parser.add_option("-v", "--verbose",
  223. action="store_true",
  224. dest="verbose",
  225. default=False,
  226. help="verbose command execution")
  227. parser.add_config_file_option(option_name="color",
  228. dest="color",
  229. type='tristate')
  230. parser.add_config_file_option(option_name="color-scheme",
  231. dest="color_scheme")
  232. parser.add_option("--remote-name",
  233. dest="name",
  234. default="origin",
  235. help="The name of the remote, default is 'origin'")
  236. parser.add_config_file_option(option_name="template-dir",
  237. dest="template_dir")
  238. parser.add_config_file_option(option_name="remote-config",
  239. dest="remote_config")
  240. return parser
  241. def parse_args(argv, sections=[]):
  242. """
  243. Parse the command line arguments and config files.
  244. @param argv: the command line arguments
  245. @type argv: C{list} of C{str}
  246. @param sections: additional sections to add to the config file parser
  247. besides the command name
  248. @type sections: C{list} of C{str}
  249. """
  250. # We simpley handle the template section as an additional config file
  251. # section to parse, this makes e.g. --help work as expected:
  252. for arg in argv:
  253. if arg.startswith('--remote-config='):
  254. sections = ['remote-config %s' % arg.split('=',1)[1]]
  255. break
  256. else:
  257. sections = []
  258. parser = build_parser(argv[0], sections)
  259. if not parser:
  260. return None, None
  261. return parser.parse_args(argv)
  262. def main(argv):
  263. retval = 0
  264. changelog = 'debian/changelog'
  265. cmd = []
  266. try:
  267. options, args = parse_args(argv)
  268. except Exception as e:
  269. print("%s" % e, file=sys.stderr)
  270. return 1
  271. gbp.log.setup(options.color, options.verbose, options.color_scheme)
  272. try:
  273. repo = DebianGitRepository(os.path.curdir)
  274. except GitRepositoryError:
  275. gbp.log.err("%s is not a git repository" % (os.path.abspath('.')))
  276. return 1
  277. try:
  278. branches = []
  279. for branch in [ options.debian_branch, options.upstream_branch ]:
  280. if repo.has_branch(branch):
  281. branches += [ branch ]
  282. if repo.has_pristine_tar_branch() and options.pristine_tar:
  283. branches += [ repo.pristine_tar_branch ]
  284. try:
  285. cp = ChangeLog(filename=changelog)
  286. pkg = cp['Source']
  287. except NoChangeLogError:
  288. pkg = None
  289. if not pkg:
  290. gbp.log.warn("Couldn't parse changelog, will use directory name.")
  291. pkg = os.path.basename(os.path.abspath(os.path.curdir))
  292. pkg = os.path.splitext(pkg)[0]
  293. remote = parse_url(options.remote_url,
  294. options.name,
  295. pkg,
  296. options.template_dir)
  297. if repo.has_remote_repo(options.name):
  298. raise GbpError("You already have a remote name '%s' defined for this repository." % options.name)
  299. gbp.log.info("Shall I create a repository for '%(pkg)s' at '%(url)s' now? (y/n)?" % remote)
  300. if not read_yn():
  301. raise GbpError("Aborted.")
  302. remote_script = build_remote_script(remote, branches[0])
  303. if options.verbose:
  304. print(remote_script)
  305. cmd = build_cmd(remote)
  306. if options.verbose:
  307. print(cmd)
  308. proc = subprocess.Popen(cmd, stdin=subprocess.PIPE)
  309. proc.communicate(remote_script)
  310. if proc.returncode:
  311. raise GbpError("Error creating remote repository")
  312. push_branches(remote, branches)
  313. if options.track:
  314. setup_branch_tracking(repo, remote, branches)
  315. else:
  316. gbp.log.info("You can now add:")
  317. print_config(remote, branches)
  318. gbp.log.info("to your .git/config to 'gbp-pull' and 'git push' in the future.")
  319. except CommandExecFailed:
  320. retval = 1
  321. except (GbpError, GitRepositoryError) as err:
  322. if str(err):
  323. gbp.log.err(err)
  324. retval = 1
  325. return retval
  326. if __name__ == '__main__':
  327. sys.exit(main(sys.argv))
  328. # vim:et:ts=4:sw=4:et:sts=4:ai:set list listchars=tab\:»·,trail\:·: