github.com/driusan/dgit@v0.0.0-20221118233547-f39f0c15edbb/cmd/clean.go (about)

     1  package cmd
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"github.com/driusan/dgit/git"
     7  )
     8  
     9  func newFlagSet(name string) *flag.FlagSet {
    10  	flags := flag.NewFlagSet(name, flag.ExitOnError)
    11  	flags.SetOutput(flag.CommandLine.Output())
    12  	flags.Usage = func() {
    13  		flag.Usage()
    14  		fmt.Fprintf(flag.CommandLine.Output(), "\n\nOptions: \n")
    15  		flags.PrintDefaults()
    16  	}
    17  	return flags
    18  }
    19  
    20  func Clean(c *git.Client, args []string) error {
    21  	flags := newFlagSet("clean")
    22  	opts := git.CleanOptions{}
    23  	flags.BoolVar(&opts.Directory, "d", false, "Remove untracked directories in addition to files")
    24  	flags.BoolVar(&opts.Force, "force", false, "Do deletion even if clean.requireForce is not false")
    25  	flags.BoolVar(&opts.Force, "f", false, "Alias of --force")
    26  	flags.BoolVar(&opts.Interactive, "i", false, "Not implemented")
    27  	flags.BoolVar(&opts.Interactive, "interactive", false, "Not implemented")
    28  	flags.BoolVar(&opts.DryRun, "dry-run", false, "Do not do deletion, just show what would be done")
    29  	flags.BoolVar(&opts.DryRun, "n", false, "Alias of --dry-run")
    30  	flags.BoolVar(&opts.Quiet, "quiet", false, "Do not print file names as they are deleted")
    31  	flags.BoolVar(&opts.Quiet, "q", false, "Alias of --quiet")
    32  
    33  	flags.Var(NewMultiStringValue(&opts.ExcludePatterns), "exclude", "Add pattern to standard exclude patterns")
    34  	flags.Var(NewMultiStringValue(&opts.ExcludePatterns), "e", "Alias of --exclude")
    35  	flags.BoolVar(&opts.NoStandardExclude, "x", false, "Do not use standard .gitignore and .git/info/exclude patterns")
    36  	flags.BoolVar(&opts.OnlyExcluded, "X", false, "Only remove files ignored by git")
    37  
    38  	flags.Parse(args)
    39  
    40  	config, err := git.LoadLocalConfig(c)
    41  	if err != nil {
    42  		return err
    43  	}
    44  	requireforce, _ := config.GetConfig("clean.requireforce")
    45  	if requireforce != "false" && !(opts.DryRun || opts.Force) {
    46  		return fmt.Errorf("clean.requireforce defaults to true and -f or -n not set")
    47  	}
    48  	paths := flags.Args()
    49  	var files []git.File
    50  	for _, p := range paths {
    51  		files = append(files, git.File(p))
    52  	}
    53  	return git.Clean(c, opts, files)
    54  }