git-lfs/commands/command_untrack.go
Chris Darroch 4d561b8289 commands,config,t: use single quotes in messages
A number of message strings contain embedded filenames, and while
in many cases these are already delimited with single quotes, in
other cases they are not.

We therefore rework the formatting of these messages to accord
with the general practice of using single quotes to delimit
specific filenames.

Note that some of these messages are not yet passed as translation
strings, but we will address that issue in a subsequent commit.
2022-01-29 22:35:10 -08:00

73 lines
1.6 KiB
Go

package commands
import (
"bufio"
"io/ioutil"
"os"
"strings"
"github.com/git-lfs/git-lfs/v3/tr"
"github.com/spf13/cobra"
)
// untrackCommand takes a list of paths as an argument, and removes each path from the
// default attributes file (.gitattributes), if it exists.
func untrackCommand(cmd *cobra.Command, args []string) {
setupWorkingCopy()
installHooks(false)
if len(args) < 1 {
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(tr.Tr.Get("Error opening '.gitattributes' for writing"))
return
}
defer attributesFile.Close()
scanner := bufio.NewScanner(attributes)
// 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
}
path := strings.Fields(line)[0]
if removePath(path, args) {
Print(tr.Tr.Get("Untracking %q", unescapeAttrPattern(path)))
} else {
attributesFile.WriteString(line + "\n")
}
}
}
func removePath(path string, args []string) bool {
withoutCurrentDir := trimCurrentPrefix(path)
for _, t := range args {
if withoutCurrentDir == escapeAttrPattern(trimCurrentPrefix(t)) {
return true
}
}
return false
}
func init() {
RegisterCommand("untrack", untrackCommand, nil)
}