Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option to obtain properties from a user specified directory #535

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions core/src/main/java/pl/project13/core/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 boolean offline;

protected String projectDirectory;

public GitDataProvider(@Nonnull LoggerBridge log) {
this.log = log;
}
Expand Down Expand Up @@ -114,6 +116,11 @@ public GitDataProvider setOffline(boolean offline) {
return this;
}

public GitDataProvider setProjectDirectory(String projectDirectory) {
this.projectDirectory = projectDirectory;
return this;
}

public void loadGitData(@Nonnull String evaluateOnCommit, @Nonnull Properties properties) throws GitCommitIdExecutionException {
this.evaluateOnCommit = evaluateOnCommit;
init();
Expand Down
55 changes: 39 additions & 16 deletions core/src/main/java/pl/project13/core/JGitProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,31 +82,54 @@ public String getBuildAuthorEmail() throws GitCommitIdExecutionException {
@Override
public void prepareGitToExtractMoreDetailedRepoInformation() throws GitCommitIdExecutionException {
try {
// more details parsed out bellow
Ref evaluateOnCommitReference = git.findRef(evaluateOnCommit);
ObjectId evaluateOnCommitResolvedObjectId = git.resolve(evaluateOnCommit);

if ((evaluateOnCommitReference == null) && (evaluateOnCommitResolvedObjectId == null)) {
throw new GitCommitIdExecutionException("Could not get " + evaluateOnCommit + " Ref, are you sure you have set the dotGitDirectory property of this plugin to a valid path?");
}
revWalk = new RevWalk(git);
ObjectId headObjectId;
if (evaluateOnCommitReference != null) {
headObjectId = evaluateOnCommitReference.getObjectId();
if (projectDirectory != null) {
evalCommit = getCommitFromProjectDirectory(projectDirectory);
} else {
headObjectId = evaluateOnCommitResolvedObjectId;
evalCommit = getCommitFromRef();
}

if (headObjectId == null) {
throw new GitCommitIdExecutionException("Could not get " + evaluateOnCommit + " Ref, are you sure you have some commits in the dotGitDirectory?");
}
evalCommit = revWalk.parseCommit(headObjectId);
revWalk.markStart(evalCommit);
} catch (Exception e) {
throw new GitCommitIdExecutionException("Error", e);
}
}

private RevCommit getCommitFromProjectDirectory(String projectDirectory) throws GitAPIException, GitCommitIdExecutionException {
//retrieve last commit in folder projectDirectory
Iterator<RevCommit> iterator = new Git(git).log().addPath(projectDirectory).call().iterator();
if (!iterator.hasNext()) {
throw new GitCommitIdExecutionException("Could not get commit from folder " + projectDirectory + " , are you sure you have some commits in the folder " + projectDirectory + "?");
}

RevCommit revCommit = iterator.next();
if (revCommit == null) {
throw new GitCommitIdExecutionException("Could not get commit from folder " + projectDirectory + " , are you sure you have some commits in the folder " + projectDirectory + "?");
}

return revCommit;
}

private RevCommit getCommitFromRef() throws IOException, GitCommitIdExecutionException {
// more details parsed out bellow
Ref evaluateOnCommitReference = git.findRef(evaluateOnCommit);
ObjectId evaluateOnCommitResolvedObjectId = git.resolve(evaluateOnCommit);

if ((evaluateOnCommitReference == null) && (evaluateOnCommitResolvedObjectId == null)) {
throw new GitCommitIdExecutionException("Could not get " + evaluateOnCommit + " Ref, are you sure you have set the dotGitDirectory property of this plugin to a valid path?");
}
ObjectId headObjectId;
if (evaluateOnCommitReference != null) {
headObjectId = evaluateOnCommitReference.getObjectId();
} else {
headObjectId = evaluateOnCommitResolvedObjectId;
}

if (headObjectId == null) {
throw new GitCommitIdExecutionException("Could not get " + evaluateOnCommit + " Ref, are you sure you have some commits in the dotGitDirectory?");
}
return revWalk.parseCommit(headObjectId);
}

@Override
public String getBranchName() throws GitCommitIdExecutionException {
try {
Expand Down
16 changes: 15 additions & 1 deletion maven/src/main/java/pl/project13/maven/git/GitCommitIdMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ public class GitCommitIdMojo extends AbstractMojo {
@Parameter(defaultValue = "${project.basedir}/.git")
File dotGitDirectory;

/**
* Get version number / build number based of the last git commit in the module itself,
* and not based on the last git commit of the whole git project.
* Works only in JGit mode.
*/
@Parameter(defaultValue = "false")
boolean enablePerModuleVersions;

/**
* Configuration for the {@code 'git-describe'} command.
* You can modify the dirty marker, abbrev length and other options here.
Expand Down Expand Up @@ -695,6 +703,11 @@ private void loadGitDataWithNativeGit(@Nonnull Properties properties) throws Git
}

private void loadGitDataWithJGit(@Nonnull Properties properties) throws GitCommitIdExecutionException {
String projectDirectory = null;
if (enablePerModuleVersions) {
String projectRootDir = dotGitDirectory.getAbsolutePath().substring(0, dotGitDirectory.getAbsolutePath().indexOf(".git"));
projectDirectory = project.getBasedir().getAbsolutePath().substring(projectRootDir.length()).replace("\\", "/");
}
GitDataProvider jGitProvider = JGitProvider
.on(dotGitDirectory, log)
.setPrefixDot(prefixDot)
Expand All @@ -706,7 +719,8 @@ private void loadGitDataWithJGit(@Nonnull Properties properties) throws GitCommi
.setUseBranchNameFromBuildEnvironment(useBranchNameFromBuildEnvironment)
.setExcludeProperties(excludeProperties)
.setIncludeOnlyProperties(includeOnlyProperties)
.setOffline(offline || settings.isOffline());
.setOffline(offline || settings.isOffline())
.setProjectDirectory(projectDirectory);

jGitProvider.loadGitData(evaluateOnCommit, properties);
}
Expand Down