git.go 4.2 KB
Newer Older
P
Phodal Huang 已提交
1 2 3
package cmd

import (
P
Phodal Huang 已提交
4
	"encoding/json"
P
Phodal Huang 已提交
5 6
	"fmt"
	"github.com/olekukonko/tablewriter"
P
Phodal Huang 已提交
7
	"github.com/phodal/coca/cmd/cmd_util"
P
Phodal Huang 已提交
8
	. "github.com/phodal/coca/core/context/git"
P
Phodal Huang 已提交
9 10
	"github.com/spf13/cobra"
	"io/ioutil"
P
Phodal Huang 已提交
11
	"log"
P
Phodal Huang 已提交
12
	"os"
P
Phodal Huang 已提交
13
	"os/exec"
P
Phodal Huang 已提交
14
	"strconv"
P
Phodal Huang 已提交
15
	"time"
P
Phodal Huang 已提交
16 17
)

18
type GitCmdConfig struct {
P
Phodal Huang 已提交
19 20
	Size        int
	ShowSummary bool
21 22
}

P
Phodal Huang 已提交
23 24
var (
	relatedConfig string
25
	gitCmdConfig  GitCmdConfig
P
Phodal Huang 已提交
26 27
)

P
Phodal Huang 已提交
28
var gitCmd = &cobra.Command{
29
	Use:   "git",
P
Phodal Huang 已提交
30
	Short: "analysis git commit history for revs count, summary and suggest",
P
Phodal Huang 已提交
31 32
	Long:  ``,
	Run: func(cmd *cobra.Command, args []string) {
P
Phodal Huang 已提交
33 34
		message := getCommitMessage()
		commitMessages := BuildMessageByInput(message)
P
Phodal Huang 已提交
35
		cModel, _ := json.MarshalIndent(commitMessages, "", "\t")
P
Phodal Huang 已提交
36
		cmd_util.WriteToCocaFile("commits.json", string(cModel))
P
Phodal Huang 已提交
37

P
Phodal Huang 已提交
38 39 40
		if *&gitCmdConfig.ShowSummary {
			ShowChangeLogSummary(commitMessages)
		}
P
Phodal Huang 已提交
41

P
Phodal Huang 已提交
42 43
		isFullMessage := cmd.Flag("full").Value.String() == "true"
		size := *&gitCmdConfig.Size
P
Phodal Huang 已提交
44 45 46 47

		if cmd.Flag("basic").Value.String() == "true" {
			table := tablewriter.NewWriter(os.Stdout)

P
Phodal Huang 已提交
48
			basicSummary := BasicSummary(commitMessages)
P
Phodal Huang 已提交
49 50 51 52 53 54 55 56 57 58 59
			table.SetHeader([]string{"Statistic", "Number"})
			table.Append([]string{"Commits", strconv.Itoa(basicSummary.Commits)})
			table.Append([]string{"Entities", strconv.Itoa(basicSummary.Entities)})
			table.Append([]string{"Changes", strconv.Itoa(basicSummary.Changes)})
			table.Append([]string{"Authors", strconv.Itoa(basicSummary.Authors)})
			table.Render()
		}

		if cmd.Flag("team").Value.String() == "true" {
			table := tablewriter.NewWriter(os.Stdout)

P
Phodal Huang 已提交
60
			teamSummary := GetTeamSummary(commitMessages)
P
Phodal Huang 已提交
61 62
			table.SetHeader([]string{"EntityName", "RevsCount", "AuthorCount"})

63 64
			if len(teamSummary) > size && isFullMessage {
				teamSummary = teamSummary[:size]
P
Phodal Huang 已提交
65 66 67 68
			}
			for _, v := range teamSummary {
				table.Append([]string{v.EntityName, strconv.Itoa(v.RevsCount), strconv.Itoa(v.AuthorCount)})
			}
69
			table.Render()
P
Phodal Huang 已提交
70 71 72 73 74
		}

		if cmd.Flag("age").Value.String() == "true" {
			table := tablewriter.NewWriter(os.Stdout)

P
Phodal Huang 已提交
75
			ages := CalculateCodeAge(commitMessages)
P
Phodal Huang 已提交
76 77 78 79 80 81 82 83 84
			var agesDisplay []CodeAgeDisplay
			for _, info := range ages {
				const secondsOfOneMonth = 2600640
				month := time.Since(info.Age).Seconds() / secondsOfOneMonth
				displayMonth := strconv.FormatFloat(month, 'f', 2, 64)
				agesDisplay = append(agesDisplay, *&CodeAgeDisplay{info.EntityName, displayMonth})
			}

			table.SetHeader([]string{"EntityName", "Month"})
P
Phodal Huang 已提交
85

P
Phodal Huang 已提交
86 87
			if len(agesDisplay) > size && isFullMessage {
				agesDisplay = agesDisplay[:size]
P
Phodal Huang 已提交
88
			}
P
Phodal Huang 已提交
89 90
			for _, v := range agesDisplay {
				table.Append([]string{v.EntityName, v.Month})
P
Phodal Huang 已提交
91 92 93 94 95 96 97
			}
			table.Render()
		}

		if cmd.Flag("top").Value.String() == "true" {
			table := tablewriter.NewWriter(os.Stdout)

P
Phodal Huang 已提交
98
			authors := GetTopAuthors(commitMessages)
P
Phodal Huang 已提交
99 100
			table.SetHeader([]string{"Author", "CommitCount", "LineCount"})

101 102
			if len(authors) > size && isFullMessage {
				authors = authors[:size]
P
Phodal Huang 已提交
103 104 105 106 107 108 109 110 111 112
			}
			for _, v := range authors {
				table.Append([]string{v.Name, strconv.Itoa(v.CommitCount), strconv.Itoa(v.LineCount)})
			}
			table.Render()
		}

		if relatedConfig != "" {
			config, err := ioutil.ReadFile(relatedConfig)
			if err != nil {
P
Phodal Huang 已提交
113
				_ = fmt.Errorf("lost related json %s", err)
P
Phodal Huang 已提交
114 115 116
				return
			}

P
Phodal Huang 已提交
117
			GetRelatedFiles(commitMessages, config)
P
Phodal Huang 已提交
118 119 120 121
		}
	},
}

P
Phodal Huang 已提交
122
func getCommitMessage() string {
P
Phodal Huang 已提交
123
	historyArgs := []string{"log", "--pretty=\"format:[%h] %aN %ad %s\"", "--date=short", "--numstat", "--reverse", "--summary"}
P
Phodal Huang 已提交
124 125 126
	cmd := exec.Command("git", historyArgs...)
	out, err := cmd.CombinedOutput()
	if err != nil {
P
Phodal Huang 已提交
127
		fmt.Println(string(out))
P
Phodal Huang 已提交
128 129 130 131 132 133
		log.Fatalf("cmd.Run() failed with %s\n", err)
	}

	return string(out)
}

P
Phodal Huang 已提交
134 135 136 137 138 139 140 141
func init() {
	rootCmd.AddCommand(gitCmd)

	gitCmd.PersistentFlags().BoolP("basic", "b", false, "Basic Summary")
	gitCmd.PersistentFlags().BoolP("team", "t", false, "Team Summary")
	gitCmd.PersistentFlags().BoolP("age", "a", false, "Code Age")
	gitCmd.PersistentFlags().BoolP("top", "o", false, "Top Authors")
	gitCmd.PersistentFlags().BoolP("full", "f", false, "full")
142
	gitCmd.PersistentFlags().BoolVarP(&gitCmdConfig.ShowSummary, "summary", "m", false, "full")
143
	gitCmd.PersistentFlags().IntVarP(&gitCmdConfig.Size, "size", "s", 20, "full")
P
Phodal Huang 已提交
144
	gitCmd.PersistentFlags().StringVarP(&relatedConfig, "related", "r", "", "related")
P
Phodal Huang 已提交
145
}