@@ -245,17 +245,59 @@ public String resolveDynamicVersion(NisseConfiguration configuration, Repository
245245 vi = new VersionInformation (useVersion .get ());
246246 logger .debug ("Using explicit version from useVersion property: {}" , useVersion .get ());
247247 } else {
248- // Try to find version hint tags first
248+ // First, get version from git history (regular release tags)
249+ VersionInformation gitHistoryVersion = getVersionFromGit (configuration , repository );
250+ logger .debug ("Version from git history: {}" , gitHistoryVersion .toString ());
251+
252+ // Check if using custom version hint pattern
253+ String versionHintPattern = configuration
254+ .getConfiguration ()
255+ .getOrDefault (JGIT_CONF_SYSTEM_PROPERTY_VERSION_HINT_PATTERN , DEFAULT_VERSION_HINT_PATTERN );
256+ boolean isCustomPattern = !DEFAULT_VERSION_HINT_PATTERN .equals (versionHintPattern );
257+
258+ // Then, check for version hint tags
249259 Optional <String > versionHint = findVersionHint (configuration , repository );
250260 if (versionHint .isPresent ()) {
251- vi = new VersionInformation (versionHint .get ());
252- // Version hints are treated as if the previous commit was tagged,
253- // so we should add SNAPSHOT qualifier since we're ahead of that "previous commit"
254- vi = mayAddSnapshotQualifier (configuration , vi );
255- logger .debug ("Using version hint from tag: {}" , versionHint .get ());
261+ VersionInformation hintVersion = new VersionInformation (versionHint .get ());
262+ logger .debug ("Version hint found: {}" , hintVersion .toString ());
263+
264+ if (isCustomPattern ) {
265+ // With custom pattern, version hints take priority (git history only contains matching tags)
266+ vi = mayAddSnapshotQualifier (configuration , hintVersion );
267+ logger .debug ("Using version hint (custom pattern): {}" , versionHint .get ());
268+ } else {
269+ // With default pattern, compare versions
270+ // Check if git history version is the default (meaning no regular release tags found)
271+ boolean isDefaultGitVersion = gitHistoryVersion .getMajor () == 0
272+ && gitHistoryVersion .getMinor () == 0
273+ && gitHistoryVersion .getPatch () == 1 ;
274+
275+ if (isDefaultGitVersion ) {
276+ // No regular release tags found, use version hint directly
277+ vi = mayAddSnapshotQualifier (configuration , hintVersion );
278+ logger .debug ("Using version hint (no regular release tags found): {}" , versionHint .get ());
279+ } else {
280+ // Compare versions - use hint only if it's higher than git history version
281+ Version gitHistoryVersionParsed = version (gitHistoryVersion .toString ());
282+ Version hintVersionParsed = version (hintVersion .toString ());
283+
284+ if (hintVersionParsed .compareTo (gitHistoryVersionParsed ) > 0 ) {
285+ // Version hint is higher, use it
286+ vi = mayAddSnapshotQualifier (configuration , hintVersion );
287+ logger .debug ("Using version hint (higher than git history): {}" , versionHint .get ());
288+ } else {
289+ // Git history version is higher or equal, use it
290+ vi = gitHistoryVersion ;
291+ logger .debug (
292+ "Using git history version (higher than or equal to version hint): {}" ,
293+ gitHistoryVersion .toString ());
294+ }
295+ }
296+ }
256297 } else {
257- vi = getVersionFromGit (configuration , repository );
258- logger .debug ("Using version resolved from git history" );
298+ // No version hint, use git history version
299+ vi = gitHistoryVersion ;
300+ logger .debug ("Using version resolved from git history (no version hint found)" );
259301 }
260302 }
261303
@@ -274,7 +316,7 @@ protected VersionInformation getVersionFromGit(NisseConfiguration configuration,
274316 Iterable <RevCommit > commits = git .log ().call ();
275317 int count = 0 ;
276318 for (RevCommit commit : commits ) {
277- Optional <VersionInformation > ovi = getHighestVersionTagForCommit (git , commit );
319+ Optional <VersionInformation > ovi = getHighestVersionTagForCommit (configuration , git , commit );
278320
279321 if (ovi .isPresent ()) {
280322 VersionInformation vi = ovi .get ();
@@ -295,17 +337,24 @@ protected VersionInformation getVersionFromGit(NisseConfiguration configuration,
295337 }
296338 }
297339
298- private Optional <VersionInformation > getHighestVersionTagForCommit (Git git , RevCommit commit )
299- throws GitAPIException {
340+ private Optional <VersionInformation > getHighestVersionTagForCommit (
341+ NisseConfiguration configuration , Git git , RevCommit commit ) throws GitAPIException {
300342 // get tags use semantic version (X.Y.Z or vX.Y.Z) for for commit
301- List <String > versionTagsForCommit = getVersionedTagsForCommit (git , commit );
343+ List <String > versionTagsForCommit = getVersionedTagsForCommit (configuration , git , commit );
302344 logger .debug ("commit {} {}: {}" , commit .getId (), commit .getShortMessage (), versionTagsForCommit .toString ());
303345 Optional <VersionInformation > ovi = findHighestVersion (versionTagsForCommit );
304346
305347 return ovi ;
306348 }
307349
308- protected List <String > getVersionedTagsForCommit (Git git , RevCommit commit ) throws GitAPIException {
350+ protected List <String > getVersionedTagsForCommit (NisseConfiguration configuration , Git git , RevCommit commit )
351+ throws GitAPIException {
352+ // Check if using custom version hint pattern
353+ String versionHintPattern = configuration
354+ .getConfiguration ()
355+ .getOrDefault (JGIT_CONF_SYSTEM_PROPERTY_VERSION_HINT_PATTERN , DEFAULT_VERSION_HINT_PATTERN );
356+ boolean isCustomPattern = !DEFAULT_VERSION_HINT_PATTERN .equals (versionHintPattern );
357+
309358 return git .tagList ().call ().stream ()
310359 .filter (tag -> {
311360 try {
@@ -320,6 +369,15 @@ protected List<String> getVersionedTagsForCommit(Git git, RevCommit commit) thro
320369 }
321370 })
322371 .map (Ref ::getName )
372+ .filter (tagName -> {
373+ if (isCustomPattern ) {
374+ // With custom pattern, only consider tags that match the pattern
375+ return isVersionHintTag (configuration , tagName );
376+ } else {
377+ // With default pattern, exclude version hint tags to avoid double-counting
378+ return !isVersionHintTag (configuration , tagName );
379+ }
380+ })
323381 .map (TAG_VERSION_PATTERN ::matcher )
324382 .filter (m -> m .matches () && m .groupCount () > 0 )
325383 .map (m -> m .group (1 ))
@@ -419,4 +477,30 @@ protected List<String> findVersionHintTags(Git git, String hintPattern) throws G
419477 protected Optional <String > findHighestVersionFromHints (List <String > hintVersions ) {
420478 return hintVersions .stream ().max (Comparator .comparing (this ::version ));
421479 }
480+
481+ /**
482+ * Check if a tag name matches the version hint pattern.
483+ *
484+ * @param configuration The Nisse configuration
485+ * @param tagName The full tag name (e.g., "refs/tags/1.0.0-SNAPSHOT")
486+ * @return true if the tag matches the version hint pattern
487+ */
488+ protected boolean isVersionHintTag (NisseConfiguration configuration , String tagName ) {
489+ String versionHintPattern = configuration
490+ .getConfiguration ()
491+ .getOrDefault (JGIT_CONF_SYSTEM_PROPERTY_VERSION_HINT_PATTERN , DEFAULT_VERSION_HINT_PATTERN );
492+
493+ // Convert hint pattern to regex pattern (same logic as findVersionHintTags)
494+ // First, escape special regex characters in the pattern (except the placeholder)
495+ String regexPattern = versionHintPattern
496+ .replace ("." , "\\ ." ) // Escape literal dots
497+ .replace ("-" , "\\ -" ); // Escape literal dashes
498+
499+ // Then replace the placeholder with the version regex
500+ regexPattern = regexPattern .replace ("${version}" , "(\\ d+\\ .\\ d+\\ .\\ d+)" );
501+
502+ Pattern hintTagPattern = Pattern .compile ("refs/tags/v?" + regexPattern );
503+
504+ return hintTagPattern .matcher (tagName ).matches ();
505+ }
422506}
0 commit comments