List of usage examples for org.eclipse.jgit.api Git Git
public Git(Repository repo)
From source file:org.craftercms.studio.impl.v1.repository.git.GitContentRepositoryHelper.java
License:Open Source License
public String commitFile(Repository repo, String site, String path, String comment, PersonIdent user) { String commitId = null;//from w w w .ja v a 2 s .co m String gitPath = getGitPath(path); Status status; try (Git git = new Git(repo)) { status = git.status().addPath(gitPath).call(); // TODO: SJ: Below needs more thought and refactoring to detect issues with git repo and report them if (status.hasUncommittedChanges() || !status.isClean()) { RevCommit commit; commit = git.commit().setOnly(gitPath).setAuthor(user).setCommitter(user).setMessage(comment) .call(); commitId = commit.getName(); } git.close(); } catch (GitAPIException e) { logger.error("error adding and committing file to git: site: " + site + " path: " + path, e); } return commitId; }
From source file:org.craftercms.studio.impl.v1.util.git.CherryPickCommandEx.java
License:Eclipse Distribution License
/** * Executes the {@code Cherry-Pick} command with all the options and * parameters collected by the setter methods (e.g. {@link #include(Ref)} of * this class. Each instance of this class should only be used for one * invocation of the command. Don't call this method twice on an instance. * * @return the result of the cherry-pick * @throws GitAPIException// w ww . j ava 2 s . c om * @throws WrongRepositoryStateException * @throws ConcurrentRefUpdateException * @throws UnmergedPathsException * @throws NoMessageException * @throws NoHeadException */ @Override public CherryPickResult call() throws GitAPIException { RevCommit newHead = null; List<Ref> cherryPickedRefs = new LinkedList<>(); checkCallable(); try (RevWalk revWalk = new RevWalk(repo)) { // get the head commit Ref headRef = repo.exactRef(Constants.HEAD); if (headRef == null) throw new NoHeadException(JGitText.get().commitOnRepoWithoutHEADCurrentlyNotSupported); newHead = revWalk.parseCommit(headRef.getObjectId()); // loop through all refs to be cherry-picked for (Ref src : commits) { // get the commit to be cherry-picked // handle annotated tags ObjectId srcObjectId = src.getPeeledObjectId(); if (srcObjectId == null) srcObjectId = src.getObjectId(); RevCommit srcCommit = revWalk.parseCommit(srcObjectId); Merger merger = strategy.newMerger(repo); if (merger.merge(newHead, srcCommit)) { if (AnyObjectId.equals(newHead.getTree().getId(), merger.getResultTreeId())) continue; DirCacheCheckout dco = new DirCacheCheckout(repo, newHead.getTree(), repo.lockDirCache(), merger.getResultTreeId()); dco.setFailOnConflict(true); dco.checkout(); if (!noCommit) newHead = new Git(getRepository()).commit().setMessage(srcCommit.getFullMessage()) .setReflogComment(reflogPrefix + " " //$NON-NLS-1$ + srcCommit.getShortMessage()) .setAuthor(srcCommit.getAuthorIdent()).setNoVerify(true).call(); cherryPickedRefs.add(src); } else { return CherryPickResult.CONFLICT; } } } catch (IOException e) { throw new JGitInternalException( MessageFormat.format(JGitText.get().exceptionCaughtDuringExecutionOfCherryPickCommand, e), e); } return new CherryPickResult(newHead, cherryPickedRefs); }
From source file:org.dstadler.jgit.OpenRepository.java
License:Apache License
private static File createSampleGitRepo() throws IOException, GitAPIException { try (Repository repository = CookbookHelper.createNewRepository()) { System.out.println("Temporary repository at " + repository.getDirectory()); // create the file File myfile = new File(repository.getDirectory().getParent(), "testfile"); if (!myfile.createNewFile()) { throw new IOException("Could not create file " + myfile); }// www. j av a2s . c o m // run the add-call try (Git git = new Git(repository)) { git.add().addFilepattern("testfile").call(); // and then commit the changes git.commit().setMessage("Added testfile").call(); } System.out.println("Added file " + myfile + " to repository at " + repository.getDirectory()); return repository.getDirectory(); } }
From source file:org.dstadler.jgit.porcelain.AddFile.java
License:Apache License
public static void main(String[] args) throws IOException, GitAPIException { // prepare a new test-repository try (Repository repository = CookbookHelper.createNewRepository()) { try (Git git = new Git(repository)) { // create the file File myfile = new File(repository.getDirectory().getParent(), "testfile"); if (!myfile.createNewFile()) { throw new IOException("Could not create file " + myfile); }/*from ww w . ja v a 2 s.co m*/ // run the add-call git.add().addFilepattern("testfile").call(); System.out.println("Added file " + myfile + " to repository at " + repository.getDirectory()); } } }
From source file:org.dstadler.jgit.porcelain.CleanUntrackedFiles.java
License:Apache License
public static void main(String[] args) throws IOException, GitAPIException { try (Repository repository = CookbookHelper.createNewRepository()) { System.out.println("Repository at " + repository.getWorkTree()); File untrackedFile = File.createTempFile("untracked", ".txt", repository.getWorkTree()); File untrackedDir = File.createTempFile("untrackedDir", "", repository.getWorkTree()); if (!untrackedDir.delete()) { throw new IOException("Could not delete file " + untrackedDir); }//from w w w .j ava2s . c om if (!untrackedDir.mkdirs()) { throw new IOException("Could not create directory " + untrackedDir); } System.out.println("Untracked exists: " + untrackedFile.exists() + " Dir: " + untrackedDir.exists() + "/" + untrackedDir.isDirectory()); try (Git git = new Git(repository)) { Set<String> removed = git.clean().setCleanDirectories(true).call(); for (String item : removed) { System.out.println("Removed: " + item); } System.out.println("Removed " + removed.size() + " items"); } System.out.println("Untracked after: " + untrackedFile.exists() + " Dir: " + untrackedDir.exists() + "/" + untrackedDir.isDirectory()); } }
From source file:org.dstadler.jgit.porcelain.CollectGarbage.java
License:Apache License
public static void main(String[] args) throws IOException, GitAPIException { try (Repository repository = CookbookHelper.openJGitCookbookRepository()) { try (Git git = new Git(repository)) { Properties ret = git.gc().setProgressMonitor(new PrintlnProgressMonitor()).call(); for (Map.Entry<Object, Object> entry : ret.entrySet()) { System.out.println("Ret: " + entry.getKey() + ": " + entry.getValue()); }/* w w w .jav a2 s .c o m*/ } } }
From source file:org.dstadler.jgit.porcelain.CommitFile.java
License:Apache License
public static void main(String[] args) throws IOException, GitAPIException { // prepare a new test-repository try (Repository repository = CookbookHelper.createNewRepository()) { try (Git git = new Git(repository)) { // create the file File myfile = new File(repository.getDirectory().getParent(), "testfile"); if (!myfile.createNewFile()) { throw new IOException("Could not create file " + myfile); }/* w w w . ja v a 2s . c om*/ // run the add git.add().addFilepattern("testfile").call(); // and then commit the changes git.commit().setMessage("Added testfile").call(); System.out.println("Committed file " + myfile + " to repository at " + repository.getDirectory()); } } }
From source file:org.dstadler.jgit.porcelain.CreateListApplyAndDropStash.java
License:Apache License
public static void main(String[] args) throws IOException, GitAPIException { // prepare a new test-repository try (Repository repository = CookbookHelper.createNewRepository()) { try (Git git = new Git(repository)) { // create a file File file1 = new File(repository.getDirectory().getParent(), "testfile"); FileUtils.writeStringToFile(file1, "some text", "UTF-8"); File file2 = new File(repository.getDirectory().getParent(), "testfile2"); FileUtils.writeStringToFile(file2, "some text", "UTF-8"); // add and commit the file git.add().addFilepattern("testfile").call(); git.add().addFilepattern("testfile2").call(); git.commit().setMessage("Added testfiles").call(); // then modify the file FileUtils.writeStringToFile(file1, "some more text", "UTF-8", true); // push the changes to a new stash RevCommit stash = git.stashCreate().call(); System.out.println("Created stash " + stash); // then modify the 2nd file FileUtils.writeStringToFile(file2, "some more text", "UTF-8", true); // push the changes to a new stash stash = git.stashCreate().call(); System.out.println("Created stash " + stash); // list the stashes Collection<RevCommit> stashes = git.stashList().call(); for (RevCommit rev : stashes) { System.out.println("Found stash: " + rev + ": " + rev.getFullMessage()); }// w w w. java 2 s .co m // drop the 1st stash without applying it ObjectId call = git.stashDrop().setStashRef(0).call(); System.out.println("StashDrop returned: " + call); ObjectId applied = git.stashApply().setStashRef(stash.getName()).call(); System.out.println("Applied 2nd stash as: " + applied); } } }
From source file:org.dstadler.jgit.porcelain.RebaseToOriginMaster.java
License:Apache License
public static void main(String[] args) throws IOException, GitAPIException { try (Repository repository = CookbookHelper.openJGitCookbookRepository()) { // all refs try (Git git = new Git(repository)) { InteractiveHandler handler = new InteractiveHandler() { @Override//w w w . j a va 2s . c o m public void prepareSteps(List<RebaseTodoLine> steps) { // the handler receives the list of commits that are rebased, i.e. the ones on the local branch for (RebaseTodoLine step : steps) { // for each step, you can decide which action should be taken // default is PICK try { // by selecting "EDIT", the rebase will stop and ask you to edit the commit-contents step.setAction(Action.EDIT); } catch (IllegalTodoFileModification e) { throw new IllegalStateException(e); } } } @Override public String modifyCommitMessage(String oldMessage) { return oldMessage; } }; RebaseResult result = git.rebase().setUpstream("origin/master").runInteractively(handler).call(); System.out.println("Rebase had state: " + result.getStatus() + ": " + result.getConflicts()); // because of the "EDIT" in the InteractiveHandler, the rebase was stopped in-between // now you can adjust the commit and continue rebasing with setOperation(RebaseCommand.Operation.CONTINUE) // to use the local changes for the commit or setOperation(RebaseCommand.Operation.SKIP) to skip this // commit (i.e. remove it from the branch!) if (!result.getStatus().isSuccessful()) { // if rebasing stopped or failed, you can get back to the original state by running it with setOperation(RebaseCommand.Operation.ABORT) result = git.rebase().setUpstream("origin/master").runInteractively(handler) .setOperation(RebaseCommand.Operation.ABORT).call(); System.out.println( "Aborted reabse with state: " + result.getStatus() + ": " + result.getConflicts()); } } } }
From source file:org.dstadler.jgit.porcelain.ShowLog.java
License:Apache License
@SuppressWarnings("unused") public static void main(String[] args) throws IOException, GitAPIException { try (Repository repository = CookbookHelper.openJGitCookbookRepository()) { try (Git git = new Git(repository)) { Iterable<RevCommit> logs = git.log().call(); int count = 0; for (RevCommit rev : logs) { //System.out.println("Commit: " + rev /* + ", name: " + rev.getName() + ", id: " + rev.getId().getName() */); count++;//from w ww .j ava 2 s . com } System.out.println("Had " + count + " commits overall on current branch"); logs = git.log().add(repository.resolve("remotes/origin/testbranch")).call(); count = 0; for (RevCommit rev : logs) { System.out.println( "Commit: " + rev /* + ", name: " + rev.getName() + ", id: " + rev.getId().getName() */); count++; } System.out.println("Had " + count + " commits overall on test-branch"); logs = git.log().not(repository.resolve("master")) .add(repository.resolve("remotes/origin/testbranch")).call(); count = 0; for (RevCommit rev : logs) { System.out.println( "Commit: " + rev /* + ", name: " + rev.getName() + ", id: " + rev.getId().getName() */); count++; } System.out.println("Had " + count + " commits only on test-branch"); logs = git.log().all().call(); count = 0; for (RevCommit rev : logs) { //System.out.println("Commit: " + rev /* + ", name: " + rev.getName() + ", id: " + rev.getId().getName() */); count++; } System.out.println("Had " + count + " commits overall in repository"); logs = git.log() // for all log.all() .addPath("README.md").call(); count = 0; for (RevCommit rev : logs) { //System.out.println("Commit: " + rev /* + ", name: " + rev.getName() + ", id: " + rev.getId().getName() */); count++; } System.out.println("Had " + count + " commits on README.md"); logs = git.log() // for all log.all() .addPath("pom.xml").call(); count = 0; for (RevCommit rev : logs) { //System.out.println("Commit: " + rev /* + ", name: " + rev.getName() + ", id: " + rev.getId().getName() */); count++; } System.out.println("Had " + count + " commits on pom.xml"); } } }