-
Notifications
You must be signed in to change notification settings - Fork 304
365 set ahead and behind properties #395
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package pl.project13.maven.git; | ||
|
||
import java.util.Objects; | ||
|
||
public class AheadBehind { | ||
|
||
public static final AheadBehind NO_REMOTE = AheadBehind.of("NO_REMOTE", "NO_REMOTE"); | ||
|
||
private final String ahead; | ||
|
||
private final String behind; | ||
|
||
private AheadBehind(String ahead, String behind) { | ||
this.ahead = ahead; | ||
this.behind = behind; | ||
} | ||
|
||
public static AheadBehind of(int ahead, int behind) { | ||
return new AheadBehind(String.valueOf(ahead), String.valueOf(behind)); | ||
} | ||
|
||
public static AheadBehind of(String ahead, String behind) { | ||
return new AheadBehind(ahead, behind); | ||
} | ||
|
||
public String ahead() { | ||
return ahead; | ||
} | ||
|
||
public String behind() { | ||
return behind; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(ahead, behind); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) { | ||
return true; | ||
} | ||
if (obj == null) { | ||
return false; | ||
} | ||
if (getClass() != obj.getClass()) { | ||
return false; | ||
} | ||
AheadBehind other = (AheadBehind) obj; | ||
|
||
if (!Objects.equals(ahead, other.ahead)) { | ||
return false; | ||
} | ||
if (!Objects.equals(behind, other.behind)) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
src/test/java/pl/project13/maven/git/AheadBehindTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package pl.project13.maven.git; | ||
|
||
import static org.junit.Assert.assertThat; | ||
|
||
import java.io.File; | ||
|
||
import org.eclipse.jgit.api.Git; | ||
import org.eclipse.jgit.lib.ConfigConstants; | ||
import org.eclipse.jgit.lib.StoredConfig; | ||
import org.hamcrest.CoreMatchers; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.TemporaryFolder; | ||
|
||
public abstract class AheadBehindTest<T extends GitProvider> { | ||
|
||
@Rule | ||
public TemporaryFolder remoteRepository = new TemporaryFolder(); | ||
|
||
@Rule | ||
public TemporaryFolder localRepository = new TemporaryFolder(); | ||
|
||
@Rule | ||
public TemporaryFolder secondLocalRepository = new TemporaryFolder(); | ||
|
||
protected Git localRepositoryGit; | ||
|
||
protected Git secondLocalRepositoryGit; | ||
|
||
protected T gitProvider; | ||
|
||
@Before | ||
public void setup() throws Exception { | ||
|
||
createRemoteRepository(); | ||
|
||
setupLocalRepository(); | ||
|
||
createAndPushInitialCommit(); | ||
|
||
setupSecondLocalRepository(); | ||
|
||
gitProvider = gitProvider(); | ||
|
||
extraSetup(); | ||
} | ||
|
||
protected abstract T gitProvider(); | ||
|
||
protected void extraSetup() { | ||
// Override in subclass to perform extra stuff in setup | ||
} | ||
|
||
@Test | ||
public void shouldNotBeAheadOrBehind() throws Exception { | ||
|
||
AheadBehind aheadBehind = gitProvider.getAheadBehind(); | ||
assertThat(aheadBehind.ahead(), CoreMatchers.is("0")); | ||
assertThat(aheadBehind.behind(), CoreMatchers.is("0")); | ||
} | ||
|
||
@Test | ||
public void shouldBe1Ahead() throws Exception { | ||
|
||
createLocalCommit(); | ||
|
||
AheadBehind aheadBehind = gitProvider.getAheadBehind(); | ||
assertThat(aheadBehind.ahead(), CoreMatchers.is("1")); | ||
assertThat(aheadBehind.behind(), CoreMatchers.is("0")); | ||
} | ||
|
||
@Test | ||
public void shouldBe1Behind() throws Exception { | ||
|
||
createCommitInSecondRepoAndPush(); | ||
|
||
AheadBehind aheadBehind = gitProvider.getAheadBehind(); | ||
assertThat(aheadBehind.ahead(), CoreMatchers.is("0")); | ||
assertThat(aheadBehind.behind(), CoreMatchers.is("1")); | ||
} | ||
|
||
@Test | ||
public void shouldBe1AheadAnd1Behind() throws Exception { | ||
|
||
createLocalCommit(); | ||
createCommitInSecondRepoAndPush(); | ||
|
||
AheadBehind aheadBehind = gitProvider.getAheadBehind(); | ||
assertThat(aheadBehind.ahead(), CoreMatchers.is("1")); | ||
assertThat(aheadBehind.behind(), CoreMatchers.is("1")); | ||
} | ||
|
||
protected void createLocalCommit() throws Exception { | ||
File newFile = localRepository.newFile(); | ||
localRepositoryGit.add().addFilepattern(newFile.getName()).call(); | ||
localRepositoryGit.commit().setMessage("ahead").call(); | ||
} | ||
|
||
protected void createCommitInSecondRepoAndPush() throws Exception { | ||
secondLocalRepositoryGit.pull().call(); | ||
|
||
File newFile = secondLocalRepository.newFile(); | ||
secondLocalRepositoryGit.add().addFilepattern(newFile.getName()).call(); | ||
secondLocalRepositoryGit.commit().setMessage("behind").call(); | ||
|
||
secondLocalRepositoryGit.push().call(); | ||
} | ||
|
||
protected void createRemoteRepository() throws Exception { | ||
Git.init().setBare(true).setDirectory(remoteRepository.getRoot()).call(); | ||
} | ||
|
||
protected void setupLocalRepository() throws Exception { | ||
localRepositoryGit = Git.cloneRepository().setURI(remoteRepository.getRoot().toURI().toString()) | ||
.setDirectory(localRepository.getRoot()).setBranch("master").call(); | ||
|
||
StoredConfig config = localRepositoryGit.getRepository().getConfig(); | ||
config.setString(ConfigConstants.CONFIG_BRANCH_SECTION, "master", "remote", "origin"); | ||
config.setString(ConfigConstants.CONFIG_BRANCH_SECTION, "master", "merge", "refs/heads/master"); | ||
config.save(); | ||
} | ||
|
||
protected void setupSecondLocalRepository() throws Exception { | ||
secondLocalRepositoryGit = Git.cloneRepository().setURI(remoteRepository.getRoot().toURI().toString()) | ||
.setDirectory(secondLocalRepository.getRoot()).setBranch("master").call(); | ||
} | ||
|
||
protected void createAndPushInitialCommit() throws Exception { | ||
File newFile = localRepository.newFile(); | ||
localRepositoryGit.add().addFilepattern(newFile.getName()).call(); | ||
localRepositoryGit.commit().setMessage("initial").call(); | ||
|
||
localRepositoryGit.push().call(); | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
src/test/java/pl/project13/maven/git/JgitProviderAheadBehindTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package pl.project13.maven.git; | ||
|
||
import java.nio.file.Paths; | ||
|
||
public class JgitProviderAheadBehindTest extends AheadBehindTest<JGitProvider> { | ||
TheSnoozer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
@Override | ||
public void extraSetup() { | ||
gitProvider.setRepository(localRepositoryGit.getRepository()); | ||
} | ||
|
||
@Override | ||
protected JGitProvider gitProvider() { | ||
return new JGitProvider(Paths.get(localRepository.getRoot().getAbsolutePath(), ".git").toFile(), null); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/test/java/pl/project13/maven/git/NativeProviderAheadBehindTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package pl.project13.maven.git; | ||
|
||
public class NativeProviderAheadBehindTest extends AheadBehindTest<NativeGitProvider> { | ||
|
||
@Override | ||
protected NativeGitProvider gitProvider() { | ||
return new NativeGitProvider(localRepository.getRoot(), 1000l, null); | ||
} | ||
|
||
@Override | ||
protected void extraSetup() { | ||
gitProvider.setEvaluateOnCommit("HEAD"); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.