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.amazonaws.eclipse.elasticbeanstalk.git.AWSGitPushCommand.java

License:Open Source License

public void execute() throws CoreException {
    Iterable<PushResult> pushResults = null;
    try {//from   ww  w  .ja va2 s.  c o  m
        Repository repository = initializeRepository();

        // Add the new files
        clearOutRepository(repository);
        extractZipFile(archiveFile, repoLocation);
        commitChanges(repository, "Incremental Deployment: " + new Date().toString());

        // Push to AWS
        String remoteUrl = getRemoteUrl();

        if (log.isLoggable(Level.FINE))
            log.fine("Pushing to: " + remoteUrl);
        PushCommand pushCommand = new Git(repository).push().setRemote(remoteUrl).setForce(true).add("master");
        // TODO: we could use a ProgressMonitor here for reporting status back to the UI
        pushResults = pushCommand.call();
    } catch (Throwable t) {
        throwCoreException(null, t);
    }

    for (PushResult pushResult : pushResults) {
        String messages = pushResult.getMessages();
        if (messages != null && messages.trim().length() > 0) {
            throwCoreException(messages, null);
        }
    }
}

From source file:com.amazonaws.eclipse.elasticbeanstalk.git.AWSGitPushCommand.java

License:Open Source License

private void commitChanges(Repository repository, String message) throws GitAPIException, IOException {
    Git git = new Git(repository);
    // Add once (without the update flag) to add new and modified files
    git.add().addFilepattern(".").call();
    // Then again with the update flag to add deleted and modified files
    git.add().addFilepattern(".").setUpdate(true).call();

    git.commit().setMessage(message).call();
}

From source file:com.amd.gerrit.plugins.manifestsubscription.VersionedManifests.java

License:Open Source License

static void tagManifest(GitRepositoryManager gitRepoManager, Manifest manifest, final String tag)
        throws GitAPIException, IOException {
    Table<String, String, String> lookup = HashBasedTable.create();
    String defaultRef = null;/*from  ww  w .j a  v  a 2s  . c o  m*/

    if (manifest.getDefault() != null) {
        defaultRef = manifest.getDefault().getRevision();
    }

    ManifestOp op = new ManifestOp() {
        @Override
        public boolean apply(com.amd.gerrit.plugins.manifestsubscription.manifest.Project project, String hash,
                String name, GitRepositoryManager gitRepoManager) throws GitAPIException, IOException {
            Project.NameKey p = new Project.NameKey(project.getName());
            try (Repository db = gitRepoManager.openRepository(p);
                    Git git = new Git(db);
                    RevWalk walk = new RevWalk(db)) {
                RevCommit commit = walk.parseCommit(db.resolve(hash));
                git.tag().setName(tag).setObjectId(commit).setAnnotated(true).call();
            }
            return true;
        }
    };

    traverseManifestAndApplyOp(gitRepoManager, manifest.getProject(), defaultRef, op, lookup);
}

From source file:com.amd.gerrit.plugins.manifestsubscription.VersionedManifests.java

License:Open Source License

static void branchManifest(GitRepositoryManager gitRepoManager, Manifest manifest, final String branch,
        final ChangeHooks changeHooks) throws GitAPIException, IOException {
    Table<String, String, String> lookup = HashBasedTable.create();
    String defaultRef = null;/*w  ww.java 2 s.c o  m*/

    if (manifest.getDefault() != null) {
        defaultRef = manifest.getDefault().getRevision();
    }

    ManifestOp op = new ManifestOp() {
        @Override
        public boolean apply(com.amd.gerrit.plugins.manifestsubscription.manifest.Project project, String hash,
                String name, GitRepositoryManager gitRepoManager) throws GitAPIException, IOException {
            Project.NameKey p = new Project.NameKey(project.getName());
            try (Repository db = gitRepoManager.openRepository(p); Git git = new Git(db)) {
                try {
                    Ref r = git.branchCreate().setName(branch).setStartPoint(hash).call();
                    changeHooks.doRefUpdatedHook(new Branch.NameKey(p, branch), ObjectId.zeroId(),
                            r.getObjectId(), null);
                } catch (Exception e) {

                }
            }
            return true;
        }
    };

    traverseManifestAndApplyOp(gitRepoManager, manifest.getProject(), defaultRef, op, lookup);
}

From source file:com.bb.extensions.plugin.unittests.internal.git.GitWrapper.java

License:Open Source License

/**
 * Clones the git repository located at the given url to the given path.
 * //from www  .  j av  a  2  s .c  o m
 * @param path
 * 
 * @param url
 * @return The GitWrapper to interact with the clone git repository.
 * @throws IllegalArgumentException
 */
public static GitWrapper cloneRepository(String path, String url) throws IllegalArgumentException {
    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    File gitDir = new File(path);
    if (gitDir.exists() == false) {
        gitDir.mkdir();
    } else if (gitDir.list().length > 0) {
        if (isGitRepository(gitDir)) {
            return openRepository(path);
        } else {
            throw new IllegalArgumentException("Cannot clone to a non-empty directory");
        }
    }
    Repository repository;
    try {
        repository = builder.setGitDir(gitDir).readEnvironment().findGitDir().build();
        GitWrapper gitWrapper = new GitWrapper();
        gitWrapper.git = new Git(repository);
        CloneCommand clone = Git.cloneRepository();
        clone.setBare(false);
        clone.setCloneAllBranches(true);
        clone.setDirectory(gitDir).setURI(url);
        // we have to close the newly returned Git object as call() creates
        // a new one every time
        clone.call().getRepository().close();
        return gitWrapper;
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InvalidRemoteException e) {
        e.printStackTrace();
    } catch (TransportException e) {
        e.printStackTrace();
    } catch (GitAPIException e) {
        e.printStackTrace();
    }

    return null;
}

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

License:Apache License

public static void addAndListNoteOfCommit() 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;
                    }/*from   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:com.buildautomation.jgit.api.CollectGarbage.java

License:Apache License

public static void collectGarbage() 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());
            }/*from w  ww. j a  v a 2s  . c o  m*/
        }
    }
}

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

License:Apache License

public static void createAndDeleteBranch() 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  www. j av a2s  .c  o m

            // 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:com.buildautomation.jgit.api.CreateArchive.java

License:Apache License

private 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();
        }/* w ww.ja v  a  2 s.  com*/
    }

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

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

License:Apache License

public static void createListApplyAndDropStash() 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());
            }/*from  w  w  w  . j ava2s.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);
        }
    }
}