github.com/stffabi/git-lfs@v2.3.5-0.20180214015214-8eeaa8d88902+incompatible/commands/command_untrack.go (about) 1 package commands 2 3 import ( 4 "bufio" 5 "io/ioutil" 6 "os" 7 "strings" 8 9 "github.com/spf13/cobra" 10 ) 11 12 // untrackCommand takes a list of paths as an argument, and removes each path from the 13 // default attributes file (.gitattributes), if it exists. 14 func untrackCommand(cmd *cobra.Command, args []string) { 15 if cfg.LocalGitDir() == "" { 16 Print("Not a git repository.") 17 os.Exit(128) 18 } 19 if cfg.LocalWorkingDir() == "" { 20 Print("This operation must be run in a work tree.") 21 os.Exit(128) 22 } 23 24 installHooks(false) 25 26 if len(args) < 1 { 27 Print("git lfs untrack <path> [path]*") 28 return 29 } 30 31 data, err := ioutil.ReadFile(".gitattributes") 32 if err != nil { 33 return 34 } 35 36 attributes := strings.NewReader(string(data)) 37 38 attributesFile, err := os.Create(".gitattributes") 39 if err != nil { 40 Print("Error opening .gitattributes for writing") 41 return 42 } 43 defer attributesFile.Close() 44 45 scanner := bufio.NewScanner(attributes) 46 47 // Iterate through each line of the attributes file and rewrite it, 48 // if the path was meant to be untracked, omit it, and print a message instead. 49 for scanner.Scan() { 50 line := scanner.Text() 51 if !strings.Contains(line, "filter=lfs") { 52 attributesFile.WriteString(line + "\n") 53 continue 54 } 55 56 path := strings.Fields(line)[0] 57 if removePath(path, args) { 58 Print("Untracking %q", unescapeTrackPattern(path)) 59 } else { 60 attributesFile.WriteString(line + "\n") 61 } 62 } 63 } 64 65 func removePath(path string, args []string) bool { 66 for _, t := range args { 67 if path == escapeTrackPattern(t) { 68 return true 69 } 70 } 71 72 return false 73 } 74 75 func init() { 76 RegisterCommand("untrack", untrackCommand, nil) 77 }