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.ShowChangedFilesBetweenCommits.java

License:Apache License

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

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

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

License:Apache License

public static void main(String[] args) throws IOException, GitAPIException {
    try (Repository repository = CookbookHelper.openJGitCookbookRepository()) {
        // the diff works on TreeIterators, we prepare two for the two branches
        AbstractTreeIterator oldTreeParser = prepareTreeParser(repository,
                "09c65401f3730eb3e619c33bf31e2376fb393727");
        AbstractTreeIterator newTreeParser = prepareTreeParser(repository,
                "aa31703b65774e4a06010824601e56375a70078c");

        // then the procelain diff-command returns a list of diff entries
        try (Git git = new Git(repository)) {
            List<DiffEntry> diff = git.diff().setOldTree(oldTreeParser).setNewTree(newTreeParser)
                    .setPathFilter(PathFilter.create("README.md")).call();
            for (DiffEntry entry : diff) {
                System.out.println(
                        "Entry: " + entry + ", from: " + entry.getOldId() + ", to: " + entry.getNewId());
                try (DiffFormatter formatter = new DiffFormatter(System.out)) {
                    formatter.setRepository(repository);
                    formatter.format(entry);
                }//from w  w w.  j a  v  a 2s  .  c o  m
            }
        }
    }
}

From source file:edu.nju.cs.inform.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 www.  j ava 2s. c  o  m
            }
            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().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");
        }
    }
}

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

License:Apache License

public static void main(String[] args) throws IOException, GitAPIException {
    try (Repository repository = CookbookHelper.openJGitCookbookRepository()) {
        try (Git git = new Git(repository)) {
            Status status = git.status().call();
            System.out.println("Added: " + status.getAdded());
            System.out.println("Changed: " + status.getChanged());
            System.out.println("Conflicting: " + status.getConflicting());
            System.out.println("ConflictingStageState: " + status.getConflictingStageState());
            System.out.println("IgnoredNotInIndex: " + status.getIgnoredNotInIndex());
            System.out.println("Missing: " + status.getMissing());
            System.out.println("Modified: " + status.getModified());
            System.out.println("Removed: " + status.getRemoved());
            System.out.println("Untracked: " + status.getUntracked());
            System.out.println("UntrackedFolders: " + status.getUntrackedFolders());
        }/*  w  w w  .  j av a2 s .  c om*/
    }
}

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

License:Apache License

public static void main(String[] args) throws IOException, InvalidRefNameException, GitAPIException {
    try (Repository repository = CookbookHelper.openJGitCookbookRepository()) {
        try (Git git = new Git(repository)) {
            Iterable<RevCommit> commits = git.log().all().call();
            int count = 0;
            for (RevCommit commit : commits) {
                System.out.println("LogCommit: " + commit);
                count++;//  w ww. j a  v  a  2 s. c  om
            }
            System.out.println(count);
        }
    }
}

From source file:edu.nju.cs.inform.jgit.unfinished.ListRefLog.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> refs = git.branchList().call();
            for (Ref ref : refs) {
                System.out.println("Branch: " + ref + " " + ref.getName() + " " + ref.getObjectId().getName());

                listReflog(repository, ref);
            }/*from   w  w w . j a  va  2s.  c  o m*/

            List<Ref> call = git.tagList().call();
            for (Ref ref : call) {
                System.out.println("Tag: " + ref + " " + ref.getName() + " " + ref.getObjectId().getName());

                listReflog(repository, ref);
            }
        }
    }
}

From source file:edu.nju.cs.inform.jgit.unfinished.ListRefLog.java

License:Apache License

private static void listReflog(Repository repository, Ref ref) throws GitAPIException, InvalidRefNameException {
    /*// www .ja  va  2s.  com
     * Ref head = repository.getRef(ref.getName());
     * RevWalk walk = new RevWalk(repository);
     * RevCommit commit = walk.parseCommit(head.getObjectId());
     */

    try (Git git = new Git(repository)) {
        Collection<ReflogEntry> call = git.reflog().setRef(ref.getName()).call();
        Iterator<ReflogEntry> it = call.iterator();
        while (it.hasNext()) {
            ReflogEntry reflog = it.next();
            System.out.println("Reflog: " + reflog);
        }
    }
}

From source file:edu.nju.cs.inform.jgit.unfinished.PullFromRemoteRepository.java

License:Apache License

public static void main(String[] args)
        throws IOException, InvalidRemoteException, TransportException, GitAPIException {
    // prepare a new folder for the cloned repository
    File localPath = File.createTempFile("TestGitRepository", "");
    localPath.delete();/*  w  w  w. ja v  a2  s .com*/

    // then clone
    System.out.println("Cloning from " + REMOTE_URL + " to " + localPath);
    try (Git result = Git.cloneRepository().setURI(REMOTE_URL).setDirectory(localPath).call()) {
        // Note: the call() returns an opened repository already which needs to be closed to avoid file handle leaks!
        System.out.println("Having repository: " + result.getRepository().getDirectory());
        try (Git git = new Git(result.getRepository())) {
            git.pull().call();
        }

        System.out.println("Pulled from remote repository to local repository at "
                + result.getRepository().getDirectory());
    }
}

From source file:edu.nju.cs.inform.jgit.unfinished.PullRemoteRepository.java

License:Apache License

public static void main(String[] args)
        throws IOException, InvalidRemoteException, TransportException, GitAPIException {
    try (Repository repository = cloneRepository()) {
        System.out.println("Having repository: " + repository.getDirectory() + " with head: "
                + repository.getRef(Constants.HEAD) + "/" + repository.resolve("HEAD") + "/"
                + repository.resolve("refs/heads/master"));

        // TODO: why do we get null here for HEAD?!? See also
        // http://stackoverflow.com/questions/17979660/jgit-pull-noheadexception

        try (Git git = new Git(repository)) {
            PullResult call = git.pull().call();

            System.out.println("Pulled from the remote repository: " + call);
        }//from   w  w  w  .ja v  a 2s.  com
    }
}

From source file:edu.nju.cs.inform.jgit.unfinished.PushToRemoteRepository.java

License:Apache License

public static void main(String[] args)
        throws IOException, InvalidRemoteException, TransportException, GitAPIException {
    // prepare a new folder for the cloned repository
    File localPath = File.createTempFile("TestGitRepository", "");
    localPath.delete();//from   w w  w . ja v  a  2 s  .  c om

    // then clone
    System.out.println("Cloning from " + REMOTE_URL + " to " + localPath);
    try (Git result = Git.cloneRepository().setURI(REMOTE_URL).setDirectory(localPath).call()) {
        // prepare a second folder for the 2nd clone
        File localPath2 = File.createTempFile("TestGitRepository", "");
        localPath2.delete();

        // then clone again
        System.out.println("Cloning from file://" + localPath + " to " + localPath2);
        try (Git result2 = Git.cloneRepository().setURI("file://" + localPath).setDirectory(localPath2)
                .call()) {
            // now open the created repository
            FileRepositoryBuilder builder = new FileRepositoryBuilder();
            try (Repository repository = builder.setGitDir(localPath2).readEnvironment() // scan environment GIT_* variables
                    .findGitDir() // scan up the file system tree
                    .build()) {
                try (Git git = new Git(repository)) {
                    git.push().call();
                }

                System.out.println("Pushed from repository: " + repository.getDirectory()
                        + " to remote repository at " + REMOTE_URL);
            }
        }
    }
}