uast.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. package uast
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "io/ioutil"
  8. "log"
  9. "os"
  10. "path"
  11. "runtime"
  12. "strings"
  13. "sync"
  14. "time"
  15. "github.com/Jeffail/tunny"
  16. "github.com/gogo/protobuf/proto"
  17. "gopkg.in/bblfsh/client-go.v3"
  18. "gopkg.in/bblfsh/sdk.v2/uast/nodes"
  19. "gopkg.in/bblfsh/sdk.v2/uast/nodes/nodesproto"
  20. "gopkg.in/src-d/go-git.v4"
  21. "gopkg.in/src-d/go-git.v4/plumbing"
  22. "gopkg.in/src-d/go-git.v4/plumbing/object"
  23. "gopkg.in/src-d/go-git.v4/utils/merkletrie"
  24. "gopkg.in/src-d/hercules.v9/internal/core"
  25. "gopkg.in/src-d/hercules.v9/internal/pb"
  26. items "gopkg.in/src-d/hercules.v9/internal/plumbing"
  27. )
  28. // Extractor retrieves UASTs from Babelfish server which correspond to changed files in a commit.
  29. // It is a PipelineItem.
  30. type Extractor struct {
  31. core.NoopMerger
  32. Endpoint string
  33. Context func() (context.Context, context.CancelFunc)
  34. PoolSize int
  35. FailOnErrors bool
  36. ProcessedFiles map[string]int
  37. IgnoredMissingDrivers map[string]bool
  38. clients []*bblfsh.Client
  39. pool *tunny.Pool
  40. }
  41. const (
  42. // ConfigUASTEndpoint is the name of the configuration option (Extractor.Configure())
  43. // which sets the Babelfish server address.
  44. ConfigUASTEndpoint = "UAST.Endpoint"
  45. // ConfigUASTTimeout is the name of the configuration option (Extractor.Configure())
  46. // which sets the maximum amount of time to wait for a Babelfish server response.
  47. ConfigUASTTimeout = "UAST.Timeout"
  48. // ConfigUASTPoolSize is the name of the configuration option (Extractor.Configure())
  49. // which sets the number of goroutines to run for UAST parse queries.
  50. ConfigUASTPoolSize = "UAST.PoolSize"
  51. // ConfigUASTFailOnErrors is the name of the configuration option (Extractor.Configure())
  52. // which enables early exit in case of any Babelfish UAST parsing errors.
  53. ConfigUASTFailOnErrors = "UAST.FailOnErrors"
  54. // ConfigUASTIgnoreMissingDrivers is the name of the configuration option (Extractor.Configure())
  55. // which sets the ignored missing driver names.
  56. ConfigUASTIgnoreMissingDrivers = "UAST.IgnoreMissingDrivers"
  57. // DefaultBabelfishEndpoint is the default address of the Babelfish parsing server.
  58. DefaultBabelfishEndpoint = "0.0.0.0:9432"
  59. // DefaultBabelfishTimeout is the default value of the RPC timeout in seconds.
  60. DefaultBabelfishTimeout = 20
  61. // FeatureUast is the name of the Pipeline feature which activates all the items related to UAST.
  62. FeatureUast = "uast"
  63. // DependencyUasts is the name of the dependency provided by Extractor.
  64. DependencyUasts = "uasts"
  65. )
  66. var (
  67. // DefaultBabelfishWorkers is the default number of parsing RPC goroutines.
  68. DefaultBabelfishWorkers = runtime.NumCPU() * 2
  69. // DefaultIgnoredMissingDrivers is the languages which are ignored if the Babelfish driver is missing.
  70. DefaultIgnoredMissingDrivers = []string{"markdown", "text", "yaml", "json"}
  71. )
  72. type uastTask struct {
  73. Lock *sync.RWMutex
  74. Dest map[plumbing.Hash]nodes.Node
  75. Name string
  76. Hash plumbing.Hash
  77. Data []byte
  78. Errors *[]error
  79. }
  80. type worker struct {
  81. Client *bblfsh.Client
  82. Extractor *Extractor
  83. }
  84. // Process will synchronously perform a job and return the result.
  85. func (w worker) Process(data interface{}) interface{} {
  86. return w.Extractor.extractTask(w.Client, data)
  87. }
  88. func (w worker) BlockUntilReady() {}
  89. func (w worker) Interrupt() {}
  90. func (w worker) Terminate() {}
  91. // Name of this PipelineItem. Uniquely identifies the type, used for mapping keys, etc.
  92. func (exr *Extractor) Name() string {
  93. return "UAST"
  94. }
  95. // Provides returns the list of names of entities which are produced by this PipelineItem.
  96. // Each produced entity will be inserted into `deps` of dependent Consume()-s according
  97. // to this list. Also used by core.Registry to build the global map of providers.
  98. func (exr *Extractor) Provides() []string {
  99. arr := [...]string{DependencyUasts}
  100. return arr[:]
  101. }
  102. // Requires returns the list of names of entities which are needed by this PipelineItem.
  103. // Each requested entity will be inserted into `deps` of Consume(). In turn, those
  104. // entities are Provides() upstream.
  105. func (exr *Extractor) Requires() []string {
  106. arr := [...]string{items.DependencyTreeChanges, items.DependencyBlobCache}
  107. return arr[:]
  108. }
  109. // Features which must be enabled for this PipelineItem to be automatically inserted into the DAG.
  110. func (exr *Extractor) Features() []string {
  111. arr := [...]string{FeatureUast}
  112. return arr[:]
  113. }
  114. // ListConfigurationOptions returns the list of changeable public properties of this PipelineItem.
  115. func (exr *Extractor) ListConfigurationOptions() []core.ConfigurationOption {
  116. options := [...]core.ConfigurationOption{{
  117. Name: ConfigUASTEndpoint,
  118. Description: "How many days there are in a single band.",
  119. Flag: "bblfsh",
  120. Type: core.StringConfigurationOption,
  121. Default: DefaultBabelfishEndpoint}, {
  122. Name: ConfigUASTTimeout,
  123. Description: "Babelfish's server timeout in seconds.",
  124. Flag: "bblfsh-timeout",
  125. Type: core.IntConfigurationOption,
  126. Default: DefaultBabelfishTimeout}, {
  127. Name: ConfigUASTPoolSize,
  128. Description: "Number of goroutines to extract UASTs.",
  129. Flag: "bblfsh-pool-size",
  130. Type: core.IntConfigurationOption,
  131. Default: DefaultBabelfishWorkers}, {
  132. Name: ConfigUASTFailOnErrors,
  133. Description: "Panic if there is a UAST extraction error.",
  134. Flag: "bblfsh-fail-on-error",
  135. Type: core.BoolConfigurationOption,
  136. Default: false}, {
  137. Name: ConfigUASTIgnoreMissingDrivers,
  138. Description: "Do not warn about missing drivers for the specified languages.",
  139. Flag: "bblfsh-ignored-drivers",
  140. Type: core.StringsConfigurationOption,
  141. Default: DefaultIgnoredMissingDrivers},
  142. }
  143. return options[:]
  144. }
  145. // Configure sets the properties previously published by ListConfigurationOptions().
  146. func (exr *Extractor) Configure(facts map[string]interface{}) error {
  147. if val, exists := facts[ConfigUASTEndpoint].(string); exists {
  148. exr.Endpoint = val
  149. }
  150. if val, exists := facts[ConfigUASTTimeout].(int); exists {
  151. exr.Context = func() (context.Context, context.CancelFunc) {
  152. return context.WithTimeout(context.Background(),
  153. time.Duration(val)*time.Second)
  154. }
  155. }
  156. if val, exists := facts[ConfigUASTPoolSize].(int); exists {
  157. exr.PoolSize = val
  158. }
  159. if val, exists := facts[ConfigUASTFailOnErrors].(bool); exists {
  160. exr.FailOnErrors = val
  161. }
  162. if val, exists := facts[ConfigUASTIgnoreMissingDrivers].([]string); exists {
  163. exr.IgnoredMissingDrivers = map[string]bool{}
  164. for _, name := range val {
  165. exr.IgnoredMissingDrivers[name] = true
  166. }
  167. }
  168. return nil
  169. }
  170. // Initialize resets the temporary caches and prepares this PipelineItem for a series of Consume()
  171. // calls. The repository which is going to be analysed is supplied as an argument.
  172. func (exr *Extractor) Initialize(repository *git.Repository) error {
  173. if exr.Context == nil {
  174. exr.Context = func() (context.Context, context.CancelFunc) {
  175. return context.WithTimeout(context.Background(),
  176. time.Duration(DefaultBabelfishTimeout)*time.Second)
  177. }
  178. }
  179. if exr.Endpoint == "" {
  180. exr.Endpoint = DefaultBabelfishEndpoint
  181. }
  182. if exr.PoolSize == 0 {
  183. exr.PoolSize = DefaultBabelfishWorkers
  184. }
  185. poolSize := exr.PoolSize
  186. if poolSize == 0 {
  187. poolSize = runtime.NumCPU()
  188. }
  189. exr.clients = make([]*bblfsh.Client, poolSize)
  190. for i := 0; i < poolSize; i++ {
  191. client, err := bblfsh.NewClient(exr.Endpoint)
  192. if err != nil {
  193. if err.Error() == "context deadline exceeded" {
  194. log.Println("Looks like the Babelfish server is not running. Please refer " +
  195. "to https://docs.sourced.tech/babelfish/using-babelfish/getting-started#running-with-docker-recommended")
  196. }
  197. return err
  198. }
  199. exr.clients[i] = client
  200. }
  201. if exr.pool != nil {
  202. exr.pool.Close()
  203. }
  204. {
  205. i := 0
  206. exr.pool = tunny.New(poolSize, func() tunny.Worker {
  207. w := worker{Client: exr.clients[i], Extractor: exr}
  208. i++
  209. return w
  210. })
  211. }
  212. if exr.pool == nil {
  213. panic("UAST goroutine pool was not created")
  214. }
  215. exr.ProcessedFiles = map[string]int{}
  216. if exr.IgnoredMissingDrivers == nil {
  217. exr.IgnoredMissingDrivers = map[string]bool{}
  218. for _, name := range DefaultIgnoredMissingDrivers {
  219. exr.IgnoredMissingDrivers[name] = true
  220. }
  221. }
  222. return nil
  223. }
  224. // Consume runs this PipelineItem on the next commit data.
  225. // `deps` contain all the results from upstream PipelineItem-s as requested by Requires().
  226. // Additionally, DependencyCommit is always present there and represents the analysed *object.Commit.
  227. // This function returns the mapping with analysis results. The keys must be the same as
  228. // in Provides(). If there was an error, nil is returned.
  229. func (exr *Extractor) Consume(deps map[string]interface{}) (map[string]interface{}, error) {
  230. cache := deps[items.DependencyBlobCache].(map[plumbing.Hash]*items.CachedBlob)
  231. treeDiffs := deps[items.DependencyTreeChanges].(object.Changes)
  232. uasts := map[plumbing.Hash]nodes.Node{}
  233. lock := sync.RWMutex{}
  234. errs := make([]error, 0)
  235. wg := sync.WaitGroup{}
  236. submit := func(change *object.Change) {
  237. exr.ProcessedFiles[change.To.Name]++
  238. wg.Add(1)
  239. go func(task interface{}) {
  240. exr.pool.Process(task)
  241. wg.Done()
  242. }(uastTask{
  243. Lock: &lock,
  244. Dest: uasts,
  245. Name: change.To.Name,
  246. Hash: change.To.TreeEntry.Hash,
  247. Data: cache[change.To.TreeEntry.Hash].Data,
  248. Errors: &errs,
  249. })
  250. }
  251. for _, change := range treeDiffs {
  252. action, err := change.Action()
  253. if err != nil {
  254. return nil, err
  255. }
  256. switch action {
  257. case merkletrie.Insert:
  258. submit(change)
  259. case merkletrie.Delete:
  260. continue
  261. case merkletrie.Modify:
  262. submit(change)
  263. }
  264. }
  265. wg.Wait()
  266. if len(errs) > 0 {
  267. msgs := make([]string, len(errs))
  268. for i, err := range errs {
  269. msgs[i] = err.Error()
  270. }
  271. joined := strings.Join(msgs, "\n")
  272. if exr.FailOnErrors {
  273. return nil, errors.New(joined)
  274. }
  275. log.Println(joined)
  276. }
  277. return map[string]interface{}{DependencyUasts: uasts}, nil
  278. }
  279. // Fork clones this PipelineItem.
  280. func (exr *Extractor) Fork(n int) []core.PipelineItem {
  281. return core.ForkSamePipelineItem(exr, n)
  282. }
  283. func (exr *Extractor) extractUAST(
  284. client *bblfsh.Client, name string, data []byte) (nodes.Node, error) {
  285. ctx, cancel := exr.Context()
  286. if cancel != nil {
  287. defer cancel()
  288. }
  289. request := client.NewParseRequest().
  290. Content(string(data)).Filename(name).Mode(bblfsh.Semantic).Context(ctx)
  291. response, _, err := request.UAST()
  292. if err != nil {
  293. if strings.Contains("missing driver", err.Error()) {
  294. return nil, nil
  295. }
  296. return nil, err
  297. }
  298. return response, nil
  299. }
  300. func (exr *Extractor) extractTask(client *bblfsh.Client, data interface{}) interface{} {
  301. task := data.(uastTask)
  302. node, err := exr.extractUAST(client, task.Name, task.Data)
  303. task.Lock.Lock()
  304. defer task.Lock.Unlock()
  305. if err != nil {
  306. for lang := range exr.IgnoredMissingDrivers {
  307. if strings.HasSuffix(err.Error(), "\""+lang+"\"") {
  308. return nil
  309. }
  310. }
  311. *task.Errors = append(*task.Errors,
  312. fmt.Errorf("\nfile %s, blob %s: %v", task.Name, task.Hash.String(), err))
  313. return nil
  314. }
  315. if node != nil {
  316. task.Dest[task.Hash] = node
  317. }
  318. return nil
  319. }
  320. // Change is the type of the items in the list of changes which is provided by Changes.
  321. type Change struct {
  322. Before nodes.Node
  323. After nodes.Node
  324. Change *object.Change
  325. }
  326. const (
  327. // DependencyUastChanges is the name of the dependency provided by Changes.
  328. DependencyUastChanges = "changed_uasts"
  329. )
  330. // Changes is a structured analog of TreeDiff: it provides UASTs for every logical change
  331. // in a commit. It is a PipelineItem.
  332. type Changes struct {
  333. core.NoopMerger
  334. cache map[plumbing.Hash]nodes.Node
  335. }
  336. // Name of this PipelineItem. Uniquely identifies the type, used for mapping keys, etc.
  337. func (uc *Changes) Name() string {
  338. return "UASTChanges"
  339. }
  340. // Provides returns the list of names of entities which are produced by this PipelineItem.
  341. // Each produced entity will be inserted into `deps` of dependent Consume()-s according
  342. // to this list. Also used by core.Registry to build the global map of providers.
  343. func (uc *Changes) Provides() []string {
  344. arr := [...]string{DependencyUastChanges}
  345. return arr[:]
  346. }
  347. // Requires returns the list of names of entities which are needed by this PipelineItem.
  348. // Each requested entity will be inserted into `deps` of Consume(). In turn, those
  349. // entities are Provides() upstream.
  350. func (uc *Changes) Requires() []string {
  351. arr := [...]string{DependencyUasts, items.DependencyTreeChanges}
  352. return arr[:]
  353. }
  354. // ListConfigurationOptions returns the list of changeable public properties of this PipelineItem.
  355. func (uc *Changes) ListConfigurationOptions() []core.ConfigurationOption {
  356. return []core.ConfigurationOption{}
  357. }
  358. // Configure sets the properties previously published by ListConfigurationOptions().
  359. func (uc *Changes) Configure(facts map[string]interface{}) error {
  360. return nil
  361. }
  362. // Initialize resets the temporary caches and prepares this PipelineItem for a series of Consume()
  363. // calls. The repository which is going to be analysed is supplied as an argument.
  364. func (uc *Changes) Initialize(repository *git.Repository) error {
  365. uc.cache = map[plumbing.Hash]nodes.Node{}
  366. return nil
  367. }
  368. // Consume runs this PipelineItem on the next commit data.
  369. // `deps` contain all the results from upstream PipelineItem-s as requested by Requires().
  370. // Additionally, DependencyCommit is always present there and represents the analysed *object.Commit.
  371. // This function returns the mapping with analysis results. The keys must be the same as
  372. // in Provides(). If there was an error, nil is returned.
  373. func (uc *Changes) Consume(deps map[string]interface{}) (map[string]interface{}, error) {
  374. uasts := deps[DependencyUasts].(map[plumbing.Hash]nodes.Node)
  375. treeDiffs := deps[items.DependencyTreeChanges].(object.Changes)
  376. commit := make([]Change, 0, len(treeDiffs))
  377. for _, change := range treeDiffs {
  378. action, err := change.Action()
  379. if err != nil {
  380. return nil, err
  381. }
  382. switch action {
  383. case merkletrie.Insert:
  384. hashTo := change.To.TreeEntry.Hash
  385. uastTo := uasts[hashTo]
  386. commit = append(commit, Change{Before: nil, After: uastTo, Change: change})
  387. uc.cache[hashTo] = uastTo
  388. case merkletrie.Delete:
  389. hashFrom := change.From.TreeEntry.Hash
  390. commit = append(commit, Change{Before: uc.cache[hashFrom], After: nil, Change: change})
  391. delete(uc.cache, hashFrom)
  392. case merkletrie.Modify:
  393. hashFrom := change.From.TreeEntry.Hash
  394. hashTo := change.To.TreeEntry.Hash
  395. uastTo := uasts[hashTo]
  396. commit = append(commit, Change{Before: uc.cache[hashFrom], After: uastTo, Change: change})
  397. delete(uc.cache, hashFrom)
  398. uc.cache[hashTo] = uastTo
  399. }
  400. }
  401. return map[string]interface{}{DependencyUastChanges: commit}, nil
  402. }
  403. // Fork clones this PipelineItem.
  404. func (uc *Changes) Fork(n int) []core.PipelineItem {
  405. ucs := make([]core.PipelineItem, n)
  406. for i := 0; i < n; i++ {
  407. clone := &Changes{
  408. cache: map[plumbing.Hash]nodes.Node{},
  409. }
  410. for key, val := range uc.cache {
  411. clone.cache[key] = val
  412. }
  413. ucs[i] = clone
  414. }
  415. return ucs
  416. }
  417. // ChangesSaver dumps changed files and corresponding UASTs for every commit.
  418. // it is a LeafPipelineItem.
  419. type ChangesSaver struct {
  420. core.NoopMerger
  421. core.OneShotMergeProcessor
  422. // OutputPath points to the target directory with UASTs
  423. OutputPath string
  424. repository *git.Repository
  425. result [][]Change
  426. }
  427. const (
  428. // ConfigUASTChangesSaverOutputPath is the name of the configuration option
  429. // (ChangesSaver.Configure()) which sets the target directory where to save the files.
  430. ConfigUASTChangesSaverOutputPath = "ChangesSaver.OutputPath"
  431. )
  432. // Name of this PipelineItem. Uniquely identifies the type, used for mapping keys, etc.
  433. func (saver *ChangesSaver) Name() string {
  434. return "UASTChangesSaver"
  435. }
  436. // Provides returns the list of names of entities which are produced by this PipelineItem.
  437. // Each produced entity will be inserted into `deps` of dependent Consume()-s according
  438. // to this list. Also used by core.Registry to build the global map of providers.
  439. func (saver *ChangesSaver) Provides() []string {
  440. return []string{}
  441. }
  442. // Requires returns the list of names of entities which are needed by this PipelineItem.
  443. // Each requested entity will be inserted into `deps` of Consume(). In turn, those
  444. // entities are Provides() upstream.
  445. func (saver *ChangesSaver) Requires() []string {
  446. arr := [...]string{DependencyUastChanges}
  447. return arr[:]
  448. }
  449. // ListConfigurationOptions returns the list of changeable public properties of this PipelineItem.
  450. func (saver *ChangesSaver) ListConfigurationOptions() []core.ConfigurationOption {
  451. options := [...]core.ConfigurationOption{{
  452. Name: ConfigUASTChangesSaverOutputPath,
  453. Description: "The target directory where to store the changed UAST files.",
  454. Flag: "changed-uast-dir",
  455. Type: core.PathConfigurationOption,
  456. Default: "."},
  457. }
  458. return options[:]
  459. }
  460. // Flag for the command line switch which enables this analysis.
  461. func (saver *ChangesSaver) Flag() string {
  462. return "dump-uast-changes"
  463. }
  464. // Description returns the text which explains what the analysis is doing.
  465. func (saver *ChangesSaver) Description() string {
  466. return "Saves UASTs and file contents on disk for each commit."
  467. }
  468. // Configure sets the properties previously published by ListConfigurationOptions().
  469. func (saver *ChangesSaver) Configure(facts map[string]interface{}) error {
  470. if val, exists := facts[ConfigUASTChangesSaverOutputPath]; exists {
  471. saver.OutputPath = val.(string)
  472. }
  473. return nil
  474. }
  475. // Initialize resets the temporary caches and prepares this PipelineItem for a series of Consume()
  476. // calls. The repository which is going to be analysed is supplied as an argument.
  477. func (saver *ChangesSaver) Initialize(repository *git.Repository) error {
  478. saver.repository = repository
  479. saver.result = [][]Change{}
  480. saver.OneShotMergeProcessor.Initialize()
  481. return nil
  482. }
  483. // Consume runs this PipelineItem on the next commit data.
  484. // `deps` contain all the results from upstream PipelineItem-s as requested by Requires().
  485. // Additionally, DependencyCommit is always present there and represents the analysed *object.Commit.
  486. // This function returns the mapping with analysis results. The keys must be the same as
  487. // in Provides(). If there was an error, nil is returned.
  488. func (saver *ChangesSaver) Consume(deps map[string]interface{}) (map[string]interface{}, error) {
  489. if !saver.ShouldConsumeCommit(deps) {
  490. return nil, nil
  491. }
  492. changes := deps[DependencyUastChanges].([]Change)
  493. saver.result = append(saver.result, changes)
  494. return nil, nil
  495. }
  496. // Finalize returns the result of the analysis. Further Consume() calls are not expected.
  497. func (saver *ChangesSaver) Finalize() interface{} {
  498. return saver.result
  499. }
  500. // Fork clones this PipelineItem.
  501. func (saver *ChangesSaver) Fork(n int) []core.PipelineItem {
  502. return core.ForkSamePipelineItem(saver, n)
  503. }
  504. // Serialize converts the analysis result as returned by Finalize() to text or bytes.
  505. // The text format is YAML and the bytes format is Protocol Buffers.
  506. func (saver *ChangesSaver) Serialize(result interface{}, binary bool, writer io.Writer) error {
  507. saverResult := result.([][]Change)
  508. fileNames := saver.dumpFiles(saverResult)
  509. if binary {
  510. return saver.serializeBinary(fileNames, writer)
  511. }
  512. saver.serializeText(fileNames, writer)
  513. return nil
  514. }
  515. func (saver *ChangesSaver) dumpFiles(result [][]Change) []*pb.UASTChange {
  516. var fileNames []*pb.UASTChange
  517. dumpUast := func(uast nodes.Node, path string) {
  518. f, err := os.Create(path)
  519. if err != nil {
  520. panic(err)
  521. }
  522. defer f.Close()
  523. err = nodesproto.WriteTo(f, uast)
  524. if err != nil {
  525. panic(err)
  526. }
  527. }
  528. for i, changes := range result {
  529. for j, change := range changes {
  530. if change.Before == nil || change.After == nil {
  531. continue
  532. }
  533. record := &pb.UASTChange{FileName: change.Change.To.Name}
  534. record.UastBefore = path.Join(saver.OutputPath, fmt.Sprintf(
  535. "%d_%d_before_%s.pb", i, j, change.Change.From.TreeEntry.Hash.String()))
  536. dumpUast(change.Before, record.UastBefore)
  537. blob, _ := saver.repository.BlobObject(change.Change.From.TreeEntry.Hash)
  538. s, _ := (&object.File{Blob: *blob}).Contents()
  539. record.SrcBefore = path.Join(saver.OutputPath, fmt.Sprintf(
  540. "%d_%d_before_%s.src", i, j, change.Change.From.TreeEntry.Hash.String()))
  541. err := ioutil.WriteFile(record.SrcBefore, []byte(s), 0666)
  542. if err != nil {
  543. panic(err)
  544. }
  545. record.UastAfter = path.Join(saver.OutputPath, fmt.Sprintf(
  546. "%d_%d_after_%s.pb", i, j, change.Change.To.TreeEntry.Hash.String()))
  547. dumpUast(change.After, record.UastAfter)
  548. blob, _ = saver.repository.BlobObject(change.Change.To.TreeEntry.Hash)
  549. s, _ = (&object.File{Blob: *blob}).Contents()
  550. record.SrcAfter = path.Join(saver.OutputPath, fmt.Sprintf(
  551. "%d_%d_after_%s.src", i, j, change.Change.To.TreeEntry.Hash.String()))
  552. err = ioutil.WriteFile(record.SrcAfter, []byte(s), 0666)
  553. if err != nil {
  554. panic(err)
  555. }
  556. fileNames = append(fileNames, record)
  557. }
  558. }
  559. return fileNames
  560. }
  561. func (saver *ChangesSaver) serializeText(result []*pb.UASTChange, writer io.Writer) {
  562. for _, sc := range result {
  563. kv := [...]string{
  564. "file: " + sc.FileName,
  565. "src0: " + sc.SrcBefore, "src1: " + sc.SrcAfter,
  566. "uast0: " + sc.UastBefore, "uast1: " + sc.UastAfter,
  567. }
  568. fmt.Fprintf(writer, " - {%s}\n", strings.Join(kv[:], ", "))
  569. }
  570. }
  571. func (saver *ChangesSaver) serializeBinary(result []*pb.UASTChange, writer io.Writer) error {
  572. message := pb.UASTChangesSaverResults{Changes: result}
  573. serialized, err := proto.Marshal(&message)
  574. if err != nil {
  575. return err
  576. }
  577. _, err = writer.Write(serialized)
  578. return err
  579. }
  580. func init() {
  581. core.Registry.Register(&Extractor{})
  582. core.Registry.Register(&Changes{})
  583. core.Registry.Register(&ChangesSaver{})
  584. }