def runReifyCustomNetRcTest(self, fake_git_config): netrc_file = os.path.join(self.repos_dir, 'custom_netrc') with open(netrc_file, 'w') as f: f.write( 'machine localhost login test-username password test-password\n' ) fake_home = os.path.abspath(os.path.join(self.repos_dir, 'fake_home')) os.makedirs(fake_home) if fake_git_config: with open(os.path.join(fake_home, '.gitconfig'), 'w') as f: f.write(fake_git_config) prev_env = os.environ.copy() os.environ['HOME'] = fake_home os.environ.pop('INFRA_GIT_WRAPPER_HOME', None) # see crbug.com/756224#c33 try: r = self.mkRepo() # make a mirror of THAT, .netrc is not actually used, but whatever, code # path is covered, which is good. r2 = git2.Repo('file://' + r.repo_path) r2.repos_dir = os.path.join(self.repos_dir, 'repos') r2.netrc_file = netrc_file self.capture_stdio(r2.reify) yield r2 finally: os.environ = prev_env
def check_url(s): parsed = urlparse.urlparse(s) if parsed.scheme not in ('https', 'git', 'file'): raise argparse.ArgumentTypeError( 'Repo URL must use https, git or file protocol.') if not parsed.path.strip('/'): raise argparse.ArgumentTypeError('URL is missing a path?') return git2.Repo(s)
def testEmptyRepo(self): r = git2.Repo('doesnt_exist') r.repos_dir = self.repos_dir with self.assertRaises(git2.CalledProcessError): self.capture_stdio(r.reify) with self.assertRaises(AssertionError): r.run('show-ref')
def testShareObjectsFrom(self): r = self.mkRepo() # make a mirror of THAT r2 = git2.Repo('file://' + r.repo_path) r2.repos_dir = os.path.join(self.repos_dir, 'repos') self.capture_stdio(r2.reify, share_from=r) data = 'super-cool-blob' hsh = r.intern(data) self.assertEqual(r2.run('cat-file', 'blob', hsh), data)
def parse_args(args): # pragma: no cover parser = argparse.ArgumentParser('python -m %s' % __package__) parser.add_argument('--dry_run', action='store_true', help='Do not actually push anything.') parser.add_argument('--repo_dir', metavar='DIR', default='tag_pusher_repos', help=('The directory to use for git clones ' '(default: %(default)s)')) parser.add_argument('--spec_json', metavar='SPEC', required=True, help=('JSON file with configuration: ' '{<repo_url>: [{"refs" : [<ref>], "url": <url>}]}')) parser.add_argument('--json_output', metavar='PATH', help='Path to write JSON with results of the run to') logs.add_argparse_options(parser) outer_loop.add_argparse_options(parser) opts = parser.parse_args(args) logs.process_argparse_options(opts) loop_opts = outer_loop.process_argparse_options(opts) # Read and validate the spec JSON. with open(opts.spec_json, 'r') as f: spec = json.load(f) if not isinstance(spec, dict): parser.error('Expecting dict as a spec') for repo_url, push_list in spec.iteritems(): # Repo URL. parsed = urlparse.urlparse(repo_url) if parsed.scheme not in ('https', 'git', 'file'): parser.error('Repo URL must use https, git or file protocol.') if not parsed.path.strip('/'): parser.error('Repo URL is missing a path?') # Ref and URL to fetch. for d in push_list: refs = d.get('refs') or [] url_to_read = d.get('url') for ref in refs: if not ref or not ref.startswith('refs/'): parser.error('Ref to push should start with refs/') if not url_to_read or not url_to_read.startswith('https://'): parser.error('URL to read SHA1 from should use https') # git2.Repo -> [([ref_to_push], url_to_read)]. spec_by_repo = {} for url, push_list in spec.iteritems(): repo = git2.Repo(url) repo.dry_run = opts.dry_run repo.repos_dir = opts.repo_dir spec_by_repo[repo] = [(d['refs'], d['url']) for d in push_list] return Options(spec_by_repo, loop_opts, opts.json_output)
def testShareObjectsStringPath(self): r = self.mkRepo() data = 'super-cool-blob' hsh = r.intern(data) # make a mirror of THAT r2 = git2.Repo('file://' + r.repo_path) r2.repos_dir = os.path.join(self.repos_dir, 'repos') self.capture_stdio(r2.reify) # blob is not there because it's a clone, but the blob wasn't in a commit with self.assertRaises(git2.CalledProcessError): r2.run('cat-file', 'blob', hsh) self.capture_stdio(r2.reify, share_from=r.repo_path) self.assertEqual(r2.run('cat-file', 'blob', hsh), data)
def testShareObjectsAdd(self): r = self.mkRepo() data = 'super-cool-blob' hsh = r.intern(data) # make a mirror of THAT r2 = git2.Repo('file://' + r.repo_path) r2.repos_dir = os.path.join(self.repos_dir, 'repos') self.capture_stdio(r2.reify) # blob is not there because it's a clone, but the blob wasn't in a commit with self.assertRaises(git2.CalledProcessError): r2.run('cat-file', 'blob', hsh) self.capture_stdio(r2.reify, share_from=r) self.assertEqual(r2.run('cat-file', 'blob', hsh), data) # reifying a second time shouldn't change the alternates file with open(os.path.join(r2.repo_path, 'objects', 'info', 'alternates')) as f: altfile = f.read() self.capture_stdio(r2.reify, share_from=r) with open(os.path.join(r2.repo_path, 'objects', 'info', 'alternates')) as f: self.assertEqual(altfile, f.read())
def mkRepo(self): # pragma: no cover r = git2.Repo(self.repo.repo_path) r.repos_dir = os.path.join(self.repos_dir, 'repos') self.capture_stdio(r.reify) return r
def testDefaultPartialClone(self): r = git2.Repo(self.repo.repo_path) r.repos_dir = os.path.join(self.repos_dir, 'repos') self.capture_stdio(r.reify, partial_clone=True) self.assertEqual( r.run('rev-parse', 'branch_F').strip(), self.repo['F'])