From f4e7b3ba2d0fc56a5aa86eb7b033458508f759d2 Mon Sep 17 00:00:00 2001 From: Taylor Blau Date: Wed, 24 May 2017 16:54:12 -0600 Subject: [PATCH] git/rev_list_scanner: extract RevListOrder.Flag() --- git/rev_list_scanner.go | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/git/rev_list_scanner.go b/git/rev_list_scanner.go index 96d3c96c..7a3c3b39 100644 --- a/git/rev_list_scanner.go +++ b/git/rev_list_scanner.go @@ -3,6 +3,7 @@ package git import ( "bufio" "encoding/hex" + "fmt" "io" "io/ioutil" "os/exec" @@ -48,6 +49,27 @@ const ( TopoRevListOrder ) +// Flag returns the command-line flag to be passed to git-rev-list(1) in order +// to order the output according to the given RevListOrder. It returns both the +// flag ("--date-order", "--topo-order", etc) and a bool, whether or not to +// append the flag (for instance, DefaultRevListOrder requires no flag). +// +// Given a type other than those defined above, Flag() will panic(). +func (o RevListOrder) Flag() (string, bool) { + switch o { + case DefaultRevListOrder: + return "", false + case DateRevListOrder: + return "--date-order", true + case AuthorDateRevListOrder: + return "--author-date-order", true + case TopoRevListOrder: + return "--topo-order", true + default: + panic(fmt.Sprintf("git/rev_list_scanner: unknown RevListOrder %d", o)) + } +} + // ScanRefsOptions is an "options" type that is used to configure a scan // operation on the `*git.RevListScanner` instance when given to the function // `NewRevListScanner()`. @@ -192,13 +214,8 @@ func revListArgs(l, r string, opt *ScanRefsOptions) (io.Reader, []string, error) var stdin io.Reader args := []string{"rev-list", "--objects"} - switch opt.Order { - case DateRevListOrder: - args = append(args, "--date-order") - case AuthorDateRevListOrder: - args = append(args, "--author-date-order") - case TopoRevListOrder: - args = append(args, "--topo-order") + if orderFlag, ok := opt.Order.Flag(); ok { + args = append(args, orderFlag) } switch opt.Mode {