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.api.ShowBranchTrackingStatus.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.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();
            }// w ww  . ja  va 2  s.  c om
        }
    }
}

From source file:edu.nju.cs.inform.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");
        myfile.createNewFile();//from ww  w  .  ja va  2s . 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());

        File dir = repository.getDirectory();

        return dir;
    }
}

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

License:Apache License

public static void main(String[] args) throws IOException, GitAPIException {
    try (Repository repository = CookbookHelper.openJGitCookbookRepository()) {
        Ref head = repository.exactRef("refs/heads/master");
        System.out.println("Found head: " + head);

        try (RevWalk walk = new RevWalk(repository)) {
            RevCommit commit = walk.parseCommit(head.getObjectId());
            System.out.println("Found Commit: " + commit);

            try (Git git = new Git(repository)) {
                git.notesAdd().setMessage("some note message").setObjectId(commit).call();
                System.out.println("Added Note to commit " + commit);

                List<Note> call = git.notesList().call();
                System.out.println("Listing " + call.size() + " notes");
                for (Note note : call) {
                    // check if we found the note for this commit
                    if (!note.getName().equals(head.getObjectId().getName())) {
                        System.out.println("Note " + note + " did not match commit " + head);
                        continue;
                    }// w w w .j av a  2 s . c o  m
                    System.out.println("Found note: " + note + " for commit " + head);

                    // displaying the contents of the note is done via a simple blob-read
                    ObjectLoader loader = repository.open(note.getData());
                    loader.copyTo(System.out);
                }
            }

            walk.dispose();
        }
    }
}

From source file:edu.nju.cs.inform.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");
            myfile.createNewFile();//from w w w  .  j ava2  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:edu.nju.cs.inform.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());
        untrackedDir.delete();/*from w  ww .  j  av a  2 s.  c  o m*/
        untrackedDir.mkdirs();

        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:edu.nju.cs.inform.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().call();
            for (Map.Entry<Object, Object> entry : ret.entrySet()) {
                System.out.println("Ret: " + entry.getKey() + ": " + entry.getValue());
            }/*  ww  w  .  j  a v a  2 s.  c o  m*/
        }
    }
}

From source file:edu.nju.cs.inform.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");
            myfile.createNewFile();/*from   www .  ja va  2s . c o  m*/

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

License:Apache License

public static void main(String[] args) throws IOException, GitAPIException {
    // prepare test-repository
    try (Repository repository = CookbookHelper.openJGitCookbookRepository()) {
        try (Git git = new Git(repository)) {
            List<Ref> call = git.branchList().call();
            for (Ref ref : call) {
                System.out.println(
                        "Branch-Before: " + ref + " " + ref.getName() + " " + ref.getObjectId().getName());
            }//from   w w w. j ava  2  s.  com

            // make sure the branch is not there
            List<Ref> refs = git.branchList().call();
            for (Ref ref : refs) {
                System.out.println("Had branch: " + ref.getName());
                if (ref.getName().equals("refs/heads/testbranch")) {
                    System.out.println("Removing branch before");
                    git.branchDelete().setBranchNames("testbranch").setForce(true).call();

                    break;
                }
            }

            // run the add-call
            git.branchCreate().setName("testbranch").call();

            call = git.branchList().call();
            for (Ref ref : call) {
                System.out.println(
                        "Branch-Created: " + ref + " " + ref.getName() + " " + ref.getObjectId().getName());
            }

            // run the delete-call
            git.branchDelete().setBranchNames("testbranch").call();

            call = git.branchList().call();
            for (Ref ref : call) {
                System.out.println(
                        "Branch-After: " + ref + " " + ref.getName() + " " + ref.getObjectId().getName());
            }
        }
    }
}

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

License:Apache License

public static void main(String[] args) throws IOException, GitAPIException {
    // prepare test-repository
    try (Repository repository = CookbookHelper.openJGitCookbookRepository()) {
        try (Git git = new Git(repository)) {
            // remove the tag before creating it
            git.tagDelete().setTags("tag_for_testing").call();

            // set it on the current HEAD
            Ref tag = git.tag().setName("tag_for_testing").call();
            System.out.println("Created/moved tag " + tag + " to repository at " + repository.getDirectory());

            // remove the tag again
            git.tagDelete().setTags("tag_for_testing").call();

            // read some other commit and set the tag on it
            ObjectId id = repository.resolve("HEAD^");
            try (RevWalk walk = new RevWalk(repository)) {
                RevCommit commit = walk.parseCommit(id);
                tag = git.tag().setObjectId(commit).setName("tag_for_testing").call();
                System.out/*w w w .  ja va 2 s.  c o  m*/
                        .println("Created/moved tag " + tag + " to repository at " + repository.getDirectory());

                // remove the tag again
                git.tagDelete().setTags("tag_for_testing").call();

                // create an annotated tag
                tag = git.tag().setName("tag_for_testing").setAnnotated(true).call();
                System.out
                        .println("Created/moved tag " + tag + " to repository at " + repository.getDirectory());

                // remove the tag again
                git.tagDelete().setTags("tag_for_testing").call();

                walk.dispose();
            }
        }
    }
}

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

License:Apache License

public static void write(Repository repository, String suffix, String format)
        throws IOException, GitAPIException {
    // this is the file that we write the archive to
    File file = File.createTempFile("test", suffix);
    try (OutputStream out = new FileOutputStream(file)) {
        // finally call the ArchiveCommand to write out using the various supported formats
        try (Git git = new Git(repository)) {
            git.archive().setTree(repository.resolve("master")).setFormat(format).setOutputStream(out).call();
        }//ww w. j a va  2  s  . c  o  m
    }

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