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);
}
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;
}
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;
}
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;
}
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;
}
Aggregations