Skip to content

Commit

Permalink
Fix for #5
Browse files Browse the repository at this point in the history
Refactored RewriteBranch to RewriteRef in order to prepare for detached
HEAD rewrite.

Moved branch recovery to HistoryUpdate

Improved HistoryUpdate, CacheCommitUpdate and RewriteRef

Renamed RewriteRef to IndexUpdate

Rewrite branch is not needed anymore, because we only use the index now
  • Loading branch information
renelink committed Jan 5, 2015
1 parent 5c18263 commit c1b0abc
Show file tree
Hide file tree
Showing 10 changed files with 170 additions and 224 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ public void save() throws IOException {
public void restore() throws GitAPIException {
if (branchName != null) {
git.reset().setMode(ResetType.HARD).call();
git.clean().setCleanDirectories(true).setIgnore(true).call();
git.checkout().setName(branchName).call();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@

import org.eclipse.jgit.dircache.DirCache;
import org.eclipse.jgit.lib.CommitBuilder;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectInserter;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.RefUpdate;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;

public class CacheCommitUpdate implements CommitUpdate {

Expand Down Expand Up @@ -63,7 +59,6 @@ void writeCommit() throws IOException {
return;
}
Repository repo = gitRepository.getRepository();
RevWalk rw = new RevWalk(repo);
ObjectInserter odi = repo.newObjectInserter();
try {
if (treeUpdate != null) {
Expand All @@ -87,14 +82,7 @@ void writeCommit() throws IOException {
ObjectId commitId = odi.insert(commit);
odi.flush();

RevCommit revCommit = rw.parseCommit(commitId);
odi.flush();
RefUpdate ru = repo.updateRef(Constants.HEAD);
ru.setNewObjectId(commitId);
ru.setForceUpdate(true);
ru.update(rw);

historyUpdate.replaceCommit(this.commit, revCommit);
historyUpdate.replaceCommit(this.commit, commitId);
} finally {
odi.release();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,15 +220,9 @@ public TreeObject getCommitRangeTree(List<? extends Ref> selectedRefs,

public void applyFilter(Collection<CommitRange> commitRanges,
IndexFilter indexFilter, ProgressListener progressListener)
throws IOException, GitAPIException, RewriteBranchExistsException {
Git git = getGit();

BranchMemento currentBranchMemento = new BranchMemento(git);
currentBranchMemento.save();

throws IOException, GitAPIException {
HistoryUpdate historyUpdate = new HistoryUpdate(this);
RewriteBranch rewriteBranch = historyUpdate.begin();

IndexUpdate indexUpdate = historyUpdate.begin();

int totalWork = getTotalWork(commitRanges);
CommitWalker commitWalk = createCommitWalker(commitRanges);
Expand All @@ -240,7 +234,7 @@ public void applyFilter(Collection<CommitRange> commitRanges,
while (rewriteIterator.hasNext()) {
Commit commit = rewriteIterator.next();

CacheCommitUpdate commitUpdate = rewriteBranch
CacheCommitUpdate commitUpdate = indexUpdate
.beginUpdate(commit);

indexFilter.apply(commitUpdate);
Expand All @@ -259,7 +253,6 @@ public void applyFilter(Collection<CommitRange> commitRanges,
}
} finally {
try {
currentBranchMemento.restore();
historyUpdate.close();
} catch (GitAPIException e) {
}
Expand Down Expand Up @@ -288,33 +281,32 @@ public Git getGit() {

public void applyFilter(Collection<CommitRange> commitRanges,
IndexFilter indexFilter) throws IOException,
CheckoutConflictException, GitAPIException,
RewriteBranchExistsException {
CheckoutConflictException, GitAPIException {
applyFilter(commitRanges, indexFilter, NullProgressListener.INSTANCE);
}

public void applyFilter(IndexFilter indexFilter) throws IOException,
GitAPIException, RewriteBranchExistsException {
GitAPIException {
List<Ref> refs = getRefs(Ref.class);
applyFilter(refs, indexFilter, NullProgressListener.INSTANCE);
}

public void applyFilter(IndexFilter indexFilter,
ProgressListener progressListener) throws IOException,
GitAPIException, RewriteBranchExistsException {
GitAPIException {
List<Ref> refs = getRefs(Ref.class);
applyFilter(refs, indexFilter, progressListener);
}

public void applyFilter(List<? extends Ref> refs, IndexFilter indexFilter)
throws IOException, GitAPIException, RewriteBranchExistsException {
throws IOException, GitAPIException {
Collection<CommitRange> commitRanges = getCommitRanges(refs);
applyFilter(commitRanges, indexFilter, NullProgressListener.INSTANCE);
}

public void applyFilter(List<? extends Ref> refs, IndexFilter indexFilter,
ProgressListener progressListener) throws IOException,
GitAPIException, RewriteBranchExistsException {
GitAPIException {
Collection<CommitRange> commitRanges = getCommitRanges(refs);
applyFilter(commitRanges, indexFilter, progressListener);
}
Expand Down Expand Up @@ -348,4 +340,9 @@ public void release() {
repository.close();
}

public void getRefs(org.eclipse.jgit.lib.Ref jgitRef) {
// TODO Auto-generated method stub

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,41 @@
import java.util.Map;
import java.util.Set;

import org.eclipse.jgit.api.CheckoutCommand;
import org.eclipse.jgit.api.GarbageCollectCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.ResetCommand.ResetType;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ReflogEntry;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;

import com.link_intersystems.gitdirstat.domain.ExpireReflogCommand.ReflogEntryFilter;

public class HistoryUpdate {

private class SkipReflogEntryFilter implements ReflogEntryFilter {

private Set<String> expireCommits;
private Set<ObjectId> expireCommits;

public SkipReflogEntryFilter(Set<String> expireCommits) {
public SkipReflogEntryFilter(Set<ObjectId> expireCommits) {
this.expireCommits = expireCommits;
}

@Override
public boolean accept(ReflogEntry reflogEntry) {
ObjectId newId = reflogEntry.getNewId();
String newCommitId = newId.getName();

ObjectId oldId = reflogEntry.getOldId();
String oldCommitId = oldId.getName();
boolean oldIdExpired = !expireCommits.contains(oldCommitId);
boolean newIdExpired = !expireCommits.contains(newCommitId);

boolean oldIdExpired = !expireCommits.contains(oldId);
boolean newIdExpired = !expireCommits.contains(newId);

boolean noIdExpired = !oldIdExpired || !newIdExpired;
return noIdExpired;
}
}

Map<String, RevCommit> replacedCommits = new HashMap<String, RevCommit>();
Map<ObjectId, ObjectId> replacedCommits = new HashMap<ObjectId, ObjectId>();
private GitRepository gitRepository;
String rewriteBranchName = "rewrite_branch";
private RewriteBranch rewriteBranch;
private IndexUpdate indexUpdate;

public HistoryUpdate(GitRepository gitRepository) {
this.gitRepository = gitRepository;
Expand All @@ -61,11 +57,11 @@ public void updateRefs() throws IOException, GitAPIException {
}

public void cleanupRepository() throws GitAPIException {
Set<String> expireCommits = new HashSet<String>();
Set<ObjectId> expireCommits = new HashSet<ObjectId>();
expireCommits.addAll(replacedCommits.keySet());

if (rewriteBranch != null) {
expireCommits = rewriteBranch.getTouchedCommits();
if (indexUpdate != null) {
expireCommits = indexUpdate.getTouchedCommits();
}

ExpireReflogCommand expireReflogCommand = new ExpireReflogCommand(
Expand All @@ -87,73 +83,51 @@ private void gc() throws GitAPIException {

private void nullSafeCommitUpdate(Ref ref) throws IOException {
ObjectId objectId = ref.getCommitId();
if (objectId != null) {
String objectName = objectId.name();
if (replacedCommits.containsKey(objectName)) {
RevCommit rewrittenCommit = replacedCommits.get(objectName);
ref.update(rewrittenCommit.getId());
}
ObjectId rewrittenCommit = replacedCommits.get(objectId);
if (rewrittenCommit != null) {
ref.update(rewrittenCommit);
}
}

public boolean hasReplacedParents(Commit commit) {
ObjectId[] parentIds = commit.getParentIds();
for (int i = 0; i < parentIds.length; i++) {
ObjectId parentId = parentIds[i];
String parentName = parentId.name();
if (replacedCommits.containsKey(parentName)) {
if (replacedCommits.containsKey(parentId)) {
return true;
}
}
return false;
}

public void replaceCommit(Commit commit, RevCommit revCommit) {
String commitName = commit.getId().name();
replacedCommits.put(commitName, revCommit);
public void replaceCommit(Commit commit, ObjectId replacedCommitId) {
replacedCommits.put(commit.getId(), replacedCommitId);
}

public ObjectId[] getParentIds(Commit commit) {
ObjectId[] parentIds = commit.getParentIds();
ObjectId[] replacedParentIds = new ObjectId[parentIds.length];
for (int i = 0; i < parentIds.length; i++) {
ObjectId parentId = parentIds[i];
String parentName = parentId.name();
RevCommit replacedCommit = replacedCommits.get(parentName);
ObjectId replacedCommit = replacedCommits.get(parentId);
if (replacedCommit != null) {
parentId = replacedCommit.getId();
replacedParentIds[i] = replacedCommit;
}
replacedParentIds[i] = parentId;
}
return replacedParentIds;
}

public RewriteBranch begin() throws IOException, GitAPIException {
if (rewriteBranch != null) {
throw new IllegalStateException("begin() can only be invoked once");
}

Repository repository = gitRepository.getRepository();
Git git = gitRepository.getGit();
org.eclipse.jgit.lib.Ref ref = repository.getRef(rewriteBranchName);
if (ref == null) {
CheckoutCommand checkout = git.checkout();
checkout.setName(rewriteBranchName);
checkout.setCreateBranch(true);
org.eclipse.jgit.lib.Ref rewriteRef = checkout.call();
rewriteBranch = new RewriteBranch(rewriteRef, gitRepository, this);
return rewriteBranch;
} else {
throw new RewriteBranchExistsException(
"Can not begin history rewrite. Branch "
+ rewriteBranchName + " already exists",
ref.getName());
}
public IndexUpdate begin() throws IOException, GitAPIException {
indexUpdate = new IndexUpdate(gitRepository, this);
return indexUpdate;
}

public void close() throws GitAPIException {
if (rewriteBranch != null) {
rewriteBranch.close();
Git git = gitRepository.getGit();
git.reset().setMode(ResetType.HARD).call();

if (indexUpdate != null) {
indexUpdate.close();
}
}
}
Loading

0 comments on commit c1b0abc

Please sign in to comment.