Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/using-the-plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,18 @@ It's really simple to setup this plugin; below is a sample pom that you may base
-->
<skip>false</skip>

<!-- @since 3.0.1 -->
<--
Default (optional):
false

Explanation:
When set to `true`, the plugin will not try to contact any remote repositories.
Any operations will only use the local state of the repo. If set to `false`, it will execute `git fetch` operations
e.g. to determine the `ahead` and `behind` branch information.
-->
<offline>false</offline>

<!-- @since 2.1.12 -->
<!--
Default (optional):
Expand Down
23 changes: 20 additions & 3 deletions src/main/java/pl/project13/maven/git/GitCommitIdMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.settings.Settings;
import org.sonatype.plexus.build.incremental.BuildContext;

import pl.project13.maven.git.build.BuildServerDataProvider;
Expand Down Expand Up @@ -70,6 +71,12 @@ public class GitCommitIdMojo extends AbstractMojo {
@Parameter(property = "session", required = true, readonly = true)
MavenSession session;

/**
* The Maven settings.
*/
@Parameter(property = "settings", required = true, readonly = true)
Settings settings;
Copy link
Collaborator

@TheSnoozer TheSnoozer Jul 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new settings sadly makes the tests fail.

Inside the GitIntegrationTest you also would need to mock them:

  private static Settings mockSettings() {
    Settings settings = mock(Settings.class);
    when(settings.isOffline()).thenReturn(false);
    return settings;
  }

Could you perhaps add this mock? I think besides that this should be good to be merged.


/**
* <p>Set this to {@code 'true'} to inject git properties into all reactor projects, not just the current one.</p>
*
Expand Down Expand Up @@ -331,7 +338,15 @@ public class GitCommitIdMojo extends AbstractMojo {
*/
@Parameter(defaultValue = "true")
boolean injectIntoSysProperties;


/**
* Controls whether the git plugin tries to access remote repos to fetch latest information
* or only use local information.
* @since 3.0.1
*/
@Parameter(defaultValue = "false")
boolean offline;

/**
* Injected {@link BuildContext} to recognize incremental builds.
*/
Expand Down Expand Up @@ -563,7 +578,8 @@ private void loadGitDataWithNativeGit(@Nonnull Properties properties) throws Git
.setCommitIdGenerationMode(commitIdGenerationModeEnum)
.setUseBranchNameFromBuildEnvironment(useBranchNameFromBuildEnvironment)
.setExcludeProperties(excludeProperties)
.setIncludeOnlyProperties(includeOnlyProperties);
.setIncludeOnlyProperties(includeOnlyProperties)
.setOffline(offline || settings.isOffline());

nativeGitProvider.loadGitData(evaluateOnCommit, properties);
} catch (IOException e) {
Expand All @@ -582,7 +598,8 @@ private void loadGitDataWithJGit(@Nonnull Properties properties) throws GitCommi
.setCommitIdGenerationMode(commitIdGenerationModeEnum)
.setUseBranchNameFromBuildEnvironment(useBranchNameFromBuildEnvironment)
.setExcludeProperties(excludeProperties)
.setIncludeOnlyProperties(includeOnlyProperties);
.setIncludeOnlyProperties(includeOnlyProperties)
.setOffline(offline || settings.isOffline());

jGitProvider.loadGitData(evaluateOnCommit, properties);
}
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/pl/project13/maven/git/GitDataProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public abstract class GitDataProvider implements GitProvider {

protected List<String> includeOnlyProperties;

protected boolean offline;

public GitDataProvider(@Nonnull LoggerBridge log) {
this.log = log;
}
Expand Down Expand Up @@ -109,6 +111,11 @@ public GitDataProvider setIncludeOnlyProperties(List<String> includeOnlyProperti
return this;
}

public GitDataProvider setOffline(boolean offline) {
this.offline = offline;
return this;
}

public void loadGitData(@Nonnull String evaluateOnCommit, @Nonnull Properties properties) throws GitCommitIdExecutionException {
this.evaluateOnCommit = evaluateOnCommit;
init();
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/pl/project13/maven/git/JGitProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,9 @@ private Repository getGitRepository() throws GitCommitIdExecutionException {
@Override
public AheadBehind getAheadBehind() throws GitCommitIdExecutionException {
try {
fetch();
if (!offline) {
fetch();
}
Optional<BranchTrackingStatus> branchTrackingStatus = Optional.ofNullable(BranchTrackingStatus.of(git, getBranchName()));
return branchTrackingStatus.map(bts -> AheadBehind.of(bts.getAheadCount(), bts.getBehindCount()))
.orElse(AheadBehind.NO_REMOTE);
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/pl/project13/maven/git/NativeGitProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,9 @@ public AheadBehind getAheadBehind() throws GitCommitIdExecutionException {
if (!remoteBranch.isPresent()) {
return AheadBehind.NO_REMOTE;
}
fetch(remoteBranch.get());
if (!offline) {
fetch(remoteBranch.get());
}
String localBranchName = getBranchName();
String ahead = runQuietGitCommand(canonical, nativeGitTimeoutInMs, "rev-list --right-only --count " + remoteBranch.get() + "..." + localBranchName);
String behind = runQuietGitCommand(canonical, nativeGitTimeoutInMs, "rev-list --left-only --count " + remoteBranch.get() + "..." + localBranchName);
Expand Down