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:edu.nju.cs.inform.jgit.porcelain.CreateCustomFormatArchive.java

License:Apache License

public static void main(String[] args) throws IOException, GitAPIException {
    try (Repository repository = CookbookHelper.openJGitCookbookRepository()) {
        File file = File.createTempFile("test", ".mzip");
        // make the archive format known
        ArchiveCommand.registerFormat("myzip", new ZipArchiveFormat());
        try {/* w  w w.j  a va2s. c  om*/
            // this is the file that we write the archive to
            try (OutputStream out = new FileOutputStream(file)) {
                // finally call the ArchiveCommand to write out using the given format
                try (Git git = new Git(repository)) {
                    git.archive().setTree(repository.resolve("master")).setFormat("myzip").setOutputStream(out)
                            .call();
                }
            }
        } finally {
            ArchiveCommand.unregisterFormat("myzip");
        }

        System.out.println("Wrote " + file.length() + " bytes to " + file);
    }
}

From source file:edu.nju.cs.inform.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");
            File file2 = new File(repository.getDirectory().getParent(), "testfile2");
            FileUtils.writeStringToFile(file2, "some text");

            // 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", 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", 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. j a v a  2 s.c  o 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:edu.nju.cs.inform.jgit.porcelain.FetchRemoteCommits.java

License:Apache License

public static void main(String[] args) 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());
        }/*from ww  w. j  a  va2s  .c  o m*/
    }
}

From source file:edu.nju.cs.inform.jgit.porcelain.ListBranches.java

License:Apache License

public static void main(String[] args) 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());
            }/*from   w ww  .j a va2 s. c  o  m*/

            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:edu.nju.cs.inform.jgit.porcelain.ListNotes.java

License:Apache License

public static void main(String[] args) 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);
            }//www  . j av a  2s .  c  o  m
        }
    }
}

From source file:edu.nju.cs.inform.jgit.porcelain.ListRemotes.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)) {
            Collection<Ref> refs = git.lsRemote().call();
            for (Ref ref : refs) {
                System.out.println("Ref: " + ref);
            }/*from   w  w  w. jav  a 2 s  . co m*/

            // 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:edu.nju.cs.inform.jgit.porcelain.ListTags.java

License:Apache License

public static void main(String[] args) 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   w w  w  . j av  a 2  s. co m

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

From source file:edu.nju.cs.inform.jgit.porcelain.ListUncommittedChanges.java

License:Apache License

public static void main(String[] args) 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  ww . ja  va2  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:edu.nju.cs.inform.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 ava 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:edu.nju.cs.inform.jgit.porcelain.ShowBranchDiff.java

License:Apache License

public static void main(String[] args) 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);
            }//from w w w  .  j a  v  a2 s .co 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);
            }
        }
    }
}