Skip to content

Commit 4585dab

Browse files
committed
Checking target git commit within list of tags to prevent false-positive checkouts
1 parent 221554f commit 4585dab

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/main/groovy/com/alexvasilkov/vcs/util/GitHelper.groovy

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ class GitHelper {
2222
}
2323

2424
String targetCommit = repo.commit
25-
String localCommit = git.head().id
2625

27-
if (repo.keepUpdated && !localCommit.startsWith(targetCommit)) {
26+
if (repo.keepUpdated && !isLocalCommit(git, targetCommit)) {
27+
String localCommit = git.head().id
2828
println "Git local version '${localCommit}' is not eqaul to target " +
2929
"'${targetCommit}' for '${repo.repoDir}'"
3030

@@ -55,6 +55,14 @@ class GitHelper {
5555
.getString('remote', Constants.DEFAULT_REMOTE_NAME, 'url')
5656
}
5757

58+
private static boolean isLocalCommit(Grgit git, String targetId) {
59+
def head = git.head()
60+
// Checking if local commit is equal to (starts with) requested one.
61+
// If not then we should check if there are tags with target name pointing to current head.
62+
return head.id.startsWith(targetId) ||
63+
git.tag.list().find { it.commit == head && it.name == targetId }
64+
}
65+
5866
private static boolean hasLocalChanges(Grgit git) {
5967
Status status = git.status()
6068
return !status.staged.allChanges.isEmpty() || !status.unstaged.allChanges.isEmpty()

0 commit comments

Comments
 (0)