git-lfs/commands/command_untrack.go

86 lines
1.7 KiB
Go
Raw Normal View History

package commands
import (
"bufio"
"io/ioutil"
"os"
"strings"
2015-05-13 19:43:41 +00:00
"github.com/github/git-lfs/config"
2015-05-13 19:43:41 +00:00
"github.com/github/git-lfs/lfs"
"github.com/spf13/cobra"
)
2015-04-11 10:54:05 +00:00
// untrackCommand takes a list of paths as an argument, and removes each path from the
2015-10-01 19:37:35 +00:00
// default attributes file (.gitattributes), if it exists.
2015-03-22 18:50:06 +00:00
func untrackCommand(cmd *cobra.Command, args []string) {
if config.LocalGitDir == "" {
Print("Not a git repository.")
os.Exit(128)
}
if config.LocalWorkingDir == "" {
Print("This operation must be run in a work tree.")
os.Exit(128)
}
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
}
defer attributesFile.Close()
scanner := bufio.NewScanner(attributes)
2015-04-11 11:48:44 +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()
if !strings.Contains(line, "filter=lfs") {
attributesFile.WriteString(line + "\n")
continue
}
2015-06-16 13:55:21 +00:00
path := strings.Fields(line)[0]
if removePath(path, args) {
Print("Untracking %s", path)
} else {
attributesFile.WriteString(line + "\n")
}
2015-06-16 13:55:21 +00:00
}
}
2015-06-16 13:55:21 +00:00
func removePath(path string, args []string) bool {
for _, t := range args {
if path == t {
return true
}
}
2015-06-16 13:55:21 +00:00
return false
}
func init() {
RegisterSubcommand(func() *cobra.Command {
return &cobra.Command{
Use: "untrack",
PreRun: resolveLocalStorage,
Run: untrackCommand,
}
})
}