github.com/q2/git-lfs@v0.5.1-0.20150410234700-03a0d4cec40e/commands/command_untrack.go (about)

     1  package commands
     2  
     3  import (
     4  	"bufio"
     5  	"github.com/github/git-lfs/lfs"
     6  	"github.com/spf13/cobra"
     7  	"io/ioutil"
     8  	"os"
     9  	"strings"
    10  )
    11  
    12  var (
    13  	untrackCmd = &cobra.Command{
    14  		Use:   "untrack",
    15  		Short: "Remove an entry from .gitattributes",
    16  		Run:   untrackCommand,
    17  	}
    18  )
    19  
    20  func untrackCommand(cmd *cobra.Command, args []string) {
    21  	lfs.InstallHooks(false)
    22  
    23  	if len(args) < 1 {
    24  		Print("git lfs untrack <path> [path]*")
    25  		return
    26  	}
    27  
    28  	data, err := ioutil.ReadFile(".gitattributes")
    29  	if err != nil {
    30  		return
    31  	}
    32  
    33  	attributes := strings.NewReader(string(data))
    34  
    35  	attributesFile, err := os.Create(".gitattributes")
    36  	if err != nil {
    37  		Print("Error opening .gitattributes for writing")
    38  		return
    39  	}
    40  
    41  	scanner := bufio.NewScanner(attributes)
    42  	for scanner.Scan() {
    43  		line := scanner.Text()
    44  		if strings.Contains(line, "filter=lfs") {
    45  			fields := strings.Fields(line)
    46  			removeThisPath := false
    47  			for _, t := range args {
    48  				if t == fields[0] {
    49  					removeThisPath = true
    50  				}
    51  			}
    52  
    53  			if !removeThisPath {
    54  				attributesFile.WriteString(line + "\n")
    55  			} else {
    56  				Print("Untracking %s", fields[0])
    57  			}
    58  		}
    59  	}
    60  
    61  	attributesFile.Close()
    62  }
    63  
    64  func init() {
    65  	RootCmd.AddCommand(untrackCmd)
    66  }