aboutsummaryrefslogtreecommitdiffhomepage
path: root/gbp/scripts/clone.py
diff options
context:
space:
mode:
Diffstat (limited to 'gbp/scripts/clone.py')
-rwxr-xr-xgbp/scripts/clone.py50
1 files changed, 34 insertions, 16 deletions
diff --git a/gbp/scripts/clone.py b/gbp/scripts/clone.py
index 216bf993..63b1468a 100755
--- a/gbp/scripts/clone.py
+++ b/gbp/scripts/clone.py
@@ -19,13 +19,14 @@
#
"""Clone a Git repository and set it up for gbp"""
-from six.moves import configparser
import sys
-import os, os.path
+import os
from gbp.config import (GbpOptionParser, GbpOptionGroup)
from gbp.deb.git import DebianGitRepository
from gbp.git import (GitRepository, GitRepositoryError)
from gbp.errors import GbpError
+from gbp.scripts.common import ExitCodes
+from gbp.scripts.common.hook import Hook
import gbp.log
@@ -33,12 +34,14 @@ def build_parser(name):
try:
parser = GbpOptionParser(command=os.path.basename(name), prefix='',
usage='%prog [options] repository - clone a remote repository')
- except configparser.ParsingError as err:
+ except GbpError as err:
gbp.log.err(err)
return None
branch_group = GbpOptionGroup(parser, "branch options", "branch tracking and layout options")
+ cmd_group = GbpOptionGroup(parser, "external command options", "how and when to invoke hooks")
parser.add_option_group(branch_group)
+ parser.add_option_group(cmd_group)
branch_group.add_option("--all", action="store_true", dest="all", default=False,
help="track all branches, not only debian and upstream")
@@ -49,6 +52,10 @@ def build_parser(name):
help="git history depth (for creating shallow clones)")
branch_group.add_option("--reference", action="store", dest="reference", default=None,
help="git reference repository (use local copies where possible)")
+ cmd_group.add_config_file_option(option_name="postclone", dest="postclone",
+ help="hook to run after cloning the source tree, "
+ "default is '%(postclone)s'")
+ cmd_group.add_boolean_config_file_option(option_name="hooks", dest="hooks")
parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False,
help="verbose command execution")
@@ -58,7 +65,7 @@ def build_parser(name):
return parser
-def parse_args (argv):
+def parse_args(argv):
parser = build_parser(argv[0])
if not parser:
return None, None
@@ -73,7 +80,7 @@ def main(argv):
(options, args) = parse_args(argv)
if not options:
- return 1
+ return ExitCodes.parse_error
if len(args) < 2:
gbp.log.err("Need a repository to clone.")
@@ -90,12 +97,14 @@ def main(argv):
pass
try:
+ gbp.log.info("Cloning from '%s'%s" % (source, " into '%s'" % clone_to if not auto_name else ''))
repo = DebianGitRepository.clone(clone_to, source, options.depth,
- auto_name=auto_name,reference=options.reference)
+ auto_name=auto_name, reference=options.reference)
os.chdir(repo.path)
# Reparse the config files of the cloned repository so we pick up the
- # branch information from there:
+ # branch information from there but don't overwrite hooks:
+ postclone = options.postclone
(options, args) = parse_args(argv)
# Track all branches:
@@ -103,22 +112,30 @@ def main(argv):
remotes = repo.get_remote_branches()
for remote in remotes:
local = remote.replace("origin/", "", 1)
- if not repo.has_branch(local) and \
- local != "HEAD":
- repo.create_branch(local, remote)
- else: # only track gbp's default branches
- branches = [ options.debian_branch, options.upstream_branch ]
+ if (not repo.has_branch(local) and
+ local != "HEAD"):
+ repo.create_branch(local, remote)
+ else: # only track gbp's default branches
+ branches = [options.debian_branch, options.upstream_branch]
if options.pristine_tar:
- branches += [ repo.pristine_tar_branch ]
+ branches += [repo.pristine_tar_branch]
gbp.log.debug('Will track branches: %s' % branches)
for branch in branches:
remote = 'origin/%s' % branch
- if repo.has_branch(remote, remote=True) and \
- not repo.has_branch(branch):
- repo.create_branch(branch, remote)
+ if (repo.has_branch(remote, remote=True) and
+ not repo.has_branch(branch)):
+ repo.create_branch(branch, remote)
repo.set_branch(options.debian_branch)
+ if postclone:
+ Hook('Postclone', options.postclone,
+ extra_env={'GBP_GIT_DIR': repo.git_dir},
+ )()
+
+ except KeyboardInterrupt:
+ retval = 1
+ gbp.log.err("Interrupted. Aborting.")
except GitRepositoryError as err:
gbp.log.err("Git command failed: %s" % err)
retval = 1
@@ -129,6 +146,7 @@ def main(argv):
return retval
+
if __name__ == '__main__':
sys.exit(main(sys.argv))