root.go 15 KB

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