root.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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.v6"
  36. "gopkg.in/src-d/hercules.v6/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. pflag.Var(&pluginFlags, pluginFlagName, pluginDesc)
  138. fs.Parse(os.Args[1:])
  139. for path := range pluginFlags {
  140. _, err := plugin.Open(path)
  141. if err != nil {
  142. log.Printf("Failed to load plugin from %s %s\n", path, err)
  143. }
  144. }
  145. }
  146. // rootCmd represents the base command when called without any subcommands
  147. var rootCmd = &cobra.Command{
  148. Use: "hercules",
  149. Short: "Analyse a Git repository.",
  150. Long: `Hercules is a flexible and fast Git repository analysis engine. The base command executes
  151. the commit processing pipeline which is automatically generated from the dependencies of one
  152. or several analysis targets. The list of the available targets is printed in --help. External
  153. targets can be added using the --plugin system.`,
  154. Args: cobra.RangeArgs(1, 2),
  155. Run: func(cmd *cobra.Command, args []string) {
  156. flags := cmd.Flags()
  157. firstParent, _ := flags.GetBool("first-parent")
  158. commitsFile, _ := flags.GetString("commits")
  159. protobuf, _ := flags.GetBool("pb")
  160. profile, _ := flags.GetBool("profile")
  161. disableStatus, _ := flags.GetBool("quiet")
  162. sshIdentity, _ := flags.GetString("ssh-identity")
  163. if profile {
  164. go http.ListenAndServe("localhost:6060", nil)
  165. prof, _ := os.Create("hercules.pprof")
  166. pprof.StartCPUProfile(prof)
  167. defer pprof.StopCPUProfile()
  168. }
  169. uri := args[0]
  170. cachePath := ""
  171. if len(args) == 2 {
  172. cachePath = args[1]
  173. }
  174. repository := loadRepository(uri, cachePath, disableStatus, sshIdentity)
  175. // core logic
  176. pipeline := hercules.NewPipeline(repository)
  177. pipeline.SetFeaturesFromFlags()
  178. var bar *progress.ProgressBar
  179. if !disableStatus {
  180. pipeline.OnProgress = func(commit, length int) {
  181. if bar == nil {
  182. bar = progress.New(length)
  183. bar.Callback = func(msg string) {
  184. os.Stderr.WriteString("\r" + msg)
  185. }
  186. bar.NotPrint = true
  187. bar.ShowPercent = false
  188. bar.ShowSpeed = false
  189. bar.SetMaxWidth(80)
  190. bar.Start()
  191. }
  192. if commit == length-1 {
  193. bar.Finish()
  194. fmt.Fprint(os.Stderr, "\r"+strings.Repeat(" ", 80)+"\rfinalizing...")
  195. } else {
  196. bar.Set(commit)
  197. }
  198. }
  199. }
  200. var commits []*object.Commit
  201. var err error
  202. if commitsFile == "" {
  203. fmt.Fprint(os.Stderr, "git log...\r")
  204. commits, err = pipeline.Commits(firstParent)
  205. } else {
  206. commits, err = hercules.LoadCommitsFromFile(commitsFile, repository)
  207. }
  208. if err != nil {
  209. log.Fatalf("failed to list the commits: %v", err)
  210. }
  211. cmdlineFacts[hercules.ConfigPipelineCommits] = commits
  212. var deployed []hercules.LeafPipelineItem
  213. for name, valPtr := range cmdlineDeployed {
  214. if *valPtr {
  215. item := pipeline.DeployItem(hercules.Registry.Summon(name)[0])
  216. deployed = append(deployed, item.(hercules.LeafPipelineItem))
  217. }
  218. }
  219. err = pipeline.Initialize(cmdlineFacts)
  220. if err != nil {
  221. log.Fatal(err)
  222. }
  223. if dryRun, _ := cmdlineFacts[hercules.ConfigPipelineDryRun].(bool); dryRun {
  224. return
  225. }
  226. results, err := pipeline.Run(commits)
  227. if err != nil {
  228. log.Fatalf("failed to run the pipeline: %v", err)
  229. }
  230. if !disableStatus {
  231. fmt.Fprint(os.Stderr, "\r"+strings.Repeat(" ", 80)+"\r")
  232. // if not a terminal, the user will not see the output, so show the status
  233. if !terminal.IsTerminal(int(os.Stdout.Fd())) {
  234. fmt.Fprint(os.Stderr, "writing...\r")
  235. }
  236. }
  237. if !protobuf {
  238. printResults(uri, deployed, results)
  239. } else {
  240. protobufResults(uri, deployed, results)
  241. }
  242. },
  243. }
  244. func printResults(
  245. uri string, deployed []hercules.LeafPipelineItem,
  246. results map[hercules.LeafPipelineItem]interface{}) {
  247. commonResult := results[nil].(*hercules.CommonAnalysisResult)
  248. fmt.Println("hercules:")
  249. fmt.Println(" version: 3")
  250. fmt.Println(" hash:", hercules.BinaryGitHash)
  251. fmt.Println(" repository:", uri)
  252. fmt.Println(" begin_unix_time:", commonResult.BeginTime)
  253. fmt.Println(" end_unix_time:", commonResult.EndTime)
  254. fmt.Println(" commits:", commonResult.CommitsNumber)
  255. fmt.Println(" run_time:", commonResult.RunTime.Nanoseconds()/1e6)
  256. for _, item := range deployed {
  257. result := results[item]
  258. fmt.Printf("%s:\n", item.Name())
  259. if err := item.Serialize(result, false, os.Stdout); err != nil {
  260. panic(err)
  261. }
  262. }
  263. }
  264. func protobufResults(
  265. uri string, deployed []hercules.LeafPipelineItem,
  266. results map[hercules.LeafPipelineItem]interface{}) {
  267. header := pb.Metadata{
  268. Version: 2,
  269. Hash: hercules.BinaryGitHash,
  270. Repository: uri,
  271. }
  272. results[nil].(*hercules.CommonAnalysisResult).FillMetadata(&header)
  273. message := pb.AnalysisResults{
  274. Header: &header,
  275. Contents: map[string][]byte{},
  276. }
  277. for _, item := range deployed {
  278. result := results[item]
  279. buffer := &bytes.Buffer{}
  280. if err := item.Serialize(result, true, buffer); err != nil {
  281. panic(err)
  282. }
  283. message.Contents[item.Name()] = buffer.Bytes()
  284. }
  285. serialized, err := proto.Marshal(&message)
  286. if err != nil {
  287. panic(err)
  288. }
  289. os.Stdout.Write(serialized)
  290. }
  291. // trimRightSpace removes the trailing whitespace characters.
  292. func trimRightSpace(s string) string {
  293. return strings.TrimRightFunc(s, unicode.IsSpace)
  294. }
  295. // rpad adds padding to the right of a string.
  296. func rpad(s string, padding int) string {
  297. return fmt.Sprintf(fmt.Sprintf("%%-%ds", padding), s)
  298. }
  299. // tmpl was adapted from cobra/cobra.go
  300. func tmpl(w io.Writer, text string, data interface{}) error {
  301. var templateFuncs = template.FuncMap{
  302. "trim": strings.TrimSpace,
  303. "trimRightSpace": trimRightSpace,
  304. "trimTrailingWhitespaces": trimRightSpace,
  305. "rpad": rpad,
  306. "gt": cobra.Gt,
  307. "eq": cobra.Eq,
  308. }
  309. for k, v := range sprig.TxtFuncMap() {
  310. templateFuncs[k] = v
  311. }
  312. t := template.New("top")
  313. t.Funcs(templateFuncs)
  314. template.Must(t.Parse(text))
  315. return t.Execute(w, data)
  316. }
  317. func formatUsage(c *cobra.Command) error {
  318. // the default UsageFunc() does some private magic c.mergePersistentFlags()
  319. // this should stay on top
  320. localFlags := c.LocalFlags()
  321. leaves := hercules.Registry.GetLeaves()
  322. plumbing := hercules.Registry.GetPlumbingItems()
  323. features := hercules.Registry.GetFeaturedItems()
  324. filter := map[string]bool{}
  325. for _, l := range leaves {
  326. filter[l.Flag()] = true
  327. for _, cfg := range l.ListConfigurationOptions() {
  328. filter[cfg.Flag] = true
  329. }
  330. }
  331. for _, i := range plumbing {
  332. for _, cfg := range i.ListConfigurationOptions() {
  333. filter[cfg.Flag] = true
  334. }
  335. }
  336. for key := range filter {
  337. localFlags.Lookup(key).Hidden = true
  338. }
  339. args := map[string]interface{}{
  340. "c": c,
  341. "leaves": leaves,
  342. "plumbing": plumbing,
  343. "features": features,
  344. }
  345. helpTemplate := `Usage:{{if .c.Runnable}}
  346. {{.c.UseLine}}{{end}}{{if .c.HasAvailableSubCommands}}
  347. {{.c.CommandPath}} [command]{{end}}{{if gt (len .c.Aliases) 0}}
  348. Aliases:
  349. {{.c.NameAndAliases}}{{end}}{{if .c.HasExample}}
  350. Examples:
  351. {{.c.Example}}{{end}}{{if .c.HasAvailableSubCommands}}
  352. Available Commands:{{range .c.Commands}}{{if (or .IsAvailableCommand (eq .Name "help"))}}
  353. {{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{if .c.HasAvailableLocalFlags}}
  354. Flags:
  355. {{range $line := .c.LocalFlags.FlagUsages | trimTrailingWhitespaces | split "\n"}}
  356. {{- $desc := splitList " " $line | last}}
  357. {{- $offset := sub ($desc | len) ($desc | trim | len)}}
  358. {{- $indent := splitList " " $line | initial | join " " | len | add 3 | add $offset | int}}
  359. {{- $wrap := sub 120 $indent | int}}
  360. {{- splitList " " $line | initial | join " "}} {{cat "!" $desc | wrap $wrap | indent $indent | substr $indent -1 | substr 2 -1}}
  361. {{end}}{{end}}
  362. Analysis Targets:{{range .leaves}}
  363. --{{rpad .Flag 40}}Runs {{.Name}} analysis.{{wrap 72 .Description | nindent 48}}{{range .ListConfigurationOptions}}
  364. --{{if .Type.String}}{{rpad (print .Flag " " .Type.String) 40}}{{else}}{{rpad .Flag 40}}{{end}}
  365. {{- $desc := dict "desc" .Description}}
  366. {{- if .Default}}{{$_ := set $desc "desc" (print .Description " The default value is " .FormatDefault ".")}}
  367. {{- end}}
  368. {{- $desc := pluck "desc" $desc | first}}
  369. {{- $desc | wrap 68 | indent 52 | substr 52 -1}}{{end}}
  370. {{end}}
  371. Plumbing Options:{{range .plumbing}}{{$name := .Name}}{{range .ListConfigurationOptions}}
  372. --{{if .Type.String}}{{rpad (print .Flag " " .Type.String " [" $name "]") 40}}{{else}}{{rpad (print .Flag " [" $name "]") 40}}
  373. {{- end}}
  374. {{- $desc := dict "desc" .Description}}
  375. {{- if .Default}}{{$_ := set $desc "desc" (print .Description " The default value is " .FormatDefault ".")}}
  376. {{- end}}
  377. {{- $desc := pluck "desc" $desc | first}}{{$desc | wrap 72 | indent 48 | substr 48 -1}}{{end}}{{end}}
  378. --feature:{{range $key, $value := .features}}
  379. {{rpad $key 42}}Enables {{range $index, $item := $value}}{{if $index}}, {{end}}{{$item.Name}}{{end}}.{{end}}{{if .c.HasAvailableInheritedFlags}}
  380. Global Flags:
  381. {{.c.InheritedFlags.FlagUsages | trimTrailingWhitespaces}}{{end}}{{if .c.HasHelpSubCommands}}
  382. Additional help topics:{{range .c.Commands}}{{if .IsAdditionalHelpTopicCommand}}
  383. {{rpad .CommandPath .CommandPathPadding}} {{.Short}}{{end}}{{end}}{{end}}{{if .c.HasAvailableSubCommands}}
  384. Use "{{.c.CommandPath}} [command] --help" for more information about a command.{{end}}
  385. `
  386. err := tmpl(c.OutOrStderr(), helpTemplate, args)
  387. for key := range filter {
  388. localFlags.Lookup(key).Hidden = false
  389. }
  390. if err != nil {
  391. c.Println(err)
  392. }
  393. return err
  394. }
  395. // versionCmd prints the API version and the Git commit hash
  396. var versionCmd = &cobra.Command{
  397. Use: "version",
  398. Short: "Print version information and exit.",
  399. Long: ``,
  400. Args: cobra.MaximumNArgs(0),
  401. Run: func(cmd *cobra.Command, args []string) {
  402. fmt.Printf("Version: %d\nGit: %s\n", hercules.BinaryVersion, hercules.BinaryGitHash)
  403. },
  404. }
  405. var cmdlineFacts map[string]interface{}
  406. var cmdlineDeployed map[string]*bool
  407. func init() {
  408. loadPlugins()
  409. rootCmd.MarkFlagFilename("plugin")
  410. rootFlags := rootCmd.Flags()
  411. rootFlags.String("commits", "", "Path to the text file with the "+
  412. "commit history to follow instead of the default `git log`. "+
  413. "The format is the list of hashes, each hash on a "+
  414. "separate line. The first hash is the root.")
  415. rootCmd.MarkFlagFilename("commits")
  416. rootFlags.Bool("first-parent", false, "Follow only the first parent in the commit history - "+
  417. "\"git log --first-parent\".")
  418. rootFlags.Bool("pb", false, "The output format will be Protocol Buffers instead of YAML.")
  419. rootFlags.Bool("quiet", !terminal.IsTerminal(int(os.Stdin.Fd())),
  420. "Do not print status updates to stderr.")
  421. rootFlags.Bool("profile", false, "Collect the profile to hercules.pprof.")
  422. rootFlags.String("ssh-identity", "", "Path to SSH identity file (e.g., ~/.ssh/id_rsa) to clone from an SSH remote.")
  423. cmdlineFacts, cmdlineDeployed = hercules.Registry.AddFlags(rootFlags)
  424. rootCmd.SetUsageFunc(formatUsage)
  425. rootCmd.AddCommand(versionCmd)
  426. versionCmd.SetUsageFunc(versionCmd.UsageFunc())
  427. }
  428. func main() {
  429. if err := rootCmd.Execute(); err != nil {
  430. fmt.Fprintln(os.Stderr, err)
  431. os.Exit(1)
  432. }
  433. }