Search in sources :

Example 16 with CannotResolveRevisionException

use of com.google.copybara.exception.CannotResolveRevisionException in project copybara by google.

the class GitVisitorUtil method visitChanges.

/**
 * Visits
 */
static void visitChanges(GitRevision start, ChangesVisitor visitor, ChangeReader.Builder queryChanges, GeneralOptions generalOptions, String type, int visitChangePageSize) throws RepoException, ValidationException {
    Preconditions.checkNotNull(start);
    int skip = 0;
    boolean finished = false;
    try (ProfilerTask ignore = generalOptions.profiler().start(type + "/visit_changes")) {
        while (!finished) {
            ImmutableList<Change<GitRevision>> result;
            try (ProfilerTask ignore2 = generalOptions.profiler().start("git_log_" + skip + "_" + visitChangePageSize)) {
                result = queryChanges.setSkip(skip).setLimit(visitChangePageSize).build().run(start.getSha1()).reverse();
            }
            if (result.isEmpty()) {
                break;
            }
            skip += result.size();
            for (Change<GitRevision> current : result) {
                if (visitor.visit(current) == VisitResult.TERMINATE) {
                    finished = true;
                    break;
                }
            }
        }
    }
    if (skip == 0) {
        throw new CannotResolveRevisionException("Cannot resolve reference " + start.getSha1());
    }
}
Also used : ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask) CannotResolveRevisionException(com.google.copybara.exception.CannotResolveRevisionException) Change(com.google.copybara.Change)

Example 17 with CannotResolveRevisionException

use of com.google.copybara.exception.CannotResolveRevisionException in project copybara by google.

the class HgRepository method identify.

/**
 * Returns a revision object given a reference.
 *
 * <p> A reference can be any of the following:
 * <ul>
 *   <li> A global identifier for a revision. Example: f4e0e692208520203de05557244e573e981f6c72
 *   <li> A local identifier for a revision in the repository. Example: 1
 *   <li> A bookmark in the repository.
 *   <li> A branch in the repository, which returns the tip of that branch. Example: default
 *   <li> A tag in the repository. Example: tip
 * </ul>
 */
public HgRevision identify(String reference) throws RepoException, CannotResolveRevisionException {
    try {
        CommandOutput commandOutput = hg(hgDir, "identify", "--template", "{node}\n", "--id", "--rev", reference);
        String globalId = commandOutput.getStdout().trim();
        return new HgRevision(globalId, reference);
    } catch (RepoException e) {
        if (UNKNOWN_REVISION.matcher(e.getMessage()).find()) {
            throw new CannotResolveRevisionException(String.format("Unknown revision: %s", e.getMessage()));
        }
        throw e;
    }
}
Also used : CommandOutput(com.google.copybara.util.CommandOutput) CannotResolveRevisionException(com.google.copybara.exception.CannotResolveRevisionException) RepoException(com.google.copybara.exception.RepoException)

Example 18 with CannotResolveRevisionException

use of com.google.copybara.exception.CannotResolveRevisionException in project copybara by google.

the class WorkflowTest method iterativeWorkflowNoPreviousRef.

@Test
public void iterativeWorkflowNoPreviousRef() throws Exception {
    origin.addSimpleChange(/*timestamp*/
    1);
    Workflow<?, ?> workflow = iterativeWorkflow(/*previousRef=*/
    null);
    CannotResolveRevisionException thrown = assertThrows(CannotResolveRevisionException.class, () -> workflow.run(workdir, ImmutableList.of("0")));
    assertThat(thrown).hasMessageThat().contains("Previous revision label DummyOrigin-RevId could not be found");
}
Also used : CannotResolveRevisionException(com.google.copybara.exception.CannotResolveRevisionException) Test(org.junit.Test)

Example 19 with CannotResolveRevisionException

use of com.google.copybara.exception.CannotResolveRevisionException in project copybara by google.

the class GitHubPrOriginTest method testCheckout_noMergeRef.

@Test
public void testCheckout_noMergeRef() throws Exception {
    GitRepository remote = gitUtil.mockRemoteRepo("github.com/google/example");
    addFiles(remote, "base", ImmutableMap.<String, String>builder().put("test.txt", "a").buildOrThrow());
    String prHeadSha1 = remote.parseRef("HEAD");
    remote.simpleCommand("update-ref", GitHubUtil.asHeadRef(123), prHeadSha1);
    MockPullRequest.create(gitUtil).setState("open").setPrNumber(123).mock();
    // Now try with merge ref
    GitHubPrOrigin origin = githubPrOrigin("url = 'https://github.com/google/example'", "use_merge = True");
    CannotResolveRevisionException thrown = assertThrows(CannotResolveRevisionException.class, () -> origin.newReader(Glob.ALL_FILES, authoring).checkout(origin.resolve("123"), workdir));
    assertThat(thrown).hasMessageThat().contains("Cannot find a merge reference for Pull Request 123");
}
Also used : CannotResolveRevisionException(com.google.copybara.exception.CannotResolveRevisionException) Test(org.junit.Test)

Example 20 with CannotResolveRevisionException

use of com.google.copybara.exception.CannotResolveRevisionException in project copybara by google.

the class GitHubPrOriginTest method testCheckout_nullMergeable_mergeCommitDoesntExist.

@Test
public void testCheckout_nullMergeable_mergeCommitDoesntExist() throws Exception {
    GitRepository remote = withTmpWorktree(gitUtil.mockRemoteRepo("github.com/google/example"));
    String baseRef = addFiles(remote, "base", ImmutableMap.of("test.txt", "a"));
    remote.simpleCommand("branch", "feature");
    String featureRef = addFiles(remote, "commit to feature branch", ImmutableMap.of("test.txt", "c"));
    remote.forceCheckout(baseRef);
    String mainRef = addFiles(remote, "commit to main branch", ImmutableMap.of("test.txt", "b"));
    remote.forceCheckout(mainRef);
    remote.simpleCommand("update-ref", GitHubUtil.asHeadRef(123), featureRef);
    MockPullRequest.create(gitUtil).setState("open").setPrimaryBranch("main").setPrNumber(123).setMergeable(null).mock();
    GitHubPrOrigin origin = githubPrOrigin("url = 'https://github.com/google/example'", "use_merge = True");
    CannotResolveRevisionException thrown = assertThrows(CannotResolveRevisionException.class, () -> origin.resolve("123"));
    assertThat(thrown).hasMessageThat().contains("Cannot find a merge reference for Pull Request 123");
}
Also used : CannotResolveRevisionException(com.google.copybara.exception.CannotResolveRevisionException) Test(org.junit.Test)

Aggregations

CannotResolveRevisionException (com.google.copybara.exception.CannotResolveRevisionException)23 Test (org.junit.Test)10 CharMatcher (com.google.common.base.CharMatcher)6 ProfilerTask (com.google.copybara.profiler.Profiler.ProfilerTask)5 ImmutableList (com.google.common.collect.ImmutableList)4 Endpoint (com.google.copybara.Endpoint)4 EmptyChangeException (com.google.copybara.exception.EmptyChangeException)4 RepoException (com.google.copybara.exception.RepoException)4 PullRequest (com.google.copybara.git.github.api.PullRequest)4 Change (com.google.copybara.Change)3 Path (java.nio.file.Path)3 Matcher (java.util.regex.Matcher)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 Preconditions (com.google.common.base.Preconditions)2 Splitter (com.google.common.base.Splitter)2 Collections2 (com.google.common.collect.Collections2)2 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)2 ImmutableListMultimap (com.google.common.collect.ImmutableListMultimap)2 ImmutableListMultimap.toImmutableListMultimap (com.google.common.collect.ImmutableListMultimap.toImmutableListMultimap)2 ImmutableMap (com.google.common.collect.ImmutableMap)2