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:ShowFileDiff.java

License:Apache License

public static void main(String[] args) throws IOException, GitAPIException {
    Repository repository = null; //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
    List<DiffEntry> diff = new Git(repository).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());
        DiffFormatter formatter = new DiffFormatter(System.out);
        formatter.setRepository(repository);
        formatter.format(entry);//from w  ww.jav a 2s . co  m
    }

    repository.close();
}

From source file:at.nonblocking.maven.nonsnapshot.impl.ScmHandlerGitImpl.java

License:Apache License

@Override
public void init(File baseDir, String scmUser, String scmPassword, Properties properties) {
    this.baseDir = findGitRepo(baseDir);
    if (this.baseDir == null) {
        LOG.error("Project seems not be within a GIT repository!");
        return;/*from w ww . j  av a  2s  .  c  o m*/
    }

    LOG.info("Using GIT repository: {}", this.baseDir.getAbsolutePath());

    try {
        FileRepository localRepo = new FileRepository(this.baseDir + "/.git");
        this.git = new Git(localRepo);
        if (scmPassword != null && !scmPassword.trim().isEmpty()) {
            this.credentialsProvider = new UsernamePasswordAndPassphraseCredentialProvider(scmUser,
                    scmPassword);
        }
        if (properties != null && "false".equals(properties.getProperty("gitDoPush"))) {
            this.doPush = false;
            LOG.info("GIT push is disabled");
        }

    } catch (Exception e) {
        LOG.error("Project seems not be within a GIT repository!", e);
    }
}

From source file:au.id.soundadvice.systemdesign.versioning.jgit.GitVersionControl.java

License:Open Source License

public GitVersionControl(Path path) throws IOException {
    try {/*from  w  w  w .j  a  va 2s .  com*/
        // Cribbed from Git.open, but with findGitDir rather than setGitDir
        // and extracting the location.
        FS fs = FS.DETECTED;
        RepositoryCache.FileKey key = RepositoryCache.FileKey.lenient(path.toFile(), fs);
        RepositoryBuilder builder = new RepositoryBuilder().setFS(fs).findGitDir(key.getFile())
                .setMustExist(true);
        repositoryRoot = Paths.get(builder.getGitDir().getAbsolutePath()).getParent();
        repo = new Git(builder.build());

        checkCSVMergeDriver(repositoryRoot);
    } catch (RuntimeException ex) {
        throw new IOException(ex);
    }
}

From source file:boa.datagen.scm.GitConnector.java

License:Apache License

public GitConnector(final String path) {
    try {//from w  ww.ja  v a2  s  . c o  m
        this.path = path;
        this.repository = new FileRepositoryBuilder().setGitDir(new File(path + "/.git")).build();
        this.git = new Git(this.repository);
        this.revwalk = new RevWalk(this.repository);
    } catch (final IOException e) {
        if (debug)
            System.err.println("Git Error connecting to " + path + ". " + e.getMessage());
    }
}

From source file:br.com.riselabs.cotonet.util.CodefaceHelper.java

License:Open Source License

private static void createMergeBasedTags(Project project) throws IOException {
    Map<String, String> tagsMap;
    MergeScenarioDAO dao = new MergeScenarioDAO();
    Repository repository = project.getRepository();
    List<MergeScenario> scenarios = new ArrayList<MergeScenario>();
    try {/*from  www .  java2s.c  o  m*/
        scenarios = dao.list(project.getID());
    } catch (InvalidCotonetBeanException e1) {
        Logger.logStackTrace(e1);
    }
    try (Git git = new Git(repository)) {
        // remove existing tags
        for (String tag : repository.getTags().keySet()) {
            git.tagDelete().setTags(tag).call();
        }
    } catch (GitAPIException e) {
        Logger.logStackTrace(e);
    }

    tagsMap = new HashMap<String, String>();
    for (MergeScenario scenario : scenarios) {
        String tagT = project.getName() + "T" + scenario.getID();
        String tagB = project.getName() + "B" + scenario.getID();

        tagsMap.put(tagB, scenario.getSHA1Merge());
        RevCommit earlier = getEarlierCommit(project.getRepository(), scenario.getSHA1Merge(), 3);
        tagsMap.put(tagT, earlier.getName());

        // prepare test-repository
        try (Git git = new Git(repository)) {

            try (RevWalk walk = new RevWalk(repository)) {
                ObjectId refCommitID = ObjectId.fromString(scenario.getSHA1Merge());
                RevCommit refCommit = walk.parseCommit(refCommitID);
                git.tag().setObjectId(refCommit).setName(tagB).call();
                git.tag().setObjectId(earlier).setName(tagT).call();

                walk.dispose();
            } catch (ConcurrentRefUpdateException e) {
                e.printStackTrace();
            } catch (InvalidTagNameException e) {
                e.printStackTrace();
            } catch (NoHeadException e) {
                e.printStackTrace();
            } catch (GitAPIException e) {
                e.printStackTrace();
            }
        } catch (RevisionSyntaxException e) {
            Logger.logStackTrace(e);
        }
    }
    try {
        createTagToSHA1MappingFile(project.getName(), tagsMap);
    } catch (NullPointerException | EmptyContentException e) {
        Logger.logStackTrace(e);
    }
}

From source file:br.com.riselabs.cotonet.util.CodefaceHelper.java

License:Open Source License

/**
 * Returns the list of TAGs from the given repository.
 * //from w w  w. ja  v  a 2 s  . co  m
 * @param repository
 * @return
 * @throws IOException
 * @throws GitAPIException
 */
@Deprecated
public List<String> getTags(Repository repository) throws IOException, GitAPIException {
    List<String> tags = new ArrayList<String>();
    try (Git git = new Git(repository)) {
        List<Ref> call = git.tagList().call();
        for (Ref ref : call) {
            tags.add(ref.getName());
        }
    }
    return tags;
}

From source file:ch.uzh.ifi.seal.permo.history.git.core.model.jgit.JGitCommit.java

License:Apache License

private List<Diff> createDiffs() {
    try {//from ww w  .j  ava  2  s  . c o m
        final RevCommit parent = isRoot() ? null : revCommit.getParent(0);
        final AbstractTreeIterator newTreeIt = getTreeIteratorOfCommit(repository, revCommit);
        final AbstractTreeIterator oldTreeIt = isRoot() ? new EmptyTreeIterator()
                : getTreeIteratorOfCommit(repository, parent);
        final List<DiffEntry> diffEntries = new Git(repository).diff().setNewTree(newTreeIt)
                .setOldTree(oldTreeIt).call();
        final Commit parentCommit = isRoot() ? null : JGitCommit.of(repository, parent);
        return ListUtil.map(diffEntries, (diffEntry) -> JGitDiff.of(parentCommit, this, repository, diffEntry));
    } catch (final IOException | GitAPIException e) {
        return Lists.newArrayList();
    }
}

From source file:ch.uzh.ifi.seal.permo.history.git.core.model.jgit.JGitRepository.java

License:Apache License

/**
 * {@inheritDoc}//from   w  ww .  j  a  v  a  2s  . c o m
 */
@Override
public List<Commit> getCommits(final TimePeriod timePeriod) {
    if (commits == null) {
        commits = Lists.newArrayList();
        try {
            fetchFromUpstream();
            final ObjectId idOfLatestCommit = repository.getRef(JGitUtil.getDefaultRemoteBranch(repository))
                    .getObjectId();
            final Git git = new Git(repository);
            for (final RevCommit revCommit : git.log().add(idOfLatestCommit).call()) {
                final LocalDateTime commitTime = DateTimes.ofEpochSeconds(revCommit.getCommitTime());
                if (commitTime.isBefore(timePeriod.getStartDateTime())) {
                    break;
                } else if (!commitTime.isAfter(timePeriod.getEndDateTime())) {
                    commits.add(JGitCommit.of(repository, revCommit));
                }
            }
        } catch (final IOException | GitAPIException e) {
        }
    }
    return commits;
}

From source file:ch.uzh.ifi.seal.permo.history.git.core.model.jgit.JGitRepository.java

License:Apache License

private void fetchFromUpstream() throws InvalidRemoteException, TransportException, GitAPIException {
    final boolean fetchAutomatically = Preferences.getStore()
            .getBoolean(Preferences.PERMO__FETCH_AUTOMATICALLY);
    if (fetchAutomatically) {
        final Git git = new Git(repository);
        git.fetch().setRemote(JGitUtil.getDefaultRemote(repository)).call();
    }/*from  w w  w.  j av a  2 s  . com*/
}

From source file:com.addthis.hydra.job.store.JobStoreGit.java

License:Apache License

/**
 * Set up a git repository in a directory.
 *
 * @param baseDir The directory that will store the files.
 * @throws IOException If there is a problem writing to the directory
 *///from  w  w w  .j a  va  2  s  .com
public JobStoreGit(File baseDir) throws Exception {
    this.baseDir = baseDir;
    this.gitDir = new File(baseDir, ".git");
    this.jobDir = new File(baseDir, "jobs");
    repository = new FileRepository(gitDir);
    git = new Git(repository);
    initialize();
}