Search in sources :

Example 6 with ObjectId

use of org.eclipse.jgit.lib.ObjectId in project che by eclipse.

the class JGitDiffPage method commitToCommit.

/**
     * Show changes between specified two revisions and index. If
     * <code>commitAId == null</code> then view changes between HEAD and revision commitBId.
     *
     * @param commitAId
     *            id of commit A, pass <code>null</code> is the same as pass HEAD
     * @param commitBId
     *            id of commit B
     * @param formatter
     *            diff formatter
     * @return list of diff entries
     * @throws IOException
     *             if any i/o errors occurs
     */
private List<DiffEntry> commitToCommit(String commitAId, String commitBId, DiffFormatter formatter) throws IOException {
    if (commitAId == null) {
        commitAId = Constants.HEAD;
    }
    ObjectId commitA = repository.resolve(commitAId);
    if (commitA == null) {
        throw new IllegalArgumentException("Invalid commit id " + commitAId);
    }
    ObjectId commitB = repository.resolve(commitBId);
    if (commitB == null) {
        throw new IllegalArgumentException("Invalid commit id " + commitBId);
    }
    RevTree treeA;
    try (RevWalk revWalkA = new RevWalk(repository)) {
        treeA = revWalkA.parseTree(commitA);
    }
    RevTree treeB;
    try (RevWalk revWalkB = new RevWalk(repository)) {
        treeB = revWalkB.parseTree(commitB);
    }
    if (!params.isNoRenames()) {
        // Use embedded RenameDetector it works well with index and revision
        // history.
        formatter.setDetectRenames(true);
        int renameLimit = params.getRenameLimit();
        if (renameLimit > 0) {
            formatter.getRenameDetector().setRenameLimit(renameLimit);
        }
    }
    return formatter.scan(treeA, treeB);
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) RevWalk(org.eclipse.jgit.revwalk.RevWalk) RevTree(org.eclipse.jgit.revwalk.RevTree)

Example 7 with ObjectId

use of org.eclipse.jgit.lib.ObjectId in project che by eclipse.

the class JGitDiffPage method emptyToCommit.

/**
     * Show changes between specified revision and empty tree.
     *
     * @param commitId
     *            id of commit
     * @param formatter
     *            diff formatter
     * @return list of diff entries
     * @throws IOException
     *             if any i/o errors occurs
     */
private List<DiffEntry> emptyToCommit(String commitId, DiffFormatter formatter) throws IOException {
    ObjectId commit = repository.resolve(commitId);
    checkArgument(commit != null, "Invalid commit id " + commitId);
    RevTree tree;
    try (RevWalk revWalkA = new RevWalk(repository)) {
        tree = revWalkA.parseTree(commit);
    }
    List<DiffEntry> diff;
    try (ObjectReader reader = repository.newObjectReader()) {
        CanonicalTreeParser iterator = new CanonicalTreeParser();
        iterator.reset(reader, tree);
        diff = formatter.scan(new EmptyTreeIterator(), iterator);
    }
    return diff;
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) EmptyTreeIterator(org.eclipse.jgit.treewalk.EmptyTreeIterator) ObjectReader(org.eclipse.jgit.lib.ObjectReader) RevWalk(org.eclipse.jgit.revwalk.RevWalk) RevTree(org.eclipse.jgit.revwalk.RevTree) CanonicalTreeParser(org.eclipse.jgit.treewalk.CanonicalTreeParser) DiffEntry(org.eclipse.jgit.diff.DiffEntry)

Example 8 with ObjectId

use of org.eclipse.jgit.lib.ObjectId in project bazel by bazelbuild.

the class GitCloner method isUpToDate.

private static boolean isUpToDate(GitRepositoryDescriptor descriptor) {
    // Initializing/checking status of/etc submodules cleanly is hard, so don't try for now.
    if (descriptor.initSubmodules) {
        return false;
    }
    Repository repository = null;
    try {
        repository = new FileRepositoryBuilder().setGitDir(descriptor.directory.getChild(Constants.DOT_GIT).getPathFile()).setMustExist(true).build();
        ObjectId head = repository.resolve(Constants.HEAD);
        ObjectId checkout = repository.resolve(descriptor.checkout);
        if (head != null && checkout != null && head.equals(checkout)) {
            Status status = Git.wrap(repository).status().call();
            if (!status.hasUncommittedChanges()) {
                // new_git_repository puts (only) BUILD and WORKSPACE, and
                // git_repository doesn't add any files.
                Set<String> untracked = status.getUntracked();
                if (untracked.isEmpty() || (untracked.size() == 2 && untracked.contains("BUILD") && untracked.contains("WORKSPACE"))) {
                    return true;
                }
            }
        }
    } catch (GitAPIException | IOException e) {
    // Any exceptions here, we'll just blow it away and try cloning fresh.
    // The fresh clone avoids any weirdness due to what's there and has nicer
    // error reporting.
    } finally {
        if (repository != null) {
            repository.close();
        }
    }
    return false;
}
Also used : Status(org.eclipse.jgit.api.Status) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Repository(org.eclipse.jgit.lib.Repository) ObjectId(org.eclipse.jgit.lib.ObjectId) IOException(java.io.IOException) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder)

Example 9 with ObjectId

use of org.eclipse.jgit.lib.ObjectId in project gitblit by gitblit.

the class BugtraqConfig method getBaseConfig.

// Utils ==================================================================
@Nullable
private static Config getBaseConfig(@NotNull Repository repository, @NotNull String configFileName) throws IOException, ConfigInvalidException {
    final Config baseConfig;
    if (repository.isBare()) {
        // read bugtraq config directly from the repository
        String content = null;
        RevWalk rw = new RevWalk(repository);
        TreeWalk tw = new TreeWalk(repository);
        tw.setFilter(PathFilterGroup.createFromStrings(configFileName));
        try {
            final Ref ref = repository.getRef(Constants.HEAD);
            if (ref == null) {
                return null;
            }
            ObjectId headId = ref.getTarget().getObjectId();
            if (headId == null || ObjectId.zeroId().equals(headId)) {
                return null;
            }
            RevCommit commit = rw.parseCommit(headId);
            RevTree tree = commit.getTree();
            tw.reset(tree);
            while (tw.next()) {
                ObjectId entid = tw.getObjectId(0);
                FileMode entmode = tw.getFileMode(0);
                if (FileMode.REGULAR_FILE == entmode) {
                    ObjectLoader ldr = repository.open(entid, Constants.OBJ_BLOB);
                    content = new String(ldr.getCachedBytes(), commit.getEncoding());
                    break;
                }
            }
        } finally {
            rw.dispose();
            tw.close();
        }
        if (content == null) {
            // config not found
            baseConfig = null;
        } else {
            // parse the config
            Config config = new Config();
            config.fromText(content);
            baseConfig = config;
        }
    } else {
        // read bugtraq config from work tree
        final File baseFile = new File(repository.getWorkTree(), configFileName);
        if (baseFile.isFile()) {
            FileBasedConfig fileConfig = new FileBasedConfig(baseFile, repository.getFS());
            fileConfig.load();
            baseConfig = fileConfig;
        } else {
            baseConfig = null;
        }
    }
    return baseConfig;
}
Also used : FileMode(org.eclipse.jgit.lib.FileMode) Ref(org.eclipse.jgit.lib.Ref) ObjectId(org.eclipse.jgit.lib.ObjectId) Config(org.eclipse.jgit.lib.Config) FileBasedConfig(org.eclipse.jgit.storage.file.FileBasedConfig) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader) RevWalk(org.eclipse.jgit.revwalk.RevWalk) FileBasedConfig(org.eclipse.jgit.storage.file.FileBasedConfig) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk) File(java.io.File) RevTree(org.eclipse.jgit.revwalk.RevTree) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Nullable(org.jetbrains.annotations.Nullable)

Example 10 with ObjectId

use of org.eclipse.jgit.lib.ObjectId in project gitblit by gitblit.

the class CommitCache method getCommits.

/**
	 * Get all commits for the specified repository:branch since a specific date.
	 * These commits may be retrieved from the cache if the sinceDate is after
	 * the cacheCutoffDate.
	 *
	 * @param repositoryName
	 * @param repository
	 * @param branch
	 * @param sinceDate
	 * @return a list of commits
	 */
public List<RepositoryCommit> getCommits(String repositoryName, Repository repository, String branch, Date sinceDate) {
    long start = System.nanoTime();
    Date cacheCutoffDate = getCutoffDate();
    List<RepositoryCommit> list;
    if (cacheDays > 0 && (sinceDate.getTime() >= cacheCutoffDate.getTime())) {
        // request fits within the cache window
        String repoKey = repositoryName.toLowerCase();
        String branchKey = branch.toLowerCase();
        RevCommit tip = JGitUtils.getCommit(repository, branch);
        Date tipDate = JGitUtils.getCommitDate(tip);
        ObjectCache<List<RepositoryCommit>> repoCache;
        synchronized (cache) {
            repoCache = cache.get(repoKey);
            if (repoCache == null) {
                repoCache = new ObjectCache<>();
                cache.put(repoKey, repoCache);
            }
        }
        synchronized (repoCache) {
            List<RepositoryCommit> commits;
            if (!repoCache.hasCurrent(branchKey, tipDate)) {
                commits = repoCache.getObject(branchKey);
                if (ArrayUtils.isEmpty(commits)) {
                    // we don't have any cached commits for this branch, reload
                    commits = get(repositoryName, repository, branch, cacheCutoffDate);
                    repoCache.updateObject(branchKey, tipDate, commits);
                    logger.debug(MessageFormat.format("parsed {0} commits from {1}:{2} since {3,date,yyyy-MM-dd} in {4} msecs", commits.size(), repositoryName, branch, cacheCutoffDate, TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start)));
                } else {
                    // incrementally update cache since the last cached commit
                    ObjectId sinceCommit = commits.get(0).getId();
                    List<RepositoryCommit> incremental = get(repositoryName, repository, branch, sinceCommit);
                    logger.info(MessageFormat.format("incrementally added {0} commits to cache for {1}:{2} in {3} msecs", incremental.size(), repositoryName, branch, TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start)));
                    incremental.addAll(commits);
                    repoCache.updateObject(branchKey, tipDate, incremental);
                    commits = incremental;
                }
            } else {
                // cache is current
                commits = repoCache.getObject(branchKey);
                // evict older commits outside the cache window
                commits = reduce(commits, cacheCutoffDate);
                // update cache
                repoCache.updateObject(branchKey, tipDate, commits);
            }
            if (sinceDate.equals(cacheCutoffDate)) {
                // Mustn't hand out the cached list; that's not thread-safe
                list = new ArrayList<>(commits);
            } else {
                // reduce the commits to those since the specified date
                list = reduce(commits, sinceDate);
            }
        }
        logger.debug(MessageFormat.format("retrieved {0} commits from cache of {1}:{2} since {3,date,yyyy-MM-dd} in {4} msecs", list.size(), repositoryName, branch, sinceDate, TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start)));
    } else {
        // not caching or request outside cache window
        list = get(repositoryName, repository, branch, sinceDate);
        logger.debug(MessageFormat.format("parsed {0} commits from {1}:{2} since {3,date,yyyy-MM-dd} in {4} msecs", list.size(), repositoryName, branch, sinceDate, TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start)));
    }
    return list;
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) ArrayList(java.util.ArrayList) List(java.util.List) RepositoryCommit(com.gitblit.models.RepositoryCommit) Date(java.util.Date) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

ObjectId (org.eclipse.jgit.lib.ObjectId)355 Test (org.junit.Test)128 RevCommit (org.eclipse.jgit.revwalk.RevCommit)123 RevWalk (org.eclipse.jgit.revwalk.RevWalk)84 Repository (org.eclipse.jgit.lib.Repository)63 IOException (java.io.IOException)61 Change (com.google.gerrit.reviewdb.client.Change)41 ObjectInserter (org.eclipse.jgit.lib.ObjectInserter)39 ArrayList (java.util.ArrayList)36 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)34 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)34 Ref (org.eclipse.jgit.lib.Ref)34 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)33 CommitBuilder (org.eclipse.jgit.lib.CommitBuilder)25 AnyObjectId (org.eclipse.jgit.lib.AnyObjectId)24 RefUpdate (org.eclipse.jgit.lib.RefUpdate)22 NoteMap (org.eclipse.jgit.notes.NoteMap)22 Map (java.util.Map)21 OrmException (com.google.gwtorm.server.OrmException)20 ObjectReader (org.eclipse.jgit.lib.ObjectReader)19