github.com/BenLubar/git-last-modified@v0.1.1-0.20210215221858-9b8031919630/main.go (about)

     1  // Command git-last-modified sets files in a Git repository to their last-modified date.
     2  package main
     3  
     4  import (
     5  	"flag"
     6  	"fmt"
     7  	"os"
     8  	"path/filepath"
     9  	"runtime"
    10  )
    11  
    12  var flagSet = flag.NewFlagSet("git-last-modified", flag.ContinueOnError)
    13  var flagQuiet = flagSet.Bool("q", false, "Quiet. Don't warn about files specified on the command line that are not in Git.")
    14  var flagVerbose = flagSet.Bool("v", false, "Verbose. Print each filename and modification time as they are processed.")
    15  var flagDryRun = flagSet.Bool("n", false, "Dry run. Implies -v. Don't modify any file modification times.")
    16  var flagCommit = flagSet.Bool("commit-date", false, "Use the commit date for the last commit this file was involved in instead of the author date.")
    17  
    18  var exit = os.Exit
    19  
    20  func usage() {
    21  	_, _ = fmt.Fprintln(os.Stderr, "usage: git last-modified [<options>] [[--] <path>...]")
    22  	flagSet.PrintDefaults()
    23  	exit(2)
    24  }
    25  
    26  func checkError(err error) {
    27  	if err != nil {
    28  		if *flagVerbose {
    29  			_, _ = fmt.Fprintf(os.Stderr, "git-last-modified: %+v\n", err)
    30  		} else {
    31  			_, _ = fmt.Fprintf(os.Stderr, "git-last-modified: %v\n", err)
    32  		}
    33  		exit(1)
    34  	}
    35  }
    36  
    37  func main() {
    38  	flagSet.Usage = usage
    39  	if err := flagSet.Parse(os.Args[1:]); err != nil {
    40  		usage()
    41  	}
    42  	if *flagDryRun {
    43  		*flagVerbose = true
    44  	}
    45  
    46  	files := flag.Args()
    47  	if len(files) == 0 {
    48  		*flagQuiet = true
    49  
    50  		var w walker
    51  		w.start(runtime.GOMAXPROCS(0))
    52  		checkError(filepath.Walk(".", w.callback))
    53  		w.finish()
    54  	} else {
    55  		for _, path := range files {
    56  			checkError(setModTime(path))
    57  		}
    58  	}
    59  }