diff options
author | James Moger <james.moger@gitblit.com> | 2011-07-22 17:47:53 -0400 |
---|---|---|
committer | James Moger <james.moger@gitblit.com> | 2011-07-22 17:47:53 -0400 |
commit | a2709dd91e5d6b18f573016882ccc70799573ad3 (patch) | |
tree | 6b87a08d65c462d9f51b253e7deafb95c7167bbb /src/com/gitblit/utils/DiffUtils.java | |
parent | b1dba764c201f4708b82767b2d91edb6e189ce6f (diff) | |
download | gitblit-a2709dd91e5d6b18f573016882ccc70799573ad3.tar.gz gitblit-a2709dd91e5d6b18f573016882ccc70799573ad3.zip |
Centralize default branch/HEAD resolution (issue 14)
If an object id was not specified Gitblit used HEAD to perform the
operation. This breaks under some conditions like working with a
repository that does not have any commits on master but does have
commits on a vcs-import branch.
The new approach is to centralize the resolution of unspecified object
ids to a common method which resolves HEAD first but uses the most
recently modified branch if HEAD points to nothing.
This commit also includes a non-functional method for creating an empty
branch. I couldn't figure out how to make JGit create an orphaned
branch.
Diffstat (limited to 'src/com/gitblit/utils/DiffUtils.java')
-rw-r--r-- | src/com/gitblit/utils/DiffUtils.java | 33 |
1 files changed, 23 insertions, 10 deletions
diff --git a/src/com/gitblit/utils/DiffUtils.java b/src/com/gitblit/utils/DiffUtils.java index 9f0d04fc..04b5b0b1 100644 --- a/src/com/gitblit/utils/DiffUtils.java +++ b/src/com/gitblit/utils/DiffUtils.java @@ -25,7 +25,7 @@ import org.eclipse.jgit.diff.DiffEntry; import org.eclipse.jgit.diff.DiffFormatter;
import org.eclipse.jgit.diff.RawText;
import org.eclipse.jgit.diff.RawTextComparator;
-import org.eclipse.jgit.lib.Constants;
+import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevTree;
@@ -147,10 +147,15 @@ public class DiffUtils { RevTree commitTree = commit.getTree();
RevTree baseTree;
if (baseCommit == null) {
- final RevWalk rw = new RevWalk(repository);
- RevCommit parent = rw.parseCommit(commit.getParent(0).getId());
- rw.dispose();
- baseTree = parent.getTree();
+ if (commit.getParentCount() > 0) {
+ final RevWalk rw = new RevWalk(repository);
+ RevCommit parent = rw.parseCommit(commit.getParent(0).getId());
+ rw.dispose();
+ baseTree = parent.getTree();
+ } else {
+ // FIXME initial commit. no parent?!
+ baseTree = commitTree;
+ }
} else {
baseTree = baseCommit.getTree();
}
@@ -208,9 +213,14 @@ public class DiffUtils { RevTree commitTree = commit.getTree();
RevTree baseTree;
if (baseCommit == null) {
- final RevWalk rw = new RevWalk(repository);
- RevCommit parent = rw.parseCommit(commit.getParent(0).getId());
- baseTree = parent.getTree();
+ if (commit.getParentCount() > 0) {
+ final RevWalk rw = new RevWalk(repository);
+ RevCommit parent = rw.parseCommit(commit.getParent(0).getId());
+ baseTree = parent.getTree();
+ } else {
+ // FIXME initial commit. no parent?!
+ baseTree = commitTree;
+ }
} else {
baseTree = baseCommit.getTree();
}
@@ -246,12 +256,15 @@ public class DiffUtils { public static List<AnnotatedLine> blame(Repository repository, String blobPath, String objectId) {
List<AnnotatedLine> lines = new ArrayList<AnnotatedLine>();
try {
+ ObjectId object;
if (StringUtils.isEmpty(objectId)) {
- objectId = Constants.HEAD;
+ object = JGitUtils.getDefaultBranch(repository);
+ } else {
+ object = repository.resolve(objectId);
}
BlameCommand blameCommand = new BlameCommand(repository);
blameCommand.setFilePath(blobPath);
- blameCommand.setStartCommit(repository.resolve(objectId));
+ blameCommand.setStartCommit(object);
BlameResult blameResult = blameCommand.call();
RawText rawText = blameResult.getResultContents();
int length = rawText.size();
|