├── .gitignore ├── LICENSE ├── README.md ├── git ├── tags.go ├── version.go └── version_test.go ├── go.mod ├── go.sum └── main.go /.gitignore: -------------------------------------------------------------------------------- 1 | bump 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Mozilla Public License, version 2.0 2 | 3 | 1. Definitions 4 | 5 | 1.1. “Contributor” 6 | 7 | means each individual or legal entity that creates, contributes to the 8 | creation of, or owns Covered Software. 9 | 10 | 1.2. “Contributor Version” 11 | 12 | means the combination of the Contributions of others (if any) used by a 13 | Contributor and that particular Contributor’s Contribution. 14 | 15 | 1.3. “Contribution” 16 | 17 | means Covered Software of a particular Contributor. 18 | 19 | 1.4. “Covered Software” 20 | 21 | means Source Code Form to which the initial Contributor has attached the 22 | notice in Exhibit A, the Executable Form of such Source Code Form, and 23 | Modifications of such Source Code Form, in each case including portions 24 | thereof. 25 | 26 | 1.5. “Incompatible With Secondary Licenses” 27 | means 28 | 29 | a. that the initial Contributor has attached the notice described in 30 | Exhibit B to the Covered Software; or 31 | 32 | b. that the Covered Software was made available under the terms of version 33 | 1.1 or earlier of the License, but not also under the terms of a 34 | Secondary License. 35 | 36 | 1.6. “Executable Form” 37 | 38 | means any form of the work other than Source Code Form. 39 | 40 | 1.7. “Larger Work” 41 | 42 | means a work that combines Covered Software with other material, in a separate 43 | file or files, that is not Covered Software. 44 | 45 | 1.8. “License” 46 | 47 | means this document. 48 | 49 | 1.9. “Licensable” 50 | 51 | means having the right to grant, to the maximum extent possible, whether at the 52 | time of the initial grant or subsequently, any and all of the rights conveyed by 53 | this License. 54 | 55 | 1.10. “Modifications” 56 | 57 | means any of the following: 58 | 59 | a. any file in Source Code Form that results from an addition to, deletion 60 | from, or modification of the contents of Covered Software; or 61 | 62 | b. any new file in Source Code Form that contains any Covered Software. 63 | 64 | 1.11. “Patent Claims” of a Contributor 65 | 66 | means any patent claim(s), including without limitation, method, process, 67 | and apparatus claims, in any patent Licensable by such Contributor that 68 | would be infringed, but for the grant of the License, by the making, 69 | using, selling, offering for sale, having made, import, or transfer of 70 | either its Contributions or its Contributor Version. 71 | 72 | 1.12. “Secondary License” 73 | 74 | means either the GNU General Public License, Version 2.0, the GNU Lesser 75 | General Public License, Version 2.1, the GNU Affero General Public 76 | License, Version 3.0, or any later versions of those licenses. 77 | 78 | 1.13. “Source Code Form” 79 | 80 | means the form of the work preferred for making modifications. 81 | 82 | 1.14. “You” (or “Your”) 83 | 84 | means an individual or a legal entity exercising rights under this 85 | License. For legal entities, “You” includes any entity that controls, is 86 | controlled by, or is under common control with You. For purposes of this 87 | definition, “control” means (a) the power, direct or indirect, to cause 88 | the direction or management of such entity, whether by contract or 89 | otherwise, or (b) ownership of more than fifty percent (50%) of the 90 | outstanding shares or beneficial ownership of such entity. 91 | 92 | 93 | 2. License Grants and Conditions 94 | 95 | 2.1. Grants 96 | 97 | Each Contributor hereby grants You a world-wide, royalty-free, 98 | non-exclusive license: 99 | 100 | a. under intellectual property rights (other than patent or trademark) 101 | Licensable by such Contributor to use, reproduce, make available, 102 | modify, display, perform, distribute, and otherwise exploit its 103 | Contributions, either on an unmodified basis, with Modifications, or as 104 | part of a Larger Work; and 105 | 106 | b. under Patent Claims of such Contributor to make, use, sell, offer for 107 | sale, have made, import, and otherwise transfer either its Contributions 108 | or its Contributor Version. 109 | 110 | 2.2. Effective Date 111 | 112 | The licenses granted in Section 2.1 with respect to any Contribution become 113 | effective for each Contribution on the date the Contributor first distributes 114 | such Contribution. 115 | 116 | 2.3. Limitations on Grant Scope 117 | 118 | The licenses granted in this Section 2 are the only rights granted under this 119 | License. No additional rights or licenses will be implied from the distribution 120 | or licensing of Covered Software under this License. Notwithstanding Section 121 | 2.1(b) above, no patent license is granted by a Contributor: 122 | 123 | a. for any code that a Contributor has removed from Covered Software; or 124 | 125 | b. for infringements caused by: (i) Your and any other third party’s 126 | modifications of Covered Software, or (ii) the combination of its 127 | Contributions with other software (except as part of its Contributor 128 | Version); or 129 | 130 | c. under Patent Claims infringed by Covered Software in the absence of its 131 | Contributions. 132 | 133 | This License does not grant any rights in the trademarks, service marks, or 134 | logos of any Contributor (except as may be necessary to comply with the 135 | notice requirements in Section 3.4). 136 | 137 | 2.4. Subsequent Licenses 138 | 139 | No Contributor makes additional grants as a result of Your choice to 140 | distribute the Covered Software under a subsequent version of this License 141 | (see Section 10.2) or under the terms of a Secondary License (if permitted 142 | under the terms of Section 3.3). 143 | 144 | 2.5. Representation 145 | 146 | Each Contributor represents that the Contributor believes its Contributions 147 | are its original creation(s) or it has sufficient rights to grant the 148 | rights to its Contributions conveyed by this License. 149 | 150 | 2.6. Fair Use 151 | 152 | This License is not intended to limit any rights You have under applicable 153 | copyright doctrines of fair use, fair dealing, or other equivalents. 154 | 155 | 2.7. Conditions 156 | 157 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted in 158 | Section 2.1. 159 | 160 | 161 | 3. Responsibilities 162 | 163 | 3.1. Distribution of Source Form 164 | 165 | All distribution of Covered Software in Source Code Form, including any 166 | Modifications that You create or to which You contribute, must be under the 167 | terms of this License. You must inform recipients that the Source Code Form 168 | of the Covered Software is governed by the terms of this License, and how 169 | they can obtain a copy of this License. You may not attempt to alter or 170 | restrict the recipients’ rights in the Source Code Form. 171 | 172 | 3.2. Distribution of Executable Form 173 | 174 | If You distribute Covered Software in Executable Form then: 175 | 176 | a. such Covered Software must also be made available in Source Code Form, 177 | as described in Section 3.1, and You must inform recipients of the 178 | Executable Form how they can obtain a copy of such Source Code Form by 179 | reasonable means in a timely manner, at a charge no more than the cost 180 | of distribution to the recipient; and 181 | 182 | b. You may distribute such Executable Form under the terms of this License, 183 | or sublicense it under different terms, provided that the license for 184 | the Executable Form does not attempt to limit or alter the recipients’ 185 | rights in the Source Code Form under this License. 186 | 187 | 3.3. Distribution of a Larger Work 188 | 189 | You may create and distribute a Larger Work under terms of Your choice, 190 | provided that You also comply with the requirements of this License for the 191 | Covered Software. If the Larger Work is a combination of Covered Software 192 | with a work governed by one or more Secondary Licenses, and the Covered 193 | Software is not Incompatible With Secondary Licenses, this License permits 194 | You to additionally distribute such Covered Software under the terms of 195 | such Secondary License(s), so that the recipient of the Larger Work may, at 196 | their option, further distribute the Covered Software under the terms of 197 | either this License or such Secondary License(s). 198 | 199 | 3.4. Notices 200 | 201 | You may not remove or alter the substance of any license notices (including 202 | copyright notices, patent notices, disclaimers of warranty, or limitations 203 | of liability) contained within the Source Code Form of the Covered 204 | Software, except that You may alter any license notices to the extent 205 | required to remedy known factual inaccuracies. 206 | 207 | 3.5. Application of Additional Terms 208 | 209 | You may choose to offer, and to charge a fee for, warranty, support, 210 | indemnity or liability obligations to one or more recipients of Covered 211 | Software. However, You may do so only on Your own behalf, and not on behalf 212 | of any Contributor. You must make it absolutely clear that any such 213 | warranty, support, indemnity, or liability obligation is offered by You 214 | alone, and You hereby agree to indemnify every Contributor for any 215 | liability incurred by such Contributor as a result of warranty, support, 216 | indemnity or liability terms You offer. You may include additional 217 | disclaimers of warranty and limitations of liability specific to any 218 | jurisdiction. 219 | 220 | 4. Inability to Comply Due to Statute or Regulation 221 | 222 | If it is impossible for You to comply with any of the terms of this License 223 | with respect to some or all of the Covered Software due to statute, judicial 224 | order, or regulation then You must: (a) comply with the terms of this License 225 | to the maximum extent possible; and (b) describe the limitations and the code 226 | they affect. Such description must be placed in a text file included with all 227 | distributions of the Covered Software under this License. Except to the 228 | extent prohibited by statute or regulation, such description must be 229 | sufficiently detailed for a recipient of ordinary skill to be able to 230 | understand it. 231 | 232 | 5. Termination 233 | 234 | 5.1. The rights granted under this License will terminate automatically if You 235 | fail to comply with any of its terms. However, if You become compliant, 236 | then the rights granted under this License from a particular Contributor 237 | are reinstated (a) provisionally, unless and until such Contributor 238 | explicitly and finally terminates Your grants, and (b) on an ongoing basis, 239 | if such Contributor fails to notify You of the non-compliance by some 240 | reasonable means prior to 60 days after You have come back into compliance. 241 | Moreover, Your grants from a particular Contributor are reinstated on an 242 | ongoing basis if such Contributor notifies You of the non-compliance by 243 | some reasonable means, this is the first time You have received notice of 244 | non-compliance with this License from such Contributor, and You become 245 | compliant prior to 30 days after Your receipt of the notice. 246 | 247 | 5.2. If You initiate litigation against any entity by asserting a patent 248 | infringement claim (excluding declaratory judgment actions, counter-claims, 249 | and cross-claims) alleging that a Contributor Version directly or 250 | indirectly infringes any patent, then the rights granted to You by any and 251 | all Contributors for the Covered Software under Section 2.1 of this License 252 | shall terminate. 253 | 254 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all end user 255 | license agreements (excluding distributors and resellers) which have been 256 | validly granted by You or Your distributors under this License prior to 257 | termination shall survive termination. 258 | 259 | 6. Disclaimer of Warranty 260 | 261 | Covered Software is provided under this License on an “as is” basis, without 262 | warranty of any kind, either expressed, implied, or statutory, including, 263 | without limitation, warranties that the Covered Software is free of defects, 264 | merchantable, fit for a particular purpose or non-infringing. The entire 265 | risk as to the quality and performance of the Covered Software is with You. 266 | Should any Covered Software prove defective in any respect, You (not any 267 | Contributor) assume the cost of any necessary servicing, repair, or 268 | correction. This disclaimer of warranty constitutes an essential part of this 269 | License. No use of any Covered Software is authorized under this License 270 | except under this disclaimer. 271 | 272 | 7. Limitation of Liability 273 | 274 | Under no circumstances and under no legal theory, whether tort (including 275 | negligence), contract, or otherwise, shall any Contributor, or anyone who 276 | distributes Covered Software as permitted above, be liable to You for any 277 | direct, indirect, special, incidental, or consequential damages of any 278 | character including, without limitation, damages for lost profits, loss of 279 | goodwill, work stoppage, computer failure or malfunction, or any and all 280 | other commercial damages or losses, even if such party shall have been 281 | informed of the possibility of such damages. This limitation of liability 282 | shall not apply to liability for death or personal injury resulting from such 283 | party’s negligence to the extent applicable law prohibits such limitation. 284 | Some jurisdictions do not allow the exclusion or limitation of incidental or 285 | consequential damages, so this exclusion and limitation may not apply to You. 286 | 287 | 8. Litigation 288 | 289 | Any litigation relating to this License may be brought only in the courts of 290 | a jurisdiction where the defendant maintains its principal place of business 291 | and such litigation shall be governed by laws of that jurisdiction, without 292 | reference to its conflict-of-law provisions. Nothing in this Section shall 293 | prevent a party’s ability to bring cross-claims or counter-claims. 294 | 295 | 9. Miscellaneous 296 | 297 | This License represents the complete agreement concerning the subject matter 298 | hereof. If any provision of this License is held to be unenforceable, such 299 | provision shall be reformed only to the extent necessary to make it 300 | enforceable. Any law or regulation which provides that the language of a 301 | contract shall be construed against the drafter shall not be used to construe 302 | this License against a Contributor. 303 | 304 | 305 | 10. Versions of the License 306 | 307 | 10.1. New Versions 308 | 309 | Mozilla Foundation is the license steward. Except as provided in Section 310 | 10.3, no one other than the license steward has the right to modify or 311 | publish new versions of this License. Each version will be given a 312 | distinguishing version number. 313 | 314 | 10.2. Effect of New Versions 315 | 316 | You may distribute the Covered Software under the terms of the version of 317 | the License under which You originally received the Covered Software, or 318 | under the terms of any subsequent version published by the license 319 | steward. 320 | 321 | 10.3. Modified Versions 322 | 323 | If you create software not governed by this License, and you want to 324 | create a new license for such software, you may create and use a modified 325 | version of this License if you rename the license and remove any 326 | references to the name of the license steward (except to note that such 327 | modified license differs from this License). 328 | 329 | 10.4. Distributing Source Code Form that is Incompatible With Secondary Licenses 330 | If You choose to distribute Source Code Form that is Incompatible With 331 | Secondary Licenses under the terms of this version of the License, the 332 | notice described in Exhibit B of this License must be attached. 333 | 334 | Exhibit A - Source Code Form License Notice 335 | 336 | This Source Code Form is subject to the 337 | terms of the Mozilla Public License, v. 338 | 2.0. If a copy of the MPL was not 339 | distributed with this file, You can 340 | obtain one at 341 | http://mozilla.org/MPL/2.0/. 342 | 343 | If it is not possible or desirable to put the notice in a particular file, then 344 | You may include the notice in a location (such as a LICENSE file in a relevant 345 | directory) where a recipient would be likely to look for such a notice. 346 | 347 | You may add additional accurate notices of copyright ownership. 348 | 349 | Exhibit B - “Incompatible With Secondary Licenses” Notice 350 | 351 | This Source Code Form is “Incompatible 352 | With Secondary Licenses”, as defined by 353 | the Mozilla Public License, v. 2.0. 354 | 355 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Overview 2 | 3 | `bump` is a tool for managing versions in git tags. 4 | 5 | It parses all git tags for semantic version numbers, sorts 6 | them and provides operations on them. 7 | 8 | ## Install 9 | 10 | ``` 11 | go install github.com/magiconair/bump@latest 12 | ``` 13 | 14 | ## Usage 15 | 16 | ``` 17 | # print the current version number (highest tag) 18 | bump cur 19 | 20 | # list all version numbers sorted 21 | bump list 22 | 23 | # print the next version number 24 | bump next 25 | 26 | # print the next major/minor/patch version number 27 | bump next major 28 | bump next minor 29 | bump next patch 30 | 31 | # tag with the next version number 32 | bump tag 33 | 34 | # tag with the next major/minor/patch version number 35 | bump tag major 36 | bump tag minor 37 | bump tag patch 38 | ``` 39 | -------------------------------------------------------------------------------- /git/tags.go: -------------------------------------------------------------------------------- 1 | package git 2 | 3 | import ( 4 | "bytes" 5 | "fmt" 6 | "os/exec" 7 | ) 8 | 9 | func IsEmptyRepository() (bool, error) { 10 | out, err := exec.Command("git", "count-objects", "-v").Output() 11 | if err != nil { 12 | return false, err 13 | } 14 | return bytes.Contains(out, []byte("count: 0")), nil 15 | } 16 | 17 | func Tags() ([]Version, error) { 18 | var stdout, stderr bytes.Buffer 19 | cmd := exec.Command("git", "tag") 20 | cmd.Stdout = &stdout 21 | cmd.Stderr = &stderr 22 | if err := cmd.Run(); err != nil { 23 | return nil, err 24 | } 25 | 26 | if stderr.Len() > 0 { 27 | return nil, fmt.Errorf(stderr.String()) 28 | } 29 | 30 | vv, err := Read(&stdout) 31 | if err != nil { 32 | return nil, err 33 | } 34 | return vv, nil 35 | } 36 | 37 | func Tag(v Version) error { 38 | if err := signedTag(v); err != nil { 39 | return annotatedTag(v) 40 | } 41 | return nil 42 | } 43 | 44 | func annotatedTag(v Version) error { 45 | return exec.Command("git", "tag", "-a", v.String(), "-m", v.String()).Run() 46 | } 47 | 48 | func signedTag(v Version) error { 49 | return exec.Command("git", "tag", "-s", v.String(), "-m", v.String()).Run() 50 | } 51 | -------------------------------------------------------------------------------- /git/version.go: -------------------------------------------------------------------------------- 1 | package git 2 | 3 | import ( 4 | "bufio" 5 | "fmt" 6 | "io" 7 | "regexp" 8 | "sort" 9 | "strconv" 10 | ) 11 | 12 | type Version struct { 13 | Prefix string 14 | Major int 15 | Minor int 16 | Patch int 17 | Extra string 18 | } 19 | 20 | var re = regexp.MustCompile(`(v)?([0-9]+)\.([0-9]+)(\.([0-9]+))?(-(.*))?`) 21 | 22 | func Read(r io.Reader) ([]Version, error) { 23 | var vv byVersion 24 | sc := bufio.NewScanner(r) 25 | for sc.Scan() { 26 | v, err := Parse(sc.Text()) 27 | if v.IsZero() || err != nil { 28 | continue 29 | } 30 | vv = append(vv, v) 31 | } 32 | if err := sc.Err(); err != nil { 33 | return nil, err 34 | } 35 | sort.Sort(byVersion(vv)) 36 | return vv, nil 37 | } 38 | 39 | func ParseAll(s []string) ([]Version, error) { 40 | var vv byVersion 41 | for _, x := range s { 42 | v, err := Parse(x) 43 | if v.IsZero() || err != nil { 44 | continue 45 | } 46 | vv = append(vv, v) 47 | } 48 | sort.Sort(byVersion(vv)) 49 | return vv, nil 50 | } 51 | 52 | func Parse(s string) (Version, error) { 53 | var err error 54 | atoi := func(s string) int { 55 | if s == "" || err != nil { 56 | return 0 57 | } 58 | var n int 59 | n, err = strconv.Atoi(s) 60 | if err != nil { 61 | return 0 62 | } 63 | return n 64 | } 65 | 66 | m := re.FindStringSubmatch(s) 67 | if m == nil { 68 | return Version{}, nil 69 | } 70 | 71 | prefix := m[1] 72 | major := atoi(m[2]) 73 | minor := atoi(m[3]) 74 | patch := atoi(m[5]) 75 | extra := m[7] 76 | if err != nil { 77 | return Version{}, err 78 | } 79 | 80 | return Version{ 81 | Prefix: prefix, 82 | Major: major, 83 | Minor: minor, 84 | Patch: patch, 85 | Extra: extra, 86 | }, nil 87 | } 88 | 89 | func (v Version) Ints() []int { 90 | return []int{v.Major, v.Minor, v.Patch} 91 | } 92 | 93 | func (v Version) IsZero() bool { 94 | return v.Major == 0 && v.Minor == 0 && v.Patch == 0 95 | } 96 | 97 | func (v Version) Bump() Version { 98 | return v.BumpPatch() 99 | } 100 | 101 | func (v Version) BumpMajor() Version { 102 | return Version{ 103 | Prefix: v.Prefix, 104 | Major: v.Major + 1, 105 | } 106 | } 107 | 108 | func (v Version) BumpMinor() Version { 109 | return Version{ 110 | Prefix: v.Prefix, 111 | Major: v.Major, 112 | Minor: v.Minor + 1, 113 | } 114 | } 115 | 116 | func (v Version) BumpPatch() Version { 117 | return Version{ 118 | Prefix: v.Prefix, 119 | Major: v.Major, 120 | Minor: v.Minor, 121 | Patch: v.Patch + 1, 122 | } 123 | } 124 | 125 | func (v Version) String() string { 126 | s := fmt.Sprintf("%s%d.%d.%d", v.Prefix, v.Major, v.Minor, v.Patch) 127 | if v.Extra != "" { 128 | s += "-" + v.Extra 129 | } 130 | return s 131 | } 132 | 133 | type byVersion []Version 134 | 135 | func (v byVersion) Len() int { return len(v) } 136 | func (v byVersion) Swap(i, j int) { v[i], v[j] = v[j], v[i] } 137 | func (v byVersion) Less(i, j int) bool { 138 | a, b := v[i].Ints(), v[j].Ints() 139 | 140 | return a[0] < b[0] || 141 | a[0] == b[0] && a[1] < b[1] || 142 | a[0] == b[0] && a[1] == b[1] && a[2] < b[2] || 143 | a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && v[i].Extra < v[j].Extra 144 | } 145 | -------------------------------------------------------------------------------- /git/version_test.go: -------------------------------------------------------------------------------- 1 | package git 2 | 3 | import ( 4 | "fmt" 5 | "reflect" 6 | "testing" 7 | 8 | "github.com/pascaldekloe/goe/verify" 9 | ) 10 | 11 | func TestParse(t *testing.T) { 12 | tests := []struct { 13 | s string 14 | v Version 15 | }{ 16 | {"1.0.0", Version{Major: 1}}, 17 | {"v1.0.0", Version{Prefix: "v", Major: 1}}, 18 | {"v0.28.0", Version{Prefix: "v", Major: 0, Minor: 28}}, 19 | {"v1.0.1", Version{Prefix: "v", Major: 1, Patch: 1}}, 20 | {"v1.1.1", Version{Prefix: "v", Major: 1, Minor: 1, Patch: 1}}, 21 | {"v1.1.0", Version{Prefix: "v", Major: 1, Minor: 1}}, 22 | {"v1.1.0-foo", Version{Prefix: "v", Major: 1, Minor: 1, Extra: "foo"}}, 23 | {"v1.1.1-foo", Version{Prefix: "v", Major: 1, Minor: 1, Patch: 1, Extra: "foo"}}, 24 | } 25 | 26 | for _, tt := range tests { 27 | t.Run(tt.s, func(t *testing.T) { 28 | v, err := Parse(tt.s) 29 | if err != nil { 30 | t.Fatalf("got error %s want nil", err) 31 | } 32 | if got, want := v, tt.v; !reflect.DeepEqual(got, want) { 33 | t.Fatalf("\ngot %#v\nwant %#v", got, want) 34 | } 35 | if got, want := v.String(), tt.s; got != want { 36 | t.Fatalf("got version %s want %s", got, want) 37 | } 38 | }) 39 | } 40 | } 41 | 42 | func TestSort(t *testing.T) { 43 | tests := []struct { 44 | in, out []string 45 | }{ 46 | {[]string{"1.0.0", "2.0.0"}, []string{"1.0.0", "2.0.0"}}, 47 | {[]string{"2.0.0", "1.0.0"}, []string{"1.0.0", "2.0.0"}}, 48 | {[]string{"1.2.0", "1.0.0"}, []string{"1.0.0", "1.2.0"}}, 49 | {[]string{"1.0.1", "1.0.0"}, []string{"1.0.0", "1.0.1"}}, 50 | {[]string{"v0.28.0", "v0.9.0"}, []string{"v0.9.0", "v0.28.0"}}, 51 | {[]string{"v0.24.0", "v0.22.0-test"}, []string{"v0.22.0-test", "v0.24.0"}}, 52 | } 53 | 54 | for _, tt := range tests { 55 | name := fmt.Sprintf("%#v", tt.in) 56 | t.Run(name, func(t *testing.T) { 57 | vv, err := ParseAll(tt.in) 58 | if err != nil { 59 | t.Fatalf("got error %s want nil", err) 60 | } 61 | var ss []string 62 | for _, v := range vv { 63 | ss = append(ss, v.String()) 64 | } 65 | if got, want := ss, tt.out; !reflect.DeepEqual(got, want) { 66 | t.Fatalf("\ngot %#v\nwant %#v", got, want) 67 | } 68 | }) 69 | } 70 | } 71 | 72 | func TestBump(t *testing.T) { 73 | v := Version{Prefix: "a", Major: 1, Minor: 2, Patch: 3} 74 | tests := []struct { 75 | name string 76 | got Version 77 | want Version 78 | }{ 79 | {"Bump", v.Bump(), Version{Prefix: "a", Major: 1, Minor: 2, Patch: 4}}, 80 | {"BumpPatch", v.BumpPatch(), Version{Prefix: "a", Major: 1, Minor: 2, Patch: 4}}, 81 | {"BumpMinor", v.BumpMinor(), Version{Prefix: "a", Major: 1, Minor: 3, Patch: 0}}, 82 | {"BumpMajor", v.BumpMajor(), Version{Prefix: "a", Major: 2, Minor: 0, Patch: 0}}, 83 | } 84 | for _, tt := range tests { 85 | verify.Values(t, tt.name, tt.got, tt.want) 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/magiconair/bump 2 | 3 | go 1.24.0 4 | 5 | require ( 6 | github.com/pascaldekloe/goe v0.1.0 7 | github.com/urfave/cli v1.22.16 8 | ) 9 | 10 | require ( 11 | github.com/cpuguy83/go-md2man/v2 v2.0.6 // indirect 12 | github.com/russross/blackfriday/v2 v2.1.0 // indirect 13 | ) 14 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho= 2 | github.com/cpuguy83/go-md2man/v2 v2.0.5/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= 3 | github.com/cpuguy83/go-md2man/v2 v2.0.6 h1:XJtiaUW6dEEqVuZiMTn1ldk455QWwEIsMIJlo5vtkx0= 4 | github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g= 5 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 6 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 7 | github.com/pascaldekloe/goe v0.1.0 h1:cBOtyMzM9HTpWjXfbbunk26uA6nG3a8n06Wieeh0MwY= 8 | github.com/pascaldekloe/goe v0.1.0/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= 9 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 10 | github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= 11 | github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= 12 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 13 | github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= 14 | github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= 15 | github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= 16 | github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= 17 | github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= 18 | github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= 19 | github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= 20 | github.com/urfave/cli v1.20.0 h1:fDqGv3UG/4jbVl/QkFwEdddtEDjh/5Ov6X+0B/3bPaw= 21 | github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA= 22 | github.com/urfave/cli v1.22.16 h1:MH0k6uJxdwdeWQTwhSO42Pwr4YLrNLwBtg1MRgTqPdQ= 23 | github.com/urfave/cli v1.22.16/go.mod h1:EeJR6BKodywf4zciqrdw6hpCPk68JO9z5LazXZMn5Po= 24 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 25 | gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= 26 | gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 27 | gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 28 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "log" 6 | "os" 7 | 8 | "github.com/magiconair/bump/git" 9 | 10 | "github.com/urfave/cli" 11 | ) 12 | 13 | var version = "1.2.1" 14 | 15 | func main() { 16 | log.SetFlags(0) 17 | 18 | empty, err := git.IsEmptyRepository() 19 | if err != nil { 20 | log.Fatal(err) 21 | } 22 | if empty { 23 | log.Fatal("git repository is empty. Please create at least one commit") 24 | } 25 | 26 | versions, err := git.Tags() 27 | if err != nil { 28 | log.Fatal(err) 29 | } 30 | if len(versions) == 0 { 31 | versions = append(versions, git.Version{Prefix: "v"}) 32 | } 33 | cur := versions[len(versions)-1] 34 | 35 | app := cli.NewApp() 36 | app.HideVersion = true 37 | app.HideHelp = true 38 | app.Usage = "A tool for managing versions in git tags" 39 | app.Commands = []cli.Command{ 40 | { 41 | Name: "cur", 42 | Usage: "print current version", 43 | Action: func(c *cli.Context) error { 44 | fmt.Println(cur) 45 | return nil 46 | }, 47 | }, 48 | { 49 | Name: "list", 50 | Usage: "print all version", 51 | Action: func(c *cli.Context) error { 52 | for _, v := range versions { 53 | fmt.Println(v) 54 | } 55 | return nil 56 | }, 57 | }, 58 | { 59 | Name: "next", 60 | Usage: "print next version", 61 | Action: func(c *cli.Context) error { 62 | fmt.Println(cur.Bump()) 63 | return nil 64 | }, 65 | Subcommands: []cli.Command{ 66 | { 67 | Name: "major", 68 | Usage: "print next major version", 69 | Action: func(c *cli.Context) error { 70 | fmt.Println(cur.BumpMajor()) 71 | return nil 72 | }, 73 | }, 74 | { 75 | Name: "minor", 76 | Usage: "print next minor version", 77 | Action: func(c *cli.Context) error { 78 | fmt.Println(cur.BumpMinor()) 79 | return nil 80 | }, 81 | }, 82 | { 83 | Name: "patch", 84 | Usage: "print next patch version", 85 | Action: func(c *cli.Context) error { 86 | fmt.Println(cur.BumpPatch()) 87 | return nil 88 | }, 89 | }, 90 | }, 91 | }, 92 | { 93 | Name: "tag", 94 | Usage: "tag with next version", 95 | Action: func(c *cli.Context) error { 96 | v := cur.Bump() 97 | if err := git.Tag(v); err != nil { 98 | log.Fatal(err) 99 | } 100 | log.Print(v) 101 | return nil 102 | }, 103 | Subcommands: []cli.Command{ 104 | { 105 | Name: "major", 106 | Usage: "tag with next major version", 107 | Action: func(c *cli.Context) error { 108 | v := cur.BumpMajor() 109 | if err := git.Tag(v); err != nil { 110 | log.Fatal(err) 111 | } 112 | log.Print(v) 113 | return nil 114 | }, 115 | }, 116 | { 117 | Name: "minor", 118 | Usage: "tag with next minor version", 119 | Action: func(c *cli.Context) error { 120 | v := cur.BumpMinor() 121 | if err := git.Tag(v); err != nil { 122 | log.Fatal(err) 123 | } 124 | log.Print(v) 125 | return nil 126 | }, 127 | }, 128 | { 129 | Name: "patch", 130 | Usage: "tag with next patch version", 131 | Action: func(c *cli.Context) error { 132 | v := cur.BumpPatch() 133 | if err := git.Tag(v); err != nil { 134 | log.Fatal(err) 135 | } 136 | log.Print(v) 137 | return nil 138 | }, 139 | }, 140 | }, 141 | }, 142 | { 143 | Name: "version", 144 | Usage: "print bump version", 145 | Action: func(c *cli.Context) error { 146 | fmt.Println(version) 147 | return nil 148 | }, 149 | }, 150 | } 151 | 152 | if err = app.Run(os.Args); err != nil { 153 | log.Fatal(err) 154 | } 155 | } 156 | --------------------------------------------------------------------------------