git-lfs/commands/command_untrack.go

71 lines
1.4 KiB
Go
Raw Normal View History

package commands
import (
"bufio"
2015-03-19 19:30:55 +00:00
"github.com/github/git-lfs/lfs"
"github.com/spf13/cobra"
"io/ioutil"
"os"
"strings"
)
var (
2015-03-22 18:50:06 +00:00
untrackCmd = &cobra.Command{
Use: "untrack",
Short: "Remove an entry from .gitattributes",
2015-03-22 18:50:06 +00:00
Run: untrackCommand,
2015-02-02 16:06:42 +00:00
}
)
2015-04-11 10:54:05 +00:00
// untrackCommand takes a list of paths as an argument, and removes each path from the
// default attribtues file (.gitattributes), if it's exists.
2015-03-22 18:50:06 +00:00
func untrackCommand(cmd *cobra.Command, args []string) {
2015-03-19 19:30:55 +00:00
lfs.InstallHooks(false)
if len(args) < 1 {
2015-03-22 18:50:06 +00:00
Print("git lfs untrack <path> [path]*")
return
}
data, err := ioutil.ReadFile(".gitattributes")
if err != nil {
return
}
attributes := strings.NewReader(string(data))
attributesFile, err := os.Create(".gitattributes")
if err != nil {
Print("Error opening .gitattributes for writing")
return
}
scanner := bufio.NewScanner(attributes)
2015-04-11 10:56:50 +00:00
// Iterate through each line of the attributes file and rewrite it(!),
// if the path was meant to be untracked, omit it and print a message instead.
for scanner.Scan() {
line := scanner.Text()
2015-03-19 19:30:55 +00:00
if strings.Contains(line, "filter=lfs") {
fields := strings.Fields(line)
removeThisPath := false
for _, t := range args {
if t == fields[0] {
removeThisPath = true
}
}
if !removeThisPath {
attributesFile.WriteString(line + "\n")
} else {
2015-03-22 18:50:06 +00:00
Print("Untracking %s", fields[0])
}
}
}
attributesFile.Close()
}
func init() {
2015-03-22 18:50:06 +00:00
RootCmd.AddCommand(untrackCmd)
}