Created
July 27, 2016 23:45
-
-
Save sionleroux/93ec3a01b26376e7da2f39ef24b6c4c7 to your computer and use it in GitHub Desktop.
Version bump git tag in Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"os" | |
"github.com/Symantec/Dominator/lib/verstr" // sorts semvers | |
"github.com/libgit2/git2go" // git access to read/write tags | |
"github.com/olivoil/bump" // semantically bump tag number | |
) | |
/* | |
Tags the HEAD commit of the git repo in the current directory with a | |
semver string one patch version higher than the previous highest. | |
*/ | |
func main() { | |
// path is current directory | |
pwd, err := os.Getwd() | |
if err != nil { | |
log.Fatal(err) | |
} | |
// find repo in path | |
repo, err := git.OpenRepository(pwd) | |
if err != nil { | |
log.Fatal(err) | |
} | |
// get tags | |
tags, err := repo.Tags.List() | |
if err != nil { | |
log.Fatal(err) | |
} | |
// sort tags and pick last (latest) | |
verstr.Sort(tags) | |
last := tags[len(tags)-1] | |
// new string with bumped version number | |
bumped, err := bump.String("patch", last) | |
if err != nil { | |
log.Fatal(err) | |
} | |
// get current HEAD ref | |
head, err := repo.Head() | |
if err != nil { | |
log.Fatal(err) | |
} | |
// commit to tag is at current HEAD | |
headCommit, err := repo.LookupCommit(head.Target()) | |
if err != nil { | |
log.Fatal(err) | |
} | |
// tag creation requires a tag name string and a commit | |
newtag, err := repo.Tags.CreateLightweight(bumped, headCommit, false) | |
if err != nil { | |
log.Fatal(err) | |
} | |
// done | |
fmt.Printf("bumped %s to %s on %s\n", last, bumped, newtag) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment