Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mode-1999 & Mode-2000 Fixed the handling of exceptional cases for the Git Connector #898

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -25,6 +25,7 @@

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
Expand Down Expand Up @@ -107,8 +108,8 @@ public Document execute( Repository repository,
ObjectId objId = resolveBranchOrTagOrCommitId(repository, branchOrTagOrCommitId);
RevCommit commit = walker.parseCommit(objId);
writer.addProperty(GitLexicon.OBJECT_ID, objId.name());
writer.addProperty(GitLexicon.AUTHOR, commit.getAuthorIdent().getName());
writer.addProperty(GitLexicon.COMMITTER, commit.getCommitterIdent().getName());
writer.addProperty(GitLexicon.AUTHOR, authorName(commit));
writer.addProperty(GitLexicon.COMMITTER, commiterName(commit));
writer.addProperty(GitLexicon.COMMITTED, values.dateFrom(commit.getCommitTime()));
writer.addProperty(GitLexicon.TITLE, commit.getShortMessage());
writer.addProperty(GitLexicon.MESSAGE, commit.getFullMessage().trim());// removes trailing whitespace
Expand Down Expand Up @@ -168,6 +169,11 @@ protected List<DiffEntry> computeDifferences( RevCommit commit,
}
}

if (tw.getTreeCount() == 1) {
connector.getLogger().warn(GitI18n.commitWithSingleParent, commit.getName(), tw.getObjectId(0).name());
return Collections.emptyList();
}

// Now process the diff of each file ...
return DiffEntry.scan(tw);
}
Expand Down
Expand Up @@ -36,6 +36,7 @@
import org.eclipse.jgit.api.LogCommand;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
Expand Down Expand Up @@ -307,6 +308,16 @@ protected boolean isQueryable( CallSpecification callSpec ) {
return false;
}

protected String authorName( RevCommit commit ) {
PersonIdent authorIdent = commit.getAuthorIdent();
return authorIdent != null ? authorIdent.getName() : "<unknown>";
}

protected String commiterName( RevCommit commit ) {
PersonIdent committerIdent = commit.getCommitterIdent();
return committerIdent != null ? committerIdent.getName() : "<unknown>";
}

@Override
public String toString() {
return getName();
Expand Down
Expand Up @@ -117,8 +117,8 @@ public Document execute( Repository repository,
ObjectId objId = repository.resolve(commitId);
RevCommit commit = walker.parseCommit(objId);
writer.addProperty(GitLexicon.OBJECT_ID, objId.name());
writer.addProperty(GitLexicon.AUTHOR, commit.getAuthorIdent().getName());
writer.addProperty(GitLexicon.COMMITTER, commit.getCommitterIdent().getName());
writer.addProperty(GitLexicon.AUTHOR, authorName(commit));
writer.addProperty(GitLexicon.COMMITTER, commiterName(commit));
writer.addProperty(GitLexicon.COMMITTED, values.dateFrom(commit.getCommitTime()));
writer.addProperty(GitLexicon.TITLE, commit.getShortMessage());
writer.addProperty(GitLexicon.TREE, GitTree.referenceToTree(objId, objId.name(), values));
Expand Down
Expand Up @@ -34,6 +34,8 @@ public final class GitI18n {
public static I18n directoryCannotBeRead;
public static I18n remoteDoesNotExist;
public static I18n commitWithMultipleParents;
public static I18n commitWithSingleParent;
public static I18n cannotReadCommit;

static {
try {
Expand Down
Expand Up @@ -93,20 +93,23 @@ public Document execute( Repository repository,
try {
RevCommit commit = walker.parseCommit(objId);

// Add the properties for this node ...
String committer = commit.getCommitterIdent().getName();
String author = commit.getAuthorIdent().getName();
DateTime committed = values.dateFrom(commit.getCommitTime());
writer.setPrimaryType(GitLexicon.FOLDER);
writer.addProperty(JcrLexicon.CREATED, committed);
writer.addProperty(JcrLexicon.CREATED_BY, committer);
writer.addProperty(GitLexicon.OBJECT_ID, objId.name());
writer.addProperty(GitLexicon.AUTHOR, author);
writer.addProperty(GitLexicon.COMMITTER, committer);
writer.addProperty(GitLexicon.COMMITTED, committed);
writer.addProperty(GitLexicon.TITLE, commit.getShortMessage());
writer.addProperty(GitLexicon.HISTORY, GitHistory.referenceToHistory(objId, branchOrTagOrObjectId, values));
writer.addProperty(GitLexicon.DETAIL, GitCommitDetails.referenceToCommit(objId, values));
//could happen if not enough permissions, for example
if (commit != null) {
// Add the properties for this node ...
String committer = commiterName(commit);
String author = authorName(commit);
DateTime committed = values.dateFrom(commit.getCommitTime());
writer.setPrimaryType(GitLexicon.FOLDER);
writer.addProperty(JcrLexicon.CREATED, committed);
writer.addProperty(JcrLexicon.CREATED_BY, committer);
writer.addProperty(GitLexicon.OBJECT_ID, objId.name());
writer.addProperty(GitLexicon.AUTHOR, author);
writer.addProperty(GitLexicon.COMMITTER, committer);
writer.addProperty(GitLexicon.COMMITTED, committed);
writer.addProperty(GitLexicon.TITLE, commit.getShortMessage());
writer.addProperty(GitLexicon.HISTORY, GitHistory.referenceToHistory(objId, branchOrTagOrObjectId, values));
writer.addProperty(GitLexicon.DETAIL, GitCommitDetails.referenceToCommit(objId, values));
}

// Add the top-level children of the directory ...
addInformationForPath(repository, git, writer, commit, "", spec, values);
Expand Down Expand Up @@ -186,19 +189,24 @@ protected void addInformationForPath( Repository repository,
// Find the commit in which this folder was last modified ...
// This may not be terribly efficient, but it seems to work faster on subsequent runs ...
RevCommit folderCommit = git.log().addPath(path).call().iterator().next();

// Add folder-related properties ...
String committer = folderCommit.getCommitterIdent().getName();
String author = folderCommit.getAuthorIdent().getName();
DateTime committed = values.dateFrom(folderCommit.getCommitTime());
writer.setPrimaryType(GitLexicon.FOLDER);
writer.addProperty(JcrLexicon.CREATED, committed);
writer.addProperty(JcrLexicon.CREATED_BY, committer);
writer.addProperty(GitLexicon.OBJECT_ID, folderCommit.getId().name());
writer.addProperty(GitLexicon.AUTHOR, author);
writer.addProperty(GitLexicon.COMMITTER, committer);
writer.addProperty(GitLexicon.COMMITTED, committed);
writer.addProperty(GitLexicon.TITLE, folderCommit.getShortMessage());

//could happen if not enough permissions, for example
if (folderCommit != null) {
// Add folder-related properties ...
String committer = commiterName(folderCommit);
String author = authorName(folderCommit);
DateTime committed = values.dateFrom(folderCommit.getCommitTime());
writer.addProperty(JcrLexicon.CREATED, committed);
writer.addProperty(JcrLexicon.CREATED_BY, committer);
writer.addProperty(GitLexicon.OBJECT_ID, folderCommit.getId().name());
writer.addProperty(GitLexicon.AUTHOR, author);
writer.addProperty(GitLexicon.COMMITTER, committer);
writer.addProperty(GitLexicon.COMMITTED, committed);
writer.addProperty(GitLexicon.TITLE, folderCommit.getShortMessage());
} else {
connector.getLogger().warn(GitI18n.cannotReadCommit, path);
}

// And now walk the contents of the directory ...
while (tw.next()) {
Expand All @@ -213,12 +221,18 @@ protected void addInformationForPath( Repository repository,
// This may not be terribly efficient, but it seems to work faster on subsequent runs ...
RevCommit fileCommit = git.log().addPath(path).call().iterator().next();

// Add file-related properties ...
String committer = fileCommit.getCommitterIdent().getName();
String author = fileCommit.getAuthorIdent().getName();
DateTime committed = values.dateFrom(fileCommit.getCommitTime());
if (isContentNode) {
writer.setPrimaryType(GitLexicon.RESOURCE);
if (fileCommit == null) {
//could happen if not enough permissions, for example
connector.getLogger().warn(GitI18n.cannotReadCommit, path);
return;
}
// Add file-related properties ...
String committer = commiterName(fileCommit);
String author = authorName(fileCommit);
DateTime committed = values.dateFrom(fileCommit.getCommitTime());

writer.addProperty(JcrLexicon.LAST_MODIFIED, committed);
writer.addProperty(JcrLexicon.LAST_MODIFIED_BY, committer);
writer.addProperty(GitLexicon.OBJECT_ID, fileCommit.getId().name());
Expand Down Expand Up @@ -255,6 +269,16 @@ protected void addInformationForPath( Repository repository,
}
} else {
writer.setPrimaryType(GitLexicon.FILE);
if (fileCommit == null) {
//could happen if not enough permissions, for example
connector.getLogger().warn(GitI18n.cannotReadCommit, path);
return;
}
// Add file-related properties ...
String committer = commiterName(fileCommit);
String author = authorName(fileCommit);
DateTime committed = values.dateFrom(fileCommit.getCommitTime());

writer.addProperty(JcrLexicon.CREATED, committed);
writer.addProperty(JcrLexicon.CREATED_BY, committer);
writer.addProperty(GitLexicon.OBJECT_ID, fileCommit.getId().name());
Expand Down
Expand Up @@ -26,3 +26,5 @@ directoryDoesNotExist = The directory "{0}" does not exist
directoryCannotBeRead = The directory "{0}" cannot be read
remoteDoesNotExist = The remote "{0}" could not be found in the local Git repository at "{1}"
commitWithMultipleParents = The commit "{0}" has multiple parents. Only the first one: "{1}" will be taken into account for computing the differences.
commitWithSingleParent = Cannot compute commit differences because the commit "{0}" has only one parent "{1}".
cannotReadCommit = Cannot read commit "{0}"
Expand Up @@ -145,7 +145,6 @@ public void shouldGetTags() throws Exception {

@Test
public void shouldGetFirstDozenCommitsInHistoryForTag() throws Exception {
print = true;
Ref ref = repository.getRef("modeshape-3.0.0.Final");
ref = repository.peel(ref);
RevWalk walker = new RevWalk(repository);
Expand Down Expand Up @@ -228,19 +227,16 @@ public void shouldComputeTheDiffOfACommit() throws Exception {

@Test
public void shouldGetTopLevelDirectoryContentForCommit() throws Exception {
print = true;
printTreeContent("modeshape-3.0.0.Final", "", true);
}

@Test
public void shouldGetDirectoryContentsAtPathForCommit() throws Exception {
print = true;
printTreeContent("modeshape-3.0.0.Final", "modeshape-jcr/src", true);
}

@Test
public void shouldGetFileInfoAtPathInContent() throws Exception {
print = true;
printTreeContent("modeshape-3.0.0.Final", "modeshape-jcr/src/main/java/org/modeshape/jcr/XmlNodeTypeReader.java", false);
}

Expand Down Expand Up @@ -289,7 +285,10 @@ protected void printTreeContent( String tagOrBranchOrCommit,
// Is this the most efficient way to do this, 'cuz it's expensive?
RevCommit lastCommit = git.log().addPath(parentPath).call().iterator().next();
print("commitMessage", lastCommit.getShortMessage());
print("commiter", lastCommit.getAuthorIdent().getName());
PersonIdent authorIdent = lastCommit.getAuthorIdent();
if (authorIdent != null) {
print("commiter", authorIdent.getName());
}
}
}
} finally {
Expand Down