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-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) {
|
2017-10-19 00:09:33 +00:00
|
|
|
if cfg.LocalGitDir() == "" {
|
2015-06-16 13:52:24 +00:00
|
|
|
Print("Not a git repository.")
|
|
|
|
os.Exit(128)
|
|
|
|
}
|
2017-10-19 00:09:33 +00:00
|
|
|
if cfg.LocalWorkingDir() == "" {
|
2015-06-16 13:52:24 +00:00
|
|
|
Print("This operation must be run in a work tree.")
|
|
|
|
os.Exit(128)
|
|
|
|
}
|
|
|
|
|
2017-10-18 21:42:00 +00:00
|
|
|
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) {
|
2017-03-24 17:00:33 +00:00
|
|
|
Print("Untracking %q", unescapeTrackPattern(path))
|
2015-06-16 13:55:21 +00:00
|
|
|
} 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 {
|
2017-03-23 23:19:00 +00:00
|
|
|
if path == escapeTrackPattern(t) {
|
2015-06-16 13:55:21 +00:00
|
|
|
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-09-01 14:46:26 +00:00
|
|
|
RegisterCommand("untrack", untrackCommand, nil)
|
2015-02-01 17:11:35 +00:00
|
|
|
}
|