Put repository in git context to create proper exceptions

This commit is contained in:
René Pfeuffer
2018-11-08 16:42:45 +01:00
parent cf519a7545
commit 78da8c743f
9 changed files with 34 additions and 26 deletions

View File

@@ -38,6 +38,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.repository.GitUtil;
import sonia.scm.repository.Repository;
//~--- JDK imports ------------------------------------------------------------
@@ -65,10 +66,12 @@ public class GitContext implements Closeable
*
*
* @param directory
* @param repository
*/
public GitContext(File directory)
public GitContext(File directory, Repository repository)
{
this.directory = directory;
this.repository = repository;
}
//~--- methods --------------------------------------------------------------
@@ -82,8 +85,8 @@ public class GitContext implements Closeable
{
logger.trace("close git repository {}", directory);
GitUtil.close(repository);
repository = null;
GitUtil.close(gitRepository);
gitRepository = null;
}
/**
@@ -96,13 +99,17 @@ public class GitContext implements Closeable
*/
public org.eclipse.jgit.lib.Repository open() throws IOException
{
if (repository == null)
if (gitRepository == null)
{
logger.trace("open git repository {}", directory);
repository = GitUtil.open(directory);
gitRepository = GitUtil.open(directory);
}
return gitRepository;
}
Repository getRepository() {
return repository;
}
@@ -114,7 +121,8 @@ public class GitContext implements Closeable
/** Field description */
private final File directory;
private final Repository repository;
/** Field description */
private org.eclipse.jgit.lib.Repository repository;
private org.eclipse.jgit.lib.Repository gitRepository;
}

View File

@@ -45,7 +45,7 @@ public class GitMergeCommand extends AbstractGitCommand implements MergeCommand
logger.debug("cloned repository to folder {}", repository.getWorkTree());
return new MergeWorker(repository, request).merge();
} catch (IOException e) {
throw new InternalRepositoryException("could not clone repository for merge", e);
throw new InternalRepositoryException(context.getRepository(), "could not clone repository for merge", e);
}
}
@@ -56,11 +56,11 @@ public class GitMergeCommand extends AbstractGitCommand implements MergeCommand
ResolveMerger merger = (ResolveMerger) MergeStrategy.RECURSIVE.newMerger(repository, true);
return new MergeDryRunCommandResult(merger.merge(repository.resolve(request.getBranchToMerge()), repository.resolve(request.getTargetBranch())));
} catch (IOException e) {
throw new InternalRepositoryException("could not clone repository for merge", e);
throw new InternalRepositoryException(context.getRepository(), "could not clone repository for merge", e);
}
}
private static class MergeWorker {
private class MergeWorker {
private final String target;
private final String toMerge;
@@ -92,7 +92,7 @@ public class GitMergeCommand extends AbstractGitCommand implements MergeCommand
try {
clone.checkout().setName(target).call();
} catch (GitAPIException e) {
throw new InternalRepositoryException("could not checkout target branch for merge: " + target, e);
throw new InternalRepositoryException(context.getRepository(), "could not checkout target branch for merge: " + target, e);
}
}
@@ -104,7 +104,7 @@ public class GitMergeCommand extends AbstractGitCommand implements MergeCommand
.include(toMerge, resolveRevision(toMerge))
.call();
} catch (GitAPIException e) {
throw new InternalRepositoryException("could not merge branch " + toMerge + " into " + target, e);
throw new InternalRepositoryException(context.getRepository(), "could not merge branch " + toMerge + " into " + target, e);
}
return result;
}
@@ -118,7 +118,7 @@ public class GitMergeCommand extends AbstractGitCommand implements MergeCommand
.setMessage(MessageFormat.format(determineMessageTemplate(), toMerge, target))
.call();
} catch (GitAPIException e) {
throw new InternalRepositoryException("could not commit merge between branch " + toMerge + " and " + target, e);
throw new InternalRepositoryException(context.getRepository(), "could not commit merge between branch " + toMerge + " and " + target, e);
}
}
@@ -147,7 +147,7 @@ public class GitMergeCommand extends AbstractGitCommand implements MergeCommand
try {
clone.push().call();
} catch (GitAPIException e) {
throw new InternalRepositoryException("could not push merged branch " + toMerge + " to origin", e);
throw new InternalRepositoryException(context.getRepository(), "could not push merged branch " + toMerge + " to origin", e);
}
logger.debug("pushed merged branch {}", target);
}

View File

@@ -73,7 +73,7 @@ public class GitRepositoryServiceProvider extends RepositoryServiceProvider
public GitRepositoryServiceProvider(GitRepositoryHandler handler, Repository repository) {
this.handler = handler;
this.repository = repository;
this.context = new GitContext(handler.getDirectory(repository));
this.context = new GitContext(handler.getDirectory(repository), repository);
}
//~--- methods --------------------------------------------------------------

View File

@@ -33,9 +33,9 @@ public class SimpleGitWorkdirFactory implements GitWorkdirFactory {
Repository clone = cloneRepository(gitContext.getDirectory(), createNewWorkdir());
return new WorkingCopy(clone, this::close);
} catch (GitAPIException e) {
throw new InternalRepositoryException("could not clone working copy of repository", e);
throw new InternalRepositoryException(gitContext.getRepository(), "could not clone working copy of repository", e);
} catch (IOException e) {
throw new InternalRepositoryException("could not create temporary directory for clone of repository", e);
throw new InternalRepositoryException(gitContext.getRepository(), "could not create temporary directory for clone of repository", e);
}
}

View File

@@ -65,7 +65,7 @@ public class AbstractGitCommandTestBase extends ZippedRepositoryTestBase
{
if (context == null)
{
context = new GitContext(repositoryDirectory);
context = new GitContext(repositoryDirectory, repository);
}
return context;

View File

@@ -105,7 +105,7 @@ public class GitIncomingCommandTest
commit(outgoing, "added a");
GitPullCommand pull = new GitPullCommand(handler, new GitContext(incomingDirectory), incomingRepository);
GitPullCommand pull = new GitPullCommand(handler, new GitContext(incomingDirectory, null), incomingRepository);
PullCommandRequest req = new PullCommandRequest();
req.setRemoteRepository(outgoingRepository);
pull.pull(req);
@@ -191,7 +191,7 @@ public class GitIncomingCommandTest
*/
private GitIncomingCommand createCommand()
{
return new GitIncomingCommand(handler, new GitContext(incomingDirectory),
return new GitIncomingCommand(handler, new GitContext(incomingDirectory, null),
incomingRepository);
}
}

View File

@@ -18,8 +18,8 @@ public class GitModificationsCommandTest extends AbstractRemoteCommandTestBase {
@Before
public void init() {
incomingModificationsCommand = new GitModificationsCommand(new GitContext(incomingDirectory), incomingRepository);
outgoingModificationsCommand = new GitModificationsCommand(new GitContext(outgoingDirectory), outgoingRepository);
incomingModificationsCommand = new GitModificationsCommand(new GitContext(incomingDirectory, null), incomingRepository);
outgoingModificationsCommand = new GitModificationsCommand(new GitContext(outgoingDirectory, null), outgoingRepository);
}
@Test
@@ -63,12 +63,12 @@ public class GitModificationsCommandTest extends AbstractRemoteCommandTestBase {
}
void pushOutgoingAndPullIncoming() throws IOException {
GitPushCommand cmd = new GitPushCommand(handler, new GitContext(outgoingDirectory),
GitPushCommand cmd = new GitPushCommand(handler, new GitContext(outgoingDirectory, null),
outgoingRepository);
PushCommandRequest request = new PushCommandRequest();
request.setRemoteRepository(incomingRepository);
cmd.push(request);
GitPullCommand pullCommand = new GitPullCommand(handler, new GitContext(incomingDirectory),
GitPullCommand pullCommand = new GitPullCommand(handler, new GitContext(incomingDirectory, null),
incomingRepository);
PullCommandRequest pullRequest = new PullCommandRequest();
pullRequest.setRemoteRepository(incomingRepository);

View File

@@ -106,7 +106,7 @@ public class GitOutgoingCommandTest extends AbstractRemoteCommandTestBase
commit(outgoing, "added a");
GitPushCommand push = new GitPushCommand(handler,
new GitContext(outgoingDirectory),
new GitContext(outgoingDirectory, null),
outgoingRepository);
PushCommandRequest req = new PushCommandRequest();
@@ -161,7 +161,7 @@ public class GitOutgoingCommandTest extends AbstractRemoteCommandTestBase
*/
private GitOutgoingCommand createCommand()
{
return new GitOutgoingCommand(handler, new GitContext(outgoingDirectory),
return new GitOutgoingCommand(handler, new GitContext(outgoingDirectory, null),
outgoingRepository);
}
}

View File

@@ -99,7 +99,7 @@ public class GitPushCommandTest extends AbstractRemoteCommandTestBase
*/
private GitPushCommand createCommand()
{
return new GitPushCommand(handler, new GitContext(outgoingDirectory),
return new GitPushCommand(handler, new GitContext(outgoingDirectory, null),
outgoingRepository);
}
}