|
19 | 19 | ) |
20 | 20 |
|
21 | 21 | func main() { |
22 | | - var major, minor, patch, push, print bool |
| 22 | + var major, minor, patch, push, print, check bool |
23 | 23 | var metadata, prefix, suffix, remote string |
24 | 24 | var debug bool |
25 | 25 |
|
@@ -55,13 +55,54 @@ func main() { |
55 | 55 | "major": major, |
56 | 56 | "minor": minor, |
57 | 57 | "patch": patch, |
| 58 | + "check": check, |
58 | 59 | }).Debug("Configuration") |
59 | 60 |
|
60 | 61 | if err := git.FetchSemverTags(remote, prefix, suffix); err != nil { |
61 | 62 | fmt.Printf("Error fetching tags: %v\n", err) |
62 | 63 | os.Exit(1) |
63 | 64 | } |
64 | 65 |
|
| 66 | + // Handle --check flag: validate that the tag at HEAD has its previous version as an ancestor |
| 67 | + if check { |
| 68 | + currentTag, err := git.GetTagAtHEAD(prefix, suffix) |
| 69 | + if err != nil { |
| 70 | + fmt.Printf("Error getting tag at HEAD: %v\n", err) |
| 71 | + os.Exit(1) |
| 72 | + } |
| 73 | + if currentTag == "" { |
| 74 | + fmt.Println("Error: HEAD is not tagged") |
| 75 | + os.Exit(1) |
| 76 | + } |
| 77 | + |
| 78 | + allTags, err := git.ListTags(prefix, suffix) |
| 79 | + if err != nil { |
| 80 | + fmt.Printf("Error listing tags: %v\n", err) |
| 81 | + os.Exit(1) |
| 82 | + } |
| 83 | + |
| 84 | + previousTag, err := semver.FindPreviousVersion(currentTag, allTags) |
| 85 | + if err != nil { |
| 86 | + // No previous version means this is the first version, which is valid |
| 87 | + fmt.Printf("Tag '%s' is valid (first version)\n", currentTag) |
| 88 | + os.Exit(0) |
| 89 | + } |
| 90 | + |
| 91 | + isAncestor, err := git.IsAncestor(previousTag, "HEAD") |
| 92 | + if err != nil { |
| 93 | + fmt.Printf("Error checking ancestry: %v\n", err) |
| 94 | + os.Exit(1) |
| 95 | + } |
| 96 | + |
| 97 | + if !isAncestor { |
| 98 | + fmt.Printf("Error: Previous tag '%s' is not an ancestor of current tag '%s'\n", previousTag, currentTag) |
| 99 | + os.Exit(1) |
| 100 | + } |
| 101 | + |
| 102 | + fmt.Printf("Tag '%s' is valid (previous version '%s' is an ancestor)\n", currentTag, previousTag) |
| 103 | + os.Exit(0) |
| 104 | + } |
| 105 | + |
65 | 106 | // Check if HEAD is already tagged |
66 | 107 | alreadyTagged, err := git.IsHEADAlreadyTagged(prefix, suffix) |
67 | 108 | if err != nil { |
@@ -137,6 +178,7 @@ func main() { |
137 | 178 | rootCmd.Flags().BoolVar(&patch, "patch", false, "increment the patch version") |
138 | 179 | rootCmd.Flags().BoolVar(&push, "push", false, "create and push the tag to remote") |
139 | 180 | rootCmd.Flags().BoolVar(&print, "print-only", false, "print the next tag and exit") |
| 181 | + rootCmd.Flags().BoolVar(&check, "check", false, "validate that the tag at HEAD has its previous version as an ancestor") |
140 | 182 | rootCmd.Flags().BoolVar(&debug, "debug", false, "enable debug logging") |
141 | 183 | rootCmd.Flags().StringVar(&prefix, "prefix", "", "set a prefix for the tag") |
142 | 184 | rootCmd.Flags().StringVar(&suffix, "suffix", "", "set the pre-release suffix (e.g., rc, alpha, beta)") |
|
0 commit comments