git-lfs/commands/command_track.go

242 lines
5.6 KiB
Go
Raw Normal View History

2015-03-22 18:50:06 +00:00
package commands
import (
"bufio"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"time"
"github.com/github/git-lfs/config"
"github.com/github/git-lfs/git"
2015-05-13 19:43:41 +00:00
"github.com/github/git-lfs/lfs"
"github.com/spf13/cobra"
2015-03-22 18:50:06 +00:00
)
var (
2016-07-05 22:05:58 +00:00
prefixBlocklist = []string{
".git", ".lfs",
}
2015-03-22 18:50:06 +00:00
trackCmd = &cobra.Command{
2015-09-17 22:08:28 +00:00
Use: "track",
Run: trackCommand,
2015-03-22 18:50:06 +00:00
}
trackVerboseLoggingFlag bool
trackDryRunFlag bool
2015-03-22 18:50:06 +00:00
)
func trackCommand(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-22 18:50:06 +00:00
lfs.InstallHooks(false)
knownPaths := findPaths()
2015-03-22 18:50:06 +00:00
if len(args) == 0 {
Print("Listing tracked paths")
for _, t := range knownPaths {
Print(" %s (%s)", t.Path, t.Source)
}
return
}
addTrailingLinebreak := needsTrailingLinebreak(".gitattributes")
attributesFile, err := os.OpenFile(".gitattributes", os.O_RDWR|os.O_APPEND|os.O_CREATE, 0660)
if err != nil {
Print("Error opening .gitattributes file")
return
}
defer attributesFile.Close()
2015-03-22 18:50:06 +00:00
if addTrailingLinebreak {
if _, err := attributesFile.WriteString("\n"); err != nil {
Print("Error writing to .gitattributes")
}
}
2015-06-01 10:48:06 +00:00
wd, _ := os.Getwd()
relpath, err := filepath.Rel(config.LocalWorkingDir, wd)
if err != nil {
Exit("Current directory %q outside of git working directory %q.", wd, config.LocalWorkingDir)
}
2015-06-01 10:48:06 +00:00
ArgsLoop:
for _, pattern := range args {
for _, known := range knownPaths {
if known.Path == filepath.Join(relpath, pattern) {
Print("%s already supported", pattern)
continue ArgsLoop
2015-03-22 18:50:06 +00:00
}
}
// Make sure any existing git tracked files have their timestamp updated
// so they will now show as modifed
// note this is relative to current dir which is how we write .gitattributes
// deliberately not done in parallel as a chan because we'll be marking modified
//
// NOTE: `git ls-files` does not do well with leading slashes.
// Since all `git-lfs track` calls are relative to the root of
// the repository, the leading slash is simply removed for its
// implicit counterpart.
if trackVerboseLoggingFlag {
Print("Searching for files matching pattern: %s", pattern)
}
gittracked, err := git.GetTrackedFiles(pattern)
if err != nil {
LoggedError(err, "Error getting git tracked files")
continue
}
if trackVerboseLoggingFlag {
Print("Found %d files previously added to Git matching pattern: %s", len(gittracked), pattern)
}
now := time.Now()
2016-07-05 22:05:58 +00:00
var matchedBlocklist bool
for _, f := range gittracked {
2016-07-05 22:05:58 +00:00
if forbidden := blocklistItem(f); forbidden != "" {
Print("Pattern %s matches forbidden file %s. If you would like to track %s, modify .gitattributes manually.", pattern, f, f)
2016-07-05 22:05:58 +00:00
matchedBlocklist = true
}
}
2016-07-05 22:05:58 +00:00
if matchedBlocklist {
continue
}
if !trackDryRunFlag {
encodedArg := strings.Replace(pattern, " ", "[[:space:]]", -1)
_, err := attributesFile.WriteString(fmt.Sprintf("%s filter=lfs diff=lfs merge=lfs -text\n", encodedArg))
if err != nil {
Print("Error adding path %s", pattern)
continue
}
}
Print("Tracking %s", pattern)
for _, f := range gittracked {
if trackVerboseLoggingFlag || trackDryRunFlag {
Print("Git LFS: touching %s", f)
}
if !trackDryRunFlag {
err := os.Chtimes(f, now, now)
if err != nil {
LoggedError(err, "Error marking %q modified", f)
continue
}
}
}
}
}
2015-03-22 18:50:06 +00:00
type mediaPath struct {
Path string
Source string
}
func findPaths() []mediaPath {
paths := make([]mediaPath, 0)
for _, path := range findAttributeFiles() {
attributes, err := os.Open(path)
if err != nil {
continue
2015-03-22 18:50:06 +00:00
}
scanner := bufio.NewScanner(attributes)
2015-03-22 18:50:06 +00:00
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, "filter=lfs") {
fields := strings.Fields(line)
relfile, _ := filepath.Rel(config.LocalWorkingDir, path)
pattern := fields[0]
if reldir := filepath.Dir(relfile); len(reldir) > 0 {
pattern = filepath.Join(reldir, pattern)
}
paths = append(paths, mediaPath{Path: pattern, Source: relfile})
2015-03-22 18:50:06 +00:00
}
}
}
return paths
}
2015-05-31 21:31:23 +00:00
func findAttributeFiles() []string {
paths := make([]string, 0)
repoAttributes := filepath.Join(config.LocalGitDir, "info", "attributes")
if info, err := os.Stat(repoAttributes); err == nil && !info.IsDir() {
2015-05-31 21:31:23 +00:00
paths = append(paths, repoAttributes)
}
filepath.Walk(config.LocalWorkingDir, func(path string, info os.FileInfo, err error) error {
2015-05-31 21:31:23 +00:00
if err != nil {
return err
}
if !info.IsDir() && (filepath.Base(path) == ".gitattributes") {
paths = append(paths, path)
}
return nil
})
return paths
}
2015-03-22 18:50:06 +00:00
func needsTrailingLinebreak(filename string) bool {
file, err := os.Open(filename)
if err != nil {
return false
}
defer file.Close()
2015-03-22 18:50:06 +00:00
buf := make([]byte, 16384)
bytesRead := 0
for {
n, err := file.Read(buf)
if err == io.EOF {
break
} else if err != nil {
return false
}
bytesRead = n
}
return !strings.HasSuffix(string(buf[0:bytesRead]), "\n")
}
2016-07-05 22:05:58 +00:00
// blocklistItem returns the name of the blocklist item preventing the given
// file-name from being tracked, or an empty string, if there is none.
2016-07-05 22:05:58 +00:00
func blocklistItem(name string) string {
base := filepath.Base(name)
2016-07-05 22:05:58 +00:00
for _, p := range prefixBlocklist {
if strings.HasPrefix(base, p) {
return p
}
}
return ""
}
2015-03-22 18:50:06 +00:00
func init() {
trackCmd.Flags().BoolVarP(&trackVerboseLoggingFlag, "verbose", "v", false, "log which files are being tracked and modified")
trackCmd.Flags().BoolVarP(&trackDryRunFlag, "dry-run", "d", false, "preview results of running `git lfs track`")
2015-03-22 18:50:06 +00:00
RootCmd.AddCommand(trackCmd)
}