Skip to content

Commit

Permalink
Merge pull request #246 from mergestat/blame-cmd
Browse files Browse the repository at this point in the history
feat: implement `blame` subcommand to show summary of blame info
  • Loading branch information
patrickdevivo committed Feb 2, 2022
2 parents 4187d12 + 59caeb1 commit e29a72d
Show file tree
Hide file tree
Showing 6 changed files with 563 additions and 39 deletions.
52 changes: 52 additions & 0 deletions cmd/blame.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package cmd

import (
"fmt"
"os"

tea "github.com/charmbracelet/bubbletea"
"github.com/mergestat/mergestat/cmd/blame"
"github.com/spf13/cobra"
)

var (
blameOutputJSON bool
)

func init() {
blameCmd.Flags().BoolVar(&blameOutputJSON, "json", false, "output as JSON")
}

var blameCmd = &cobra.Command{
Use: "blame [file pattern]",
Short: "Print a summary of the blame context for a file",
Long: `Prints a summary of the blame context for all files matching the supplied path pattern in the default repo (--repo or current directory).
Specify a file path pattern as the first argument to see aggregate blame data for all files that match the pattern.
Use '%' to match all file paths or as a wildcard (e.g. '%.go' for all .go files). You may specify a full file path (no wildcard) as well.
`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
pathPattern := args[0]

var ui *blame.TermUI
var err error
if ui, err = blame.NewTermUI(pathPattern); err != nil {
handleExitError(err)
}
defer ui.Close()

if blameOutputJSON {
fmt.Println(ui.PrintJSON())
return
}

// check if output is a terminal (https://rosettacode.org/wiki/Check_output_device_is_a_terminal#Go)
if fileInfo, _ := os.Stdout.Stat(); (fileInfo.Mode() & os.ModeCharDevice) != 0 {
if err := tea.NewProgram(ui).Start(); err != nil {
handleExitError(err)
}
} else {
fmt.Print(ui.PrintNoTTY())
}
},
}

0 comments on commit e29a72d

Please sign in to comment.