1 ##############################################################################
2 # Copyright (c) 2017 Feng Pan (fpan@redhat.com) and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
14 from pygerrit2.rest import GerritRestAPI
22 logging.info("Cloning {}".format(args.repo))
25 cm = git.Repo(search_parent_directories=True).commit().message
26 except git.exc.InvalidGitRepositoryError:
27 logging.debug('Current Apex directory is not a git repo: {}'
31 logging.info("Current commit message: {}".format(cm))
32 m = re.search('{}:\s*(\S+)'.format(args.repo), cm)
35 change_id = m.group(1)
36 logging.info("Using change ID {} from {}".format(change_id, args.repo))
37 rest = GerritRestAPI(url=args.url)
38 change_str = "changes/{}?o=CURRENT_REVISION".format(change_id)
39 change = rest.get(change_str)
41 assert change['status'] not in 'ABANDONED' 'CLOSED',\
42 'Change {} is in {} state'.format(change_id, change['status'])
43 if change['status'] == 'MERGED':
44 logging.info('Change {} is merged, ignoring...'
47 current_revision = change['current_revision']
48 ref = change['revisions'][current_revision]['ref']
49 logging.info('setting ref to {}'.format(ref))
51 logging.error('Failed to get valid change data structure from url '
52 '{}/{}, data returned: \n{}'
53 .format(change_id, change_str, change))
56 # remove existing file or directory named repo
57 if os.path.exists(args.repo):
58 if os.path.isdir(args.repo):
59 shutil.rmtree(args.repo)
63 ws = git.Repo.clone_from("{}/{}".format(args.url, args.repo),
64 args.repo, b=args.branch)
67 git_cmd.fetch("{}/{}".format(args.url, args.repo), ref)
68 git_cmd.checkout('FETCH_HEAD')
69 logging.info('Checked out commit:\n{}'.format(ws.head.commit.message))
73 parser = argparse.ArgumentParser()
74 parser.add_argument('--debug', action='store_true', default=False,
75 help="Turn on debug messages")
76 subparsers = parser.add_subparsers()
77 fork = subparsers.add_parser('clone-fork',
78 help='Clone fork of dependent repo')
79 fork.add_argument('-r', '--repo', required=True, help='Name of repository')
80 fork.add_argument('-u', '--url',
81 default='https://gerrit.opnfv.org/gerrit',
82 help='Gerrit URL of repository')
83 fork.add_argument('-b', '--branch',
85 help='Branch to checkout')
86 fork.set_defaults(func=clone_fork)
92 args = parser.parse_args(sys.argv[1:])
94 logging_level = logging.DEBUG
96 logging_level = logging.INFO
98 logging.basicConfig(format='%(asctime)s %(levelname)s: %(message)s',
99 datefmt='%m/%d/%Y %I:%M:%S %p',
101 if hasattr(args, 'func'):
107 if __name__ == "__main__":