Move filter installation under gitmedia package. Shave some yaks to make that happen.
This commit is contained in:
parent
2719233145
commit
cffa7d3de8
@ -1,18 +1,13 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/github/git-media/gitconfig"
|
||||
"github.com/github/git-media/gitmedia"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
type InitCommand struct {
|
||||
*Command
|
||||
}
|
||||
|
||||
var valueRegexp = regexp.MustCompile("\\Agit[\\-\\s]media")
|
||||
|
||||
func (c *InitCommand) Run() {
|
||||
var sub string
|
||||
if len(c.SubCommands) > 0 {
|
||||
@ -40,49 +35,13 @@ func (c *InitCommand) runInit() {
|
||||
}
|
||||
|
||||
func (c *InitCommand) globalInit() {
|
||||
setFilter("clean")
|
||||
setFilter("smudge")
|
||||
requireFilters()
|
||||
gitmedia.InstallFilters()
|
||||
}
|
||||
|
||||
func (c *InitCommand) hookInit() error {
|
||||
return gitmedia.InstallHooks()
|
||||
}
|
||||
|
||||
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) {
|
||||
gitmedia.Print("Installing %s filter", filterName)
|
||||
gitconfig.UnsetGlobal(key)
|
||||
gitconfig.SetGlobal(key, value)
|
||||
} else if existing != value {
|
||||
gitmedia.Print("The %s filter should be \"%s\" but is \"%s\"", 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 {
|
||||
gitmedia.Print("Media filters should be required but are not")
|
||||
}
|
||||
}
|
||||
|
||||
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}
|
||||
|
@ -1,17 +1,39 @@
|
||||
package gitconfig
|
||||
|
||||
import (
|
||||
"github.com/github/git-media/gitmedia"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func Find(val string) string {
|
||||
return gitmedia.SimpleExec("git", "config", val)
|
||||
output, _ := SimpleExec("git", "config", val)
|
||||
return output
|
||||
}
|
||||
|
||||
func SetGlobal(key, val string) {
|
||||
gitmedia.SimpleExec("git", "config", "--global", "--add", key, val)
|
||||
SimpleExec("git", "config", "--global", "--add", key, val)
|
||||
}
|
||||
|
||||
func UnsetGlobal(key string) {
|
||||
gitmedia.SimpleExec("git", "config", "--global", "--unset", key)
|
||||
SimpleExec("git", "config", "--global", "--unset", key)
|
||||
}
|
||||
|
||||
func List() (string, error) {
|
||||
return SimpleExec("git", "config", "-l")
|
||||
}
|
||||
|
||||
func ListFromFile() (string, error) {
|
||||
return SimpleExec("git", "config", "-l", "-f", ".gitconfig")
|
||||
}
|
||||
|
||||
func SimpleExec(name string, arg ...string) (string, error) {
|
||||
output, err := exec.Command(name, arg...).Output()
|
||||
if _, ok := err.(*exec.ExitError); ok {
|
||||
return "", nil
|
||||
} else if err != nil {
|
||||
return fmt.Sprintf("Error running %s %s", name, arg), err
|
||||
}
|
||||
|
||||
return strings.Trim(string(output), " \n"), nil
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package gitmedia
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/github/git-media/gitconfig"
|
||||
"os"
|
||||
"path"
|
||||
"regexp"
|
||||
@ -88,9 +89,17 @@ func (c *Configuration) loadGitConfig() {
|
||||
c.gitConfig = make(map[string]string)
|
||||
|
||||
var output string
|
||||
output = SimpleExec("git", "config", "-l")
|
||||
output += "\n"
|
||||
output += SimpleExec("git", "config", "-l", "-f", ".gitconfig")
|
||||
listOutput, err := gitconfig.List()
|
||||
if err != nil {
|
||||
Panic(err, listOutput)
|
||||
}
|
||||
|
||||
fileOutput, err := gitconfig.ListFromFile()
|
||||
if err != nil {
|
||||
Panic(err, fileOutput)
|
||||
}
|
||||
|
||||
output = listOutput + "\n" + fileOutput
|
||||
|
||||
lines := strings.Split(output, "\n")
|
||||
for _, line := range lines {
|
||||
|
@ -1,11 +1,9 @@
|
||||
package gitmedia
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
@ -25,17 +23,6 @@ func TempFile() (*os.File, error) {
|
||||
return ioutil.TempFile(TempDir, "")
|
||||
}
|
||||
|
||||
func SimpleExec(name string, arg ...string) string {
|
||||
output, err := exec.Command(name, arg...).Output()
|
||||
if _, ok := err.(*exec.ExitError); ok {
|
||||
return ""
|
||||
} else if err != nil {
|
||||
Panic(err, "Error running %s %s", name, arg)
|
||||
}
|
||||
|
||||
return strings.Trim(string(output), " \n")
|
||||
}
|
||||
|
||||
func LocalMediaPath(sha string) string {
|
||||
path := filepath.Join(LocalMediaDir, sha[0:2], sha[2:4])
|
||||
if err := os.MkdirAll(path, 0744); err != nil {
|
||||
@ -67,14 +54,6 @@ func InRepo() bool {
|
||||
return LocalWorkingDir != ""
|
||||
}
|
||||
|
||||
func InstallHooks() error {
|
||||
if !InRepo() {
|
||||
return errors.New("Not in a repository")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
var err error
|
||||
LocalWorkingDir, LocalGitDir, err = resolveGitDir()
|
||||
|
58
gitmedia/setup.go
Normal file
58
gitmedia/setup.go
Normal file
@ -0,0 +1,58 @@
|
||||
package gitmedia
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/github/git-media/gitconfig"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
var valueRegexp = regexp.MustCompile("\\Agit[\\-\\s]media")
|
||||
|
||||
func InstallHooks() error {
|
||||
if !InRepo() {
|
||||
return errors.New("Not in a repository")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func InstallFilters() {
|
||||
setFilter("clean")
|
||||
setFilter("smudge")
|
||||
requireFilters()
|
||||
}
|
||||
|
||||
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) {
|
||||
Print("Installing %s filter", filterName)
|
||||
gitconfig.UnsetGlobal(key)
|
||||
gitconfig.SetGlobal(key, value)
|
||||
} else if existing != value {
|
||||
Print("The %s filter should be \"%s\" but is \"%s\"", 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 {
|
||||
Print("Media filters should be required but are not")
|
||||
}
|
||||
}
|
||||
|
||||
func shouldReset(value string) bool {
|
||||
if len(value) == 0 {
|
||||
return true
|
||||
}
|
||||
return valueRegexp.MatchString(value)
|
||||
}
|
Loading…
Reference in New Issue
Block a user