-
Notifications
You must be signed in to change notification settings - Fork 300
Expand file tree
/
Copy pathDescribeCommandAbbrevIntegrationTest.java
More file actions
114 lines (98 loc) · 4.01 KB
/
DescribeCommandAbbrevIntegrationTest.java
File metadata and controls
114 lines (98 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
* This file is part of git-commit-id-maven-plugin by Konrad 'ktoso' Malawski <konrad.malawski@java.pl>
*
* git-commit-id-maven-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-maven-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-maven-plugin. If not, see <http://www.gnu.org/licenses/>.
*/
package pl.project13.maven.jgit;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.lib.Repository;
import org.junit.Test;
import pl.project13.core.jgit.DescribeCommand;
import pl.project13.core.jgit.DescribeResult;
import pl.project13.core.log.StdOutLoggerBridge;
import pl.project13.maven.git.AvailableGitTestRepo;
import pl.project13.maven.git.GitIntegrationTest;
import javax.annotation.Nonnull;
import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;
public class DescribeCommandAbbrevIntegrationTest extends GitIntegrationTest {
static final String PROJECT_NAME = "my-jar-project";
@Override
protected Optional<String> projectDir() {
return Optional.of(PROJECT_NAME);
}
/**
* Test for such situation:
* <pre>
* master!tag-test$ lg
* b6a73ed - (HEAD, master) third addition (8 hours ago) <p>Konrad Malawski</p>
* d37a598 - (lightweight-tag) second line (8 hours ago) <p>Konrad Malawski</p>
* 9597545 - (annotated-tag) initial commit (8 hours ago) <p>Konrad Malawski</p>
*
* master!tag-test$ describe --abbrev=1
* annotated-tag-2-gb6a7
*
* master!tag-test$ describe --abbrev=2
* annotated-tag-2-gb6a7
* </pre>
*
* <p>
* Notice that git will not use less than 4 chars for the abbrev, and in large repositories,
* it will use the abbrev so long that it's guaranteed to be unique.
* </p>
*/
@Test
public void shouldGiveTheCommitIdAndDirtyMarkerWhenNothingElseCanBeFound() throws Exception {
// given
mavenSandbox
.withParentProject(PROJECT_NAME, "jar")
.withNoChildProject()
.withGitRepoInParent(AvailableGitTestRepo.WITH_LIGHTWEIGHT_TAG_BEFORE_ANNOTATED_TAG)
.create();
try (final Git git = git(); final Repository repo = git.getRepository()) {
// when
DescribeResult res = DescribeCommand
.on(evaluateOnCommit, repo, new StdOutLoggerBridge(true))
.abbrev(2) // 2 is enough to be unique in this small repo
.call();
// then
// git will notice this, and fallback to use 4 chars
String smallestAbbrevGitWillUse = abbrev("b6a73ed747dd8dc98642d731ddbf09824efb9d48", 2);
assertThat(res.prefixedCommitId()).isEqualTo("g" + smallestAbbrevGitWillUse);
}
}
@Test
public void onGitCommitIdsRepo_shouldNoticeThat2CharsIsTooLittleToBeUniqueAndUse4CharsInstead() throws Exception {
// given
mavenSandbox
.withParentProject(PROJECT_NAME, "jar")
.withNoChildProject()
.withGitRepoInParent(AvailableGitTestRepo.GIT_COMMIT_ID)
.create();
try (final Git git = git(); final Repository repo = git.getRepository()) {
// when
DescribeResult res = DescribeCommand
.on(evaluateOnCommit, repo, new StdOutLoggerBridge(true))
.abbrev(2) // way too small to be unique in git-commit-id's repo!
.call();
// then
// git will notice this, and fallback to use 4 chars
String smallestAbbrevGitWillUse = abbrev("7181832b7d9afdeb86c32cf51214abfca63625be", 4);
assertThat(res.prefixedCommitId()).isEqualTo("g" + smallestAbbrevGitWillUse);
}
}
String abbrev(@Nonnull String id, int n) {
return id.substring(0, n);
}
}