def Execute(self, opt, args): git_require(MIN_GIT_VERSION_HARD, fail=True) if not git_require(MIN_GIT_VERSION_SOFT): print( 'repo: warning: git-%s+ will soon be required; please upgrade your ' 'version of git to maintain support.' % ('.'.join(str(x) for x in MIN_GIT_VERSION_SOFT), ), file=sys.stderr) rp = self.manifest.repoProject # Handle new --repo-url requests. if opt.repo_url: remote = rp.GetRemote('origin') remote.url = opt.repo_url remote.Save() # Handle new --repo-rev requests. if opt.repo_rev: wrapper = Wrapper() remote_ref, rev = wrapper.check_repo_rev( rp.gitdir, opt.repo_rev, repo_verify=opt.repo_verify, quiet=opt.quiet) branch = rp.GetBranch('default') branch.merge = remote_ref rp.work_git.reset('--hard', rev) branch.Save() if opt.worktree: # Older versions of git supported worktree, but had dangerous gc bugs. git_require((2, 15, 0), fail=True, msg='git gc worktree corruption') self._SyncManifest(opt) self._LinkManifest(opt.manifest_name) if self.manifest.manifestProject.config.GetBoolean( 'repo.superproject'): self._CloneSuperproject(opt) if os.isatty(0) and os.isatty(1) and not self.manifest.IsMirror: if opt.config_name or self._ShouldConfigureUser(opt): self._ConfigureUser(opt) self._ConfigureColor() self._DisplayResult(opt)
def setUpClass(cls): # Create a repo to operate on, but do it once per-class. cls.GIT_DIR = tempfile.mkdtemp(prefix='repo-rev-tests') run_git = wrapper.Wrapper().run_git remote = os.path.join(cls.GIT_DIR, 'remote') os.mkdir(remote) # Tests need to assume, that main is default branch at init, # which is not supported in config until 2.28. if git_command.git_require((2, 28, 0)): initstr = '--initial-branch=main' else: # Use template dir for init. templatedir = tempfile.mkdtemp(prefix='.test-template') with open(os.path.join(templatedir, 'HEAD'), 'w') as fp: fp.write('ref: refs/heads/main\n') initstr = '--template=' + templatedir run_git('init', initstr, cwd=remote) run_git('commit', '--allow-empty', '-minit', cwd=remote) run_git('branch', 'stable', cwd=remote) run_git('tag', 'v1.0', cwd=remote) run_git('commit', '--allow-empty', '-m2nd commit', cwd=remote) cls.REV_LIST = run_git('rev-list', 'HEAD', cwd=remote).stdout.splitlines() run_git('init', cwd=cls.GIT_DIR) run_git('fetch', remote, '+refs/heads/*:refs/remotes/origin/*', cwd=cls.GIT_DIR)
def _Fetch(self, url): """Fetches a local copy of a superproject for the manifest based on url. Args: url: superproject's url. Returns: True if fetch is successful, or False. """ if not os.path.exists(self._work_git): print('git fetch missing drectory: %s' % self._work_git, file=sys.stderr) return False if not git_require((2, 28, 0)): print('superproject requires a git version 2.28 or later', file=sys.stderr) return False cmd = ['fetch', url, '--depth', '1', '--force', '--no-tags', '--filter', 'blob:none'] if self._branch: cmd += [self._branch + ':' + self._branch] p = GitCommand(None, cmd, cwd=self._work_git, capture_stdout=True, capture_stderr=True) retval = p.Wait() if retval: print('repo: error: git fetch call failed with return code: %r, stderr: %r' % (retval, p.stderr), file=sys.stderr) return False return True
def TempGitTree(): """Create a new empty git checkout for testing.""" # TODO(vapier): Convert this to tempfile.TemporaryDirectory once we drop # Python 2 support entirely. try: tempdir = tempfile.mkdtemp(prefix='repo-tests') # Tests need to assume, that main is default branch at init, # which is not supported in config until 2.28. cmd = ['git', 'init'] if git_command.git_require((2, 28, 0)): cmd += ['--initial-branch=main'] else: # Use template dir for init. templatedir = tempfile.mkdtemp(prefix='.test-template') with open(os.path.join(templatedir, 'HEAD'), 'w') as fp: fp.write('ref: refs/heads/main\n') cmd += ['--template', templatedir] subprocess.check_call(cmd, cwd=tempdir) yield tempdir finally: platform_utils.rmtree(tempdir)
def test_newer_nonfatal(self): """Test non-fatal require calls with newer versions.""" self.assertTrue(git_command.git_require((0, ))) self.assertTrue(git_command.git_require((1, 0))) self.assertTrue(git_command.git_require((1, 2, 0))) self.assertTrue(git_command.git_require((1, 2, 3, 0)))
def test_older_nonfatal(self): """Test non-fatal require calls with old versions.""" self.assertFalse(git_command.git_require((2, ))) self.assertFalse(git_command.git_require((1, 3))) self.assertFalse(git_command.git_require((1, 2, 4))) self.assertFalse(git_command.git_require((1, 2, 3, 5)))
def test_older_fatal_msg(self): """Test fatal require calls with old versions and message.""" with self.assertRaises(SystemExit) as e: git_command.git_require((2, ), fail=True, msg='so sad') self.assertNotEqual(0, e.code)
def test_equal_nonfatal(self): """Test require calls with equal values.""" self.assertTrue(git_command.git_require((1, 2, 3, 4), fail=False)) self.assertTrue(git_command.git_require((1, 2, 3, 4), fail=True))