mock_repo.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. /*
  2. Copyright 2015 Google Inc. All rights reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package repository
  14. import (
  15. "crypto/sha1"
  16. "encoding/json"
  17. "errors"
  18. "fmt"
  19. "strings"
  20. )
  21. // Constants used for testing.
  22. // We initialize our mock repo with two branches (one of which holds a pending review),
  23. // and commit history that looks like this:
  24. //
  25. // Master Branch: A--B--D--E--F--J
  26. // \ / \ \
  27. // C \ \
  28. // \ \
  29. // Review Branch: G--H--I
  30. //
  31. // Where commits "B" and "D" represent reviews that have been submitted, and "G"
  32. // is a pending review.
  33. const (
  34. TestTargetRef = "refs/heads/master"
  35. TestReviewRef = "refs/heads/ojarjur/mychange"
  36. TestAlternateReviewRef = "refs/review/mychange"
  37. TestRequestsRef = "refs/notes/devtools/reviews"
  38. TestCommentsRef = "refs/notes/devtools/discuss"
  39. TestCommitA = "A"
  40. TestCommitB = "B"
  41. TestCommitC = "C"
  42. TestCommitD = "D"
  43. TestCommitE = "E"
  44. TestCommitF = "F"
  45. TestCommitG = "G"
  46. TestCommitH = "H"
  47. TestCommitI = "I"
  48. TestCommitJ = "J"
  49. TestRequestB = `{"timestamp": "0000000001", "reviewRef": "refs/heads/ojarjur/mychange", "targetRef": "refs/heads/master", "requester": "ojarjur", "reviewers": ["ojarjur"], "description": "B"}`
  50. TestRequestD = `{"timestamp": "0000000002", "reviewRef": "refs/heads/ojarjur/mychange", "targetRef": "refs/heads/master", "requester": "ojarjur", "reviewers": ["ojarjur"], "description": "D"}`
  51. TestRequestG = `{"timestamp": "0000000004", "reviewRef": "refs/heads/ojarjur/mychange", "targetRef": "refs/heads/master", "requester": "ojarjur", "reviewers": ["ojarjur"], "description": "G"}
  52. {"timestamp": "0000000005", "reviewRef": "refs/heads/ojarjur/mychange", "targetRef": "refs/heads/master", "requester": "ojarjur", "reviewers": ["ojarjur"], "description": "Updated description of G"}
  53. {"timestamp": "0000000005", "reviewRef": "refs/heads/ojarjur/mychange", "targetRef": "refs/heads/master", "requester": "ojarjur", "reviewers": ["ojarjur"], "description": "Final description of G"}`
  54. TestDiscussB = `{"timestamp": "0000000001", "author": "ojarjur", "location": {"commit": "B"}, "resolved": true}`
  55. TestDiscussD = `{"timestamp": "0000000003", "author": "ojarjur", "location": {"commit": "E"}, "resolved": true}`
  56. )
  57. type mockCommit struct {
  58. Message string `json:"message,omitempty"`
  59. Time string `json:"time,omitempty"`
  60. Parents []string `json:"parents,omitempty"`
  61. }
  62. // mockRepoForTest defines an instance of Repo that can be used for testing.
  63. type mockRepoForTest struct {
  64. Head string
  65. Refs map[string]string `json:"refs,omitempty"`
  66. Commits map[string]mockCommit `json:"commits,omitempty"`
  67. Notes map[string]map[string]string `json:"notes,omitempty"`
  68. }
  69. func (r *mockRepoForTest) createCommit(message string, time string, parents []string) (string, error) {
  70. newCommit := mockCommit{
  71. Message: message,
  72. Time: time,
  73. Parents: parents,
  74. }
  75. newCommitJSON, err := json.Marshal(newCommit)
  76. if err != nil {
  77. return "", err
  78. }
  79. newCommitHash := fmt.Sprintf("%x", sha1.Sum([]byte(newCommitJSON)))
  80. r.Commits[newCommitHash] = newCommit
  81. return newCommitHash, nil
  82. }
  83. // NewMockRepoForTest returns a mocked-out instance of the Repo interface that has been pre-populated with test data.
  84. func NewMockRepoForTest() Repo {
  85. commitA := mockCommit{
  86. Message: "First commit",
  87. Time: "0",
  88. Parents: nil,
  89. }
  90. commitB := mockCommit{
  91. Message: "Second commit",
  92. Time: "1",
  93. Parents: []string{TestCommitA},
  94. }
  95. commitC := mockCommit{
  96. Message: "No, I'm the second commit",
  97. Time: "1",
  98. Parents: []string{TestCommitA},
  99. }
  100. commitD := mockCommit{
  101. Message: "Fourth commit",
  102. Time: "2",
  103. Parents: []string{TestCommitB, TestCommitC},
  104. }
  105. commitE := mockCommit{
  106. Message: "Fifth commit",
  107. Time: "3",
  108. Parents: []string{TestCommitD},
  109. }
  110. commitF := mockCommit{
  111. Message: "Sixth commit",
  112. Time: "4",
  113. Parents: []string{TestCommitE},
  114. }
  115. commitG := mockCommit{
  116. Message: "No, I'm the sixth commit",
  117. Time: "4",
  118. Parents: []string{TestCommitE},
  119. }
  120. commitH := mockCommit{
  121. Message: "Seventh commit",
  122. Time: "5",
  123. Parents: []string{TestCommitG, TestCommitF},
  124. }
  125. commitI := mockCommit{
  126. Message: "Eighth commit",
  127. Time: "6",
  128. Parents: []string{TestCommitH},
  129. }
  130. commitJ := mockCommit{
  131. Message: "No, I'm the eighth commit",
  132. Time: "6",
  133. Parents: []string{TestCommitF},
  134. }
  135. return &mockRepoForTest{
  136. Head: TestTargetRef,
  137. Refs: map[string]string{
  138. TestTargetRef: TestCommitJ,
  139. TestReviewRef: TestCommitI,
  140. TestAlternateReviewRef: TestCommitI,
  141. },
  142. Commits: map[string]mockCommit{
  143. TestCommitA: commitA,
  144. TestCommitB: commitB,
  145. TestCommitC: commitC,
  146. TestCommitD: commitD,
  147. TestCommitE: commitE,
  148. TestCommitF: commitF,
  149. TestCommitG: commitG,
  150. TestCommitH: commitH,
  151. TestCommitI: commitI,
  152. TestCommitJ: commitJ,
  153. },
  154. Notes: map[string]map[string]string{
  155. TestRequestsRef: map[string]string{
  156. TestCommitB: TestRequestB,
  157. TestCommitD: TestRequestD,
  158. TestCommitG: TestRequestG,
  159. },
  160. TestCommentsRef: map[string]string{
  161. TestCommitB: TestDiscussB,
  162. TestCommitD: TestDiscussD,
  163. },
  164. },
  165. }
  166. }
  167. // GetPath returns the path to the repo.
  168. func (r *mockRepoForTest) GetPath() string { return "~/mockRepo/" }
  169. // GetRepoStateHash returns a hash which embodies the entire current state of a repository.
  170. func (r *mockRepoForTest) GetRepoStateHash() (string, error) {
  171. repoJSON, err := json.Marshal(r)
  172. if err != nil {
  173. return "", err
  174. }
  175. return fmt.Sprintf("%x", sha1.Sum([]byte(repoJSON))), nil
  176. }
  177. // GetUserEmail returns the email address that the user has used to configure git.
  178. func (r *mockRepoForTest) GetUserEmail() (string, error) { return "user@example.com", nil }
  179. // GetUserSigningKey returns the key id the user has configured for
  180. // sigining git artifacts.
  181. func (r *mockRepoForTest) GetUserSigningKey() (string, error) {
  182. return "gpgsig", nil
  183. }
  184. // GetCoreEditor returns the name of the editor that the user has used to configure git.
  185. func (r *mockRepoForTest) GetCoreEditor() (string, error) { return "vi", nil }
  186. // GetSubmitStrategy returns the way in which a review is submitted
  187. func (r *mockRepoForTest) GetSubmitStrategy() (string, error) { return "merge", nil }
  188. // HasUncommittedChanges returns true if there are local, uncommitted changes.
  189. func (r *mockRepoForTest) HasUncommittedChanges() (bool, error) { return false, nil }
  190. func (r *mockRepoForTest) resolveLocalRef(ref string) (string, error) {
  191. if ref == "HEAD" {
  192. ref = r.Head
  193. }
  194. if commit, ok := r.Refs[ref]; ok {
  195. return commit, nil
  196. }
  197. if _, ok := r.Commits[ref]; ok {
  198. return ref, nil
  199. }
  200. return "", fmt.Errorf("The ref %q does not exist", ref)
  201. }
  202. // HasRef checks whether the specified ref exists in the repo.
  203. func (r *mockRepoForTest) HasRef(ref string) (bool, error) {
  204. if _, ok := r.Refs[ref]; !ok {
  205. return false, nil
  206. }
  207. return true, nil
  208. }
  209. // HasObject reports whether or not the repo contains an object with the given hash
  210. func (r *mockRepoForTest) HasObject(hash string) (bool, error) {
  211. return false, errors.New("Not implemented")
  212. }
  213. // VerifyCommit verifies that the supplied hash points to a known commit.
  214. func (r *mockRepoForTest) VerifyCommit(hash string) error {
  215. if _, ok := r.Commits[hash]; !ok {
  216. return fmt.Errorf("The given hash %q is not a known commit", hash)
  217. }
  218. return nil
  219. }
  220. // VerifyGitRef verifies that the supplied ref points to a known commit.
  221. func (r *mockRepoForTest) VerifyGitRef(ref string) error {
  222. _, err := r.resolveLocalRef(ref)
  223. return err
  224. }
  225. // GetHeadRef returns the ref that is the current HEAD.
  226. func (r *mockRepoForTest) GetHeadRef() (string, error) { return r.Head, nil }
  227. // GetCommitHash returns the hash of the commit pointed to by the given ref.
  228. func (r *mockRepoForTest) GetCommitHash(ref string) (string, error) {
  229. err := r.VerifyGitRef(ref)
  230. if err != nil {
  231. return "", err
  232. }
  233. return r.resolveLocalRef(ref)
  234. }
  235. // ResolveRefCommit returns the commit pointed to by the given ref, which may be a remote ref.
  236. //
  237. // This differs from GetCommitHash which only works on exact matches, in that it will try to
  238. // intelligently handle the scenario of a ref not existing locally, but being known to exist
  239. // in a remote repo.
  240. //
  241. // This method should be used when a command may be performed by either the reviewer or the
  242. // reviewee, while GetCommitHash should be used when the encompassing command should only be
  243. // performed by the reviewee.
  244. func (r *mockRepoForTest) ResolveRefCommit(ref string) (string, error) {
  245. if commit, err := r.resolveLocalRef(ref); err == nil {
  246. return commit, err
  247. }
  248. return r.resolveLocalRef(strings.Replace(ref, "refs/heads/", "refs/remotes/origin/", 1))
  249. }
  250. func (r *mockRepoForTest) getCommit(ref string) (mockCommit, error) {
  251. commit, err := r.resolveLocalRef(ref)
  252. return r.Commits[commit], err
  253. }
  254. // GetCommitMessage returns the message stored in the commit pointed to by the given ref.
  255. func (r *mockRepoForTest) GetCommitMessage(ref string) (string, error) {
  256. commit, err := r.getCommit(ref)
  257. if err != nil {
  258. return "", err
  259. }
  260. return commit.Message, nil
  261. }
  262. // GetCommitTime returns the commit time of the commit pointed to by the given ref.
  263. func (r *mockRepoForTest) GetCommitTime(ref string) (string, error) {
  264. commit, err := r.getCommit(ref)
  265. if err != nil {
  266. return "", err
  267. }
  268. return commit.Time, nil
  269. }
  270. // GetLastParent returns the last parent of the given commit (as ordered by git).
  271. func (r *mockRepoForTest) GetLastParent(ref string) (string, error) {
  272. commit, err := r.getCommit(ref)
  273. if len(commit.Parents) > 0 {
  274. return commit.Parents[len(commit.Parents)-1], err
  275. }
  276. return "", err
  277. }
  278. // GetCommitDetails returns the details of a commit's metadata.
  279. func (r *mockRepoForTest) GetCommitDetails(ref string) (*CommitDetails, error) {
  280. commit, err := r.getCommit(ref)
  281. if err != nil {
  282. return nil, err
  283. }
  284. var details CommitDetails
  285. details.Author = "Test Author"
  286. details.AuthorEmail = "author@example.com"
  287. details.Summary = commit.Message
  288. details.Time = commit.Time
  289. details.Parents = commit.Parents
  290. return &details, nil
  291. }
  292. // ancestors returns the breadth-first traversal of a commit's ancestors
  293. func (r *mockRepoForTest) ancestors(commit string) ([]string, error) {
  294. queue := []string{commit}
  295. var ancestors []string
  296. for queue != nil {
  297. var nextQueue []string
  298. for _, c := range queue {
  299. commit, err := r.getCommit(c)
  300. if err != nil {
  301. return nil, err
  302. }
  303. parents := commit.Parents
  304. nextQueue = append(nextQueue, parents...)
  305. ancestors = append(ancestors, parents...)
  306. }
  307. queue = nextQueue
  308. }
  309. return ancestors, nil
  310. }
  311. // IsAncestor determines if the first argument points to a commit that is an ancestor of the second.
  312. func (r *mockRepoForTest) IsAncestor(ancestor, descendant string) (bool, error) {
  313. var err error
  314. ancestor, err = r.resolveLocalRef(ancestor)
  315. if err != nil {
  316. return false, err
  317. }
  318. descendant, err = r.resolveLocalRef(descendant)
  319. if err != nil {
  320. return false, err
  321. }
  322. if ancestor == descendant {
  323. return true, nil
  324. }
  325. descendantCommit, err := r.getCommit(descendant)
  326. if err != nil {
  327. return false, err
  328. }
  329. for _, parent := range descendantCommit.Parents {
  330. if t, e := r.IsAncestor(ancestor, parent); e == nil && t {
  331. return true, nil
  332. }
  333. }
  334. return false, nil
  335. }
  336. // MergeBase determines if the first commit that is an ancestor of the two arguments.
  337. func (r *mockRepoForTest) MergeBase(a, b string) (string, error) {
  338. ancestors, err := r.ancestors(a)
  339. if err != nil {
  340. return "", err
  341. }
  342. for _, ancestor := range ancestors {
  343. if t, e := r.IsAncestor(ancestor, b); e == nil && t {
  344. return ancestor, nil
  345. }
  346. }
  347. return "", nil
  348. }
  349. // Diff computes the diff between two given commits.
  350. func (r *mockRepoForTest) Diff(left, right string, diffArgs ...string) (string, error) {
  351. return fmt.Sprintf("Diff between %q and %q", left, right), nil
  352. }
  353. // Show returns the contents of the given file at the given commit.
  354. func (r *mockRepoForTest) Show(commit, path string) (string, error) {
  355. return fmt.Sprintf("%s:%s", commit, path), nil
  356. }
  357. // SwitchToRef changes the currently-checked-out ref.
  358. func (r *mockRepoForTest) SwitchToRef(ref string) error {
  359. r.Head = ref
  360. return nil
  361. }
  362. // ArchiveRef adds the current commit pointed to by the 'ref' argument
  363. // under the ref specified in the 'archive' argument.
  364. //
  365. // Both the 'ref' and 'archive' arguments are expected to be the fully
  366. // qualified names of git refs (e.g. 'refs/heads/my-change' or
  367. // 'refs/archive/devtools').
  368. //
  369. // If the ref pointed to by the 'archive' argument does not exist
  370. // yet, then it will be created.
  371. func (r *mockRepoForTest) ArchiveRef(ref, archive string) error {
  372. commitToArchive, err := r.resolveLocalRef(ref)
  373. if err != nil {
  374. return err
  375. }
  376. var archiveParents []string
  377. if archiveCommit, err := r.resolveLocalRef(archive); err == nil {
  378. archiveParents = []string{archiveCommit, commitToArchive}
  379. } else {
  380. archiveParents = []string{commitToArchive}
  381. }
  382. archiveCommit, err := r.createCommit("Archiving", "Nowish", archiveParents)
  383. if err != nil {
  384. return err
  385. }
  386. r.Refs[archive] = archiveCommit
  387. return nil
  388. }
  389. // MergeRef merges the given ref into the current one.
  390. //
  391. // The ref argument is the ref to merge, and fastForward indicates that the
  392. // current ref should only move forward, as opposed to creating a bubble merge.
  393. func (r *mockRepoForTest) MergeRef(ref string, fastForward bool, messages ...string) error {
  394. newCommitHash, err := r.resolveLocalRef(ref)
  395. if err != nil {
  396. return err
  397. }
  398. if !fastForward {
  399. origCommit, err := r.resolveLocalRef(r.Head)
  400. if err != nil {
  401. return err
  402. }
  403. newCommit, err := r.getCommit(ref)
  404. if err != nil {
  405. return err
  406. }
  407. message := strings.Join(messages, "\n\n")
  408. time := newCommit.Time
  409. parents := []string{origCommit, newCommitHash}
  410. newCommitHash, err = r.createCommit(message, time, parents)
  411. if err != nil {
  412. return err
  413. }
  414. }
  415. r.Refs[r.Head] = newCommitHash
  416. return nil
  417. }
  418. // MergeAndSignRef merges the given ref into the current one and signs the
  419. // merge.
  420. //
  421. // The ref argument is the ref to merge, and fastForward indicates that the
  422. // current ref should only move forward, as opposed to creating a bubble merge.
  423. func (r *mockRepoForTest) MergeAndSignRef(ref string, fastForward bool,
  424. messages ...string) error {
  425. return nil
  426. }
  427. // RebaseRef rebases the current ref onto the given one.
  428. func (r *mockRepoForTest) RebaseRef(ref string) error {
  429. parentHash := r.Refs[ref]
  430. origCommit, err := r.getCommit(r.Head)
  431. if err != nil {
  432. return err
  433. }
  434. newCommitHash, err := r.createCommit(origCommit.Message, origCommit.Time, []string{parentHash})
  435. if err != nil {
  436. return err
  437. }
  438. if strings.HasPrefix(r.Head, "refs/heads/") {
  439. r.Refs[r.Head] = newCommitHash
  440. } else {
  441. // The current head is not a branch, so updating
  442. // it should leave us in a detached-head state.
  443. r.Head = newCommitHash
  444. }
  445. return nil
  446. }
  447. // RebaseAndSignRef rebases the current ref onto the given one and signs the
  448. // result.
  449. func (r *mockRepoForTest) RebaseAndSignRef(ref string) error { return nil }
  450. // ListCommits returns the list of commits reachable from the given ref.
  451. //
  452. // The generated list is in chronological order (with the oldest commit first).
  453. //
  454. // If the specified ref does not exist, then this method returns an empty result.
  455. func (r *mockRepoForTest) ListCommits(ref string) []string { return nil }
  456. // ListCommitsBetween returns the list of commits between the two given revisions.
  457. //
  458. // The "from" parameter is the starting point (exclusive), and the "to"
  459. // parameter is the ending point (inclusive).
  460. //
  461. // The "from" commit does not need to be an ancestor of the "to" commit. If it
  462. // is not, then the merge base of the two is used as the starting point.
  463. // Admittedly, this makes calling these the "between" commits is a bit of a
  464. // misnomer, but it also makes the method easier to use when you want to
  465. // generate the list of changes in a feature branch, as it eliminates the need
  466. // to explicitly calculate the merge base. This also makes the semantics of the
  467. // method compatible with git's built-in "rev-list" command.
  468. //
  469. // The generated list is in chronological order (with the oldest commit first).
  470. func (r *mockRepoForTest) ListCommitsBetween(from, to string) ([]string, error) {
  471. commits := []string{to}
  472. potentialCommits, _ := r.ancestors(to)
  473. for _, commit := range potentialCommits {
  474. blocked, err := r.IsAncestor(commit, from)
  475. if err != nil {
  476. return nil, err
  477. }
  478. if !blocked {
  479. commits = append(commits, commit)
  480. }
  481. }
  482. return commits, nil
  483. }
  484. // StoreBlob writes the given file to the repository and returns its hash.
  485. func (r *mockRepoForTest) StoreBlob(contents string) (string, error) {
  486. return "", fmt.Errorf("not implemented")
  487. }
  488. // StoreTree writes the given file tree to the repository and returns its hash.
  489. func (r *mockRepoForTest) StoreTree(contents map[string]TreeChild) (string, error) {
  490. return "", fmt.Errorf("not implemented")
  491. }
  492. // ReadTree reads the file tree pointed to by the given ref or hash from the repository.
  493. func (r *mockRepoForTest) ReadTree(ref string) (*Tree, error) {
  494. return nil, fmt.Errorf("not implemented")
  495. }
  496. // CreateCommit creates a commit object and returns its hash.
  497. func (r *mockRepoForTest) CreateCommit(details *CommitDetails) (string, error) {
  498. return "", fmt.Errorf("not implemented")
  499. }
  500. // CreateCommitWithTree creates a commit object with the given tree and returns its hash.
  501. func (r *mockRepoForTest) CreateCommitWithTree(details *CommitDetails, t *Tree) (string, error) {
  502. return "", fmt.Errorf("not implemented")
  503. }
  504. // SetRef sets the commit pointed to by the specified ref to `newCommitHash`,
  505. // iff the ref currently points `previousCommitHash`.
  506. func (r *mockRepoForTest) SetRef(ref, newCommitHash, previousCommitHash string) error {
  507. return fmt.Errorf("not implemented")
  508. }
  509. // GetNotes reads the notes from the given ref that annotate the given revision.
  510. func (r *mockRepoForTest) GetNotes(notesRef, revision string) []Note {
  511. notesText := r.Notes[notesRef][revision]
  512. var notes []Note
  513. for _, line := range strings.Split(notesText, "\n") {
  514. notes = append(notes, Note(line))
  515. }
  516. return notes
  517. }
  518. // GetAllNotes reads the contents of the notes under the given ref for every commit.
  519. //
  520. // The returned value is a mapping from commit hash to the list of notes for that commit.
  521. //
  522. // This is the batch version of the corresponding GetNotes(...) method.
  523. func (r *mockRepoForTest) GetAllNotes(notesRef string) (map[string][]Note, error) {
  524. notesMap := make(map[string][]Note)
  525. for _, commit := range r.ListNotedRevisions(notesRef) {
  526. notesMap[commit] = r.GetNotes(notesRef, commit)
  527. }
  528. return notesMap, nil
  529. }
  530. // AppendNote appends a note to a revision under the given ref.
  531. func (r *mockRepoForTest) AppendNote(ref, revision string, note Note) error {
  532. existingNotes := r.Notes[ref][revision]
  533. newNotes := existingNotes + "\n" + string(note)
  534. r.Notes[ref][revision] = newNotes
  535. return nil
  536. }
  537. // ListNotedRevisions returns the collection of revisions that are annotated by notes in the given ref.
  538. func (r *mockRepoForTest) ListNotedRevisions(notesRef string) []string {
  539. var revisions []string
  540. for revision := range r.Notes[notesRef] {
  541. if _, ok := r.Commits[revision]; ok {
  542. revisions = append(revisions, revision)
  543. }
  544. }
  545. return revisions
  546. }
  547. // Remotes returns a list of the remotes.
  548. func (r *mockRepoForTest) Remotes() ([]string, error) {
  549. return []string{"origin"}, nil
  550. }
  551. // Fetch fetches from the given remote using the supplied refspecs.
  552. func (r *mockRepoForTest) Fetch(remote string, refspecs ...string) error { return nil }
  553. // PushNotes pushes git notes to a remote repo.
  554. func (r *mockRepoForTest) PushNotes(remote, notesRefPattern string) error { return nil }
  555. // PullNotes fetches the contents of the given notes ref from a remote repo,
  556. // and then merges them with the corresponding local notes using the
  557. // "cat_sort_uniq" strategy.
  558. func (r *mockRepoForTest) PullNotes(remote, notesRefPattern string) error { return nil }
  559. // PushNotesAndArchive pushes the given notes and archive refs to a remote repo.
  560. func (r *mockRepoForTest) PushNotesAndArchive(remote, notesRefPattern, archiveRefPattern string) error {
  561. return nil
  562. }
  563. // PullNotesAndArchive fetches the contents of the notes and archives refs from
  564. // a remote repo, and merges them with the corresponding local refs.
  565. //
  566. // For notes refs, we assume that every note can be automatically merged using
  567. // the 'cat_sort_uniq' strategy (the git-appraise schemas fit that requirement),
  568. // so we automatically merge the remote notes into the local notes.
  569. //
  570. // For "archive" refs, they are expected to be used solely for maintaining
  571. // reachability of commits that are part of the history of any reviews,
  572. // so we do not maintain any consistency with their tree objects. Instead,
  573. // we merely ensure that their history graph includes every commit that we
  574. // intend to keep.
  575. func (r *mockRepoForTest) PullNotesAndArchive(remote, notesRefPattern, archiveRefPattern string) error {
  576. return nil
  577. }
  578. // MergeNotes merges in the remote's state of the archives reference into
  579. // the local repository's.
  580. func (r *mockRepoForTest) MergeNotes(remote, notesRefPattern string) error {
  581. return nil
  582. }
  583. // MergeArchives merges in the remote's state of the archives reference into
  584. // the local repository's.
  585. func (r *mockRepoForTest) MergeArchives(remote, archiveRefPattern string) error {
  586. return nil
  587. }
  588. // FetchAndReturnNewReviewHashes fetches the notes "branches" and then susses
  589. // out the IDs (the revision the review points to) of any new reviews, then
  590. // returns that list of IDs.
  591. //
  592. // This is accomplished by determining which files in the notes tree have
  593. // changed because the _names_ of these files correspond to the revisions they
  594. // point to.
  595. func (r *mockRepoForTest) FetchAndReturnNewReviewHashes(remote, notesRefPattern string, devtoolsRefPatterns ...string) ([]string, error) {
  596. return nil, nil
  597. }
  598. // Push pushes the given refs to a remote repo.
  599. func (r *mockRepoForTest) Push(remote string, refPattern ...string) error {
  600. return nil
  601. }