mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-12-21 15:59:48 +01:00
Cleanup
This commit is contained in:
@@ -73,6 +73,7 @@ import java.util.Optional;
|
|||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
import static java.util.Optional.empty;
|
import static java.util.Optional.empty;
|
||||||
|
import static java.util.Optional.of;
|
||||||
import static sonia.scm.ContextEntry.ContextBuilder.entity;
|
import static sonia.scm.ContextEntry.ContextBuilder.entity;
|
||||||
import static sonia.scm.NotFoundException.notFound;
|
import static sonia.scm.NotFoundException.notFound;
|
||||||
import static sonia.scm.repository.spi.SyncAsyncExecutor.ExecutionType.ASYNCHRONOUS;
|
import static sonia.scm.repository.spi.SyncAsyncExecutor.ExecutionType.ASYNCHRONOUS;
|
||||||
@@ -101,24 +102,12 @@ public class GitBrowseCommand extends AbstractGitCommand
|
|||||||
|
|
||||||
private BrowserResult browserResult;
|
private BrowserResult browserResult;
|
||||||
|
|
||||||
//~--- constructors ---------------------------------------------------------
|
public GitBrowseCommand(GitContext context, Repository repository, LfsBlobStoreFactory lfsBlobStoreFactory, SyncAsyncExecutor executor) {
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructs ...
|
|
||||||
* @param context
|
|
||||||
* @param repository
|
|
||||||
* @param lfsBlobStoreFactory
|
|
||||||
* @param executor
|
|
||||||
*/
|
|
||||||
public GitBrowseCommand(GitContext context, Repository repository, LfsBlobStoreFactory lfsBlobStoreFactory, SyncAsyncExecutor executor)
|
|
||||||
{
|
|
||||||
super(context, repository);
|
super(context, repository);
|
||||||
this.lfsBlobStoreFactory = lfsBlobStoreFactory;
|
this.lfsBlobStoreFactory = lfsBlobStoreFactory;
|
||||||
this.executor = executor;
|
this.executor = executor;
|
||||||
}
|
}
|
||||||
|
|
||||||
//~--- get methods ----------------------------------------------------------
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public BrowserResult getBrowserResult(BrowseCommandRequest request)
|
public BrowserResult getBrowserResult(BrowseCommandRequest request)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
@@ -131,8 +120,7 @@ public class GitBrowseCommand extends AbstractGitCommand
|
|||||||
browserResult = new BrowserResult(revId.getName(), request.getRevision(), getEntry(repo, request, revId));
|
browserResult = new BrowserResult(revId.getName(), request.getRevision(), getEntry(repo, request, revId));
|
||||||
executor.execute(executionType -> {
|
executor.execute(executionType -> {
|
||||||
if (executionType == ASYNCHRONOUS) {
|
if (executionType == ASYNCHRONOUS) {
|
||||||
request.updateCache(browserResult);
|
updateCache(request);
|
||||||
logger.info("updated browser result for repository {}", repository.getNamespaceAndName());
|
|
||||||
}
|
}
|
||||||
}, () -> {});
|
}, () -> {});
|
||||||
return browserResult;
|
return browserResult;
|
||||||
@@ -156,8 +144,6 @@ public class GitBrowseCommand extends AbstractGitCommand
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//~--- methods --------------------------------------------------------------
|
|
||||||
|
|
||||||
private FileObject createEmtpyRoot() {
|
private FileObject createEmtpyRoot() {
|
||||||
FileObject fileObject = new FileObject();
|
FileObject fileObject = new FileObject();
|
||||||
fileObject.setName("");
|
fileObject.setName("");
|
||||||
@@ -166,18 +152,6 @@ public class GitBrowseCommand extends AbstractGitCommand
|
|||||||
return fileObject;
|
return fileObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Method description
|
|
||||||
*
|
|
||||||
* @param repo
|
|
||||||
* @param request
|
|
||||||
* @param revId
|
|
||||||
* @param treeWalk
|
|
||||||
*
|
|
||||||
* @return
|
|
||||||
*
|
|
||||||
* @throws IOException
|
|
||||||
*/
|
|
||||||
private FileObject createFileObject(org.eclipse.jgit.lib.Repository repo,
|
private FileObject createFileObject(org.eclipse.jgit.lib.Repository repo,
|
||||||
BrowseCommandRequest request, ObjectId revId, TreeWalk treeWalk)
|
BrowseCommandRequest request, ObjectId revId, TreeWalk treeWalk)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
@@ -221,98 +195,42 @@ public class GitBrowseCommand extends AbstractGitCommand
|
|||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
|
|
||||||
//~--- get methods ----------------------------------------------------------
|
private void updateCache(BrowseCommandRequest request) {
|
||||||
|
request.updateCache(browserResult);
|
||||||
/**
|
logger.info("updated browser result for repository {}", repository.getNamespaceAndName());
|
||||||
* Method description
|
|
||||||
*
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* @param repo
|
|
||||||
* @param revId
|
|
||||||
* @param path
|
|
||||||
*
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
private RevCommit getLatestCommit(org.eclipse.jgit.lib.Repository repo,
|
|
||||||
ObjectId revId, String path)
|
|
||||||
{
|
|
||||||
RevCommit result = null;
|
|
||||||
RevWalk walk = null;
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
walk = new RevWalk(repo);
|
|
||||||
walk.setTreeFilter(AndTreeFilter.create(
|
|
||||||
TreeFilter.ANY_DIFF
|
|
||||||
,
|
|
||||||
PathFilter.create(path)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
RevCommit commit = walk.parseCommit(revId);
|
|
||||||
|
|
||||||
walk.markStart(commit);
|
|
||||||
result = Util.getFirst(walk);
|
|
||||||
}
|
|
||||||
catch (IOException ex)
|
|
||||||
{
|
|
||||||
logger.error("could not parse commit for file", ex);
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
GitUtil.release(walk);
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private FileObject getEntry(org.eclipse.jgit.lib.Repository repo, BrowseCommandRequest request, ObjectId revId) throws IOException {
|
private FileObject getEntry(org.eclipse.jgit.lib.Repository repo, BrowseCommandRequest request, ObjectId revId) throws IOException {
|
||||||
RevWalk revWalk = null;
|
|
||||||
TreeWalk treeWalk = null;
|
|
||||||
|
|
||||||
FileObject result;
|
try (RevWalk revWalk = new RevWalk(repo); TreeWalk treeWalk = new TreeWalk(repo)) {
|
||||||
|
|
||||||
try {
|
|
||||||
logger.debug("load repository browser for revision {}", revId.name());
|
logger.debug("load repository browser for revision {}", revId.name());
|
||||||
|
|
||||||
treeWalk = new TreeWalk(repo);
|
|
||||||
if (!isRootRequest(request)) {
|
if (!isRootRequest(request)) {
|
||||||
treeWalk.setFilter(PathFilter.create(request.getPath()));
|
treeWalk.setFilter(PathFilter.create(request.getPath()));
|
||||||
}
|
}
|
||||||
revWalk = new RevWalk(repo);
|
|
||||||
|
|
||||||
RevTree tree = revWalk.parseTree(revId);
|
RevTree tree = revWalk.parseTree(revId);
|
||||||
|
|
||||||
if (tree != null)
|
if (tree != null) {
|
||||||
{
|
|
||||||
treeWalk.addTree(tree);
|
treeWalk.addTree(tree);
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
throw new IllegalStateException("could not find tree for " + revId.name());
|
throw new IllegalStateException("could not find tree for " + revId.name());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isRootRequest(request)) {
|
if (isRootRequest(request)) {
|
||||||
result = createEmtpyRoot();
|
FileObject result = createEmtpyRoot();
|
||||||
findChildren(result, repo, request, revId, treeWalk);
|
findChildren(result, repo, request, revId, treeWalk);
|
||||||
|
return result;
|
||||||
} else {
|
} else {
|
||||||
result = findFirstMatch(repo, request, revId, treeWalk);
|
FileObject result = findFirstMatch(repo, request, revId, treeWalk);
|
||||||
if ( result.isDirectory() ) {
|
if ( result.isDirectory() ) {
|
||||||
treeWalk.enterSubtree();
|
treeWalk.enterSubtree();
|
||||||
findChildren(result, repo, request, revId, treeWalk);
|
findChildren(result, repo, request, revId, treeWalk);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
GitUtil.release(revWalk);
|
|
||||||
GitUtil.release(treeWalk);
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private boolean isRootRequest(BrowseCommandRequest request) {
|
private boolean isRootRequest(BrowseCommandRequest request) {
|
||||||
return Strings.isNullOrEmpty(request.getPath()) || "/".equals(request.getPath());
|
return Strings.isNullOrEmpty(request.getPath()) || "/".equals(request.getPath());
|
||||||
@@ -368,7 +286,6 @@ public class GitBrowseCommand extends AbstractGitCommand
|
|||||||
throw notFound(entity("File", request.getPath()).in("Revision", revId.getName()).in(this.repository));
|
throw notFound(entity("File", request.getPath()).in("Revision", revId.getName()).in(this.repository));
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
private Map<String,
|
private Map<String,
|
||||||
SubRepository> getSubRepositories(org.eclipse.jgit.lib.Repository repo,
|
SubRepository> getSubRepositories(org.eclipse.jgit.lib.Repository repo,
|
||||||
ObjectId revision)
|
ObjectId revision)
|
||||||
@@ -443,9 +360,9 @@ public class GitBrowseCommand extends AbstractGitCommand
|
|||||||
@Override
|
@Override
|
||||||
public void accept(SyncAsyncExecutor.ExecutionType executionType) {
|
public void accept(SyncAsyncExecutor.ExecutionType executionType) {
|
||||||
logger.trace("fetch last commit for {} at {}", path, revId.getName());
|
logger.trace("fetch last commit for {} at {}", path, revId.getName());
|
||||||
RevCommit commit = GitBrowseCommand.this.getLatestCommit(repo, revId, path);
|
Optional<RevCommit> commit = getLatestCommit(repo, revId, path);
|
||||||
|
|
||||||
Optional<LfsPointer> lfsPointer = getLfsPointer(commit);
|
Optional<LfsPointer> lfsPointer = commit.flatMap(this::getLfsPointer);
|
||||||
|
|
||||||
if (lfsPointer.isPresent()) {
|
if (lfsPointer.isPresent()) {
|
||||||
setFileLengthFromLfsBlob(lfsPointer.get());
|
setFileLengthFromLfsBlob(lfsPointer.get());
|
||||||
@@ -454,13 +371,28 @@ public class GitBrowseCommand extends AbstractGitCommand
|
|||||||
}
|
}
|
||||||
|
|
||||||
file.setPartialResult(false);
|
file.setPartialResult(false);
|
||||||
if (commit != null) {
|
if (commit.isPresent()) {
|
||||||
applyValuesFromCommit(executionType, commit);
|
applyValuesFromCommit(executionType, commit.get());
|
||||||
} else {
|
} else {
|
||||||
logger.warn("could not find latest commit for {} on {}", path, revId);
|
logger.warn("could not find latest commit for {} on {}", path, revId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Optional<RevCommit> getLatestCommit(org.eclipse.jgit.lib.Repository repo,
|
||||||
|
ObjectId revId, String path) {
|
||||||
|
try (RevWalk walk = new RevWalk(repo)) {
|
||||||
|
walk.setTreeFilter(AndTreeFilter.create(TreeFilter.ANY_DIFF, PathFilter.create(path)));
|
||||||
|
|
||||||
|
RevCommit commit = walk.parseCommit(revId);
|
||||||
|
|
||||||
|
walk.markStart(commit);
|
||||||
|
return of(Util.getFirst(walk));
|
||||||
|
} catch (IOException ex) {
|
||||||
|
logger.error("could not parse commit for file", ex);
|
||||||
|
return empty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void setFileLengthFromLfsBlob(LfsPointer lfsPointer) {
|
private void setFileLengthFromLfsBlob(LfsPointer lfsPointer) {
|
||||||
BlobStore lfsBlobStore = lfsBlobStoreFactory.getLfsBlobStore(repository);
|
BlobStore lfsBlobStore = lfsBlobStoreFactory.getLfsBlobStore(repository);
|
||||||
String oid = lfsPointer.getOid().getName();
|
String oid = lfsPointer.getOid().getName();
|
||||||
@@ -477,19 +409,16 @@ public class GitBrowseCommand extends AbstractGitCommand
|
|||||||
file.setLastModified(GitUtil.getCommitTime(commit));
|
file.setLastModified(GitUtil.getCommitTime(commit));
|
||||||
file.setDescription(commit.getShortMessage());
|
file.setDescription(commit.getShortMessage());
|
||||||
if (executionType == ASYNCHRONOUS && browserResult != null) {
|
if (executionType == ASYNCHRONOUS && browserResult != null) {
|
||||||
request.updateCache(browserResult);
|
updateCache(request);
|
||||||
logger.info("updated browser result for repository {}", repository.getNamespaceAndName());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Optional<LfsPointer> getLfsPointer(RevCommit commit) {
|
private Optional<LfsPointer> getLfsPointer(RevCommit commit) {
|
||||||
Optional<LfsPointer> lfsPointer;
|
|
||||||
try {
|
try {
|
||||||
lfsPointer = commit == null ? empty() : GitUtil.getLfsPointer(repo, path, commit, treeWalk);
|
return GitUtil.getLfsPointer(repo, path, commit, treeWalk);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new InternalRepositoryException(repository, "could not read lfs pointer", e);
|
throw new InternalRepositoryException(repository, "could not read lfs pointer", e);
|
||||||
}
|
}
|
||||||
return lfsPointer;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -506,8 +435,7 @@ public class GitBrowseCommand extends AbstractGitCommand
|
|||||||
public void run() {
|
public void run() {
|
||||||
file.setPartialResult(false);
|
file.setPartialResult(false);
|
||||||
file.setComputationAborted(true);
|
file.setComputationAborted(true);
|
||||||
request.updateCache(browserResult);
|
updateCache(request);
|
||||||
logger.info("updated browser result for repository {}", repository.getNamespaceAndName());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user