Search in sources :

Example 6 with CannotResolveRevisionException

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

the class GitRepository method commit.

// TODO(malcon): Create a CommitCmd object builder
public void commit(@Nullable String author, boolean amend, @Nullable ZonedDateTime timestamp, String message) throws RepoException, ValidationException {
    if (isEmptyStaging() && !amend) {
        String baseline = "unknown";
        try {
            baseline = parseRef("HEAD");
        } catch (CannotResolveRevisionException | RepoException e) {
            logger.atWarning().withCause(e).log("Cannot find baseline.");
        }
        throw new EmptyChangeException(String.format("Migration of the revision resulted in an empty change from baseline '%s'.\n" + "Is the change already migrated?", baseline));
    }
    ImmutableList.Builder<String> params = ImmutableList.<String>builder().add("commit");
    if (author != null) {
        params.add("--author", author);
    }
    if (timestamp != null) {
        params.add("--date", timestamp.format(ISO_OFFSET_DATE_TIME_NO_SUBSECONDS));
    }
    if (amend) {
        params.add("--amend");
    }
    if (noVerify) {
        params.add("--no-verify");
    }
    Path descriptionFile = null;
    try {
        if (message.getBytes(StandardCharsets.UTF_8).length > ARBITRARY_MAX_ARG_SIZE) {
            descriptionFile = getCwd().resolve(UUID.randomUUID().toString() + ".desc");
            Files.write(descriptionFile, message.getBytes(StandardCharsets.UTF_8));
            params.add("-F", descriptionFile.toAbsolutePath().toString());
        } else {
            params.add("-m", message);
        }
        git(getCwd(), addGitDirAndWorkTreeParams(params.build()));
    } catch (IOException e) {
        throw new RepoException("Could not commit change: Failed to write file " + descriptionFile, e);
    } finally {
        try {
            if (descriptionFile != null) {
                Files.deleteIfExists(descriptionFile);
            }
        } catch (IOException e) {
            logger.atWarning().log("Could not delete description file: %s", descriptionFile);
        }
    }
}
Also used : Path(java.nio.file.Path) ImmutableList(com.google.common.collect.ImmutableList) CannotResolveRevisionException(com.google.copybara.exception.CannotResolveRevisionException) EmptyChangeException(com.google.copybara.exception.EmptyChangeException) RepoException(com.google.copybara.exception.RepoException) IOException(java.io.IOException)

Example 7 with CannotResolveRevisionException

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

the class GitRepository method parseRef.

/**
 * Resolves a git reference to the SHA-1 reference
 */
public String parseRef(String ref) throws RepoException, CannotResolveRevisionException {
    // Runs rev-list on the reference and remove the extra newline from the output.
    CommandOutputWithStatus result = gitAllowNonZeroExit(NO_INPUT, ImmutableList.of("rev-list", "-1", ref, "--"), DEFAULT_TIMEOUT);
    if (!result.getTerminationStatus().success()) {
        throw new CannotResolveRevisionException("Cannot find reference '" + ref + "'");
    }
    String sha1 = result.getStdout().trim();
    Verify.verify(SHA1_PATTERN.matcher(sha1).matches(), "Should be resolved to a SHA-1: %s", sha1);
    return sha1;
}
Also used : CommandOutputWithStatus(com.google.copybara.util.CommandOutputWithStatus) CannotResolveRevisionException(com.google.copybara.exception.CannotResolveRevisionException)

Example 8 with CannotResolveRevisionException

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

the class WorkflowTest method invalidLastRevFlagGivesClearError.

@Test
public void invalidLastRevFlagGivesClearError() throws Exception {
    origin.addSimpleChange(/*timestamp*/
    42);
    Workflow<?, ?> workflow = iterativeWorkflow("deadbeef");
    CannotResolveRevisionException thrown = assertThrows(CannotResolveRevisionException.class, () -> workflow.run(workdir, ImmutableList.of(HEAD)));
    assertThat(thrown).hasMessageThat().contains("Could not resolve --last-rev flag. Please make sure it exists in the origin:" + " deadbeef");
}
Also used : CannotResolveRevisionException(com.google.copybara.exception.CannotResolveRevisionException) Test(org.junit.Test)

Example 9 with CannotResolveRevisionException

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

the class GerritOriginTest method testReferencePatchSetNotFound.

@Test
public void testReferencePatchSetNotFound() throws RepoException, ValidationException {
    CannotResolveRevisionException thrown = assertThrows(CannotResolveRevisionException.class, () -> origin.resolve("http://foo.com/#/c/12345/42"));
    assertThat(thrown).hasMessageThat().contains("Cannot find patch set 42");
}
Also used : CannotResolveRevisionException(com.google.copybara.exception.CannotResolveRevisionException) Test(org.junit.Test)

Example 10 with CannotResolveRevisionException

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

the class GerritOriginTest method testReferenceNotFound.

@Test
public void testReferenceNotFound() throws RepoException, ValidationException {
    CannotResolveRevisionException thrown = assertThrows(CannotResolveRevisionException.class, () -> origin.resolve("54321"));
    assertThat(thrown).hasMessageThat().contains("Cannot find change number 54321");
}
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