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 org.slf4j.LoggerFactory;
import sonia.scm.repository.GitUtil; import sonia.scm.repository.GitUtil;
import sonia.scm.repository.Repository;
//~--- JDK imports ------------------------------------------------------------ //~--- JDK imports ------------------------------------------------------------
@@ -65,10 +66,12 @@ public class GitContext implements Closeable
* *
* *
* @param directory * @param directory
* @param repository
*/ */
public GitContext(File directory) public GitContext(File directory, Repository repository)
{ {
this.directory = directory; this.directory = directory;
this.repository = repository;
} }
//~--- methods -------------------------------------------------------------- //~--- methods --------------------------------------------------------------
@@ -82,8 +85,8 @@ public class GitContext implements Closeable
{ {
logger.trace("close git repository {}", directory); logger.trace("close git repository {}", directory);
GitUtil.close(repository); GitUtil.close(gitRepository);
repository = null; gitRepository = null;
} }
/** /**
@@ -96,13 +99,17 @@ public class GitContext implements Closeable
*/ */
public org.eclipse.jgit.lib.Repository open() throws IOException public org.eclipse.jgit.lib.Repository open() throws IOException
{ {
if (repository == null) if (gitRepository == null)
{ {
logger.trace("open git repository {}", directory); logger.trace("open git repository {}", directory);
repository = GitUtil.open(directory); gitRepository = GitUtil.open(directory);
} }
return gitRepository;
}
Repository getRepository() {
return repository; return repository;
} }
@@ -114,7 +121,8 @@ public class GitContext implements Closeable
/** Field description */ /** Field description */
private final File directory; private final File directory;
private final Repository repository;
/** Field description */ /** 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()); logger.debug("cloned repository to folder {}", repository.getWorkTree());
return new MergeWorker(repository, request).merge(); return new MergeWorker(repository, request).merge();
} catch (IOException e) { } 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); ResolveMerger merger = (ResolveMerger) MergeStrategy.RECURSIVE.newMerger(repository, true);
return new MergeDryRunCommandResult(merger.merge(repository.resolve(request.getBranchToMerge()), repository.resolve(request.getTargetBranch()))); return new MergeDryRunCommandResult(merger.merge(repository.resolve(request.getBranchToMerge()), repository.resolve(request.getTargetBranch())));
} catch (IOException e) { } 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 target;
private final String toMerge; private final String toMerge;
@@ -92,7 +92,7 @@ public class GitMergeCommand extends AbstractGitCommand implements MergeCommand
try { try {
clone.checkout().setName(target).call(); clone.checkout().setName(target).call();
} catch (GitAPIException e) { } 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)) .include(toMerge, resolveRevision(toMerge))
.call(); .call();
} catch (GitAPIException e) { } 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; return result;
} }
@@ -118,7 +118,7 @@ public class GitMergeCommand extends AbstractGitCommand implements MergeCommand
.setMessage(MessageFormat.format(determineMessageTemplate(), toMerge, target)) .setMessage(MessageFormat.format(determineMessageTemplate(), toMerge, target))
.call(); .call();
} catch (GitAPIException e) { } 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 { try {
clone.push().call(); clone.push().call();
} catch (GitAPIException e) { } 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); logger.debug("pushed merged branch {}", target);
} }

View File

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

View File

@@ -33,9 +33,9 @@ public class SimpleGitWorkdirFactory implements GitWorkdirFactory {
Repository clone = cloneRepository(gitContext.getDirectory(), createNewWorkdir()); Repository clone = cloneRepository(gitContext.getDirectory(), createNewWorkdir());
return new WorkingCopy(clone, this::close); return new WorkingCopy(clone, this::close);
} catch (GitAPIException e) { } 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) { } 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) if (context == null)
{ {
context = new GitContext(repositoryDirectory); context = new GitContext(repositoryDirectory, repository);
} }
return context; return context;

View File

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

View File

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

View File

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

View File

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