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);
}
}
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);
}
Aggregations