gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. // Copyright 2016 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package gitgraph
  4. import (
  5. "bytes"
  6. "fmt"
  7. "slices"
  8. "strings"
  9. "testing"
  10. "code.gitea.io/gitea/modules/git"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. func BenchmarkGetCommitGraph(b *testing.B) {
  14. currentRepo, err := git.OpenRepository(b.Context(), ".")
  15. if err != nil || currentRepo == nil {
  16. b.Error("Could not open repository")
  17. }
  18. defer currentRepo.Close()
  19. for b.Loop() {
  20. graph, err := GetCommitGraph(currentRepo, 1, 0, false, nil, nil)
  21. if err != nil {
  22. b.Error("Could get commit graph")
  23. }
  24. if len(graph.Commits) < 100 {
  25. b.Error("Should get 100 log lines.")
  26. }
  27. }
  28. }
  29. func BenchmarkParseCommitString(b *testing.B) {
  30. testString := "* DATA:|4e61bacab44e9b4730e44a6615d04098dd3a8eaf|2016-12-20 21:10:41 +0100|4e61bac|Add route for graph"
  31. parser := &Parser{}
  32. parser.Reset()
  33. for b.Loop() {
  34. parser.Reset()
  35. graph := NewGraph()
  36. if err := parser.AddLineToGraph(graph, 0, []byte(testString)); err != nil {
  37. b.Error("could not parse teststring")
  38. }
  39. if graph.Flows[1].Commits[0].Rev != "4e61bacab44e9b4730e44a6615d04098dd3a8eaf" {
  40. b.Error("Did not get expected data")
  41. }
  42. }
  43. }
  44. func BenchmarkParseGlyphs(b *testing.B) {
  45. parser := &Parser{}
  46. parser.Reset()
  47. tgBytes := []byte(testglyphs)
  48. var tg []byte
  49. for b.Loop() {
  50. parser.Reset()
  51. tg = tgBytes
  52. idx := bytes.Index(tg, []byte("\n"))
  53. for idx > 0 {
  54. parser.ParseGlyphs(tg[:idx])
  55. tg = tg[idx+1:]
  56. idx = bytes.Index(tg, []byte("\n"))
  57. }
  58. }
  59. }
  60. func TestReleaseUnusedColors(t *testing.T) {
  61. testcases := []struct {
  62. availableColors []int
  63. oldColors []int
  64. firstInUse int // these values have to be either be correct or suggest less is
  65. firstAvailable int // available than possibly is - i.e. you cannot say 10 is available when it
  66. }{
  67. {
  68. availableColors: []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
  69. oldColors: []int{1, 1, 1, 1, 1},
  70. firstAvailable: -1,
  71. firstInUse: 1,
  72. },
  73. {
  74. availableColors: []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
  75. oldColors: []int{1, 2, 3, 4},
  76. firstAvailable: 6,
  77. firstInUse: 0,
  78. },
  79. {
  80. availableColors: []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
  81. oldColors: []int{6, 0, 3, 5, 3, 4, 0, 0},
  82. firstAvailable: 6,
  83. firstInUse: 0,
  84. },
  85. {
  86. availableColors: []int{1, 2, 3, 4, 5, 6, 7},
  87. oldColors: []int{6, 1, 3, 5, 3, 4, 2, 7},
  88. firstAvailable: -1,
  89. firstInUse: 0,
  90. },
  91. {
  92. availableColors: []int{1, 2, 3, 4, 5, 6, 7},
  93. oldColors: []int{6, 0, 3, 5, 3, 4, 2, 7},
  94. firstAvailable: -1,
  95. firstInUse: 0,
  96. },
  97. }
  98. for _, testcase := range testcases {
  99. parser := &Parser{}
  100. parser.Reset()
  101. parser.availableColors = append([]int{}, testcase.availableColors...)
  102. parser.oldColors = append(parser.oldColors, testcase.oldColors...)
  103. parser.firstAvailable = testcase.firstAvailable
  104. parser.firstInUse = testcase.firstInUse
  105. parser.releaseUnusedColors()
  106. if parser.firstAvailable == -1 {
  107. // All in use
  108. for _, color := range parser.availableColors {
  109. found := slices.Contains(parser.oldColors, color)
  110. if !found {
  111. t.Errorf("In testcase:\n%d\t%d\t%d %d =>\n%d\t%d\t%d %d: %d should be available but is not",
  112. testcase.availableColors,
  113. testcase.oldColors,
  114. testcase.firstAvailable,
  115. testcase.firstInUse,
  116. parser.availableColors,
  117. parser.oldColors,
  118. parser.firstAvailable,
  119. parser.firstInUse,
  120. color)
  121. }
  122. }
  123. } else if parser.firstInUse != -1 {
  124. // Some in use
  125. for i := parser.firstInUse; i != parser.firstAvailable; i = (i + 1) % len(parser.availableColors) {
  126. color := parser.availableColors[i]
  127. found := slices.Contains(parser.oldColors, color)
  128. if !found {
  129. t.Errorf("In testcase:\n%d\t%d\t%d %d =>\n%d\t%d\t%d %d: %d should be available but is not",
  130. testcase.availableColors,
  131. testcase.oldColors,
  132. testcase.firstAvailable,
  133. testcase.firstInUse,
  134. parser.availableColors,
  135. parser.oldColors,
  136. parser.firstAvailable,
  137. parser.firstInUse,
  138. color)
  139. }
  140. }
  141. for i := parser.firstAvailable; i != parser.firstInUse; i = (i + 1) % len(parser.availableColors) {
  142. color := parser.availableColors[i]
  143. found := slices.Contains(parser.oldColors, color)
  144. if found {
  145. t.Errorf("In testcase:\n%d\t%d\t%d %d =>\n%d\t%d\t%d %d: %d should not be available but is",
  146. testcase.availableColors,
  147. testcase.oldColors,
  148. testcase.firstAvailable,
  149. testcase.firstInUse,
  150. parser.availableColors,
  151. parser.oldColors,
  152. parser.firstAvailable,
  153. parser.firstInUse,
  154. color)
  155. }
  156. }
  157. } else {
  158. // None in use
  159. for _, color := range parser.oldColors {
  160. if color != 0 {
  161. t.Errorf("In testcase:\n%d\t%d\t%d %d =>\n%d\t%d\t%d %d: %d should not be available but is",
  162. testcase.availableColors,
  163. testcase.oldColors,
  164. testcase.firstAvailable,
  165. testcase.firstInUse,
  166. parser.availableColors,
  167. parser.oldColors,
  168. parser.firstAvailable,
  169. parser.firstInUse,
  170. color)
  171. }
  172. }
  173. }
  174. }
  175. }
  176. func TestParseGlyphs(t *testing.T) {
  177. parser := &Parser{}
  178. parser.Reset()
  179. tgBytes := []byte(testglyphs)
  180. tg := tgBytes
  181. idx := bytes.Index(tg, []byte("\n"))
  182. row := 0
  183. for idx > 0 {
  184. parser.ParseGlyphs(tg[:idx])
  185. tg = tg[idx+1:]
  186. idx = bytes.Index(tg, []byte("\n"))
  187. if parser.flows[0] != 1 {
  188. t.Errorf("First column flow should be 1 but was %d", parser.flows[0])
  189. }
  190. colorToFlow := map[int]int64{}
  191. flowToColor := map[int64]int{}
  192. for i, flow := range parser.flows {
  193. if flow == 0 {
  194. continue
  195. }
  196. color := parser.colors[i]
  197. if fColor, in := flowToColor[flow]; in && fColor != color {
  198. t.Errorf("Row %d column %d flow %d has color %d but should be %d", row, i, flow, color, fColor)
  199. }
  200. flowToColor[flow] = color
  201. if cFlow, in := colorToFlow[color]; in && cFlow != flow {
  202. t.Errorf("Row %d column %d flow %d has color %d but conflicts with flow %d", row, i, flow, color, cFlow)
  203. }
  204. colorToFlow[color] = flow
  205. }
  206. row++
  207. }
  208. assert.Len(t, parser.availableColors, 9)
  209. }
  210. func TestCommitStringParsing(t *testing.T) {
  211. dataFirstPart := "* DATA:|4e61bacab44e9b4730e44a6615d04098dd3a8eaf|2016-12-20 21:10:41 +0100|4e61bac|"
  212. tests := []struct {
  213. shouldPass bool
  214. testName string
  215. commitMessage string
  216. }{
  217. {true, "normal", "not a fancy message"},
  218. {true, "extra pipe", "An extra pipe: |"},
  219. {true, "extra 'Data:'", "DATA: might be trouble"},
  220. }
  221. for _, test := range tests {
  222. t.Run(test.testName, func(t *testing.T) {
  223. testString := fmt.Sprintf("%s%s", dataFirstPart, test.commitMessage)
  224. idx := strings.Index(testString, "DATA:")
  225. commit, err := NewCommit(0, 0, []byte(testString[idx+5:]))
  226. if err != nil && test.shouldPass {
  227. t.Errorf("Could not parse %s", testString)
  228. return
  229. }
  230. assert.Equal(t, test.commitMessage, commit.Subject)
  231. })
  232. }
  233. }
  234. var testglyphs = `*
  235. *
  236. *
  237. *
  238. *
  239. *
  240. *
  241. *
  242. |\
  243. * |
  244. * |
  245. * |
  246. * |
  247. * |
  248. | *
  249. * |
  250. | *
  251. | |\
  252. * | |
  253. | | *
  254. | | |\
  255. * | | \
  256. |\ \ \ \
  257. | * | | |
  258. | |\| | |
  259. * | | | |
  260. |/ / / /
  261. | | | *
  262. | * | |
  263. | * | |
  264. | * | |
  265. * | | |
  266. * | | |
  267. * | | |
  268. * | | |
  269. * | | |
  270. |\ \ \ \
  271. | | * | |
  272. | | |\| |
  273. | | | * |
  274. | | | | *
  275. * | | | |
  276. * | | | |
  277. * | | | |
  278. * | | | |
  279. * | | | |
  280. |\ \ \ \ \
  281. | * | | | |
  282. |/| | | | |
  283. | | |/ / /
  284. | |/| | |
  285. | | | | *
  286. | * | | |
  287. |/| | | |
  288. | * | | |
  289. |/| | | |
  290. | | |/ /
  291. | |/| |
  292. | * | |
  293. | * | |
  294. | |\ \ \
  295. | | * | |
  296. | |/| | |
  297. | | | |/
  298. | | |/|
  299. | * | |
  300. | * | |
  301. | * | |
  302. | | * |
  303. | | |\ \
  304. | | | * |
  305. | | |/| |
  306. | | | * |
  307. | | | |\ \
  308. | | | | * |
  309. | | | |/| |
  310. | | * | | |
  311. | | * | | |
  312. | | |\ \ \ \
  313. | | | * | | |
  314. | | |/| | | |
  315. | | | | | * |
  316. | | | | |/ /
  317. * | | | / /
  318. |/ / / / /
  319. * | | | |
  320. |\ \ \ \ \
  321. | * | | | |
  322. |/| | | | |
  323. | * | | | |
  324. | * | | | |
  325. | |\ \ \ \ \
  326. | | | * \ \ \
  327. | | | |\ \ \ \
  328. | | | | * | | |
  329. | | | |/| | | |
  330. | | | | | |/ /
  331. | | | | |/| |
  332. * | | | | | |
  333. * | | | | | |
  334. * | | | | | |
  335. | | | | * | |
  336. * | | | | | |
  337. | | * | | | |
  338. | |/| | | | |
  339. * | | | | | |
  340. | |/ / / / /
  341. |/| | | | |
  342. | | | | * |
  343. | | | |/ /
  344. | | |/| |
  345. | * | | |
  346. | | | | *
  347. | | * | |
  348. | | |\ \ \
  349. | | | * | |
  350. | | |/| | |
  351. | | | |/ /
  352. | | | * |
  353. | | * | |
  354. | | |\ \ \
  355. | | | * | |
  356. | | |/| | |
  357. | | | |/ /
  358. | | | * |
  359. * | | | |
  360. |\ \ \ \ \
  361. | * \ \ \ \
  362. | |\ \ \ \ \
  363. | | | |/ / /
  364. | | |/| | |
  365. | | | | * |
  366. | | | | * |
  367. * | | | | |
  368. * | | | | |
  369. |/ / / / /
  370. | | | * |
  371. * | | | |
  372. * | | | |
  373. * | | | |
  374. * | | | |
  375. |\ \ \ \ \
  376. | * | | | |
  377. |/| | | | |
  378. | | * | | |
  379. | | |\ \ \ \
  380. | | | * | | |
  381. | | |/| | | |
  382. | |/| | |/ /
  383. | | | |/| |
  384. | | | | | *
  385. | |_|_|_|/
  386. |/| | | |
  387. | | * | |
  388. | |/ / /
  389. * | | |
  390. * | | |
  391. | | * |
  392. * | | |
  393. * | | |
  394. | * | |
  395. | | * |
  396. | * | |
  397. * | | |
  398. |\ \ \ \
  399. | * | | |
  400. |/| | | |
  401. | |/ / /
  402. | * | |
  403. | |\ \ \
  404. | | * | |
  405. | |/| | |
  406. | | |/ /
  407. | | * |
  408. | | |\ \
  409. | | | * |
  410. | | |/| |
  411. * | | | |
  412. * | | | |
  413. |\ \ \ \ \
  414. | * | | | |
  415. |/| | | | |
  416. | | * | | |
  417. | | * | | |
  418. | | * | | |
  419. | |/ / / /
  420. | * | | |
  421. | |\ \ \ \
  422. | | * | | |
  423. | |/| | | |
  424. * | | | | |
  425. * | | | | |
  426. * | | | | |
  427. * | | | | |
  428. * | | | | |
  429. | | | | * |
  430. * | | | | |
  431. |\ \ \ \ \ \
  432. | * | | | | |
  433. |/| | | | | |
  434. | | | | | * |
  435. | | | | |/ /
  436. * | | | | |
  437. |\ \ \ \ \ \
  438. * | | | | | |
  439. * | | | | | |
  440. | | | | * | |
  441. * | | | | | |
  442. * | | | | | |
  443. |\ \ \ \ \ \ \
  444. | | |_|_|/ / /
  445. | |/| | | | |
  446. | | | | * | |
  447. | | | | * | |
  448. | | | | * | |
  449. | | | | * | |
  450. | | | | * | |
  451. | | | | * | |
  452. | | | |/ / /
  453. | | | * | |
  454. | | | * | |
  455. | | | * | |
  456. | | |/| | |
  457. | | | * | |
  458. | | |/| | |
  459. | | | |/ /
  460. | | * | |
  461. | |/| | |
  462. | | | * |
  463. | | |/ /
  464. | | * |
  465. | * | |
  466. | |\ \ \
  467. | * | | |
  468. | | * | |
  469. | |/| | |
  470. | | |/ /
  471. | | * |
  472. | | |\ \
  473. | | * | |
  474. * | | | |
  475. |\| | | |
  476. | * | | |
  477. | * | | |
  478. | * | | |
  479. | | * | |
  480. | * | | |
  481. | |\| | |
  482. | * | | |
  483. | | * | |
  484. | | * | |
  485. | * | | |
  486. | * | | |
  487. | * | | |
  488. | * | | |
  489. | * | | |
  490. | * | | |
  491. | * | | |
  492. | * | | |
  493. | | * | |
  494. | * | | |
  495. | * | | |
  496. | * | | |
  497. | * | | |
  498. | | * | |
  499. * | | | |
  500. |\| | | |
  501. | | * | |
  502. | * | | |
  503. | |\| | |
  504. | | * | |
  505. | | * | |
  506. | | * | |
  507. | | | * |
  508. * | | | |
  509. |\| | | |
  510. | | * | |
  511. | | |/ /
  512. | * | |
  513. | * | |
  514. | |\| |
  515. * | | |
  516. |\| | |
  517. | | * |
  518. | | * |
  519. | | * |
  520. | * | |
  521. | | * |
  522. | * | |
  523. | | * |
  524. | | * |
  525. | | * |
  526. | * | |
  527. | * | |
  528. | * | |
  529. | * | |
  530. | * | |
  531. | * | |
  532. | * | |
  533. * | | |
  534. |\| | |
  535. | * | |
  536. | |\| |
  537. | | * |
  538. | | |\ \
  539. * | | | |
  540. |\| | | |
  541. | * | | |
  542. | |\| | |
  543. | | * | |
  544. | | | * |
  545. | | |/ /
  546. * | | |
  547. * | | |
  548. |\| | |
  549. | * | |
  550. | |\| |
  551. | | * |
  552. | | * |
  553. | | * |
  554. | | | *
  555. * | | |
  556. |\| | |
  557. | * | |
  558. | * | |
  559. | | | *
  560. | | | |\
  561. * | | | |
  562. | |_|_|/
  563. |/| | |
  564. | * | |
  565. | |\| |
  566. | | * |
  567. | | * |
  568. | | * |
  569. | | * |
  570. | | * |
  571. | * | |
  572. * | | |
  573. |\| | |
  574. | * | |
  575. |/| | |
  576. | |/ /
  577. | * |
  578. | |\ \
  579. | * | |
  580. | * | |
  581. * | | |
  582. |\| | |
  583. | | * |
  584. | * | |
  585. | * | |
  586. | * | |
  587. * | | |
  588. |\| | |
  589. | * | |
  590. | * | |
  591. | | * |
  592. | | |\ \
  593. | | |/ /
  594. | |/| |
  595. | * | |
  596. * | | |
  597. |\| | |
  598. | * | |
  599. * | | |
  600. |\| | |
  601. | * | |
  602. | |\ \ \
  603. | * | | |
  604. | * | | |
  605. | | | * |
  606. | * | | |
  607. | * | | |
  608. | | |/ /
  609. | |/| |
  610. | | * |
  611. * | | |
  612. |\| | |
  613. | * | |
  614. | * | |
  615. | * | |
  616. | * | |
  617. | * | |
  618. | |\ \ \
  619. * | | | |
  620. |\| | | |
  621. | * | | |
  622. | * | | |
  623. * | | | |
  624. * | | | |
  625. |\| | | |
  626. | | | | *
  627. | | | | |\
  628. | |_|_|_|/
  629. |/| | | |
  630. | * | | |
  631. * | | | |
  632. * | | | |
  633. |\| | | |
  634. | * | | |
  635. | |\ \ \ \
  636. | | | |/ /
  637. | | |/| |
  638. | * | | |
  639. | * | | |
  640. | * | | |
  641. | * | | |
  642. | | * | |
  643. | | | * |
  644. | | |/ /
  645. | |/| |
  646. * | | |
  647. |\| | |
  648. | * | |
  649. | * | |
  650. | * | |
  651. | * | |
  652. | * | |
  653. * | | |
  654. |\| | |
  655. | * | |
  656. | * | |
  657. * | | |
  658. | * | |
  659. | * | |
  660. | * | |
  661. * | | |
  662. * | | |
  663. * | | |
  664. |\| | |
  665. | * | |
  666. * | | |
  667. * | | |
  668. * | | |
  669. * | | |
  670. | | | *
  671. * | | |
  672. |\| | |
  673. | * | |
  674. | * | |
  675. | * | |
  676. `