root.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. package main
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "log"
  8. "net/http"
  9. _ "net/http/pprof"
  10. "os"
  11. "path/filepath"
  12. "plugin"
  13. "regexp"
  14. "runtime/pprof"
  15. "strings"
  16. "text/template"
  17. "unicode"
  18. "github.com/Masterminds/sprig"
  19. "github.com/gogo/protobuf/proto"
  20. "github.com/mitchellh/go-homedir"
  21. "github.com/spf13/cobra"
  22. "github.com/spf13/pflag"
  23. "golang.org/x/crypto/ssh/terminal"
  24. progress "gopkg.in/cheggaaa/pb.v1"
  25. "gopkg.in/src-d/go-billy-siva.v4"
  26. "gopkg.in/src-d/go-billy.v4/memfs"
  27. "gopkg.in/src-d/go-billy.v4/osfs"
  28. "gopkg.in/src-d/go-git.v4"
  29. "gopkg.in/src-d/go-git.v4/plumbing/cache"
  30. "gopkg.in/src-d/go-git.v4/plumbing/object"
  31. "gopkg.in/src-d/go-git.v4/plumbing/transport/ssh"
  32. "gopkg.in/src-d/go-git.v4/storage"
  33. "gopkg.in/src-d/go-git.v4/storage/filesystem"
  34. "gopkg.in/src-d/go-git.v4/storage/memory"
  35. "gopkg.in/src-d/hercules.v9"
  36. "gopkg.in/src-d/hercules.v9/internal/pb"
  37. )
  38. // oneLineWriter splits the output data by lines and outputs one on top of another using '\r'.
  39. // It also does some dark magic to handle Git statuses.
  40. type oneLineWriter struct {
  41. Writer io.Writer
  42. }
  43. func (writer oneLineWriter) Write(p []byte) (n int, err error) {
  44. strp := strings.TrimSpace(string(p))
  45. if strings.HasSuffix(strp, "done.") || len(strp) == 0 {
  46. strp = "cloning..."
  47. } else {
  48. strp = strings.Replace(strp, "\n", "\033[2K\r", -1)
  49. }
  50. _, err = writer.Writer.Write([]byte("\033[2K\r"))
  51. if err != nil {
  52. return
  53. }
  54. n, err = writer.Writer.Write([]byte(strp))
  55. return
  56. }
  57. func loadSSHIdentity(sshIdentity string) (*ssh.PublicKeys, error) {
  58. actual, err := homedir.Expand(sshIdentity)
  59. if err != nil {
  60. return nil, err
  61. }
  62. return ssh.NewPublicKeysFromFile("git", actual, "")
  63. }
  64. func loadRepository(uri string, cachePath string, disableStatus bool, sshIdentity string) *git.Repository {
  65. var repository *git.Repository
  66. var backend storage.Storer
  67. var err error
  68. if strings.Contains(uri, "://") || regexp.MustCompile("^[A-Za-z]\\w*@[A-Za-z0-9][\\w.]*:").MatchString(uri) {
  69. if cachePath != "" {
  70. backend = filesystem.NewStorage(osfs.New(cachePath), cache.NewObjectLRUDefault())
  71. _, err = os.Stat(cachePath)
  72. if !os.IsNotExist(err) {
  73. log.Printf("warning: deleted %s\n", cachePath)
  74. os.RemoveAll(cachePath)
  75. }
  76. } else {
  77. backend = memory.NewStorage()
  78. }
  79. cloneOptions := &git.CloneOptions{URL: uri}
  80. if !disableStatus {
  81. fmt.Fprint(os.Stderr, "connecting...\r")
  82. cloneOptions.Progress = oneLineWriter{Writer: os.Stderr}
  83. }
  84. if sshIdentity != "" {
  85. auth, err := loadSSHIdentity(sshIdentity)
  86. if err != nil {
  87. log.Printf("Failed loading SSH Identity %s\n", err)
  88. }
  89. cloneOptions.Auth = auth
  90. }
  91. repository, err = git.Clone(backend, nil, cloneOptions)
  92. if !disableStatus {
  93. fmt.Fprint(os.Stderr, "\033[2K\r")
  94. }
  95. } else if stat, err2 := os.Stat(uri); err2 == nil && !stat.IsDir() {
  96. localFs := osfs.New(filepath.Dir(uri))
  97. tmpFs := memfs.New()
  98. basePath := filepath.Base(uri)
  99. fs, err2 := sivafs.NewFilesystem(localFs, basePath, tmpFs)
  100. if err2 != nil {
  101. log.Panicf("unable to create a siva filesystem from %s: %v", uri, err2)
  102. }
  103. sivaStorage := filesystem.NewStorage(fs, cache.NewObjectLRUDefault())
  104. repository, err = git.Open(sivaStorage, tmpFs)
  105. } else {
  106. if uri[len(uri)-1] == os.PathSeparator {
  107. uri = uri[:len(uri)-1]
  108. }
  109. repository, err = git.PlainOpen(uri)
  110. }
  111. if err != nil {
  112. log.Panicf("failed to open %s: %v", uri, err)
  113. }
  114. return repository
  115. }
  116. type arrayPluginFlags map[string]bool
  117. func (apf *arrayPluginFlags) String() string {
  118. var list []string
  119. for key := range *apf {
  120. list = append(list, key)
  121. }
  122. return strings.Join(list, ", ")
  123. }
  124. func (apf *arrayPluginFlags) Set(value string) error {
  125. (*apf)[value] = true
  126. return nil
  127. }
  128. func (apf *arrayPluginFlags) Type() string {
  129. return "path"
  130. }
  131. func loadPlugins() {
  132. pluginFlags := arrayPluginFlags{}
  133. fs := pflag.NewFlagSet(os.Args[0], pflag.ContinueOnError)
  134. fs.SetOutput(ioutil.Discard)
  135. pluginFlagName := "plugin"
  136. const pluginDesc = "Load the specified plugin by the full or relative path. " +
  137. "Can be specified multiple times."
  138. fs.Var(&pluginFlags, pluginFlagName, pluginDesc)
  139. err := cobra.MarkFlagFilename(fs, "plugin")
  140. if err != nil {
  141. panic(err)
  142. }
  143. pflag.Var(&pluginFlags, pluginFlagName, pluginDesc)
  144. fs.Parse(os.Args[1:])
  145. for path := range pluginFlags {
  146. _, err := plugin.Open(path)
  147. if err != nil {
  148. log.Printf("Failed to load plugin from %s %s\n", path, err)
  149. }
  150. }
  151. }
  152. // rootCmd represents the base command when called without any subcommands
  153. var rootCmd = &cobra.Command{
  154. Use: "hercules",
  155. Short: "Analyse a Git repository.",
  156. Long: `Hercules is a flexible and fast Git repository analysis engine. The base command executes
  157. the commit processing pipeline which is automatically generated from the dependencies of one
  158. or several analysis targets. The list of the available targets is printed in --help. External
  159. targets can be added using the --plugin system.`,
  160. Args: cobra.RangeArgs(1, 2),
  161. Run: func(cmd *cobra.Command, args []string) {
  162. flags := cmd.Flags()
  163. getBool := func(name string) bool {
  164. value, err := flags.GetBool(name)
  165. if err != nil {
  166. panic(err)
  167. }
  168. return value
  169. }
  170. getString := func(name string) string {
  171. value, err := flags.GetString(name)
  172. if err != nil {
  173. panic(err)
  174. }
  175. return value
  176. }
  177. firstParent := getBool("first-parent")
  178. commitsFile := getString("commits")
  179. protobuf := getBool("pb")
  180. profile := getBool("profile")
  181. disableStatus := getBool("quiet")
  182. sshIdentity := getString("ssh-identity")
  183. if profile {
  184. go func() {
  185. err := http.ListenAndServe("localhost:6060", nil)
  186. if err != nil {
  187. panic(err)
  188. }
  189. }()
  190. prof, _ := os.Create("hercules.pprof")
  191. err := pprof.StartCPUProfile(prof)
  192. if err != nil {
  193. panic(err)
  194. }
  195. defer pprof.StopCPUProfile()
  196. }
  197. uri := args[0]
  198. cachePath := ""
  199. if len(args) == 2 {
  200. cachePath = args[1]
  201. }
  202. repository := loadRepository(uri, cachePath, disableStatus, sshIdentity)
  203. // core logic
  204. pipeline := hercules.NewPipeline(repository)
  205. pipeline.SetFeaturesFromFlags()
  206. var bar *progress.ProgressBar
  207. if !disableStatus {
  208. pipeline.OnProgress = func(commit, length int, action string) {
  209. if bar == nil {
  210. bar = progress.New(length)
  211. bar.Callback = func(msg string) {
  212. os.Stderr.WriteString("\033[2K\r" + msg)
  213. }
  214. bar.NotPrint = true
  215. bar.ShowPercent = false
  216. bar.ShowSpeed = false
  217. bar.SetMaxWidth(80).Start()
  218. }
  219. if action == hercules.MessageFinalize {
  220. bar.Finish()
  221. fmt.Fprint(os.Stderr, "\033[2K\rfinalizing...")
  222. } else {
  223. bar.Set(commit).Postfix(" [" + action + "] ")
  224. }
  225. }
  226. }
  227. var commits []*object.Commit
  228. var err error
  229. if commitsFile == "" {
  230. fmt.Fprint(os.Stderr, "git log...\r")
  231. commits, err = pipeline.Commits(firstParent)
  232. } else {
  233. commits, err = hercules.LoadCommitsFromFile(commitsFile, repository)
  234. }
  235. if err != nil {
  236. log.Fatalf("failed to list the commits: %v", err)
  237. }
  238. cmdlineFacts[hercules.ConfigPipelineCommits] = commits
  239. dryRun, _ := cmdlineFacts[hercules.ConfigPipelineDryRun].(bool)
  240. var deployed []hercules.LeafPipelineItem
  241. for name, valPtr := range cmdlineDeployed {
  242. if *valPtr {
  243. item := pipeline.DeployItem(hercules.Registry.Summon(name)[0])
  244. if !dryRun {
  245. deployed = append(deployed, item.(hercules.LeafPipelineItem))
  246. }
  247. }
  248. }
  249. err = pipeline.Initialize(cmdlineFacts)
  250. if err != nil {
  251. log.Fatal(err)
  252. }
  253. results, err := pipeline.Run(commits)
  254. if err != nil {
  255. log.Fatalf("failed to run the pipeline: %v", err)
  256. }
  257. if !disableStatus {
  258. fmt.Fprint(os.Stderr, "\033[2K\r")
  259. // if not a terminal, the user will not see the output, so show the status
  260. if !terminal.IsTerminal(int(os.Stdout.Fd())) {
  261. fmt.Fprint(os.Stderr, "writing...\r")
  262. }
  263. }
  264. if !protobuf {
  265. printResults(uri, deployed, results)
  266. } else {
  267. protobufResults(uri, deployed, results)
  268. }
  269. },
  270. }
  271. func printResults(
  272. uri string, deployed []hercules.LeafPipelineItem,
  273. results map[hercules.LeafPipelineItem]interface{}) {
  274. commonResult := results[nil].(*hercules.CommonAnalysisResult)
  275. fmt.Println("hercules:")
  276. fmt.Printf(" version: %d\n", hercules.BinaryVersion)
  277. fmt.Println(" hash:", hercules.BinaryGitHash)
  278. fmt.Println(" repository:", uri)
  279. fmt.Println(" begin_unix_time:", commonResult.BeginTime)
  280. fmt.Println(" end_unix_time:", commonResult.EndTime)
  281. fmt.Println(" commits:", commonResult.CommitsNumber)
  282. fmt.Println(" run_time:", commonResult.RunTime.Nanoseconds()/1e6)
  283. for _, item := range deployed {
  284. result := results[item]
  285. fmt.Printf("%s:\n", item.Name())
  286. if err := item.Serialize(result, false, os.Stdout); err != nil {
  287. panic(err)
  288. }
  289. }
  290. }
  291. func protobufResults(
  292. uri string, deployed []hercules.LeafPipelineItem,
  293. results map[hercules.LeafPipelineItem]interface{}) {
  294. header := pb.Metadata{
  295. Version: 2,
  296. Hash: hercules.BinaryGitHash,
  297. Repository: uri,
  298. }
  299. results[nil].(*hercules.CommonAnalysisResult).FillMetadata(&header)
  300. message := pb.AnalysisResults{
  301. Header: &header,
  302. Contents: map[string][]byte{},
  303. }
  304. for _, item := range deployed {
  305. result := results[item]
  306. buffer := &bytes.Buffer{}
  307. if err := item.Serialize(result, true, buffer); err != nil {
  308. panic(err)
  309. }
  310. message.Contents[item.Name()] = buffer.Bytes()
  311. }
  312. serialized, err := proto.Marshal(&message)
  313. if err != nil {
  314. panic(err)
  315. }
  316. os.Stdout.Write(serialized)
  317. }
  318. // trimRightSpace removes the trailing whitespace characters.
  319. func trimRightSpace(s string) string {
  320. return strings.TrimRightFunc(s, unicode.IsSpace)
  321. }
  322. // rpad adds padding to the right of a string.
  323. func rpad(s string, padding int) string {
  324. return fmt.Sprintf(fmt.Sprintf("%%-%ds", padding), s)
  325. }
  326. // tmpl was adapted from cobra/cobra.go
  327. func tmpl(w io.Writer, text string, data interface{}) error {
  328. var templateFuncs = template.FuncMap{
  329. "trim": strings.TrimSpace,
  330. "trimRightSpace": trimRightSpace,
  331. "trimTrailingWhitespaces": trimRightSpace,
  332. "rpad": rpad,
  333. "gt": cobra.Gt,
  334. "eq": cobra.Eq,
  335. }
  336. for k, v := range sprig.TxtFuncMap() {
  337. templateFuncs[k] = v
  338. }
  339. t := template.New("top")
  340. t.Funcs(templateFuncs)
  341. template.Must(t.Parse(text))
  342. return t.Execute(w, data)
  343. }
  344. func formatUsage(c *cobra.Command) error {
  345. // the default UsageFunc() does some private magic c.mergePersistentFlags()
  346. // this should stay on top
  347. localFlags := c.LocalFlags()
  348. leaves := hercules.Registry.GetLeaves()
  349. plumbing := hercules.Registry.GetPlumbingItems()
  350. features := hercules.Registry.GetFeaturedItems()
  351. hercules.EnablePathFlagTypeMasquerade()
  352. filter := map[string]bool{}
  353. for _, l := range leaves {
  354. filter[l.Flag()] = true
  355. for _, cfg := range l.ListConfigurationOptions() {
  356. filter[cfg.Flag] = true
  357. }
  358. }
  359. for _, i := range plumbing {
  360. for _, cfg := range i.ListConfigurationOptions() {
  361. filter[cfg.Flag] = true
  362. }
  363. }
  364. for key := range filter {
  365. localFlags.Lookup(key).Hidden = true
  366. }
  367. args := map[string]interface{}{
  368. "c": c,
  369. "leaves": leaves,
  370. "plumbing": plumbing,
  371. "features": features,
  372. }
  373. helpTemplate := `Usage:{{if .c.Runnable}}
  374. {{.c.UseLine}}{{end}}{{if .c.HasAvailableSubCommands}}
  375. {{.c.CommandPath}} [command]{{end}}{{if gt (len .c.Aliases) 0}}
  376. Aliases:
  377. {{.c.NameAndAliases}}{{end}}{{if .c.HasExample}}
  378. Examples:
  379. {{.c.Example}}{{end}}{{if .c.HasAvailableSubCommands}}
  380. Available Commands:{{range .c.Commands}}{{if (or .IsAvailableCommand (eq .Name "help"))}}
  381. {{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .c.HasAvailableLocalFlags}}
  382. Flags:
  383. {{range $line := .c.LocalFlags.FlagUsages | trimTrailingWhitespaces | split "\n"}}
  384. {{- $desc := splitList " " $line | last}}
  385. {{- $offset := sub ($desc | len) ($desc | trim | len)}}
  386. {{- $indent := splitList " " $line | initial | join " " | len | add 3 | add $offset | int}}
  387. {{- $wrap := sub 120 $indent | int}}
  388. {{- splitList " " $line | initial | join " "}} {{cat "!" $desc | wrap $wrap | indent $indent | substr $indent -1 | substr 2 -1}}
  389. {{end}}{{end}}
  390. Analysis Targets:{{range .leaves}}
  391. --{{rpad .Flag 40}}Runs {{.Name}} analysis.{{wrap 72 .Description | nindent 48}}{{range .ListConfigurationOptions}}
  392. --{{if .Type.String}}{{rpad (print .Flag " " .Type.String) 40}}{{else}}{{rpad .Flag 40}}{{end}}
  393. {{- $desc := dict "desc" .Description}}
  394. {{- if .Default}}{{$_ := set $desc "desc" (print .Description " The default value is " .FormatDefault ".")}}
  395. {{- end}}
  396. {{- $desc := pluck "desc" $desc | first}}
  397. {{- $desc | wrap 68 | indent 52 | substr 52 -1}}{{end}}
  398. {{end}}
  399. Plumbing Options:{{range .plumbing}}{{$name := .Name}}{{range .ListConfigurationOptions}}
  400. --{{if .Type.String}}{{rpad (print .Flag " " .Type.String " [" $name "]") 40}}{{else}}{{rpad (print .Flag " [" $name "]") 40}}
  401. {{- end}}
  402. {{- $desc := dict "desc" .Description}}
  403. {{- if .Default}}{{$_ := set $desc "desc" (print .Description " The default value is " .FormatDefault ".")}}
  404. {{- end}}
  405. {{- $desc := pluck "desc" $desc | first}}{{$desc | wrap 72 | indent 48 | substr 48 -1}}{{end}}{{end}}
  406. --feature:{{range $key, $value := .features}}
  407. {{rpad $key 42}}Enables {{range $index, $item := $value}}{{if $index}}, {{end}}{{$item.Name}}{{end}}.{{end}}{{if .c.HasAvailableInheritedFlags}}
  408. Global Flags:
  409. {{.c.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .c.HasHelpSubCommands}}
  410. Additional help topics:{{range .c.Commands}}{{if .IsAdditionalHelpTopicCommand}}
  411. {{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .c.HasAvailableSubCommands}}
  412. Use "{{.c.CommandPath}} [command] --help" for more information about a command.{{end}}
  413. `
  414. err := tmpl(c.OutOrStderr(), helpTemplate, args)
  415. for key := range filter {
  416. localFlags.Lookup(key).Hidden = false
  417. }
  418. if err != nil {
  419. c.Println(err)
  420. }
  421. return err
  422. }
  423. // versionCmd prints the API version and the Git commit hash
  424. var versionCmd = &cobra.Command{
  425. Use: "version",
  426. Short: "Print version information and exit.",
  427. Long: ``,
  428. Args: cobra.MaximumNArgs(0),
  429. Run: func(cmd *cobra.Command, args []string) {
  430. fmt.Printf("Version: %d\nGit: %s\n", hercules.BinaryVersion, hercules.BinaryGitHash)
  431. },
  432. }
  433. var cmdlineFacts map[string]interface{}
  434. var cmdlineDeployed map[string]*bool
  435. func init() {
  436. loadPlugins()
  437. rootFlags := rootCmd.Flags()
  438. rootFlags.String("commits", "", "Path to the text file with the "+
  439. "commit history to follow instead of the default 'git log'. "+
  440. "The format is the list of hashes, each hash on a "+
  441. "separate line. The first hash is the root.")
  442. err := rootCmd.MarkFlagFilename("commits")
  443. if err != nil {
  444. panic(err)
  445. }
  446. hercules.PathifyFlagValue(rootFlags.Lookup("commits"))
  447. rootFlags.Bool("first-parent", false, "Follow only the first parent in the commit history - "+
  448. "\"git log --first-parent\".")
  449. rootFlags.Bool("pb", false, "The output format will be Protocol Buffers instead of YAML.")
  450. rootFlags.Bool("quiet", !terminal.IsTerminal(int(os.Stdin.Fd())),
  451. "Do not print status updates to stderr.")
  452. rootFlags.Bool("profile", false, "Collect the profile to hercules.pprof.")
  453. rootFlags.String("ssh-identity", "", "Path to SSH identity file (e.g., ~/.ssh/id_rsa) to clone from an SSH remote.")
  454. err = rootCmd.MarkFlagFilename("ssh-identity")
  455. if err != nil {
  456. panic(err)
  457. }
  458. hercules.PathifyFlagValue(rootFlags.Lookup("ssh-identity"))
  459. cmdlineFacts, cmdlineDeployed = hercules.Registry.AddFlags(rootFlags)
  460. rootCmd.SetUsageFunc(formatUsage)
  461. rootCmd.AddCommand(versionCmd)
  462. versionCmd.SetUsageFunc(versionCmd.UsageFunc())
  463. }
  464. func main() {
  465. if err := rootCmd.Execute(); err != nil {
  466. fmt.Fprintln(os.Stderr, err)
  467. os.Exit(1)
  468. }
  469. }