Skip to content

Instantly share code, notes, and snippets.

@orekyuu
Created December 24, 2015 11:36
Show Gist options
  • Save orekyuu/84f66c351e952e552291 to your computer and use it in GitHub Desktop.
Save orekyuu/84f66c351e952e552291 to your computer and use it in GitHub Desktop.
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.MergeCommand;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.diff.DiffEntry;
import org.eclipse.jgit.diff.DiffFormatter;
import org.eclipse.jgit.lib.ObjectId;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.List;
public class JGit {
public static void main(String[] args) throws GitAPIException, IOException {
//git clone https://github.com/orekyuu/JavaBeamStudio.git
File test = new File("testrepo");
Git git;
if (test.exists()) {
git = Git.open(test);
} else {
git = Git.cloneRepository()
.setURI("https://github.com/orekyuu/JavaBeamStudio.git")
.setDirectory(test)
.call();
}
//git checkout develop
git.checkout().setName("develop").call();
ObjectId objectId = git.getRepository().resolve("remotes/origin/css-update");
//git merge --no-ff --no-commit css-update
git.merge()
.setFastForward(MergeCommand.FastForwardMode.NO_FF)
.setCommit(false)
.include(objectId)
.call();
//git diff ?
List<DiffEntry> call = git.diff().call();
//diffの差分が見たい
for (DiffEntry entry : call) {
try (ByteArrayOutputStream out = new ByteArrayOutputStream();
DiffFormatter formatter = new DiffFormatter(out)) {
formatter.setRepository(git.getRepository());
formatter.format(entry);// org.eclipse.jgit.errors.MissingObjectException: Missing blob 0ca666588a0f454e6fa94e4a86c8fdfa67986d5c
String diffText = new String(out.toByteArray());
System.out.println(diffText);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment