Skip to content

Commit

Permalink
#3: Create any GitCommand via an injected command factory
Browse files Browse the repository at this point in the history
- add interface IGitCommandFactory.java
- add DefaultGitCommandFactory.java as default implementation
- ScmProviderGit.java:
  - update to get GitCommand instances via IGitCommandFactory
  • Loading branch information
mhoffrog committed Feb 18, 2024
1 parent ad51be3 commit 9a83a9b
Show file tree
Hide file tree
Showing 3 changed files with 319 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.inject.Inject;

import org.eclipse.jgit.api.AddCommand;
import org.eclipse.jgit.api.CheckoutCommand;
import org.eclipse.jgit.api.CloneCommand;
Expand Down Expand Up @@ -61,6 +63,7 @@
import com.itemis.maven.plugins.unleash.scm.ScmProvider;
import com.itemis.maven.plugins.unleash.scm.ScmProviderInitialization;
import com.itemis.maven.plugins.unleash.scm.annotations.ScmProviderType;
import com.itemis.maven.plugins.unleash.scm.providers.cdi.IGitCommandFactory;
import com.itemis.maven.plugins.unleash.scm.providers.merge.UnleashGitFullMergeStrategy;
import com.itemis.maven.plugins.unleash.scm.providers.util.GitUtil;
import com.itemis.maven.plugins.unleash.scm.requests.BranchRequest;
Expand Down Expand Up @@ -93,6 +96,8 @@ public class ScmProviderGit implements ScmProvider {
private File workingDir;
private List<String> additionalThingsToPush;
private GitUtil util;
@Inject
private IGitCommandFactory gitCommandFactory;

@Override
public void initialize(final ScmProviderInitialization initialization) {
Expand Down Expand Up @@ -167,7 +172,8 @@ public void checkout(CheckoutRequest request) throws ScmException {
this.log.fine(message.toString());
}

CloneCommand clone = Git.cloneRepository().setDirectory(this.workingDir).setURI(request.getRemoteRepositoryUrl());
CloneCommand clone = this.gitCommandFactory.getCloneCommand().setDirectory(this.workingDir)
.setURI(request.getRemoteRepositoryUrl());
setAuthenticationDetails(clone);
if (!request.checkoutWholeRepository()) {
clone.setNoCheckout(true);
Expand Down Expand Up @@ -197,7 +203,8 @@ public void checkout(CheckoutRequest request) throws ScmException {
}

try {
CheckoutCommand checkout = this.git.checkout().setStartPoint(revision).setAllPaths(false);
CheckoutCommand checkout = this.gitCommandFactory.getCheckoutCommand(this.git, request).setStartPoint(revision)
.setAllPaths(false);
for (String path : request.getPathsToCheckout()) {
checkout.addPath(path);
}
Expand All @@ -224,8 +231,9 @@ public void checkout(CheckoutRequest request) throws ScmException {
}

try {
CheckoutCommand checkout = this.git.checkout().setName(request.getBranch().get()).setCreateBranch(true)
.setUpstreamMode(SetupUpstreamMode.SET_UPSTREAM).setStartPoint(startPoint);
CheckoutCommand checkout = this.gitCommandFactory.getCheckoutCommand(this.git, request)
.setName(request.getBranch().get()).setCreateBranch(true).setUpstreamMode(SetupUpstreamMode.SET_UPSTREAM)
.setStartPoint(startPoint);
checkout.call();
} catch (GitAPIException e) {
throw new ScmException(ScmOperation.CHECKOUT, "Unable to checkout '" + request.getBranch().get()
Expand All @@ -246,7 +254,8 @@ public void checkout(CheckoutRequest request) throws ScmException {
}
if (hasTag(request.getTag().get())) {
try {
CheckoutCommand checkout = this.git.checkout().setName(request.getTag().get());
CheckoutCommand checkout = this.gitCommandFactory.getCheckoutCommand(this.git, request)
.setName(request.getTag().get());
checkout.call();
} catch (GitAPIException e) {
throw new ScmException(ScmOperation.CHECKOUT, "Unable to checkout tag '" + request.getTag().get()
Expand All @@ -270,7 +279,8 @@ public void checkout(CheckoutRequest request) throws ScmException {
this.log.fine(message.toString());
}
try {
CheckoutCommand checkout = this.git.checkout().setName(request.getRevision().get());
CheckoutCommand checkout = this.gitCommandFactory.getCheckoutCommand(this.git, request)
.setName(request.getRevision().get());
checkout.call();
} catch (GitAPIException e) {
throw new ScmException(ScmOperation.CHECKOUT,
Expand Down Expand Up @@ -311,7 +321,7 @@ public String commit(CommitRequest request) throws ScmException {
}

// add all changes to be committed (either everything or the specified paths)
AddCommand add = this.git.add();
AddCommand add = this.gitCommandFactory.getAddCommand(this.git);
if (request.commitAllChanges()) {
if (request.includeUntrackedFiles()) {
add.addFilepattern(".");
Expand All @@ -332,7 +342,8 @@ public String commit(CommitRequest request) throws ScmException {
}

// commit all added changes
CommitCommand commit = this.git.commit().setMessage(request.getMessage()).setCommitter(this.personIdent);
CommitCommand commit = this.gitCommandFactory.getCommitCommand(this.git, request).setMessage(request.getMessage())
.setCommitter(this.personIdent);
if (request.commitAllChanges()) {
commit.setAll(true);
} else {
Expand Down Expand Up @@ -392,7 +403,8 @@ public String push(PushRequest request) throws ScmException {

try {
// 2. push local changes to remote repository
PushCommand push = this.git.push().setRemote(remoteName).setPushAll().setPushTags();
PushCommand push = this.gitCommandFactory.getPushCommand(this.git).setRemote(remoteName).setPushAll()
.setPushTags();
setAuthenticationDetails(push);
for (String additional : this.additionalThingsToPush) {
push.add(additional);
Expand Down Expand Up @@ -455,16 +467,17 @@ public String update(UpdateRequest request) throws ScmException {
}

try {
FetchCommand fetch = this.git.fetch().setRemote(remoteName).setTagOpt(TagOpt.AUTO_FOLLOW)
.setRemoveDeletedRefs(true);
FetchCommand fetch = this.gitCommandFactory.getFetchCommand(this.git).setRemote(remoteName)
.setTagOpt(TagOpt.AUTO_FOLLOW).setRemoveDeletedRefs(true);
setAuthenticationDetails(fetch);
fetch.call();
} catch (GitAPIException e) {
throw new ScmException(ScmOperation.UPDATE,
"Could not fetch changes from Git remote '" + remoteName + " [" + connectionUrl + "]'.", e);
}

MergeCommand merge = this.git.merge().setFastForward(FastForwardMode.FF).setCommit(true).setMessage("Merge");
MergeCommand merge = this.gitCommandFactory.getMergeCommand(this.git).setFastForward(FastForwardMode.FF)
.setCommit(true).setMessage("Merge");
switch (request.getMergeStrategy()) {
case USE_LOCAL:
merge.setStrategy(MergeStrategy.OURS);
Expand Down Expand Up @@ -558,8 +571,8 @@ public String tag(TagRequest request) throws ScmException {

try {
// 2. tag local revision
TagCommand tag = this.git.tag().setName(request.getTagName()).setMessage(request.getMessage())
.setAnnotated(true).setTagger(this.personIdent);
TagCommand tag = this.gitCommandFactory.getTagCommand(this.git, request).setName(request.getTagName())
.setMessage(request.getMessage()).setAnnotated(true).setTagger(this.personIdent);
tag.call();
} catch (GitAPIException e) {
throw new ScmException(ScmOperation.TAG, "An error occurred during local Git tag creation.", e);
Expand All @@ -568,7 +581,8 @@ public String tag(TagRequest request) throws ScmException {
if (!request.commitBeforeTagging()) {
try {
// 3. deletes the local commit that had been done for tag creation.
this.git.reset().setMode(ResetType.MIXED).setRef(Constants.HEAD + "~1").call();
this.gitCommandFactory.getResetCommand(this.git).setMode(ResetType.MIXED).setRef(Constants.HEAD + "~1")
.call();
} catch (GitAPIException e) {
throw new ScmException(ScmOperation.TAG,
"An error occurred during local commit resetting (no pre-tag commit was requested).", e);
Expand All @@ -589,7 +603,7 @@ public String tag(TagRequest request) throws ScmException {
String remoteName = this.util.getRemoteName(localBranchName);
String connectionUrl = this.util.getConnectionUrlOfRemote(remoteName);
try {
PushCommand push = this.git.push().setRemote(remoteName).add(tagPushName);
PushCommand push = this.gitCommandFactory.getPushCommand(this.git).setRemote(remoteName).add(tagPushName);
setAuthenticationDetails(push);
push.call();
newRevision = getLatestRemoteRevision();
Expand Down Expand Up @@ -635,7 +649,8 @@ public boolean hasTag(String tagName) throws ScmException {
}

try {
LsRemoteCommand lsRemote = this.git.lsRemote().setRemote(remoteName).setTags(true);
LsRemoteCommand lsRemote = this.gitCommandFactory.getLsRemoteCommand(this.git).setRemote(remoteName)
.setTags(true);
setAuthenticationDetails(lsRemote);

String tagRefName = GitUtil.TAG_NAME_PREFIX + tagName;
Expand Down Expand Up @@ -668,7 +683,8 @@ public String deleteTag(DeleteTagRequest request) throws ScmException {
this.log.fine(LOG_PREFIX + "Fetching remote tag");
}
try {
FetchCommand fetch = this.git.fetch().setRemote(remoteName).setTagOpt(TagOpt.FETCH_TAGS);
FetchCommand fetch = this.gitCommandFactory.getFetchCommand(this.git).setRemote(remoteName)
.setTagOpt(TagOpt.FETCH_TAGS);
setAuthenticationDetails(fetch);
fetch.call();
} catch (GitAPIException e) {
Expand All @@ -689,7 +705,8 @@ public String deleteTag(DeleteTagRequest request) throws ScmException {

try {
// 2. delete the tag locally
DeleteTagCommand deleteTag = this.git.tagDelete().setTags(GitUtil.TAG_NAME_PREFIX + request.getTagName());
DeleteTagCommand deleteTag = this.gitCommandFactory.getDeleteTagCommand(this.git, request)
.setTags(GitUtil.TAG_NAME_PREFIX + request.getTagName());
deleteTag.call();
} catch (GitAPIException e) {
throw new ScmException(ScmOperation.DELETE_TAG,
Expand All @@ -702,7 +719,7 @@ public String deleteTag(DeleteTagRequest request) throws ScmException {
if (hasRemoteTag) {
String tagPushName = ":" + GitUtil.TAG_NAME_PREFIX + request.getTagName();
if (request.push()) {
PushCommand push = this.git.push().setRemote(remoteName).add(tagPushName);
PushCommand push = this.gitCommandFactory.getPushCommand(this.git).setRemote(remoteName).add(tagPushName);
setAuthenticationDetails(push);
push.call();
} else {
Expand Down Expand Up @@ -761,8 +778,9 @@ public String branch(BranchRequest request) throws ScmException {

try {
// 2. branch from WC
CreateBranchCommand branch = this.git.branchCreate().setName(request.getBranchName())
.setUpstreamMode(SetupUpstreamMode.TRACK).setStartPoint(request.getRevision().or(Constants.HEAD));
CreateBranchCommand branch = this.gitCommandFactory.getCreateBranchCommand(this.git, request)
.setName(request.getBranchName()).setUpstreamMode(SetupUpstreamMode.TRACK)
.setStartPoint(request.getRevision().or(Constants.HEAD));
branch.call();
} catch (GitAPIException e) {
throw new ScmException(ScmOperation.BRANCH, "Could not create local branch '" + request.getBranchName()
Expand All @@ -772,7 +790,8 @@ public String branch(BranchRequest request) throws ScmException {
// 3. deletes the local commit that had been done for branch creation.
if (!request.commitBeforeBranching()) {
try {
this.git.reset().setMode(ResetType.MIXED).setRef(Constants.HEAD + "~1").call();
this.gitCommandFactory.getResetCommand(this.git).setMode(ResetType.MIXED).setRef(Constants.HEAD + "~1")
.call();
} catch (GitAPIException e) {
throw new ScmException(ScmOperation.BRANCH,
"An error occurred during local commit resetting (no pre-branch commit was requested).", e);
Expand All @@ -792,7 +811,8 @@ public String branch(BranchRequest request) throws ScmException {
String remoteName = this.util.getRemoteName(localBranchName);
String connectionUrl = this.util.getConnectionUrlOfRemote(remoteName);
try {
PushCommand push = this.git.push().setRemote(remoteName).add(branchPushName);
PushCommand push = this.gitCommandFactory.getPushCommand(this.git).setRemote(remoteName)
.add(branchPushName);
setAuthenticationDetails(push);
push.call();
newRevision = getLatestRemoteRevision();
Expand Down Expand Up @@ -836,7 +856,8 @@ public boolean hasBranch(String branchName) throws ScmException {
}

try {
LsRemoteCommand lsRemote = this.git.lsRemote().setRemote(remoteName).setHeads(true);
LsRemoteCommand lsRemote = this.gitCommandFactory.getLsRemoteCommand(this.git).setRemote(remoteName)
.setHeads(true);
setAuthenticationDetails(lsRemote);
Collection<Ref> branches = lsRemote.call();
for (Ref branch : branches) {
Expand Down Expand Up @@ -871,8 +892,8 @@ public String deleteBranch(DeleteBranchRequest request) throws ScmException {

if (this.util.hasLocalBranch(request.getBranchName())) {
try {
this.git.branchDelete().setBranchNames(GitUtil.HEADS_NAME_PREFIX + request.getBranchName()).setForce(true)
.call();
this.gitCommandFactory.getDeleteBranchCommand(this.git, request)
.setBranchNames(GitUtil.HEADS_NAME_PREFIX + request.getBranchName()).setForce(true).call();
} catch (GitAPIException e) {
e.printStackTrace();
}
Expand All @@ -881,7 +902,7 @@ public String deleteBranch(DeleteBranchRequest request) throws ScmException {
if (hasBranch(request.getBranchName())) {
if (request.push()) {
try {
PushCommand push = this.git.push().setRemote(remoteName)
PushCommand push = this.gitCommandFactory.getPushCommand(this.git).setRemote(remoteName)
.add(":" + GitUtil.HEADS_NAME_PREFIX + request.getBranchName());
setAuthenticationDetails(push);
push.call();
Expand Down Expand Up @@ -929,7 +950,7 @@ public String revertCommits(RevertCommitsRequest request) throws ScmException {
}

try {
RevertCommand revert = this.git.revert();
RevertCommand revert = this.gitCommandFactory.getRevertCommand(this.git, request);

List<RevCommit> commitsToRevert = this.util.resolveCommitRange(request.getToRevision(),
request.getFromRevision());
Expand Down Expand Up @@ -978,7 +999,7 @@ public String revertCommits(RevertCommitsRequest request) throws ScmException {
@Override
public String getLocalRevision() {
try {
RevCommit revCommit = this.git.log().call().iterator().next();
RevCommit revCommit = this.gitCommandFactory.getLogCommand(this.git).call().iterator().next();
return revCommit.getName();
} catch (GitAPIException e) {
throw new IllegalStateException("Could not determine the last revision commit of the local repository.", e);
Expand All @@ -992,7 +1013,8 @@ public String getLatestRemoteRevision() {
String remoteName = this.util.getRemoteName(localBranchName);
String remoteNameBranch = this.util.getRemoteBranchName(localBranchName);

LsRemoteCommand lsRemote = this.git.lsRemote().setHeads(true).setRemote(remoteName);
LsRemoteCommand lsRemote = this.gitCommandFactory.getLsRemoteCommand(this.git).setHeads(true)
.setRemote(remoteName);
setAuthenticationDetails(lsRemote);
Collection<Ref> branches = lsRemote.call();
for (Ref branch : branches) {
Expand Down Expand Up @@ -1034,7 +1056,7 @@ public HistoryResult getHistory(HistoryRequest request) throws ScmException {
}

try {
LogCommand logCommand = this.git.log();
LogCommand logCommand = this.gitCommandFactory.getLogCommand(this.git);
if (request.getMessageFilters().isEmpty()) {
// set the limit of commits to be retrieved only if no filters are provided since the user wants to see the
// specified number of commits but some could be filtered out
Expand Down Expand Up @@ -1096,7 +1118,7 @@ private AnyObjectId getTagRevisionOrDefault(Optional<String> tag, String default
if (tag.isPresent()) {
try {
String tagName = GitUtil.TAG_NAME_PREFIX + tag.get();
for (Ref ref : this.git.tagList().call()) {
for (Ref ref : this.gitCommandFactory.getListTagCommand(this.git).call()) {
if (Objects.equal(tagName, ref.getName())) {
Ref peeledRef = this.git.getRepository().getRefDatabase().peel(ref);
return MoreObjects.firstNonNull(peeledRef.getPeeledObjectId(), peeledRef.getObjectId());
Expand Down

0 comments on commit 9a83a9b

Please sign in to comment.