def testPushChange(self): git_log = 'Marking test_one as stable\nMarking test_two as stable\n' fake_description = 'Marking set of ebuilds as stable\n\n%s' % git_log self.mox.StubOutWithMock(cros_mark_as_stable, '_DoWeHaveLocalCommits') self.mox.StubOutWithMock(cros_mark_as_stable.GitBranch, 'CreateBranch') self.mox.StubOutWithMock(cros_mark_as_stable.GitBranch, 'Exists') self.mox.StubOutWithMock(git, 'PushWithRetry') self.mox.StubOutWithMock(git, 'GetTrackingBranch') self.mox.StubOutWithMock(git, 'SyncPushBranch') self.mox.StubOutWithMock(git, 'CreatePushBranch') self.mox.StubOutWithMock(git, 'RunGit') cros_mark_as_stable._DoWeHaveLocalCommits( self._branch, self._target_manifest_branch, '.').AndReturn(True) git.GetTrackingBranch('.', for_push=True).AndReturn( ['gerrit', 'refs/remotes/gerrit/master']) git.SyncPushBranch('.', 'gerrit', 'refs/remotes/gerrit/master') cros_mark_as_stable._DoWeHaveLocalCommits( self._branch, 'refs/remotes/gerrit/master', '.').AndReturn(True) result = cros_build_lib.CommandResult(output=git_log) cros_build_lib.RunCommandCaptureOutput( ['git', 'log', '--format=format:%s%n%n%b', 'refs/remotes/gerrit/master..%s' % self._branch], cwd='.').AndReturn(result) git.CreatePushBranch('merge_branch', '.') git.RunGit('.', ['merge', '--squash', self._branch]) git.RunGit('.', ['commit', '-m', fake_description]) git.RunGit('.', ['config', 'push.default', 'tracking']) git.PushWithRetry('merge_branch', '.', dryrun=False) self.mox.ReplayAll() cros_mark_as_stable.PushChange(self._branch, self._target_manifest_branch, False, '.') self.mox.VerifyAll()
def PushChange(stable_branch, tracking_branch, dryrun, cwd): """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. Raises: OSError: Error occurred while pushing. """ if not _DoWeHaveLocalCommits(stable_branch, tracking_branch, cwd): cros_build_lib.Info('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, push_branch = git.GetTrackingBranch(cwd, for_push=True) git.SyncPushBranch(cwd, remote, push_branch) # Check whether any local changes remain after the sync. if not _DoWeHaveLocalCommits(stable_branch, push_branch, cwd): cros_build_lib.Info('All changes already pushed for %s. Exiting', cwd) return description = cros_build_lib.RunCommandCaptureOutput([ 'git', 'log', '--format=format:%s%n%n%b', '%s..%s' % (push_branch, stable_branch) ], cwd=cwd).output description = 'Marking set of ebuilds as stable\n\n%s' % description cros_build_lib.Info('For %s, using description %s', cwd, description) git.CreatePushBranch(constants.MERGE_BRANCH, cwd) 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)
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)