Example usage for org.eclipse.jgit.api Git Git

List of usage examples for org.eclipse.jgit.api Git Git

Introduction

In this page you can find the example usage for org.eclipse.jgit.api Git Git.

Prototype

public Git(Repository repo) 

Source Link

Document

Construct a new org.eclipse.jgit.api.Git object which can interact with the specified git repository.

Usage

From source file:com.buildautomation.jgit.api.FetchRemoteCommits.java

License:Apache License

public static void fetchRemoteCommits() throws IOException, GitAPIException {
    try (Repository repository = CookbookHelper.openJGitCookbookRepository()) {
        System.out.println("Starting fetch");
        try (Git git = new Git(repository)) {
            FetchResult result = git.fetch().setCheckFetchedObjects(true).call();
            System.out.println("Messages: " + result.getMessages());
        }/* w  ww.ja v a2s .  c  o  m*/
    }
}

From source file:com.buildautomation.jgit.api.ListBranches.java

License:Apache License

public static void listBranches() throws IOException, GitAPIException {
    try (Repository repository = CookbookHelper.openJGitCookbookRepository()) {
        System.out.println("Listing local branches:");
        try (Git git = new Git(repository)) {
            List<Ref> call = git.branchList().call();
            for (Ref ref : call) {
                System.out.println("Branch: " + ref + " " + ref.getName() + " " + ref.getObjectId().getName());
            }//w w w .  j a  v  a2s  .com

            System.out.println("Now including remote branches:");
            call = git.branchList().setListMode(ListMode.ALL).call();
            for (Ref ref : call) {
                System.out.println("Branch: " + ref + " " + ref.getName() + " " + ref.getObjectId().getName());
            }
        }
    }
}

From source file:com.buildautomation.jgit.api.ListNotes.java

License:Apache License

public static void listNotes() throws IOException, GitAPIException {
    try (Repository repository = CookbookHelper.openJGitCookbookRepository()) {
        try (Git git = new Git(repository)) {
            List<Note> call = git.notesList().call();
            System.out.println("Listing " + call.size() + " notes");
            for (Note note : call) {
                System.out.println("Note: " + note + " " + note.getName() + " " + note.getData().getName()
                        + "\nContent: ");

                // displaying the contents of the note is done via a simple blob-read
                ObjectLoader loader = repository.open(note.getData());
                loader.copyTo(System.out);
            }// w ww.ja va 2 s.  c o  m
        }
    }
}

From source file:com.buildautomation.jgit.api.ListRemotes.java

License:Apache License

public static void listRemotes() throws IOException, GitAPIException {
    try (Repository repository = CookbookHelper.openJGitCookbookRepository()) {
        // all refs
        try (Git git = new Git(repository)) {
            Collection<Ref> refs = git.lsRemote().call();
            for (Ref ref : refs) {
                System.out.println("Ref: " + ref);
            }/*from w  w w .j a  v a  2 s.c om*/

            // heads only
            refs = git.lsRemote().setHeads(true).call();
            for (Ref ref : refs) {
                System.out.println("Head: " + ref);
            }

            // tags only
            refs = git.lsRemote().setTags(true).call();
            for (Ref ref : refs) {
                System.out.println("Remote tag: " + ref);
            }
        }
    }
}

From source file:com.buildautomation.jgit.api.ListTags.java

License:Apache License

public static void listTags() throws IOException, GitAPIException {
    try (Repository repository = CookbookHelper.openJGitCookbookRepository()) {
        try (Git git = new Git(repository)) {
            List<Ref> call = git.tagList().call();
            for (Ref ref : call) {
                System.out.println("Tag: " + ref + " " + ref.getName() + " " + ref.getObjectId().getName());

                // fetch all commits for this tag
                LogCommand log = git.log();

                Ref peeledRef = repository.peel(ref);
                if (peeledRef.getPeeledObjectId() != null) {
                    log.add(peeledRef.getPeeledObjectId());
                } else {
                    log.add(ref.getObjectId());
                }/*from   ww  w.ja  v  a  2s. c om*/

                Iterable<RevCommit> logs = log.call();
                for (RevCommit rev : logs) {
                    System.out.println("Commit: "
                            + rev /* + ", name: " + rev.getName() + ", id: " + rev.getId().getName() */);
                }
            }
        }
    }
}

From source file:com.buildautomation.jgit.api.ListUncommittedChanges.java

License:Apache License

public static void listUncommittedChanges() throws IOException, GitAPIException {
    try (Repository repository = CookbookHelper.openJGitCookbookRepository()) {
        System.out.println("Listing uncommitted changes:");
        try (Git git = new Git(repository)) {
            Status status = git.status().call();
            Set<String> conflicting = status.getConflicting();
            for (String conflict : conflicting) {
                System.out.println("Conflicting: " + conflict);
            }/*from   w  w  w  .j  a  va 2 s  . c  o  m*/

            Set<String> added = status.getAdded();
            for (String add : added) {
                System.out.println("Added: " + add);
            }

            Set<String> changed = status.getChanged();
            for (String change : changed) {
                System.out.println("Change: " + change);
            }

            Set<String> missing = status.getMissing();
            for (String miss : missing) {
                System.out.println("Missing: " + miss);
            }

            Set<String> modified = status.getModified();
            for (String modify : modified) {
                System.out.println("Modification: " + modify);
            }

            Set<String> removed = status.getRemoved();
            for (String remove : removed) {
                System.out.println("Removed: " + remove);
            }

            Set<String> uncommittedChanges = status.getUncommittedChanges();
            for (String uncommitted : uncommittedChanges) {
                System.out.println("Uncommitted: " + uncommitted);
            }

            Set<String> untracked = status.getUntracked();
            for (String untrack : untracked) {
                System.out.println("Untracked: " + untrack);
            }

            Set<String> untrackedFolders = status.getUntrackedFolders();
            for (String untrack : untrackedFolders) {
                System.out.println("Untracked Folder: " + untrack);
            }

            Map<String, StageState> conflictingStageState = status.getConflictingStageState();
            for (Map.Entry<String, StageState> entry : conflictingStageState.entrySet()) {
                System.out.println("ConflictingState: " + entry);
            }
        }
    }
}

From source file:com.buildautomation.jgit.api.RebaseToOriginMaster.java

License:Apache License

public static void rebaseToOriginMaster() throws IOException, GitAPIException {
    try (Repository repository = CookbookHelper.openJGitCookbookRepository()) {
        // all refs
        try (Git git = new Git(repository)) {
            InteractiveHandler handler = new InteractiveHandler() {
                @Override//from  w w w  .ja v a  2  s .c  o m
                public void prepareSteps(List<RebaseTodoLine> steps) {
                    for (RebaseTodoLine step : steps) {
                        try {
                            step.setAction(Action.EDIT);
                        } catch (IllegalTodoFileModification e) {
                            throw new IllegalStateException(e);
                        }
                    }
                }

                @Override
                public String modifyCommitMessage(String oldMessage) {
                    return oldMessage;
                }
            };

            git.rebase().setUpstream("origin/master").runInteractively(handler).call();
            System.out.println("Rebased..");
        }
    }
}

From source file:com.buildautomation.jgit.api.ShowBranchDiff.java

License:Apache License

public static void showBranchDiff() throws IOException, GitAPIException {
    try (Repository repository = CookbookHelper.openJGitCookbookRepository()) {
        try (Git git = new Git(repository)) {
            if (repository.exactRef("refs/heads/testbranch") == null) {
                // first we need to ensure that the remote branch is visible locally
                Ref ref = git.branchCreate().setName("testbranch").setStartPoint("origin/testbranch").call();

                System.out.println("Created local testbranch with ref: " + ref);
            }/*ww  w.  j  av a  2 s. c o m*/

            // the diff works on TreeIterators, we prepare two for the two branches
            AbstractTreeIterator oldTreeParser = prepareTreeParser(repository, "refs/heads/testbranch");
            AbstractTreeIterator newTreeParser = prepareTreeParser(repository, "refs/heads/master");

            // then the procelain diff-command returns a list of diff entries
            List<DiffEntry> diff = git.diff().setOldTree(oldTreeParser).setNewTree(newTreeParser).call();
            for (DiffEntry entry : diff) {
                System.out.println("Entry: " + entry);
            }
        }
    }
}

From source file:com.buildautomation.jgit.api.ShowBranchTrackingStatus.java

License:Apache License

public static void showBranchTrackingStatus() throws IOException, GitAPIException {
    try (Repository repository = CookbookHelper.openJGitCookbookRepository()) {
        try (Git git = new Git(repository)) {
            List<Ref> call = git.branchList().call();
            for (Ref ref : call) {
                List<Integer> counts = getCounts(repository, ref.getName());
                System.out.println("For branch: " + ref.getName());
                System.out.println("Commits ahead : " + counts.get(0));
                System.out.println("Commits behind : " + counts.get(1));
                System.out.println();
            }/*from  w  ww  .j av  a  2s .  c o m*/
        }
    }
}

From source file:com.buildautomation.jgit.api.ShowChangedFilesBetweenCommits.java

License:Apache License

public static void showChangedFilesBetweenCommits() throws IOException, GitAPIException {
    try (Repository repository = CookbookHelper.openJGitCookbookRepository()) {
        // The {tree} will return the underlying tree-id instead of the commit-id itself!
        // For a description of what the carets do see e.g. http://www.paulboxley.com/blog/2011/06/git-caret-and-tilde
        // This means we are selecting the parent of the parent of the parent of the parent of current HEAD and
        // take the tree-ish of it
        ObjectId oldHead = repository.resolve("HEAD^^^^{tree}");
        ObjectId head = repository.resolve("HEAD^{tree}");

        System.out.println("Printing diff between tree: " + oldHead + " and " + head);

        // prepare the two iterators to compute the diff between
        try (ObjectReader reader = repository.newObjectReader()) {
            CanonicalTreeParser oldTreeIter = new CanonicalTreeParser();
            oldTreeIter.reset(reader, oldHead);
            CanonicalTreeParser newTreeIter = new CanonicalTreeParser();
            newTreeIter.reset(reader, head);

            // finally get the list of changed files
            try (Git git = new Git(repository)) {
                List<DiffEntry> diffs = git.diff().setNewTree(newTreeIter).setOldTree(oldTreeIter).call();
                for (DiffEntry entry : diffs) {
                    System.out.println("Entry: " + entry);
                }/*  ww w .  j a v  a  2  s .c om*/
            }
        }
    }

    System.out.println("Done");
}