-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
show right server version when server is compiled from source
- Loading branch information
Showing
11 changed files
with
231 additions
and
20 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// Package main contains an utility to get the server version | ||
package main | ||
|
||
import ( | ||
"log" | ||
"os" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/go-git/go-git/v5" | ||
"github.com/go-git/go-git/v5/plumbing" | ||
) | ||
|
||
// Golang version of git describe --tags | ||
func gitDescribeTags(repo *git.Repository) (string, error) { | ||
head, err := repo.Head() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
tagIterator, err := repo.Tags() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
tags := make(map[plumbing.Hash]*plumbing.Reference) | ||
|
||
tagIterator.ForEach(func(t *plumbing.Reference) error { | ||
if to, err := repo.TagObject(t.Hash()); err == nil { | ||
tags[to.Target] = t | ||
} else { | ||
tags[t.Hash()] = t | ||
} | ||
return nil | ||
}) | ||
tagIterator.Close() | ||
|
||
cIter, err := repo.Log(&git.LogOptions{From: head.Hash()}) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
i := 0 | ||
|
||
for { | ||
commit, err := cIter.Next() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if str, ok := tags[commit.Hash]; ok { | ||
label := strings.TrimPrefix(string(str.Name()), "refs/tags/") | ||
|
||
if i != 0 { | ||
label += "-" + strconv.FormatInt(int64(i), 10) + "-" + head.Hash().String()[:8] | ||
} | ||
|
||
return label, nil | ||
} | ||
|
||
i++ | ||
} | ||
} | ||
|
||
func do() error { | ||
log.Println("getting version...") | ||
|
||
repo, err := git.PlainOpen("../..") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
label, err := gitDescribeTags(repo) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
wt, err := repo.Worktree() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
status, err := wt.Status() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if !status.IsClean() { | ||
label += "-dirty" | ||
} | ||
|
||
err = os.WriteFile("VERSION", []byte(label), 0o644) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Println("ok") | ||
return nil | ||
} | ||
|
||
func main() { | ||
err := do() | ||
if err != nil { | ||
log.Printf("ERR: %v", err) | ||
os.Exit(1) | ||
} | ||
} |
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
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
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