Ejemplo n.º 1
0
    def SanityCheck(self):
        """Checks if both last-known-good and bad commits exists.

    Also, last-known-good commit should be earlier than bad one.

    Returns:
      True if both last-known-good and bad commits exist.
    """
        logging.info(
            'Perform sanity check on good commit %s and bad commit %s',
            self.good_commit, self.bad_commit)
        # Builder is lazy syncing to HEAD. Sync to HEAD if good_commit or bad_commit
        # does not exist in repo.
        if (not git.DoesCommitExistInRepo(self.repo_dir, self.good_commit) or
                not git.DoesCommitExistInRepo(self.repo_dir, self.bad_commit)):
            logging.info(
                'Either good commit (%s) or bad commit (%s) not found in repo. '
                'Try syncing to HEAD first.', self.good_commit,
                self.bad_commit)
            self.builder.SyncToHead()
        else:
            logging.info(
                'Both good commit (%s) and bad commit (%s) are found in repo. '
                'No need to update repo.', self.good_commit, self.bad_commit)

        good_commit_timestamp = self.GetCommitTimestamp(self.good_commit)
        if not good_commit_timestamp:
            logging.error(
                'Sanity check failed: good commit %s does not exist.',
                self.good_commit)
            return False
        bad_commit_timestamp = self.GetCommitTimestamp(self.bad_commit)
        if not bad_commit_timestamp:
            logging.error('Sanity check failed: bad commit %s does not exist.',
                          self.bad_commit)
            return False

        # TODO(deanliao): Consider the case that we want to find a commit that
        #     fixed a problem.
        if bad_commit_timestamp < good_commit_timestamp:
            logging.error(
                'Sanity check failed: good commit (%s) timestamp: %s should be '
                'earlier than bad commit (%s): %s', self.good_commit,
                good_commit_timestamp, self.bad_commit, bad_commit_timestamp)
            return False
        return True
Ejemplo n.º 2
0
    def _ResetProject(self, commit_hash):
        """Actually pin project to the specified commit hash."""
        if not git.DoesCommitExistInRepo(self.abs_path, commit_hash):
            cros_build_lib.Die(
                'Commit %s not found in %s.\n'
                "You probably need to run 'repo sync --jobs=<jobs>' "
                'to update your checkout.' % (commit_hash, self.abs_path))

        result = cros_build_lib.RunCommand(['git', 'checkout', commit_hash],
                                           error_code_ok=True,
                                           cwd=self.abs_path)
        if result.returncode != 0:
            cros_build_lib.Warning(
                'Failed to pin project %s.\n'
                'You probably have uncommited local changes.' % self.abs_path)
Ejemplo n.º 3
0
        def _Reset(checkout):
            path = checkout.GetPath()

            # There is no need to reset the branch if it doesn't exist.
            if not git.DoesCommitExistInRepo(path, branch):
                return

            if fetch:
                git.RunGit(path, ['fetch', '--all'])

            def _LogBranch():
                branches = git.RunGit(path,
                                      ['branch', '-vv']).output.splitlines()
                branch_line = [b for b in branches if branch in b]
                logging.info(branch_line)

            _LogBranch()
            git.RunGit(path, ['checkout', '-f', branch])
            logging.info('Resetting to %s', checkout['tracking_branch'])
            git.RunGit(path, ['reset', checkout['tracking_branch'], '--hard'])
            _LogBranch()
def PushChange(stable_branch, tracking_branch, dryrun, cwd,
               staging_branch=None):
  """Pushes commits in the stable_branch to the remote git repository.

  Pushes local commits from calls to CommitChange to the remote git
  repository specified by current working directory. If changes are
  found to commit, they will be merged to the merge branch and pushed.
  In that case, the local repository will be left on the merge branch.

  Args:
    stable_branch: The local branch with commits we want to push.
    tracking_branch: The tracking branch of the local branch.
    dryrun: Use git push --dryrun to emulate a push.
    cwd: The directory to run commands in.
    staging_branch: The staging branch to push for a failed PFQ run

  Raises:
    OSError: Error occurred while pushing.
  """
  if not git.DoesCommitExistInRepo(cwd, stable_branch):
    logging.debug('No branch created for %s.  Exiting', cwd)
    return

  if not _DoWeHaveLocalCommits(stable_branch, tracking_branch, cwd):
    logging.debug('No work found to push in %s.  Exiting', cwd)
    return

  # For the commit queue, our local branch may contain commits that were
  # just tested and pushed during the CommitQueueCompletion stage. Sync
  # and rebase our local branch on top of the remote commits.
  remote_ref = git.GetTrackingBranch(cwd,
                                     branch=stable_branch,
                                     for_push=True)
  # SyncPushBranch rebases HEAD onto the updated remote. We need to checkout
  # stable_branch here in order to update it.
  git.RunGit(cwd, ['checkout', stable_branch])
  git.SyncPushBranch(cwd, remote_ref.remote, remote_ref.ref)

  # Check whether any local changes remain after the sync.
  if not _DoWeHaveLocalCommits(stable_branch, remote_ref.ref, cwd):
    logging.info('All changes already pushed for %s. Exiting', cwd)
    return

  # Add a failsafe check here.  Only CLs from the 'chrome-bot' user should
  # be involved here.  If any other CLs are found then complain.
  # In dryruns extra CLs are normal, though, and can be ignored.
  bad_cl_cmd = ['log', '--format=short', '--perl-regexp',
                '--author', '^(?!chrome-bot)', '%s..%s' % (
                    remote_ref.ref, stable_branch)]
  bad_cls = git.RunGit(cwd, bad_cl_cmd).output
  if bad_cls.strip() and not dryrun:
    logging.error('The Uprev stage found changes from users other than '
                  'chrome-bot:\n\n%s', bad_cls)
    raise AssertionError('Unexpected CLs found during uprev stage.')

  if staging_branch is not None:
    logging.info('PFQ FAILED. Pushing uprev change to staging branch %s',
                 staging_branch)

  description = git.RunGit(
      cwd,
      ['log', '--format=format:%s%n%n%b',
       '%s..%s' % (remote_ref.ref, stable_branch)]).output
  description = '%s\n\n%s' % (GIT_COMMIT_SUBJECT, description)
  logging.info('For %s, using description %s', cwd, description)
  git.CreatePushBranch(constants.MERGE_BRANCH, cwd,
                       remote_push_branch=remote_ref)
  git.RunGit(cwd, ['merge', '--squash', stable_branch])
  git.RunGit(cwd, ['commit', '-m', description])
  git.RunGit(cwd, ['config', 'push.default', 'tracking'])
  git.PushWithRetry(constants.MERGE_BRANCH, cwd, dryrun=dryrun,
                    staging_branch=staging_branch)
Ejemplo n.º 5
0
 def testDoesCommitExistInRepoWithAmbiguousBranchName(self):
     git1 = self._MakeRepo('git1', self.source)
     git.CreateBranch(git1, 'peach', track=True)
     self.CommitFile(git1, 'peach', 'Keep me.')
     self.assertTrue(git.DoesCommitExistInRepo(git1, 'peach'))