Skip to content
Merged
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
1 change: 1 addition & 0 deletions src/main/java/pl/project13/maven/git/GitCommitIdMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public class GitCommitIdMojo extends AbstractMojo {
public static final String BRANCH = "branch";
public static final String COMMIT_ID = "commit.id";
public static final String COMMIT_ID_ABBREV = "commit.id.abbrev";
public static final String FILES_DIRTY = "commit.files.dirty";
public static final String COMMIT_DESCRIBE = "commit.id.describe";
public static final String COMMIT_SHORT_DESCRIBE = "commit.id.describe-short";
public static final String BUILD_AUTHOR_NAME = "build.user.name";
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/pl/project13/maven/git/GitDataProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public abstract class GitDataProvider {
protected abstract String getGitDescribe() throws MojoExecutionException;
protected abstract String getCommitId();
protected abstract String getAbbrevCommitId() throws MojoExecutionException;
protected abstract boolean isDirty() throws MojoExecutionException;
protected abstract String getCommitAuthorName();
protected abstract String getCommitAuthorEmail();
protected abstract String getCommitMessageFull();
Expand Down Expand Up @@ -65,6 +66,8 @@ public void loadGitData(@NotNull Properties properties) throws IOException, Mojo
put(properties, GitCommitIdMojo.COMMIT_ID, getCommitId());
// git.commit.id.abbrev
put(properties, GitCommitIdMojo.COMMIT_ID_ABBREV, getAbbrevCommitId());
// git.files.dirty
put(properties, GitCommitIdMojo.FILES_DIRTY, Boolean.toString(isDirty()));
// git.commit.author.name
put(properties, GitCommitIdMojo.COMMIT_AUTHOR_NAME, getCommitAuthorName());
// git.commit.author.email
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/pl/project13/maven/git/JGitProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,16 @@ protected String getAbbrevCommitId() throws MojoExecutionException {
return abbrevCommitId;
}

@Override
protected boolean isDirty() throws MojoExecutionException {
Git gitObject = Git.wrap(git);
try {
return !gitObject.status().call().isClean();
Copy link
Collaborator

Choose a reason for hiding this comment

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

looks ok, though we should probably measure performance impact of wrapping at some point...

} catch (GitAPIException e) {
throw new MojoExecutionException("Failed to get git status: " + e.getMessage(), e);
}
}

@Override
protected String getCommitAuthorName() {
String commitAuthor = headCommit.getAuthorIdent().getName();
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/pl/project13/maven/git/NativeGitProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ protected String getAbbrevCommitId() throws MojoExecutionException {
return abbrevCommitId;
}

@Override
protected boolean isDirty() throws MojoExecutionException {
String output = tryToRunGitCommand(canonical, "status --porcelain");
Copy link
Collaborator

Choose a reason for hiding this comment

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

this was very expensive, I have reimplemented it to not kill performance on large dirty repositories :-)
This impl has to wait for the entire status to return, while we only want to know if it's more than 0 lines. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds reasonable. But I'm curious if -s is really faster than --porcelain? On my system both produce the same output (the former being colorized, assuming git thinks it is talking to a tty). On the largest repo I could quickly find (linux kernel) both performed identically (~100ms after 10 runs).

Copy link
Collaborator

Choose a reason for hiding this comment

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

That's not the speed up change.
The speed up change is to not collect the entire output into a string and then check isEmpty() - way too costly. Instead as I described above, we return once there's a line of output. See https://github.com/ktoso/maven-git-commit-id-plugin/blob/master/src/main/java/pl/project13/maven/git/NativeGitProvider.java#L363

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ahh, I missed that.

  1. Doesn't waitFor() wait for the process to exit? So you may not have to sit around reading all of the input in, but git still has to run to completion
  2. This won't work on Windows systems

Copy link
Collaborator

Choose a reason for hiding this comment

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

  1. it's still way faster than consuming the stream into string, as in - doesn't kill my build
  2. I don't think the native impl ever worked on windows.

return !output.trim().isEmpty();
}

@Override
protected String getCommitAuthorName() {
return tryToRunGitCommand(canonical, "log -1 --pretty=format:\"%cn\"");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public enum AvailableGitTestRepo {
WITH_ONE_COMMIT("src/test/resources/_git_one_commit"),
WITH_ONE_COMMIT_DIRTY("src/test/resources/_git_one_commit_dirty"),
GIT_COMMIT_ID("src/test/resources/_git_of_git_commit_id"),
GIT_WITH_NO_CHANGES("src/test/resources/_git_with_no_changes/_git_dir"),
GIT_WITH_CHANGES("src/test/resources/_git_with_changes/_git_dir"),
ON_A_TAG("src/test/resources/_git_on_a_tag"),
/**
* <pre>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* This file is part of git-commit-id-plugin by Konrad Malawski <konrad.malawski@java.pl>
*
* git-commit-id-plugin is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* git-commit-id-plugin is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with git-commit-id-plugin. If not, see <http://www.gnu.org/licenses/>.
*/

package pl.project13.maven.git;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Maps;
import org.apache.maven.project.MavenProject;
import org.eclipse.jgit.lib.Repository;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.Properties;

import static org.fest.assertions.Assertions.assertThat;
import static org.mockito.Mockito.*;

/**
* @author Adam Batkin
*/
public class GitCommitIdMojoDirtyFilesTest {

@Test
public void testDetectCleanWorkingDirectory() throws Exception {
File dotGitDirectory = AvailableGitTestRepo.GIT_WITH_NO_CHANGES.getDir();
GitDescribeConfig gitDescribeConfig = new GitDescribeConfig();
gitDescribeConfig.setSkip(false);

String prefix = "git";
int abbrevLength = 7;
String dateFormat = "dd.MM.yyyy '@' HH:mm:ss z";
boolean verbose = true;

GitCommitIdMojo mojo = new GitCommitIdMojo();
mojo.setDotGitDirectory(dotGitDirectory);
mojo.setPrefix(prefix);
mojo.setAbbrevLength(abbrevLength);
mojo.setDateFormat(dateFormat);
mojo.setVerbose(verbose);
mojo.useNativeGit(false);
mojo.setGitDescribe(gitDescribeConfig);


mojo.runningTests = true;
mojo.project = mock(MavenProject.class, RETURNS_MOCKS);
when(mojo.project.getPackaging()).thenReturn("jar");

mojo.execute();

Properties properties = mojo.getProperties();

assertThat(properties.get("git.commit.files.dirty")).isEqualTo("false");
}

@Test
public void testDetectDirtyWorkingDirectory() throws Exception {
File dotGitDirectory = AvailableGitTestRepo.GIT_WITH_CHANGES.getDir();
GitDescribeConfig gitDescribeConfig = new GitDescribeConfig();
gitDescribeConfig.setSkip(false);

String prefix = "git";
int abbrevLength = 7;
String dateFormat = "dd.MM.yyyy '@' HH:mm:ss z";
boolean verbose = true;

GitCommitIdMojo mojo = new GitCommitIdMojo();
mojo.setDotGitDirectory(dotGitDirectory);
mojo.setPrefix(prefix);
mojo.setAbbrevLength(abbrevLength);
mojo.setDateFormat(dateFormat);
mojo.setVerbose(verbose);
mojo.useNativeGit(false);
mojo.setGitDescribe(gitDescribeConfig);


mojo.runningTests = true;
mojo.project = mock(MavenProject.class, RETURNS_MOCKS);
when(mojo.project.getPackaging()).thenReturn("jar");

mojo.execute();

Properties properties = mojo.getProperties();

assertThat(properties.get("git.commit.files.dirty")).isEqualTo("true");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public void shouldIncludeExpectedProperties() throws Exception {
assertThat(properties).satisfies(new ContainsKeyCondition("git.branch"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.id"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.id.abbrev"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.files.dirty"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.build.user.name"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.build.user.email"));
assertThat(properties).satisfies(new ContainsKeyCondition("git.commit.user.name"));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add file.txt
1 change: 1 addition & 0 deletions src/test/resources/_git_with_changes/_git_dir/HEAD
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ref: refs/heads/master
1 change: 1 addition & 0 deletions src/test/resources/_git_with_changes/_git_dir/ORIG_HEAD
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
79c5c38ca494525c1d2d3127af13476f9759957d
5 changes: 5 additions & 0 deletions src/test/resources/_git_with_changes/_git_dir/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
1 change: 1 addition & 0 deletions src/test/resources/_git_with_changes/_git_dir/description
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Unnamed repository; edit this file 'description' to name the repository.
Binary file added src/test/resources/_git_with_changes/_git_dir/index
Binary file not shown.
7 changes: 7 additions & 0 deletions src/test/resources/_git_with_changes/_git_dir/info/exclude
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~
/_git_dir/
1 change: 1 addition & 0 deletions src/test/resources/_git_with_changes/_git_dir/info/refs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
79c5c38ca494525c1d2d3127af13476f9759957d refs/heads/master
1 change: 1 addition & 0 deletions src/test/resources/_git_with_changes/_git_dir/logs/HEAD
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0000000000000000000000000000000000000000 79c5c38ca494525c1d2d3127af13476f9759957d Adam Batkin <adam@batkin.net> 1417588098 -0500 commit (initial): add file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0000000000000000000000000000000000000000 79c5c38ca494525c1d2d3127af13476f9759957d Adam Batkin <adam@batkin.net> 1417588098 -0500 commit (initial): add file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
P pack-ca141c03c134fe0537a3990df45432bf0bf0396e.pack

Binary file not shown.
Binary file not shown.
2 changes: 2 additions & 0 deletions src/test/resources/_git_with_changes/_git_dir/packed-refs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# pack-refs with: peeled fully-peeled
79c5c38ca494525c1d2d3127af13476f9759957d refs/heads/master
Empty file.
Empty file.
17 changes: 17 additions & 0 deletions src/test/resources/_git_with_no_changes/_git_dir/COMMIT_EDITMSG
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
add file.txt

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date: Wed Dec 3 00:35:29 2014 -0500
#
# On branch master
#
# Initial commit
#
# Changes to be committed:
# new file: file.txt
#
# Untracked files:
# _git_dir/
#
1 change: 1 addition & 0 deletions src/test/resources/_git_with_no_changes/_git_dir/HEAD
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ref: refs/heads/master
5 changes: 5 additions & 0 deletions src/test/resources/_git_with_no_changes/_git_dir/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Unnamed repository; edit this file 'description' to name the repository.
Binary file not shown.
7 changes: 7 additions & 0 deletions src/test/resources/_git_with_no_changes/_git_dir/info/exclude
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~
/_git_dir/
1 change: 1 addition & 0 deletions src/test/resources/_git_with_no_changes/_git_dir/info/refs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
e65f5b15be7a302936db2cf50430eae7ef4ecc6f refs/heads/master
3 changes: 3 additions & 0 deletions src/test/resources/_git_with_no_changes/_git_dir/logs/HEAD
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
0000000000000000000000000000000000000000 ff21f63ce400e3ff07b0b318dd4c61a0fb368bd2 Adam Batkin <adam@batkin.net> 1417584929 -0500 commit (initial): add file.txt
ff21f63ce400e3ff07b0b318dd4c61a0fb368bd2 95412a0871d9c4e1421d71d4fc513556e8d6f985 Adam Batkin <adam@batkin.net> 1417588894 -0500 commit (amend): add file.txt
95412a0871d9c4e1421d71d4fc513556e8d6f985 e65f5b15be7a302936db2cf50430eae7ef4ecc6f Adam Batkin <adam@batkin.net> 1417588980 -0500 commit (amend): add file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
0000000000000000000000000000000000000000 ff21f63ce400e3ff07b0b318dd4c61a0fb368bd2 Adam Batkin <adam@batkin.net> 1417584929 -0500 commit (initial): add file.txt
ff21f63ce400e3ff07b0b318dd4c61a0fb368bd2 95412a0871d9c4e1421d71d4fc513556e8d6f985 Adam Batkin <adam@batkin.net> 1417588894 -0500 commit (amend): add file.txt
95412a0871d9c4e1421d71d4fc513556e8d6f985 e65f5b15be7a302936db2cf50430eae7ef4ecc6f Adam Batkin <adam@batkin.net> 1417588980 -0500 commit (amend): add file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
P pack-789f4a0163258377aa2befda15661493c77f3ee1.pack

Binary file not shown.
Binary file not shown.
2 changes: 2 additions & 0 deletions src/test/resources/_git_with_no_changes/_git_dir/packed-refs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# pack-refs with: peeled fully-peeled
e65f5b15be7a302936db2cf50430eae7ef4ecc6f refs/heads/master
Empty file.