2015-02-01 17:11:35 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"strings"
|
2015-05-13 19:43:41 +00:00
|
|
|
|
2016-05-13 16:38:06 +00:00
|
|
|
"github.com/github/git-lfs/config"
|
2015-05-13 19:43:41 +00:00
|
|
|
"github.com/github/git-lfs/lfs"
|
2016-05-23 18:02:27 +00:00
|
|
|
"github.com/spf13/cobra"
|
2015-02-01 17:11:35 +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
|
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) {
|
2016-05-13 16:38:06 +00:00
|
|
|
if config.LocalGitDir == "" {
|
2015-06-16 13:52:24 +00:00
|
|
|
Print("Not a git repository.")
|
|
|
|
os.Exit(128)
|
|
|
|
}
|
2016-05-13 16:38:06 +00:00
|
|
|
if config.LocalWorkingDir == "" {
|
2015-06-16 13:52:24 +00:00
|
|
|
Print("This operation must be run in a work tree.")
|
|
|
|
os.Exit(128)
|
|
|
|
}
|
|
|
|
|
2015-03-19 19:30:55 +00:00
|
|
|
lfs.InstallHooks(false)
|
2015-02-01 17:11:35 +00:00
|
|
|
|
|
|
|
if len(args) < 1 {
|
2015-03-22 18:50:06 +00:00
|
|
|
Print("git lfs untrack <path> [path]*")
|
2015-02-01 17:11:35 +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 {
|
|
|
|
Print("Error opening .gitattributes for writing")
|
|
|
|
return
|
|
|
|
}
|
2015-06-16 13:57:49 +00:00
|
|
|
defer attributesFile.Close()
|
2015-02-01 17:11:35 +00:00
|
|
|
|
|
|
|
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.
|
2015-02-01 17:11:35 +00:00
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
2015-06-16 13:53:40 +00:00
|
|
|
if !strings.Contains(line, "filter=lfs") {
|
|
|
|
attributesFile.WriteString(line + "\n")
|
|
|
|
continue
|
|
|
|
}
|
2015-02-01 17:11:35 +00:00
|
|
|
|
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-02-01 17:11:35 +00:00
|
|
|
}
|
2015-06-16 13:55:21 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-16 13:53:40 +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:53:40 +00:00
|
|
|
}
|
2015-02-01 17:11:35 +00:00
|
|
|
}
|
2015-06-16 13:55:21 +00:00
|
|
|
|
|
|
|
return false
|
2015-02-01 17:11:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2016-08-10 15:33:25 +00:00
|
|
|
RegisterSubcommand(func() *cobra.Command {
|
|
|
|
return &cobra.Command{
|
2016-08-22 17:13:37 +00:00
|
|
|
Use: "untrack",
|
|
|
|
PreRun: resolveLocalStorage,
|
|
|
|
Run: untrackCommand,
|
2016-08-10 15:33:25 +00:00
|
|
|
}
|
|
|
|
})
|
2015-02-01 17:11:35 +00:00
|
|
|
}
|