fix a bug in git submodule detection

This commit is contained in:
Sebastian Sdorra
2015-06-14 18:59:38 +02:00
parent 41df67df8b
commit 6c96a57cf8

View File

@@ -45,6 +45,7 @@ import org.eclipse.jgit.lib.ObjectLoader;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevTree;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.submodule.SubmoduleWalk;
import org.eclipse.jgit.treewalk.TreeWalk;
import org.eclipse.jgit.treewalk.filter.AndTreeFilter;
import org.eclipse.jgit.treewalk.filter.PathFilter;
@@ -168,70 +169,6 @@ public class GitBrowseCommand extends AbstractGitCommand
/**
* Method description
*
*
* @param files
* @param repo
* @param revId
* @param path
*
* @throws IOException
* @throws RepositoryException
*/
private void appendSubModules(List<FileObject> files,
org.eclipse.jgit.lib.Repository repo, ObjectId revId, String path)
throws IOException, RepositoryException
{
path = Util.nonNull(path);
Map<String, SubRepository> subRepositories = subrepositoryCache.get(revId);
if (subRepositories == null)
{
subRepositories = getSubRepositories(repo, revId);
subrepositoryCache.put(revId, subRepositories);
}
if (subRepositories != null)
{
for (Entry<String, SubRepository> e : subRepositories.entrySet())
{
String p = e.getKey();
if (p.startsWith(path))
{
p = p.substring(path.length());
if (p.startsWith("/"))
{
p = p.substring(1);
}
if (p.endsWith("/"))
{
p = p.substring(0, p.length() - 1);
}
if (!p.contains("/"))
{
FileObject fo = new FileObject();
fo.setDirectory(true);
fo.setPath(path);
fo.setName(p);
fo.setSubRepository(e.getValue());
files.add(fo);
}
}
}
}
}
/**
* Method description
*
*
*
*
* @param repo
* @param request
* @param revId
@@ -243,9 +180,9 @@ public class GitBrowseCommand extends AbstractGitCommand
*/
private FileObject createFileObject(org.eclipse.jgit.lib.Repository repo,
BrowseCommandRequest request, ObjectId revId, TreeWalk treeWalk)
throws IOException
throws IOException, RepositoryException
{
FileObject file = null;
FileObject file;
try
{
@@ -256,26 +193,43 @@ public class GitBrowseCommand extends AbstractGitCommand
file.setName(treeWalk.getNameString());
file.setPath(path);
ObjectLoader loader = repo.open(treeWalk.getObjectId(0));
SubRepository sub = null;
file.setDirectory(loader.getType() == Constants.OBJ_TREE);
file.setLength(loader.getSize());
// don't show message and date for directories to improve performance
if (!file.isDirectory() &&!request.isDisableLastCommit())
if (!request.isDisableSubRepositoryDetection())
{
logger.trace("fetch last commit for {} at {}", path, revId.getName());
sub = getSubRepository(repo, revId, path);
}
RevCommit commit = getLatestCommit(repo, revId, path);
if (sub != null)
{
logger.trace("{} seems to be a sub repository", path);
file.setDirectory(true);
file.setSubRepository(sub);
}
else
{
ObjectLoader loader = repo.open(treeWalk.getObjectId(0));
if (commit != null)
file.setDirectory(loader.getType() == Constants.OBJ_TREE);
file.setLength(loader.getSize());
// don't show message and date for directories to improve performance
if (!file.isDirectory() &&!request.isDisableLastCommit())
{
file.setLastModified(GitUtil.getCommitTime(commit));
file.setDescription(commit.getShortMessage());
}
else if (logger.isWarnEnabled())
{
logger.warn("could not find latest commit for {} on {}", path, revId);
logger.trace("fetch last commit for {} at {}", path, revId.getName());
RevCommit commit = getLatestCommit(repo, revId, path);
if (commit != null)
{
file.setLastModified(GitUtil.getCommitTime(commit));
file.setDescription(commit.getShortMessage());
}
else if (logger.isWarnEnabled())
{
logger.warn("could not find latest commit for {} on {}", path,
revId);
}
}
}
}
@@ -385,11 +339,6 @@ public class GitBrowseCommand extends AbstractGitCommand
String path = request.getPath();
if (!request.isDisableSubRepositoryDetection())
{
appendSubModules(files, repo, revId, path);
}
if (Util.isEmpty(path))
{
while (treeWalk.next())
@@ -491,6 +440,28 @@ public class GitBrowseCommand extends AbstractGitCommand
return subRepositories;
}
private SubRepository getSubRepository(org.eclipse.jgit.lib.Repository repo,
ObjectId revId, String path)
throws IOException, RepositoryException
{
Map<String, SubRepository> subRepositories = subrepositoryCache.get(revId);
if (subRepositories == null)
{
subRepositories = getSubRepositories(repo, revId);
subrepositoryCache.put(revId, subRepositories);
}
SubRepository sub = null;
if (subRepositories != null)
{
sub = subRepositories.get(path);
}
return sub;
}
//~--- fields ---------------------------------------------------------------
/** Field description */