def test_repoPush_whenRemoteUpdated(self): self.runRepo(["start", "topic"]) self.createCommit() iChangeNumber = self.push()[0] self.oApiClient.put("changes/%d/message" % iChangeNumber, json={"message": "Updated!"}) self.createCommit(bAmend=True) oProcess = self.runRepo(["push"], input="n", check=False) assert oProcess.returncode != 0 lOutputLines = list(filter(bool, oProcess.stdout.splitlines())) sName = os.path.basename(next(iter(self.dProjectFolders))) lExpectedOutputLines = ["### %s ###" % sName, "WARN: You are about to overwrite unknown changes.", "Continue? (y/n): ERROR: [%s] Operation aborted" % sName] assert lOutputLines[:len(lExpectedOutputLines)] == lExpectedOutputLines for iIdx, (sProjectFolder, sProjectName) in enumerate(self.dProjectFolders.items()): dJson = self.oApiClient.get("changes/?q=%s&o=ALL_REVISIONS" % quote_plus("p:%s" % sProjectName)) assert len(dJson) == 1 assert dJson[0]["project"] == sProjectName assert len(dJson[0]["revisions"]) == 2 with changeWorkingDir(sProjectFolder): sLastCommit = git.getLastCommit() if iIdx == 0: assert dJson[0]["current_revision"] != sLastCommit else: assert dJson[0]["current_revision"] == sLastCommit
def PUSH(self, sRepoUrl): sChangeId = gerrit.getChangeId() if not sChangeId: raise FatalError("Unable to extract Change-Id") sProject = gerrit.getProjectName() oRepoData = self.getRepoData() sLocalCommit = git.getLastCommit() try: dChangeData = self.getApiClient().getChangeData( sChangeId, sProject, lAdditionalData=["CURRENT_REVISION"]) except requests.HTTPError as e: if e.response.status_code != 404: raise else: sRemoteCommit = dChangeData["current_revision"] sLastPushedCommit = oRepoData.getLastPushedCommit( sProject, sChangeId) if sRemoteCommit == sLocalCommit: warning("No new changes") return elif sRemoteCommit != sLastPushedCommit: warning("You are about to overwrite unknown changes.") sInput = input("Continue? (y/n): ") if sInput != "y": raise FatalError("Operation aborted") print("Pushing changes to %s" % sRepoUrl) gerrit.push() oRepoData.setLastPushedCommit(sProject, sChangeId, sLocalCommit) self.saveRepoData(oRepoData)
def test_repoPull(self): self.runRepo(["start", "topic"]) self.createCommit() self.push() dCommits = self.createChange(bAmend=True) self.runRepo(["pull"]) for sProjectFolder, sProjectName in self.dProjectFolders.items(): with changeWorkingDir(sProjectFolder): assert dCommits[sProjectName] == git.getLastCommit()
def test_repoPull_whenAlreadyUpToDate(self): self.runRepo(["start", "topic"]) dCommits = self.createCommit() self.runRepo(["push"]) sOutput = self.runRepo(["pull"], capture_output=True, encoding="utf-8").stdout lOutputLines = list(filter(bool, sOutput.splitlines())) lExpectedOutputLines = [] for sProjectFolder in self.dProjectFolders: lExpectedOutputLines += ["### %s ###" % os.path.basename(sProjectFolder), "Already up-to-date.", "Done"] assert lOutputLines[:len(lExpectedOutputLines)] == lExpectedOutputLines for sProjectFolder, sProjectName in self.dProjectFolders.items(): with changeWorkingDir(sProjectFolder): assert dCommits[sProjectName] == git.getLastCommit()
def test_repoPull_whenConflict(self): self.runRepo(["start", "topic"]) self.createCommit() self.runRepo(["push"]) self.createChange(bAmend=True) dCommits = self.createCommit(bAmend=True) oProcess = self.runRepo(["pull"], capture_output=True, encoding="utf-8", check=False) assert oProcess.returncode != 0 lOutputLines = list(filter(bool, oProcess.stdout.splitlines())) lExpectedOutputLines = [] for sProjectFolder in self.dProjectFolders: sName = os.path.basename(sProjectFolder) lExpectedOutputLines += ["### %s ###" % sName, "ERROR: [%s] You have local commits unknown to Gerrit" % sName] assert lOutputLines[:len(lExpectedOutputLines)] == lExpectedOutputLines for sProjectFolder, sProjectName in self.dProjectFolders.items(): with changeWorkingDir(sProjectFolder): assert dCommits[sProjectName] == git.getLastCommit()
def createCommit(self, sId="1", bAmend=False): dCommits = {} for sProjectFolder, sProjectName in self.dProjectFolders.items(): with changeWorkingDir(sProjectFolder): sTestFileName = "test_%s.txt" % sId with open(sTestFileName, "w") as oFile: if bAmend: oFile.write("This is an amended test (%s)." % sId) else: oFile.write("This is a test (%s)." % sId) self.runGit(["add", sTestFileName]) if bAmend: sLastCommitMsg = self.runGit( ["log", "-1", "--format=format:%B"]).stdout sNewCommitMsg = "\n".join( ["Amended test commit (%s)" % sId] + sLastCommitMsg.splitlines()[1:]) self.runGit(["commit", "--amend", "-m", sNewCommitMsg]) else: self.runGit(["commit", "-m", "Test commit (%s)" % sId]) dCommits[sProjectName] = git.getLastCommit() return dCommits
def PULL(self, sRepoUrl): sChangeId = gerrit.getChangeId() if not sChangeId: raise FatalError("Unable to extract Change-Id") sProject = gerrit.getProjectName() try: dChangeData = self.getApiClient().getChangeData( sChangeId, sProject, lAdditionalData=["ALL_REVISIONS"]) except requests.HTTPError as e: if e.response.status_code == 404: warning("No remote patch") return raise e sRemoteCommit = dChangeData["current_revision"] sLocalCommit = git.getLastCommit() sLastPushedCommit = self.getRepoData().getLastPushedCommit( sProject, sChangeId) if sRemoteCommit == sLocalCommit: print("Already up-to-date.") return elif sRemoteCommit == sLastPushedCommit: print("You are ahead of Gerrit.") return elif sLocalCommit in dChangeData["revisions"]: print("Pulling changes from %s" % sRepoUrl) sBranch = git.getCurrentBranch() dFetchData = dChangeData["revisions"][sRemoteCommit]["fetch"][ "ssh"] subprocess.run( ["git", "fetch", dFetchData["url"], dFetchData["ref"]], check=True) subprocess.run(["git", "checkout", "FETCH_HEAD"], check=True) if sBranch: subprocess.run(["git", "branch", "-D", sBranch], check=True) subprocess.run(["git", "checkout", "-b", sBranch], check=True) else: raise FatalError("You have local commits unknown to Gerrit")