git-lfs/commands/command_init.go

62 lines
1.2 KiB
Go
Raw Normal View History

package gitmedia
import (
"../gitconfig"
"fmt"
2014-05-27 16:32:23 +00:00
"regexp"
)
type InitCommand struct {
*Command
}
2014-05-28 18:28:13 +00:00
var valueRegexp = regexp.MustCompile("\\Agit[\\-\\s]media")
2014-03-19 18:21:34 +00:00
func (c *InitCommand) Run() {
setFilter("clean")
setFilter("smudge")
requireFilters()
fmt.Println("git media initialized")
}
func setFilter(filterName string) {
key := fmt.Sprintf("filter.media.%s", filterName)
value := fmt.Sprintf("git media %s %%f", filterName)
existing := gitconfig.Find(key)
if shouldReset(existing) {
fmt.Printf("Installing %s filter\n", filterName)
gitconfig.UnsetGlobal(key)
gitconfig.SetGlobal(key, value)
} else if existing != value {
fmt.Printf("The %s filter should be \"%s\" but is \"%s\"\n", filterName, value, existing)
}
}
func requireFilters() {
key := "filter.media.required"
value := "true"
existing := gitconfig.Find(key)
if shouldReset(existing) {
gitconfig.UnsetGlobal(key)
gitconfig.SetGlobal(key, value)
} else if existing != value {
2014-05-30 15:04:06 +00:00
fmt.Printf("Media filters should be required but are not")
}
}
2014-05-27 16:32:23 +00:00
func shouldReset(value string) bool {
if len(value) == 0 {
return true
}
return valueRegexp.MatchString(value)
}
func init() {
registerCommand("init", func(c *Command) RunnableCommand {
return &InitCommand{Command: c}
})
}