use of org.eclipse.jgit.dircache.DirCache in project che by eclipse.
the class JGitDiffPage method commitToIndex.
/**
* Show changes between specified revision and index. If
* <code>commitId == null</code> then view changes between HEAD and index.
*
* @param commitId
* id of commit, pass <code>null</code> is the same as pass HEAD
* @param formatter
* diff formatter
* @return list of diff entries
* @throws IOException
* if any i/o errors occurs
*/
private List<DiffEntry> commitToIndex(String commitId, DiffFormatter formatter) throws IOException {
if (commitId == null) {
commitId = Constants.HEAD;
}
ObjectId commitA = repository.resolve(commitId);
if (commitA == null) {
throw new IllegalArgumentException("Invalid commit id " + commitId);
}
RevTree treeA;
try (RevWalk revWalkA = new RevWalk(repository)) {
treeA = revWalkA.parseTree(commitA);
}
DirCache dirCache = null;
List<DiffEntry> diff;
try (ObjectReader reader = repository.newObjectReader()) {
dirCache = repository.lockDirCache();
CanonicalTreeParser iterA = new CanonicalTreeParser();
iterA.reset(reader, treeA);
DirCacheIterator iterB = new DirCacheIterator(dirCache);
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);
}
}
diff = formatter.scan(iterA, iterB);
} finally {
if (dirCache != null) {
dirCache.unlock();
}
}
return diff;
}
use of org.eclipse.jgit.dircache.DirCache in project che by eclipse.
the class JGitDiffPage method indexToWorkingTree.
/**
* Show changes between index and working tree.
*
* @param formatter
* diff formatter
* @return list of diff entries
* @throws IOException
* if any i/o errors occurs
*/
private List<DiffEntry> indexToWorkingTree(DiffFormatter formatter) throws IOException {
DirCache dirCache = null;
ObjectReader reader = repository.newObjectReader();
List<DiffEntry> diff;
try {
dirCache = repository.lockDirCache();
DirCacheIterator iterA = new DirCacheIterator(dirCache);
FileTreeIterator iterB = new FileTreeIterator(repository);
// Seems bug in DiffFormatter when work with working. Disable detect
// renames by formatter and do it later.
formatter.setDetectRenames(false);
diff = formatter.scan(iterA, iterB);
if (!params.isNoRenames()) {
// Detect renames.
RenameDetector renameDetector = createRenameDetector();
ContentSource.Pair sourcePairReader = new ContentSource.Pair(ContentSource.create(reader), ContentSource.create(iterB));
renameDetector.addAll(diff);
diff = renameDetector.compute(sourcePairReader, NullProgressMonitor.INSTANCE);
}
} finally {
reader.close();
if (dirCache != null) {
dirCache.unlock();
}
}
return diff;
}
use of org.eclipse.jgit.dircache.DirCache in project gitblit by gitblit.
the class RefLogUtils method createIndex.
/**
* Creates an in-memory index of the reflog entry.
*
* @param repo
* @param headId
* @param commands
* @return an in-memory index
* @throws IOException
*/
private static DirCache createIndex(Repository repo, ObjectId headId, Collection<ReceiveCommand> commands) throws IOException {
DirCache inCoreIndex = DirCache.newInCore();
DirCacheBuilder dcBuilder = inCoreIndex.builder();
ObjectInserter inserter = repo.newObjectInserter();
long now = System.currentTimeMillis();
Set<String> ignorePaths = new TreeSet<String>();
try {
// add receive commands to the temporary index
for (ReceiveCommand command : commands) {
// use the ref names as the path names
String path = command.getRefName();
ignorePaths.add(path);
StringBuilder change = new StringBuilder();
change.append(command.getType().name()).append(' ');
switch(command.getType()) {
case CREATE:
change.append(ObjectId.zeroId().getName());
change.append(' ');
change.append(command.getNewId().getName());
break;
case UPDATE:
case UPDATE_NONFASTFORWARD:
change.append(command.getOldId().getName());
change.append(' ');
change.append(command.getNewId().getName());
break;
case DELETE:
change = null;
break;
}
if (change == null) {
// ref deleted
continue;
}
String content = change.toString();
// create an index entry for this attachment
final DirCacheEntry dcEntry = new DirCacheEntry(path);
dcEntry.setLength(content.length());
dcEntry.setLastModified(now);
dcEntry.setFileMode(FileMode.REGULAR_FILE);
// insert object
dcEntry.setObjectId(inserter.insert(org.eclipse.jgit.lib.Constants.OBJ_BLOB, content.getBytes("UTF-8")));
// add to temporary in-core index
dcBuilder.add(dcEntry);
}
// Traverse HEAD to add all other paths
TreeWalk treeWalk = new TreeWalk(repo);
int hIdx = -1;
if (headId != null)
hIdx = treeWalk.addTree(new RevWalk(repo).parseTree(headId));
treeWalk.setRecursive(true);
while (treeWalk.next()) {
String path = treeWalk.getPathString();
CanonicalTreeParser hTree = null;
if (hIdx != -1)
hTree = treeWalk.getTree(hIdx, CanonicalTreeParser.class);
if (!ignorePaths.contains(path)) {
// add entries from HEAD for all other paths
if (hTree != null) {
// create a new DirCacheEntry with data retrieved from
// HEAD
final DirCacheEntry dcEntry = new DirCacheEntry(path);
dcEntry.setObjectId(hTree.getEntryObjectId());
dcEntry.setFileMode(hTree.getEntryFileMode());
// add to temporary in-core index
dcBuilder.add(dcEntry);
}
}
}
// release the treewalk
treeWalk.close();
// finish temporary in-core index used for this commit
dcBuilder.finish();
} finally {
inserter.close();
}
return inCoreIndex;
}
use of org.eclipse.jgit.dircache.DirCache in project gitblit by gitblit.
the class BranchTicketService method writeTicketsFile.
/**
* Writes a file to the tickets branch.
*
* @param db
* @param file
* @param content
* @param createdBy
* @param msg
*/
private void writeTicketsFile(Repository db, String file, String content, String createdBy, String msg) {
if (getTicketsBranch(db) == null) {
createTicketsBranch(db);
}
DirCache newIndex = DirCache.newInCore();
DirCacheBuilder builder = newIndex.builder();
ObjectInserter inserter = db.newObjectInserter();
try {
// create an index entry for the revised index
final DirCacheEntry idIndexEntry = new DirCacheEntry(file);
idIndexEntry.setLength(content.length());
idIndexEntry.setLastModified(System.currentTimeMillis());
idIndexEntry.setFileMode(FileMode.REGULAR_FILE);
// insert new ticket index
idIndexEntry.setObjectId(inserter.insert(org.eclipse.jgit.lib.Constants.OBJ_BLOB, content.getBytes(Constants.ENCODING)));
// add to temporary in-core index
builder.add(idIndexEntry);
Set<String> ignorePaths = new HashSet<String>();
ignorePaths.add(file);
for (DirCacheEntry entry : JGitUtils.getTreeEntries(db, BRANCH, ignorePaths)) {
builder.add(entry);
}
// finish temporary in-core index used for this commit
builder.finish();
// commit the change
commitIndex(db, newIndex, createdBy, msg);
} catch (ConcurrentRefUpdateException e) {
log.error("", e);
} catch (IOException e) {
log.error("", e);
} finally {
inserter.close();
}
}
use of org.eclipse.jgit.dircache.DirCache in project gitblit by gitblit.
the class BranchTicketService method deleteTicketImpl.
/**
* Deletes a ticket from the repository.
*
* @param ticket
* @return true if successful
*/
@Override
protected synchronized boolean deleteTicketImpl(RepositoryModel repository, TicketModel ticket, String deletedBy) {
if (ticket == null) {
throw new RuntimeException("must specify a ticket!");
}
boolean success = false;
Repository db = repositoryManager.getRepository(ticket.repository);
try {
RefModel ticketsBranch = getTicketsBranch(db);
if (ticketsBranch == null) {
throw new RuntimeException(BRANCH + " does not exist!");
}
String ticketPath = toTicketPath(ticket.number);
TreeWalk treeWalk = null;
try {
ObjectId treeId = db.resolve(BRANCH + "^{tree}");
// Create the in-memory index of the new/updated ticket
DirCache index = DirCache.newInCore();
DirCacheBuilder builder = index.builder();
// Traverse HEAD to add all other paths
treeWalk = new TreeWalk(db);
int hIdx = -1;
if (treeId != null) {
hIdx = treeWalk.addTree(treeId);
}
treeWalk.setRecursive(true);
while (treeWalk.next()) {
String path = treeWalk.getPathString();
CanonicalTreeParser hTree = null;
if (hIdx != -1) {
hTree = treeWalk.getTree(hIdx, CanonicalTreeParser.class);
}
if (!path.startsWith(ticketPath)) {
// add entries from HEAD for all other paths
if (hTree != null) {
final DirCacheEntry entry = new DirCacheEntry(path);
entry.setObjectId(hTree.getEntryObjectId());
entry.setFileMode(hTree.getEntryFileMode());
// add to temporary in-core index
builder.add(entry);
}
}
}
// finish temporary in-core index used for this commit
builder.finish();
success = commitIndex(db, index, deletedBy, "- " + ticket.number);
} catch (Throwable t) {
log.error(MessageFormat.format("Failed to delete ticket {0,number,0} from {1}", ticket.number, db.getDirectory()), t);
} finally {
// release the treewalk
if (treeWalk != null) {
treeWalk.close();
}
}
} finally {
db.close();
}
return success;
}
Aggregations