git-lfs/commands/command_path.go

186 lines
3.5 KiB
Go
Raw Normal View History

package commands
import (
"bufio"
"fmt"
"github.com/github/git-media/gitmedia"
2014-06-26 20:52:16 +00:00
"github.com/spf13/cobra"
2014-03-24 14:36:27 +00:00
"io/ioutil"
"os"
"path/filepath"
"strings"
)
2014-06-26 20:52:16 +00:00
var (
pathCmd = &cobra.Command{
Use: "path",
Short: "Manipulate .gitattributes",
Run: pathCommand,
}
2014-06-26 21:29:11 +00:00
pathAddCmd = &cobra.Command{
Use: "add",
Short: "Add an entry to .gitattributes",
Run: pathAddCommand,
}
pathRemoveCmd = &cobra.Command{
Use: "remove",
Short: "Remove an entry from .gitattributes",
Run: pathRemoveCommand,
}
2014-06-26 20:52:16 +00:00
)
2014-06-26 20:52:16 +00:00
func pathCommand(cmd *cobra.Command, args []string) {
2014-06-05 18:48:23 +00:00
gitmedia.InstallHooks()
2014-06-05 16:50:20 +00:00
2014-06-26 21:44:50 +00:00
Print("Listing paths")
2014-06-26 21:29:11 +00:00
knownPaths := findPaths()
for _, t := range knownPaths {
2014-06-26 21:44:50 +00:00
Print(" %s (%s)", t.Path, t.Source)
}
}
2014-06-26 21:29:11 +00:00
func pathAddCommand(cmd *cobra.Command, args []string) {
gitmedia.InstallHooks()
if len(args) < 1 {
2014-06-26 21:44:50 +00:00
Print("git media path add <path> [path]*")
2014-03-24 14:21:31 +00:00
return
}
2014-03-25 18:13:36 +00:00
knownPaths := findPaths()
attributesFile, err := os.OpenFile(".gitattributes", os.O_RDWR|os.O_APPEND|os.O_CREATE, 0660)
2014-03-24 14:21:31 +00:00
if err != nil {
2014-06-26 21:44:50 +00:00
Print("Error opening .gitattributes file")
2014-03-24 14:21:31 +00:00
return
}
2014-06-26 21:29:11 +00:00
for _, t := range args {
2014-03-25 18:13:36 +00:00
isKnownPath := false
for _, k := range knownPaths {
if t == k.Path {
2014-03-25 18:13:36 +00:00
isKnownPath = true
2014-03-24 14:21:31 +00:00
}
}
2014-03-25 18:13:36 +00:00
if isKnownPath {
2014-06-26 21:44:50 +00:00
Print("%s already supported", t)
2014-03-24 14:21:31 +00:00
continue
}
_, err := attributesFile.WriteString(fmt.Sprintf("%s filter=media -crlf\n", t))
2014-03-24 14:21:31 +00:00
if err != nil {
2014-06-26 21:44:50 +00:00
Print("Error adding path %s", t)
2014-03-24 14:21:31 +00:00
continue
}
2014-06-26 21:44:50 +00:00
Print("Adding path %s", t)
2014-03-24 14:21:31 +00:00
}
attributesFile.Close()
}
2014-06-26 21:29:11 +00:00
func pathRemoveCommand(cmd *cobra.Command, args []string) {
gitmedia.InstallHooks()
if len(args) < 1 {
2014-06-26 21:44:50 +00:00
Print("git media path remove <path> [path]*")
2014-03-24 14:36:27 +00:00
return
}
data, err := ioutil.ReadFile(".gitattributes")
if err != nil {
return
}
attributes := strings.NewReader(string(data))
attributesFile, err := os.Create(".gitattributes")
if err != nil {
2014-06-26 21:44:50 +00:00
Print("Error opening .gitattributes for writing")
2014-03-24 14:36:27 +00:00
return
}
scanner := bufio.NewScanner(attributes)
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, "filter=media") {
fields := strings.Fields(line)
2014-03-25 18:13:36 +00:00
removeThisPath := false
2014-06-26 21:29:11 +00:00
for _, t := range args {
2014-03-24 14:36:27 +00:00
if t == fields[0] {
2014-03-25 18:13:36 +00:00
removeThisPath = true
2014-03-24 14:36:27 +00:00
}
}
2014-03-25 18:13:36 +00:00
if !removeThisPath {
2014-03-24 14:36:27 +00:00
attributesFile.WriteString(line + "\n")
} else {
2014-06-26 21:44:50 +00:00
Print("Removing path %s", fields[0])
2014-03-24 14:36:27 +00:00
}
}
}
attributesFile.Close()
}
type mediaPath struct {
Path string
Source string
}
func findAttributeFiles() []string {
paths := make([]string, 0)
repoAttributes := filepath.Join(gitmedia.LocalGitDir, "info", "attributes")
if _, err := os.Stat(repoAttributes); err == nil {
paths = append(paths, repoAttributes)
}
filepath.Walk(gitmedia.LocalWorkingDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() && (filepath.Base(path) == ".gitattributes") {
paths = append(paths, path)
}
return nil
})
return paths
}
func findPaths() []mediaPath {
paths := make([]mediaPath, 0)
2014-05-28 18:29:05 +00:00
wd, _ := os.Getwd()
2014-03-24 14:21:31 +00:00
for _, path := range findAttributeFiles() {
attributes, err := os.Open(path)
if err != nil {
return paths
}
scanner := bufio.NewScanner(attributes)
for scanner.Scan() {
line := scanner.Text()
if line == "" {
continue
}
if strings.Contains(line, "filter=media") {
fields := strings.Fields(line)
relPath, _ := filepath.Rel(wd, path)
paths = append(paths, mediaPath{Path: fields[0], Source: relPath})
}
}
}
2014-03-25 18:13:36 +00:00
return paths
}
func init() {
2014-06-26 21:29:11 +00:00
pathCmd.AddCommand(pathAddCmd, pathRemoveCmd)
2014-06-26 20:52:16 +00:00
RootCmd.AddCommand(pathCmd)
}