github.com/soypat/gitaligned@v0.3.4-0.20221228122414-e435aab44fbc/main.go (about)

     1  package main
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"os"
     7  
     8  	"github.com/spf13/pflag"
     9  )
    10  
    11  const defaultMaxCommits = 1200
    12  const defaultMaxAuthors = 30
    13  
    14  var maxAuthors, maxCommits int
    15  
    16  func run() (err error) {
    17  	var (
    18  		username    string
    19  		why         bool
    20  		showNLPTags bool
    21  		branch      string
    22  		noMerges    bool
    23  		jsonOut     bool
    24  	)
    25  	pflag.StringVarP(&username, "user", "u", "", "git username. recieves `<pattern>`")
    26  	pflag.IntVarP(&maxCommits, "max-commits", "n", defaultMaxCommits, "max amount of commits to process")
    27  	pflag.IntVarP(&maxAuthors, "max-authors", "a", defaultMaxAuthors, "max amount of authors to process")
    28  	pflag.BoolVarP(&why, "why", "y", false, "print alignments and message for each commit")
    29  	pflag.BoolVar(&jsonOut, "json", false, "JSON serialized author alignments")
    30  	pflag.BoolVar(&noMerges, "no-merges", false, "do not process commits with more than one parent")
    31  	pflag.BoolVarP(&showNLPTags, "show-nlp", "k", false, "shows natural language processing tags detected for each commit")
    32  	pflag.StringVarP(&branch, "branch", "b", "", "git branch to scan")
    33  
    34  	pflag.Parse()
    35  
    36  	filename := pflag.Arg(0)
    37  
    38  	options := []gitOption{
    39  		optionNoMerges(noMerges),
    40  		optionAuthorPattern(username),
    41  		optionBranch(branch),
    42  	}
    43  	// if username is specified we know all commits returned by git log will be processed, else undefined
    44  	if username != "" {
    45  		options = append(options, optionMaxCommits(maxCommits))
    46  	}
    47  	var authors []author
    48  	var commits []commit
    49  
    50  	// log file scan branch
    51  	fp, err := os.Open(filename)
    52  	if filename != "" && err == nil {
    53  		defer fp.Close()
    54  		commits, authors, err = GitLogScan(fp)
    55  	} else { // regular control flow branch
    56  		commits, authors, err = ScanCWD(options...)
    57  	}
    58  
    59  	if err != nil {
    60  		return err
    61  	}
    62  	if len(commits) == 0 {
    63  		return errors.New("no commits found. Are you sure username is correct? Run `git log` to see usernames")
    64  	}
    65  
    66  	if why {
    67  		SetCommitAlignments(commits, authors)
    68  		return WriteCommitAlignments(os.Stdout, commits)
    69  	}
    70  	if showNLPTags {
    71  		return WriteNLPTags(os.Stdout, commits)
    72  	}
    73  	SetAuthorAlignments(commits, authors)
    74  	if jsonOut {
    75  		return WriteAuthorAlignmentsJSON(os.Stdout, authors)
    76  	}
    77  	return WriteAuthorAlignments(os.Stdout, authors)
    78  }
    79  
    80  func main() {
    81  	if err := run(); err != nil {
    82  		fmt.Printf("Error in run: %s\n", err)
    83  	}
    84  }