use of com.google.copybara.git.GitRepository.GitLogEntry in project copybara by google.
the class GitHubPrOrigin method newReader.
@Override
public Reader<GitRevision> newReader(Glob originFiles, Authoring authoring) throws ValidationException {
return new ReaderImpl(url, originFiles, authoring, gitOptions, gitOriginOptions, generalOptions, /*includeBranchCommitLogs=*/
false, submoduleStrategy, firstParent, partialFetch, patchTransformation, describeVersion, /*configPath=*/
null, /*workflowName=*/
null) {
/**
* Disable rebase since this is controlled by useMerge field.
*/
@Override
protected void maybeRebase(GitRepository repo, GitRevision ref, Path workdir) throws RepoException, CannotResolveRevisionException {
}
@Override
public Optional<Baseline<GitRevision>> findBaseline(GitRevision startRevision, String label) throws RepoException, ValidationException {
if (!baselineFromBranch) {
return super.findBaseline(startRevision, label);
}
return findBaselinesWithoutLabel(startRevision, /*limit=*/
1).stream().map(e -> new Baseline<>(e.getSha1(), e)).findFirst();
}
@Override
public ImmutableList<GitRevision> findBaselinesWithoutLabel(GitRevision startRevision, int limit) throws RepoException, ValidationException {
String baseline = Iterables.getLast(startRevision.associatedLabels().get(GITHUB_BASE_BRANCH_SHA1), null);
checkNotNull(baseline, "%s label should be present in %s", GITHUB_BASE_BRANCH_SHA1, startRevision);
GitRevision baselineRev = getRepository().resolveReference(baseline);
// Don't skip the first change as it is already the baseline
BaselinesWithoutLabelVisitor<GitRevision> visitor = new BaselinesWithoutLabelVisitor<>(originFiles, limit, /*skipFirst=*/
false);
visitChanges(baselineRev, visitor);
return visitor.getResult();
}
@Override
public Endpoint getFeedbackEndPoint(Console console) throws ValidationException {
gitHubOptions.validateEndpointChecker(endpointChecker);
return new GitHubEndPoint(gitHubOptions.newGitHubApiSupplier(url, endpointChecker, ghHost), url, console, ghHost);
}
/**
* Deal with the case of useMerge. We have a new commit (the merge) and first-parent from that
* commit doesn't work for this case.
*/
@Override
public ChangesResponse<GitRevision> changes(@Nullable GitRevision fromRef, GitRevision toRef) throws RepoException, ValidationException {
checkCondition(toRef.associatedLabels().containsKey(GITHUB_PR_USE_MERGE), "Cannot determine whether 'use_merge' was set.");
if (toRef.associatedLabel(GITHUB_PR_USE_MERGE).contains("false")) {
return super.changes(fromRef, toRef);
}
GitLogEntry merge = Iterables.getOnlyElement(getRepository().log(toRef.getSha1()).withLimit(1).run());
// Fast-forward merge
if (merge.getParents().size() == 1) {
return super.changes(fromRef, toRef);
}
// HEAD of the Pull Request
GitRevision gitRevision = merge.getParents().get(1);
ChangesResponse<GitRevision> prChanges = super.changes(fromRef, gitRevision);
// origin_files
if (prChanges.isEmpty()) {
return prChanges;
}
try {
return ChangesResponse.forChanges(ImmutableList.<Change<GitRevision>>builder().addAll(prChanges.getChanges()).add(change(merge.getCommit())).build());
} catch (EmptyChangeException e) {
throw new RepoException("Error getting the merge commit information: " + merge, e);
}
}
};
}
use of com.google.copybara.git.GitRepository.GitLogEntry in project copybara by google.
the class WorkflowTest method testHgOriginNoFlags.
@Test
public void testHgOriginNoFlags() throws Exception {
Path originPath = Files.createTempDirectory("origin");
HgRepository origin = new HgRepository(originPath, true, DEFAULT_TIMEOUT).init();
Path destinationPath = Files.createTempDirectory("destination");
GitRepository destRepo = GitRepository.newBareRepo(destinationPath, getGitEnv(), true, DEFAULT_TIMEOUT, /*noVerify=*/
false).init();
String primaryBranch = destRepo.getPrimaryBranch();
String config = "core.workflow(" + " name = 'default'," + " origin = hg.origin( url = 'file://" + origin.getHgDir() + "', ref = 'default'),\n" + " destination = git.destination(" + " url = 'file://" + destRepo.getGitDir() + "',\n" + " fetch = '" + primaryBranch + "',\n" + " push = '" + primaryBranch + "',\n" + " ),\n" + " authoring = " + authoring + "," + " mode = '" + WorkflowMode.ITERATIVE + "'," + ")\n";
Files.write(originPath.resolve("foo.txt"), "testing foo".getBytes(UTF_8));
origin.hg(originPath, "add", "foo.txt");
origin.hg(originPath, "commit", "-m", "add foo");
options.gitDestination.committerName = "Foo";
options.gitDestination.committerEmail = "foo@foo.com";
options.setWorkdirToRealTempDir();
options.setHomeDir(Files.createTempDirectory("home").toString());
options.workflowOptions.initHistory = true;
Migration workflow = loadConfig(config).getMigration("default");
workflow.run(workdir, ImmutableList.of());
ImmutableList<GitLogEntry> destCommits = destRepo.log("HEAD").run();
assertThat(destCommits).hasSize(1);
assertThat(destCommits.get(0).getBody()).contains("add foo");
Files.write(originPath.resolve("bar.txt"), "testing bar".getBytes(UTF_8));
origin.hg(originPath, "add", "bar.txt");
origin.hg(originPath, "commit", "-m", "add bar");
options.workflowOptions.initHistory = false;
workflow.run(workdir, ImmutableList.of());
destCommits = destRepo.log("HEAD").run();
assertThat(destCommits).hasSize(2);
assertThat(destCommits.get(0).getBody()).contains("add bar");
assertThat(destCommits.get(1).getBody()).contains("add foo");
}
use of com.google.copybara.git.GitRepository.GitLogEntry in project copybara by google.
the class GerritDestinationTest method lastCommitChangeIdLineForRef.
private static String lastCommitChangeIdLineForRef(String originRef, GitRepository repo, String gitRef) throws RepoException {
GitLogEntry log = Iterables.getOnlyElement(repo.log(gitRef).withLimit(1).run());
assertThat(log.getBody()).contains("\n" + DummyOrigin.LABEL_NAME + ": " + originRef + "\n");
String line = null;
for (LabelFinder label : ChangeMessage.parseMessage(log.getBody()).getLabels()) {
if (label.isLabel(GerritDestination.CHANGE_ID_LABEL)) {
assertWithMessage(log.getBody() + " has a Change-Id label that doesn't" + " match the regex").that(label.getValue()).matches("I[0-9a-f]{40}$");
// Multiple Change-Ids are not allowed.
assertThat(line).isNull();
line = label.getLine();
}
}
assertWithMessage("Cannot find " + GerritDestination.CHANGE_ID_LABEL + " in:\n" + log.getBody()).that(line).isNotNull();
return line;
}
use of com.google.copybara.git.GitRepository.GitLogEntry in project copybara by google.
the class GitDestinationIntegrateTest method testGerritFakeMergeNoChangeId.
@Test
public void testGerritFakeMergeNoChangeId() throws ValidationException, IOException, RepoException {
Path workTree = Files.createTempDirectory("test");
GitRepository repo = gitUtil.mockRemoteRepo("example.com/gerrit").withWorkTree(workTree);
String label = new GerritIntegrateLabel(repo, options.general, "https://example.com/gerrit", 1020, 1, /*changeId=*/
null).toString();
assertThat(label).isEqualTo("gerrit https://example.com/gerrit 1020 Patch Set 1");
GitRevision firstChange = singleChange(workTree, repo, "ignore_me", "Feature1 change");
repo.simpleCommand("update-ref", "refs/changes/20/1020/1", firstChange.getSha1());
GitTestUtil.createFakeGerritNodeDbMeta(repo, 1020, CHANGE_ID);
GitDestination destination = destination(FAKE_MERGE);
GitLogEntry previous = createBaseDestinationChange(destination);
migrateOriginChange(destination, "Test change\n" + "\n" + GitModule.DEFAULT_INTEGRATE_LABEL + "=" + label + "\n", "some content");
// Make sure commit adds new text
String showResult = git("--git-dir", repoGitDir.toString(), "show", primaryBranch);
assertThat(showResult).contains("some content");
GitTesting.assertThatCheckout(repo(), primaryBranch).containsFile("test.txt", "some content").containsNoMoreFiles();
GitLogEntry merge = getLastMigratedChange(primaryBranch);
assertThat(merge.getBody()).isEqualTo("Merge Gerrit change 1020 Patch Set 1\n" + "\n" + "DummyOrigin-RevId: test\n");
assertThat(Lists.transform(merge.getParents(), GitRevision::getSha1)).isEqualTo(Lists.newArrayList(previous.getCommit().getSha1(), firstChange.getSha1()));
assertThat(console.getMessages().stream().filter(e -> e.getType() == MessageType.WARNING).findAny()).isEqualTo(Optional.empty());
}
use of com.google.copybara.git.GitRepository.GitLogEntry in project copybara by google.
the class GitDestinationIntegrateTest method testDefaultIntegration.
@Test
public void testDefaultIntegration() throws ValidationException, IOException, RepoException {
Path repoPath = Files.createTempDirectory("test");
GitRepository repo = GitRepository.newRepo(/*verbose*/
true, repoPath, getGitEnv()).init();
GitRevision feature1 = singleChange(repoPath, repo, "ignore_me", "Feature1 change");
repo.simpleCommand("branch", "feature1");
GitRevision feature2 = singleChange(repoPath, repo, "ignore_me2", "Feature2 change");
repo.simpleCommand("branch", "feature2");
GitDestination destination = destinationWithDefaultIntegrates();
migrateOriginChange(destination, "Base change\n", "not important");
GitLogEntry previous = getLastMigratedChange(primaryBranch);
migrateOriginChange(destination, "Test change\n" + "\n" + GitModule.DEFAULT_INTEGRATE_LABEL + "=file://" + repo.getWorkTree().toString() + " feature1\n" + GitModule.DEFAULT_INTEGRATE_LABEL + "=file://" + repo.getWorkTree().toString() + " feature2\n", "some content");
// Make sure commit adds new text
String showResult = git("--git-dir", repoGitDir.toString(), "show", primaryBranch + "^1");
assertThat(showResult).contains("some content");
GitTesting.assertThatCheckout(repo(), primaryBranch).containsFile("test.txt", "some content").containsFile("ignore_me", "").containsFile("ignore_me2", "").containsNoMoreFiles();
GitLogEntry feature1Merge = getLastMigratedChange(primaryBranch + "^1");
assertThat(feature1Merge.getFiles()).containsExactly("test.txt", "ignore_me");
assertThat(feature1Merge.getBody()).isEqualTo("Merge of " + feature1.getSha1() + "\n" + "\n" + DummyOrigin.LABEL_NAME + ": test\n");
assertThat(Lists.transform(feature1Merge.getParents(), GitRevision::getSha1)).isEqualTo(Lists.newArrayList(previous.getCommit().getSha1(), feature1.getSha1()));
GitLogEntry feature2Merge = getLastMigratedChange(primaryBranch);
assertThat(feature2Merge.getBody()).isEqualTo("Merge of " + feature2.getSha1() + "\n" + "\n" + DummyOrigin.LABEL_NAME + ": test\n");
assertThat(Lists.transform(feature2Merge.getParents(), GitRevision::getSha1)).isEqualTo(Lists.newArrayList(feature1Merge.getCommit().getSha1(), feature2.getSha1()));
}
Aggregations