Search in sources :

Example 1 with GitEnvironment

use of com.google.copybara.git.GitEnvironment in project copybara by google.

the class PatchingOptions method patchWithGitApply.

private static void patchWithGitApply(Path rootDir, byte[] diffContents, ImmutableList<String> excludedPaths, int stripSlashes, boolean verbose, boolean reverse, Map<String, String> environment, @Nullable Path gitDir) throws IOException, InsideGitDirException {
    GitEnvironment gitEnv = new GitEnvironment(environment);
    if (gitDir == null) {
        checkNotInsideGitRepo(rootDir, verbose, gitEnv);
    }
    ImmutableList.Builder<String> params = ImmutableList.builder();
    // Show verbose output unconditionally since it is helpful for debugging issues with patches.
    params.add(gitEnv.resolveGitBinary());
    if (gitDir != null) {
        params.add("--git-dir=" + gitDir.normalize().toAbsolutePath());
    }
    params.add("apply", "-v", "--stat", "--apply", "-p" + stripSlashes);
    if (gitDir != null) {
        params.add("--3way");
    }
    for (String excludedPath : excludedPaths) {
        params.add("--exclude", excludedPath);
    }
    if (reverse) {
        params.add("-R");
    }
    params.add("-");
    Command cmd = new Command(params.build().toArray(new String[0]), environment, rootDir.toFile());
    try {
        new CommandRunner(cmd).withVerbose(verbose).withInput(diffContents).execute();
    } catch (BadExitStatusWithOutputException e) {
        throw new IOException(String.format("Error executing 'git apply': %s. Stderr: \n%s", e.getMessage(), e.getOutput().getStderr()), e);
    } catch (CommandException e) {
        throw new IOException("Error executing 'git apply'", e);
    }
}
Also used : BadExitStatusWithOutputException(com.google.copybara.util.BadExitStatusWithOutputException) Command(com.google.copybara.shell.Command) ImmutableList(com.google.common.collect.ImmutableList) GitEnvironment(com.google.copybara.git.GitEnvironment) IOException(java.io.IOException) CommandException(com.google.copybara.shell.CommandException) CommandRunner(com.google.copybara.util.CommandRunner)

Example 2 with GitEnvironment

use of com.google.copybara.git.GitEnvironment in project copybara by google.

the class MigrateCmdTest method setUp.

@Before
public void setUp() throws Exception {
    console = new TestingConsole();
    temp = Files.createTempDirectory("temp");
    optionsBuilder = new OptionsBuilder();
    optionsBuilder.setConsole(console).setOutputRootToTmpDir();
    optionsBuilder.setForce(true);
    Path userHomeForTest = Files.createTempDirectory("home");
    optionsBuilder.setEnvironment(GitTestUtil.getGitEnv().getEnvironment());
    optionsBuilder.setHomeDir(userHomeForTest.toString());
    eventMonitor = new TestingEventMonitor();
    optionsBuilder.general.enableEventMonitor("just testing", eventMonitor);
    outPut = optionsBuilder.general.getOutputRoot();
    optionsBuilder.general.starlarkMode = StarlarkMode.STRICT.name();
    remote = Files.createTempDirectory("remote");
    repo = GitRepository.newRepo(/*verbose*/
    true, remote, new GitEnvironment(optionsBuilder.general.getEnvironment())).init();
    primaryBranch = repo.getPrimaryBranch();
    Files.createDirectories(remote.resolve("include"));
    Files.write(remote.resolve("include/fileA.txt"), new byte[0]);
    git(remote, "add", "include/fileA.txt");
    git(remote, "commit", "-m", "not include");
    optionsBuilder.setLastRevision(repo.parseRef("HEAD"));
    git(remote, "checkout", primaryBranch);
    Files.write(remote.resolve("include/mainline-file.txt"), new byte[0]);
    git(remote, "add", "include/mainline-file.txt");
    git(remote, "commit", "-m", "message_a!");
    optionsBuilder.general.dryRunMode = true;
    url = "file://" + remote.toFile().getAbsolutePath();
    writeFile(remote, "test.txt", "some content");
    writeFile(remote, "testA.txt", "some content");
    repo.add().files("test.txt", "testA.txt").run();
    git(remote, "commit", "-m", "first file", "--date", COMMIT_TIME);
    optionsBuilder.setForce(true);
    RecordsProcessCallDestination destination = new RecordsProcessCallDestination();
    optionsBuilder.testingOptions.destination = destination;
    skylark = new SkylarkTestExecutor(optionsBuilder);
}
Also used : Path(java.nio.file.Path) RecordsProcessCallDestination(com.google.copybara.testing.RecordsProcessCallDestination) TestingConsole(com.google.copybara.util.console.testing.TestingConsole) GitEnvironment(com.google.copybara.git.GitEnvironment) OptionsBuilder(com.google.copybara.testing.OptionsBuilder) SkylarkTestExecutor(com.google.copybara.testing.SkylarkTestExecutor) TestingEventMonitor(com.google.copybara.testing.TestingEventMonitor) Before(org.junit.Before)

Aggregations

GitEnvironment (com.google.copybara.git.GitEnvironment)2 ImmutableList (com.google.common.collect.ImmutableList)1 Command (com.google.copybara.shell.Command)1 CommandException (com.google.copybara.shell.CommandException)1 OptionsBuilder (com.google.copybara.testing.OptionsBuilder)1 RecordsProcessCallDestination (com.google.copybara.testing.RecordsProcessCallDestination)1 SkylarkTestExecutor (com.google.copybara.testing.SkylarkTestExecutor)1 TestingEventMonitor (com.google.copybara.testing.TestingEventMonitor)1 BadExitStatusWithOutputException (com.google.copybara.util.BadExitStatusWithOutputException)1 CommandRunner (com.google.copybara.util.CommandRunner)1 TestingConsole (com.google.copybara.util.console.testing.TestingConsole)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 Before (org.junit.Before)1