uast.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. package uast
  2. import (
  3. "bytes"
  4. "context"
  5. "errors"
  6. "fmt"
  7. "io"
  8. goioutil "io/ioutil"
  9. "os"
  10. "path"
  11. "runtime"
  12. "strings"
  13. "sync"
  14. "time"
  15. "github.com/gogo/protobuf/proto"
  16. "github.com/jeffail/tunny"
  17. "gopkg.in/bblfsh/client-go.v2"
  18. "gopkg.in/bblfsh/sdk.v1/protocol"
  19. "gopkg.in/bblfsh/sdk.v1/uast"
  20. "gopkg.in/src-d/enry.v1"
  21. "gopkg.in/src-d/go-git.v4"
  22. "gopkg.in/src-d/go-git.v4/plumbing"
  23. "gopkg.in/src-d/go-git.v4/plumbing/object"
  24. "gopkg.in/src-d/go-git.v4/utils/ioutil"
  25. "gopkg.in/src-d/go-git.v4/utils/merkletrie"
  26. "gopkg.in/src-d/hercules.v4/internal/core"
  27. "gopkg.in/src-d/hercules.v4/internal/pb"
  28. items "gopkg.in/src-d/hercules.v4/internal/plumbing"
  29. )
  30. // Extractor retrieves UASTs from Babelfish server which correspond to changed files in a commit.
  31. // It is a PipelineItem.
  32. type Extractor struct {
  33. core.NoopMerger
  34. Endpoint string
  35. Context func() (context.Context, context.CancelFunc)
  36. PoolSize int
  37. Languages map[string]bool
  38. FailOnErrors bool
  39. ProcessedFiles map[string]int
  40. clients []*bblfsh.Client
  41. pool *tunny.Pool
  42. }
  43. const (
  44. uastExtractionSkipped = -(1 << 31)
  45. // ConfigUASTEndpoint is the name of the configuration option (Extractor.Configure())
  46. // which sets the Babelfish server address.
  47. ConfigUASTEndpoint = "ConfigUASTEndpoint"
  48. // ConfigUASTTimeout is the name of the configuration option (Extractor.Configure())
  49. // which sets the maximum amount of time to wait for a Babelfish server response.
  50. ConfigUASTTimeout = "ConfigUASTTimeout"
  51. // ConfigUASTPoolSize is the name of the configuration option (Extractor.Configure())
  52. // which sets the number of goroutines to run for UAST parse queries.
  53. ConfigUASTPoolSize = "ConfigUASTPoolSize"
  54. // ConfigUASTFailOnErrors is the name of the configuration option (Extractor.Configure())
  55. // which enables early exit in case of any Babelfish UAST parsing errors.
  56. ConfigUASTFailOnErrors = "ConfigUASTFailOnErrors"
  57. // ConfigUASTLanguages is the name of the configuration option (Extractor.Configure())
  58. // which sets the list of languages to parse. Language names are at
  59. // https://doc.bblf.sh/languages.html Names are joined with a comma ",".
  60. ConfigUASTLanguages = "ConfigUASTLanguages"
  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. type uastTask struct {
  67. Lock *sync.RWMutex
  68. Dest map[plumbing.Hash]*uast.Node
  69. File *object.File
  70. Errors *[]error
  71. }
  72. type worker struct {
  73. Client *bblfsh.Client
  74. Extractor *Extractor
  75. }
  76. // Process will synchronously perform a job and return the result.
  77. func (w worker) Process(data interface{}) interface{} {
  78. return w.Extractor.extractTask(w.Client, data)
  79. }
  80. func (w worker) BlockUntilReady() {}
  81. func (w worker) Interrupt() {}
  82. func (w worker) Terminate() {}
  83. // Name of this PipelineItem. Uniquely identifies the type, used for mapping keys, etc.
  84. func (exr *Extractor) Name() string {
  85. return "UAST"
  86. }
  87. // Provides returns the list of names of entities which are produced by this PipelineItem.
  88. // Each produced entity will be inserted into `deps` of dependent Consume()-s according
  89. // to this list. Also used by core.Registry to build the global map of providers.
  90. func (exr *Extractor) Provides() []string {
  91. arr := [...]string{DependencyUasts}
  92. return arr[:]
  93. }
  94. // Requires returns the list of names of entities which are needed by this PipelineItem.
  95. // Each requested entity will be inserted into `deps` of Consume(). In turn, those
  96. // entities are Provides() upstream.
  97. func (exr *Extractor) Requires() []string {
  98. arr := [...]string{items.DependencyTreeChanges, items.DependencyBlobCache}
  99. return arr[:]
  100. }
  101. // Features which must be enabled for this PipelineItem to be automatically inserted into the DAG.
  102. func (exr *Extractor) Features() []string {
  103. arr := [...]string{FeatureUast}
  104. return arr[:]
  105. }
  106. // ListConfigurationOptions returns the list of changeable public properties of this PipelineItem.
  107. func (exr *Extractor) ListConfigurationOptions() []core.ConfigurationOption {
  108. options := [...]core.ConfigurationOption{{
  109. Name: ConfigUASTEndpoint,
  110. Description: "How many days there are in a single band.",
  111. Flag: "bblfsh",
  112. Type: core.StringConfigurationOption,
  113. Default: "0.0.0.0:9432"}, {
  114. Name: ConfigUASTTimeout,
  115. Description: "Babelfish's server timeout in seconds.",
  116. Flag: "bblfsh-timeout",
  117. Type: core.IntConfigurationOption,
  118. Default: 20}, {
  119. Name: ConfigUASTPoolSize,
  120. Description: "Number of goroutines to extract UASTs.",
  121. Flag: "bblfsh-pool-size",
  122. Type: core.IntConfigurationOption,
  123. Default: runtime.NumCPU() * 2}, {
  124. Name: ConfigUASTFailOnErrors,
  125. Description: "Panic if there is a UAST extraction error.",
  126. Flag: "bblfsh-fail-on-error",
  127. Type: core.BoolConfigurationOption,
  128. Default: false}, {
  129. Name: ConfigUASTLanguages,
  130. Description: "Programming languages from which to extract UASTs. Separated by comma \",\".",
  131. Flag: "languages",
  132. Type: core.StringConfigurationOption,
  133. Default: "Python,Java,Go,JavaScript,Ruby,PHP"},
  134. }
  135. return options[:]
  136. }
  137. // Configure sets the properties previously published by ListConfigurationOptions().
  138. func (exr *Extractor) Configure(facts map[string]interface{}) {
  139. if val, exists := facts[ConfigUASTEndpoint].(string); exists {
  140. exr.Endpoint = val
  141. }
  142. if val, exists := facts[ConfigUASTTimeout].(int); exists {
  143. exr.Context = func() (context.Context, context.CancelFunc) {
  144. return context.WithTimeout(context.Background(),
  145. time.Duration(val)*time.Second)
  146. }
  147. }
  148. if val, exists := facts[ConfigUASTPoolSize].(int); exists {
  149. exr.PoolSize = val
  150. }
  151. if val, exists := facts[ConfigUASTLanguages].(string); exists {
  152. exr.Languages = map[string]bool{}
  153. for _, lang := range strings.Split(val, ",") {
  154. exr.Languages[strings.TrimSpace(lang)] = true
  155. }
  156. }
  157. if val, exists := facts[ConfigUASTFailOnErrors].(bool); exists {
  158. exr.FailOnErrors = val
  159. }
  160. }
  161. // Initialize resets the temporary caches and prepares this PipelineItem for a series of Consume()
  162. // calls. The repository which is going to be analysed is supplied as an argument.
  163. func (exr *Extractor) Initialize(repository *git.Repository) {
  164. if exr.Context == nil {
  165. exr.Context = func() (context.Context, context.CancelFunc) {
  166. return context.Background(), nil
  167. }
  168. }
  169. poolSize := exr.PoolSize
  170. if poolSize == 0 {
  171. poolSize = runtime.NumCPU()
  172. }
  173. exr.clients = make([]*bblfsh.Client, poolSize)
  174. for i := 0; i < poolSize; i++ {
  175. client, err := bblfsh.NewClient(exr.Endpoint)
  176. if err != nil {
  177. panic(err)
  178. }
  179. exr.clients[i] = client
  180. }
  181. if exr.pool != nil {
  182. exr.pool.Close()
  183. }
  184. {
  185. i := 0
  186. exr.pool = tunny.New(poolSize, func() tunny.Worker {
  187. w := worker{Client: exr.clients[i], Extractor: exr}
  188. i++
  189. return w
  190. })
  191. }
  192. if exr.pool == nil {
  193. panic("UAST goroutine pool was not created")
  194. }
  195. exr.ProcessedFiles = map[string]int{}
  196. if exr.Languages == nil {
  197. exr.Languages = map[string]bool{}
  198. }
  199. }
  200. // Consume runs this PipelineItem on the next commit data.
  201. // `deps` contain all the results from upstream PipelineItem-s as requested by Requires().
  202. // Additionally, DependencyCommit is always present there and represents the analysed *object.Commit.
  203. // This function returns the mapping with analysis results. The keys must be the same as
  204. // in Provides(). If there was an error, nil is returned.
  205. func (exr *Extractor) Consume(deps map[string]interface{}) (map[string]interface{}, error) {
  206. cache := deps[items.DependencyBlobCache].(map[plumbing.Hash]*object.Blob)
  207. treeDiffs := deps[items.DependencyTreeChanges].(object.Changes)
  208. uasts := map[plumbing.Hash]*uast.Node{}
  209. lock := sync.RWMutex{}
  210. errs := make([]error, 0)
  211. wg := sync.WaitGroup{}
  212. submit := func(change *object.Change) {
  213. {
  214. reader, err := cache[change.To.TreeEntry.Hash].Reader()
  215. if err != nil {
  216. errs = append(errs, err)
  217. return
  218. }
  219. defer ioutil.CheckClose(reader, &err)
  220. buf := new(bytes.Buffer)
  221. if _, err := buf.ReadFrom(reader); err != nil {
  222. errs = append(errs, err)
  223. return
  224. }
  225. lang := enry.GetLanguage(change.To.Name, buf.Bytes())
  226. if _, exists := exr.Languages[lang]; !exists {
  227. exr.ProcessedFiles[change.To.Name] = uastExtractionSkipped
  228. return
  229. }
  230. exr.ProcessedFiles[change.To.Name]++
  231. }
  232. wg.Add(1)
  233. go func(task interface{}) {
  234. exr.pool.Process(task)
  235. wg.Done()
  236. }(uastTask{
  237. Lock: &lock,
  238. Dest: uasts,
  239. File: &object.File{Name: change.To.Name, Blob: *cache[change.To.TreeEntry.Hash]},
  240. Errors: &errs,
  241. })
  242. }
  243. for _, change := range treeDiffs {
  244. action, err := change.Action()
  245. if err != nil {
  246. return nil, err
  247. }
  248. switch action {
  249. case merkletrie.Insert:
  250. submit(change)
  251. case merkletrie.Delete:
  252. continue
  253. case merkletrie.Modify:
  254. submit(change)
  255. }
  256. }
  257. wg.Wait()
  258. if len(errs) > 0 {
  259. msgs := make([]string, len(errs))
  260. for i, err := range errs {
  261. msgs[i] = err.Error()
  262. }
  263. joined := strings.Join(msgs, "\n")
  264. if exr.FailOnErrors {
  265. return nil, errors.New(joined)
  266. }
  267. fmt.Fprintln(os.Stderr, joined)
  268. }
  269. return map[string]interface{}{DependencyUasts: uasts}, nil
  270. }
  271. // Fork clones this PipelineItem.
  272. func (exr *Extractor) Fork(n int) []core.PipelineItem {
  273. return core.ForkSamePipelineItem(exr, n)
  274. }
  275. func (exr *Extractor) extractUAST(
  276. client *bblfsh.Client, file *object.File) (*uast.Node, error) {
  277. request := client.NewParseRequest()
  278. contents, err := file.Contents()
  279. if err != nil {
  280. return nil, err
  281. }
  282. request.Content(contents)
  283. request.Filename(file.Name)
  284. ctx, cancel := exr.Context()
  285. if cancel != nil {
  286. defer cancel()
  287. }
  288. response, err := request.DoWithContext(ctx)
  289. if err != nil {
  290. if strings.Contains("missing driver", err.Error()) {
  291. return nil, nil
  292. }
  293. return nil, err
  294. }
  295. if response.Status != protocol.Ok {
  296. return nil, errors.New(strings.Join(response.Errors, "\n"))
  297. }
  298. if err != nil {
  299. return nil, err
  300. }
  301. return response.UAST, nil
  302. }
  303. func (exr *Extractor) extractTask(client *bblfsh.Client, data interface{}) interface{} {
  304. task := data.(uastTask)
  305. node, err := exr.extractUAST(client, task.File)
  306. task.Lock.Lock()
  307. defer task.Lock.Unlock()
  308. if err != nil {
  309. *task.Errors = append(*task.Errors,
  310. fmt.Errorf("\nfile %s, blob %s: %v", task.File.Name, task.File.Hash.String(), err))
  311. return nil
  312. }
  313. if node != nil {
  314. task.Dest[task.File.Hash] = node
  315. }
  316. return nil
  317. }
  318. // Change is the type of the items in the list of changes which is provided by Changes.
  319. type Change struct {
  320. Before *uast.Node
  321. After *uast.Node
  322. Change *object.Change
  323. }
  324. const (
  325. // DependencyUastChanges is the name of the dependency provided by Changes.
  326. DependencyUastChanges = "changed_uasts"
  327. )
  328. // Changes is a structured analog of TreeDiff: it provides UASTs for every logical change
  329. // in a commit. It is a PipelineItem.
  330. type Changes struct {
  331. core.NoopMerger
  332. cache map[plumbing.Hash]*uast.Node
  333. }
  334. // Name of this PipelineItem. Uniquely identifies the type, used for mapping keys, etc.
  335. func (uc *Changes) Name() string {
  336. return "UASTChanges"
  337. }
  338. // Provides returns the list of names of entities which are produced by this PipelineItem.
  339. // Each produced entity will be inserted into `deps` of dependent Consume()-s according
  340. // to this list. Also used by core.Registry to build the global map of providers.
  341. func (uc *Changes) Provides() []string {
  342. arr := [...]string{DependencyUastChanges}
  343. return arr[:]
  344. }
  345. // Requires returns the list of names of entities which are needed by this PipelineItem.
  346. // Each requested entity will be inserted into `deps` of Consume(). In turn, those
  347. // entities are Provides() upstream.
  348. func (uc *Changes) Requires() []string {
  349. arr := [...]string{DependencyUasts, items.DependencyTreeChanges}
  350. return arr[:]
  351. }
  352. // Features which must be enabled for this PipelineItem to be automatically inserted into the DAG.
  353. func (uc *Changes) Features() []string {
  354. arr := [...]string{FeatureUast}
  355. return arr[:]
  356. }
  357. // ListConfigurationOptions returns the list of changeable public properties of this PipelineItem.
  358. func (uc *Changes) ListConfigurationOptions() []core.ConfigurationOption {
  359. return []core.ConfigurationOption{}
  360. }
  361. // Configure sets the properties previously published by ListConfigurationOptions().
  362. func (uc *Changes) Configure(facts map[string]interface{}) {}
  363. // Initialize resets the temporary caches and prepares this PipelineItem for a series of Consume()
  364. // calls. The repository which is going to be analysed is supplied as an argument.
  365. func (uc *Changes) Initialize(repository *git.Repository) {
  366. uc.cache = map[plumbing.Hash]*uast.Node{}
  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]*uast.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]*uast.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. // Features which must be enabled for this PipelineItem to be automatically inserted into the DAG.
  450. func (saver *ChangesSaver) Features() []string {
  451. arr := [...]string{FeatureUast}
  452. return arr[:]
  453. }
  454. // ListConfigurationOptions returns the list of changeable public properties of this PipelineItem.
  455. func (saver *ChangesSaver) ListConfigurationOptions() []core.ConfigurationOption {
  456. options := [...]core.ConfigurationOption{{
  457. Name: ConfigUASTChangesSaverOutputPath,
  458. Description: "The target directory where to store the changed UAST files.",
  459. Flag: "changed-uast-dir",
  460. Type: core.StringConfigurationOption,
  461. Default: "."},
  462. }
  463. return options[:]
  464. }
  465. // Flag for the command line switch which enables this analysis.
  466. func (saver *ChangesSaver) Flag() string {
  467. return "dump-uast-changes"
  468. }
  469. // Configure sets the properties previously published by ListConfigurationOptions().
  470. func (saver *ChangesSaver) Configure(facts map[string]interface{}) {
  471. if val, exists := facts[ConfigUASTChangesSaverOutputPath]; exists {
  472. saver.OutputPath = val.(string)
  473. }
  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) {
  478. saver.repository = repository
  479. saver.result = [][]Change{}
  480. saver.OneShotMergeProcessor.Initialize()
  481. }
  482. // Consume runs this PipelineItem on the next commit data.
  483. // `deps` contain all the results from upstream PipelineItem-s as requested by Requires().
  484. // Additionally, DependencyCommit is always present there and represents the analysed *object.Commit.
  485. // This function returns the mapping with analysis results. The keys must be the same as
  486. // in Provides(). If there was an error, nil is returned.
  487. func (saver *ChangesSaver) Consume(deps map[string]interface{}) (map[string]interface{}, error) {
  488. if !saver.ShouldConsumeCommit(deps) {
  489. return nil, nil
  490. }
  491. changes := deps[DependencyUastChanges].([]Change)
  492. saver.result = append(saver.result, changes)
  493. return nil, nil
  494. }
  495. // Finalize returns the result of the analysis. Further Consume() calls are not expected.
  496. func (saver *ChangesSaver) Finalize() interface{} {
  497. return saver.result
  498. }
  499. // Fork clones this PipelineItem.
  500. func (saver *ChangesSaver) Fork(n int) []core.PipelineItem {
  501. return core.ForkSamePipelineItem(saver, n)
  502. }
  503. // Serialize converts the analysis result as returned by Finalize() to text or bytes.
  504. // The text format is YAML and the bytes format is Protocol Buffers.
  505. func (saver *ChangesSaver) Serialize(result interface{}, binary bool, writer io.Writer) error {
  506. saverResult := result.([][]Change)
  507. fileNames := saver.dumpFiles(saverResult)
  508. if binary {
  509. return saver.serializeBinary(fileNames, writer)
  510. }
  511. saver.serializeText(fileNames, writer)
  512. return nil
  513. }
  514. func (saver *ChangesSaver) dumpFiles(result [][]Change) []*pb.UASTChange {
  515. fileNames := []*pb.UASTChange{}
  516. for i, changes := range result {
  517. for j, change := range changes {
  518. if change.Before == nil || change.After == nil {
  519. continue
  520. }
  521. record := &pb.UASTChange{FileName: change.Change.To.Name}
  522. bs, _ := change.Before.Marshal()
  523. record.UastBefore = path.Join(saver.OutputPath, fmt.Sprintf(
  524. "%d_%d_before_%s.pb", i, j, change.Change.From.TreeEntry.Hash.String()))
  525. goioutil.WriteFile(record.UastBefore, bs, 0666)
  526. blob, _ := saver.repository.BlobObject(change.Change.From.TreeEntry.Hash)
  527. s, _ := (&object.File{Blob: *blob}).Contents()
  528. record.SrcBefore = path.Join(saver.OutputPath, fmt.Sprintf(
  529. "%d_%d_before_%s.src", i, j, change.Change.From.TreeEntry.Hash.String()))
  530. goioutil.WriteFile(record.SrcBefore, []byte(s), 0666)
  531. bs, _ = change.After.Marshal()
  532. record.UastAfter = path.Join(saver.OutputPath, fmt.Sprintf(
  533. "%d_%d_after_%s.pb", i, j, change.Change.To.TreeEntry.Hash.String()))
  534. goioutil.WriteFile(record.UastAfter, bs, 0666)
  535. blob, _ = saver.repository.BlobObject(change.Change.To.TreeEntry.Hash)
  536. s, _ = (&object.File{Blob: *blob}).Contents()
  537. record.SrcAfter = path.Join(saver.OutputPath, fmt.Sprintf(
  538. "%d_%d_after_%s.src", i, j, change.Change.To.TreeEntry.Hash.String()))
  539. goioutil.WriteFile(record.SrcAfter, []byte(s), 0666)
  540. fileNames = append(fileNames, record)
  541. }
  542. }
  543. return fileNames
  544. }
  545. func (saver *ChangesSaver) serializeText(result []*pb.UASTChange, writer io.Writer) {
  546. for _, sc := range result {
  547. kv := [...]string{
  548. "file: " + sc.FileName,
  549. "src0: " + sc.SrcBefore, "src1: " + sc.SrcAfter,
  550. "uast0: " + sc.UastBefore, "uast1: " + sc.UastAfter,
  551. }
  552. fmt.Fprintf(writer, " - {%s}\n", strings.Join(kv[:], ", "))
  553. }
  554. }
  555. func (saver *ChangesSaver) serializeBinary(result []*pb.UASTChange, writer io.Writer) error {
  556. message := pb.UASTChangesSaverResults{Changes: result}
  557. serialized, err := proto.Marshal(&message)
  558. if err != nil {
  559. return err
  560. }
  561. writer.Write(serialized)
  562. return nil
  563. }
  564. func init() {
  565. core.Registry.Register(&Extractor{})
  566. core.Registry.Register(&Changes{})
  567. core.Registry.Register(&ChangesSaver{})
  568. }