git-save: make ref prefix configurable

and add a method to return it
This commit is contained in:
Simone Gotti 2019-06-11 15:04:01 +02:00
parent f92bcee73b
commit 853082d4f5

View File

@ -29,7 +29,7 @@ import (
const ( const (
gitIndexFile = "index" gitIndexFile = "index"
gitRefsPrefix = "refs/gitsave" defaultRefsPrefix = "refs/gitsave"
) )
func copyFile(src, dest string) error { func copyFile(src, dest string) error {
@ -146,15 +146,30 @@ func GitPush(configPath, remote, branch string) error {
type GitSaveConfig struct { type GitSaveConfig struct {
AddUntracked bool AddUntracked bool
AddIgnored bool AddIgnored bool
RefsPrefix string
} }
type GitSave struct { type GitSave struct {
log *zap.SugaredLogger log *zap.SugaredLogger
conf *GitSaveConfig conf *GitSaveConfig
refsPrefix string
} }
func NewGitSave(logger *zap.Logger, conf *GitSaveConfig) *GitSave { func NewGitSave(logger *zap.Logger, conf *GitSaveConfig) *GitSave {
return &GitSave{log: logger.Sugar(), conf: conf} refsPrefix := conf.RefsPrefix
if refsPrefix == "" {
refsPrefix = defaultRefsPrefix
}
return &GitSave{
log: logger.Sugar(),
conf: conf,
refsPrefix: refsPrefix,
}
}
func (s *GitSave) RefsPrefix() string {
return s.refsPrefix
} }
// Save adds files to the provided index, creates a tree and a commit pointing to // Save adds files to the provided index, creates a tree and a commit pointing to
@ -231,7 +246,7 @@ func (s *GitSave) Save(message, branchName string) error {
s.log.Infof("commit: %s", commitSHA) s.log.Infof("commit: %s", commitSHA)
s.log.Infof("updating ref") s.log.Infof("updating ref")
if err = gitUpdateRef("git-save", filepath.Join(gitRefsPrefix, branchName), commitSHA); err != nil { if err = gitUpdateRef("git-save", filepath.Join(s.refsPrefix, branchName), commitSHA); err != nil {
return err return err
} }