git.go 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200
  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 contains helper methods for working with the Git repo.
  14. package repository
  15. import (
  16. "bufio"
  17. "bytes"
  18. "crypto/sha1"
  19. "encoding/json"
  20. "fmt"
  21. "io"
  22. "os"
  23. "os/exec"
  24. "sort"
  25. "strconv"
  26. "strings"
  27. )
  28. const (
  29. branchRefPrefix = "refs/heads/"
  30. notesRefPrefix = "refs/notes/"
  31. devtoolsRefPrefix = "refs/devtools/"
  32. remoteDevtoolsRefPrefix = "refs/remoteDevtools/"
  33. )
  34. // GitRepo represents an instance of a (local) git repository.
  35. type GitRepo struct {
  36. Path string
  37. }
  38. // Run the given git command with the given I/O reader/writers and environment, returning an error if it fails.
  39. func (repo *GitRepo) runGitCommandWithIOAndEnv(stdin io.Reader, stdout, stderr io.Writer, env []string, args ...string) error {
  40. cmd := exec.Command("git", args...)
  41. cmd.Dir = repo.Path
  42. cmd.Stdin = stdin
  43. cmd.Stdout = stdout
  44. cmd.Stderr = stderr
  45. cmd.Env = env
  46. return cmd.Run()
  47. }
  48. // Run the given git command with the given I/O reader/writers, returning an error if it fails.
  49. func (repo *GitRepo) runGitCommandWithIO(stdin io.Reader, stdout, stderr io.Writer, args ...string) error {
  50. return repo.runGitCommandWithIOAndEnv(stdin, stdout, stderr, nil, args...)
  51. }
  52. // Run the given git command and return its stdout, or an error if the command fails.
  53. func (repo *GitRepo) runGitCommandRaw(args ...string) (string, string, error) {
  54. var stdout bytes.Buffer
  55. var stderr bytes.Buffer
  56. err := repo.runGitCommandWithIO(nil, &stdout, &stderr, args...)
  57. return strings.TrimSpace(stdout.String()), strings.TrimSpace(stderr.String()), err
  58. }
  59. // Run the given git command and return its stdout, or an error if the command fails.
  60. func (repo *GitRepo) runGitCommand(args ...string) (string, error) {
  61. stdout, stderr, err := repo.runGitCommandRaw(args...)
  62. if err != nil {
  63. if stderr == "" {
  64. stderr = "Error running git command: " + strings.Join(args, " ")
  65. }
  66. err = fmt.Errorf(stderr)
  67. }
  68. return stdout, err
  69. }
  70. // Run the given git command and return its stdout, or an error if the command fails.
  71. func (repo *GitRepo) runGitCommandWithEnv(env []string, args ...string) (string, error) {
  72. var stdout bytes.Buffer
  73. var stderr bytes.Buffer
  74. err := repo.runGitCommandWithIOAndEnv(nil, &stdout, &stderr, env, args...)
  75. if err != nil {
  76. stderrStr := strings.TrimSpace(stderr.String())
  77. if stderrStr == "" {
  78. stderrStr = "Error running git command: " + strings.Join(args, " ")
  79. }
  80. err = fmt.Errorf(stderrStr)
  81. }
  82. return strings.TrimSpace(stdout.String()), err
  83. }
  84. // Run the given git command using the same stdin, stdout, and stderr as the review tool.
  85. func (repo *GitRepo) runGitCommandInline(args ...string) error {
  86. return repo.runGitCommandWithIO(os.Stdin, os.Stdout, os.Stderr, args...)
  87. }
  88. // NewGitRepo determines if the given working directory is inside of a git repository,
  89. // and returns the corresponding GitRepo instance if it is.
  90. func NewGitRepo(path string) (*GitRepo, error) {
  91. repo := &GitRepo{Path: path}
  92. _, _, err := repo.runGitCommandRaw("rev-parse")
  93. if err == nil {
  94. return repo, nil
  95. }
  96. if _, ok := err.(*exec.ExitError); ok {
  97. return nil, err
  98. }
  99. return nil, err
  100. }
  101. func (repo *GitRepo) HasRef(ref string) (bool, error) {
  102. _, _, err := repo.runGitCommandRaw("show-ref", "--verify", "--quiet", ref)
  103. if err == nil {
  104. return true, nil
  105. }
  106. if _, ok := err.(*exec.ExitError); ok {
  107. return false, nil
  108. }
  109. // Got an unexpected error
  110. return false, err
  111. }
  112. // HasObject returns whether or not the repo contains an object with the given hash.
  113. func (repo *GitRepo) HasObject(hash string) (bool, error) {
  114. _, err := repo.runGitCommand("cat-file", "-e", hash)
  115. if err == nil {
  116. // We verified the object exists
  117. return true, nil
  118. }
  119. if _, ok := err.(*exec.ExitError); ok {
  120. return false, nil
  121. }
  122. // Got an unexpected error
  123. return false, err
  124. }
  125. // GetPath returns the path to the repo.
  126. func (repo *GitRepo) GetPath() string {
  127. return repo.Path
  128. }
  129. // GetRepoStateHash returns a hash which embodies the entire current state of a repository.
  130. func (repo *GitRepo) GetRepoStateHash() (string, error) {
  131. stateSummary, error := repo.runGitCommand("show-ref")
  132. return fmt.Sprintf("%x", sha1.Sum([]byte(stateSummary))), error
  133. }
  134. // GetUserEmail returns the email address that the user has used to configure git.
  135. func (repo *GitRepo) GetUserEmail() (string, error) {
  136. return repo.runGitCommand("config", "user.email")
  137. }
  138. // GetUserSigningKey returns the key id the user has configured for
  139. // sigining git artifacts.
  140. func (repo *GitRepo) GetUserSigningKey() (string, error) {
  141. return repo.runGitCommand("config", "user.signingKey")
  142. }
  143. // GetCoreEditor returns the name of the editor that the user has used to configure git.
  144. func (repo *GitRepo) GetCoreEditor() (string, error) {
  145. return repo.runGitCommand("var", "GIT_EDITOR")
  146. }
  147. // GetSubmitStrategy returns the way in which a review is submitted
  148. func (repo *GitRepo) GetSubmitStrategy() (string, error) {
  149. submitStrategy, _ := repo.runGitCommand("config", "appraise.submit")
  150. return submitStrategy, nil
  151. }
  152. // HasUncommittedChanges returns true if there are local, uncommitted changes.
  153. func (repo *GitRepo) HasUncommittedChanges() (bool, error) {
  154. out, err := repo.runGitCommand("status", "--porcelain")
  155. if err != nil {
  156. return false, err
  157. }
  158. if len(out) > 0 {
  159. return true, nil
  160. }
  161. return false, nil
  162. }
  163. // VerifyCommit verifies that the supplied hash points to a known commit.
  164. func (repo *GitRepo) VerifyCommit(hash string) error {
  165. out, err := repo.runGitCommand("cat-file", "-t", hash)
  166. if err != nil {
  167. return err
  168. }
  169. objectType := strings.TrimSpace(string(out))
  170. if objectType != "commit" {
  171. return fmt.Errorf("Hash %q points to a non-commit object of type %q", hash, objectType)
  172. }
  173. return nil
  174. }
  175. // VerifyGitRef verifies that the supplied ref points to a known commit.
  176. func (repo *GitRepo) VerifyGitRef(ref string) error {
  177. _, err := repo.runGitCommand("show-ref", "--verify", ref)
  178. return err
  179. }
  180. // GetHeadRef returns the ref that is the current HEAD.
  181. func (repo *GitRepo) GetHeadRef() (string, error) {
  182. return repo.runGitCommand("symbolic-ref", "HEAD")
  183. }
  184. // GetCommitHash returns the hash of the commit pointed to by the given ref.
  185. func (repo *GitRepo) GetCommitHash(ref string) (string, error) {
  186. return repo.runGitCommand("show", "-s", "--format=%H", ref)
  187. }
  188. // ResolveRefCommit returns the commit pointed to by the given ref, which may be a remote ref.
  189. //
  190. // This differs from GetCommitHash which only works on exact matches, in that it will try to
  191. // intelligently handle the scenario of a ref not existing locally, but being known to exist
  192. // in a remote repo.
  193. //
  194. // This method should be used when a command may be performed by either the reviewer or the
  195. // reviewee, while GetCommitHash should be used when the encompassing command should only be
  196. // performed by the reviewee.
  197. func (repo *GitRepo) ResolveRefCommit(ref string) (string, error) {
  198. if err := repo.VerifyGitRef(ref); err == nil {
  199. return repo.GetCommitHash(ref)
  200. }
  201. if strings.HasPrefix(ref, "refs/heads/") {
  202. // The ref is a branch. Check if it exists in exactly one remote
  203. pattern := strings.Replace(ref, "refs/heads", "**", 1)
  204. matchingOutput, err := repo.runGitCommand("for-each-ref", "--format=%(refname)", pattern)
  205. if err != nil {
  206. return "", err
  207. }
  208. matchingRefs := strings.Split(matchingOutput, "\n")
  209. if len(matchingRefs) == 1 && matchingRefs[0] != "" {
  210. // There is exactly one match
  211. return repo.GetCommitHash(matchingRefs[0])
  212. }
  213. return "", fmt.Errorf("Unable to find a git ref matching the pattern %q", pattern)
  214. }
  215. return "", fmt.Errorf("Unknown git ref %q", ref)
  216. }
  217. // GetCommitMessage returns the message stored in the commit pointed to by the given ref.
  218. func (repo *GitRepo) GetCommitMessage(ref string) (string, error) {
  219. return repo.runGitCommand("show", "-s", "--format=%B", ref)
  220. }
  221. // GetCommitTime returns the commit time of the commit pointed to by the given ref.
  222. func (repo *GitRepo) GetCommitTime(ref string) (string, error) {
  223. return repo.runGitCommand("show", "-s", "--format=%ct", ref)
  224. }
  225. // GetLastParent returns the last parent of the given commit (as ordered by git).
  226. func (repo *GitRepo) GetLastParent(ref string) (string, error) {
  227. return repo.runGitCommand("rev-list", "--skip", "1", "-n", "1", ref)
  228. }
  229. // GetCommitDetails returns the details of a commit's metadata.
  230. func (repo GitRepo) GetCommitDetails(ref string) (*CommitDetails, error) {
  231. var err error
  232. show := func(formatString string) (result string) {
  233. if err != nil {
  234. return ""
  235. }
  236. result, err = repo.runGitCommand("show", "-s", ref, fmt.Sprintf("--format=tformat:%s", formatString))
  237. return result
  238. }
  239. jsonFormatString := "{\"tree\":\"%T\", \"time\": \"%at\"}"
  240. detailsJSON := show(jsonFormatString)
  241. if err != nil {
  242. return nil, err
  243. }
  244. var details CommitDetails
  245. err = json.Unmarshal([]byte(detailsJSON), &details)
  246. if err != nil {
  247. return nil, err
  248. }
  249. details.Author = show("%an")
  250. details.AuthorEmail = show("%ae")
  251. details.Committer = show("%cn")
  252. details.CommitterEmail = show("%ce")
  253. details.Summary = show("%s")
  254. parentsString := show("%P")
  255. details.Parents = strings.Split(parentsString, " ")
  256. if err != nil {
  257. return nil, err
  258. }
  259. return &details, nil
  260. }
  261. // MergeBase determines if the first commit that is an ancestor of the two arguments.
  262. func (repo *GitRepo) MergeBase(a, b string) (string, error) {
  263. return repo.runGitCommand("merge-base", a, b)
  264. }
  265. // IsAncestor determines if the first argument points to a commit that is an ancestor of the second.
  266. func (repo *GitRepo) IsAncestor(ancestor, descendant string) (bool, error) {
  267. _, _, err := repo.runGitCommandRaw("merge-base", "--is-ancestor", ancestor, descendant)
  268. if err == nil {
  269. return true, nil
  270. }
  271. if _, ok := err.(*exec.ExitError); ok {
  272. return false, nil
  273. }
  274. return false, fmt.Errorf("Error while trying to determine commit ancestry: %v", err)
  275. }
  276. // Diff computes the diff between two given commits.
  277. func (repo *GitRepo) Diff(left, right string, diffArgs ...string) (string, error) {
  278. args := []string{"diff"}
  279. args = append(args, diffArgs...)
  280. args = append(args, fmt.Sprintf("%s..%s", left, right))
  281. return repo.runGitCommand(args...)
  282. }
  283. // Show returns the contents of the given file at the given commit.
  284. func (repo *GitRepo) Show(commit, path string) (string, error) {
  285. return repo.runGitCommand("show", fmt.Sprintf("%s:%s", commit, path))
  286. }
  287. // SwitchToRef changes the currently-checked-out ref.
  288. func (repo *GitRepo) SwitchToRef(ref string) error {
  289. // If the ref starts with "refs/heads/", then we have to trim that prefix,
  290. // or else we will wind up in a detached HEAD state.
  291. if strings.HasPrefix(ref, branchRefPrefix) {
  292. ref = ref[len(branchRefPrefix):]
  293. }
  294. _, err := repo.runGitCommand("checkout", ref)
  295. return err
  296. }
  297. // mergeArchives merges two archive refs.
  298. func (repo *GitRepo) mergeArchives(archive, remoteArchive string) error {
  299. hasRemote, err := repo.HasRef(remoteArchive)
  300. if err != nil {
  301. return err
  302. }
  303. if !hasRemote {
  304. // The remote archive does not exist, so we have nothing to do
  305. return nil
  306. }
  307. remoteHash, err := repo.GetCommitHash(remoteArchive)
  308. if err != nil {
  309. return err
  310. }
  311. hasLocal, err := repo.HasRef(archive)
  312. if err != nil {
  313. return err
  314. }
  315. if !hasLocal {
  316. // The local archive does not exist, so we merely need to set it
  317. _, err := repo.runGitCommand("update-ref", archive, remoteHash)
  318. return err
  319. }
  320. archiveHash, err := repo.GetCommitHash(archive)
  321. if err != nil {
  322. return err
  323. }
  324. isAncestor, err := repo.IsAncestor(archiveHash, remoteHash)
  325. if err != nil {
  326. return err
  327. }
  328. if isAncestor {
  329. // The archive can simply be fast-forwarded
  330. _, err := repo.runGitCommand("update-ref", archive, remoteHash, archiveHash)
  331. return err
  332. }
  333. // Create a merge commit of the two archives
  334. refDetails, err := repo.GetCommitDetails(remoteArchive)
  335. if err != nil {
  336. return err
  337. }
  338. newArchiveHash, err := repo.runGitCommand("commit-tree", "-p", remoteHash, "-p", archiveHash, "-m", "Merge local and remote archives", refDetails.Tree)
  339. if err != nil {
  340. return err
  341. }
  342. newArchiveHash = strings.TrimSpace(newArchiveHash)
  343. _, err = repo.runGitCommand("update-ref", archive, newArchiveHash, archiveHash)
  344. return err
  345. }
  346. // ArchiveRef adds the current commit pointed to by the 'ref' argument
  347. // under the ref specified in the 'archive' argument.
  348. //
  349. // Both the 'ref' and 'archive' arguments are expected to be the fully
  350. // qualified names of git refs (e.g. 'refs/heads/my-change' or
  351. // 'refs/devtools/archives/reviews').
  352. //
  353. // If the ref pointed to by the 'archive' argument does not exist
  354. // yet, then it will be created.
  355. func (repo *GitRepo) ArchiveRef(ref, archive string) error {
  356. refHash, err := repo.GetCommitHash(ref)
  357. if err != nil {
  358. return err
  359. }
  360. refDetails, err := repo.GetCommitDetails(ref)
  361. if err != nil {
  362. return err
  363. }
  364. commitTreeArgs := []string{"commit-tree"}
  365. archiveHash, err := repo.GetCommitHash(archive)
  366. if err != nil {
  367. archiveHash = ""
  368. } else {
  369. commitTreeArgs = append(commitTreeArgs, "-p", archiveHash)
  370. }
  371. commitTreeArgs = append(commitTreeArgs, "-p", refHash, "-m", fmt.Sprintf("Archive %s", refHash), refDetails.Tree)
  372. newArchiveHash, err := repo.runGitCommand(commitTreeArgs...)
  373. if err != nil {
  374. return err
  375. }
  376. newArchiveHash = strings.TrimSpace(newArchiveHash)
  377. updateRefArgs := []string{"update-ref", archive, newArchiveHash}
  378. if archiveHash != "" {
  379. updateRefArgs = append(updateRefArgs, archiveHash)
  380. }
  381. _, err = repo.runGitCommand(updateRefArgs...)
  382. return err
  383. }
  384. // MergeRef merges the given ref into the current one.
  385. //
  386. // The ref argument is the ref to merge, and fastForward indicates that the
  387. // current ref should only move forward, as opposed to creating a bubble merge.
  388. // The messages argument(s) provide text that should be included in the default
  389. // merge commit message (separated by blank lines).
  390. func (repo *GitRepo) MergeRef(ref string, fastForward bool, messages ...string) error {
  391. args := []string{"merge"}
  392. if fastForward {
  393. args = append(args, "--ff", "--ff-only")
  394. } else {
  395. args = append(args, "--no-ff")
  396. }
  397. if len(messages) > 0 {
  398. commitMessage := strings.Join(messages, "\n\n")
  399. args = append(args, "-e", "-m", commitMessage)
  400. }
  401. args = append(args, ref)
  402. return repo.runGitCommandInline(args...)
  403. }
  404. // MergeAndSignRef merges the given ref into the current one and signs the
  405. // merge.
  406. //
  407. // The ref argument is the ref to merge, and fastForward indicates that the
  408. // current ref should only move forward, as opposed to creating a bubble merge.
  409. // The messages argument(s) provide text that should be included in the default
  410. // merge commit message (separated by blank lines).
  411. func (repo *GitRepo) MergeAndSignRef(ref string, fastForward bool,
  412. messages ...string) error {
  413. args := []string{"merge"}
  414. if fastForward {
  415. args = append(args, "--ff", "--ff-only", "-S")
  416. } else {
  417. args = append(args, "--no-ff", "-S")
  418. }
  419. if len(messages) > 0 {
  420. commitMessage := strings.Join(messages, "\n\n")
  421. args = append(args, "-e", "-m", commitMessage)
  422. }
  423. args = append(args, ref)
  424. return repo.runGitCommandInline(args...)
  425. }
  426. // RebaseRef rebases the current ref onto the given one.
  427. func (repo *GitRepo) RebaseRef(ref string) error {
  428. return repo.runGitCommandInline("rebase", "-i", ref)
  429. }
  430. // RebaseAndSignRef rebases the current ref onto the given one and signs the
  431. // result.
  432. func (repo *GitRepo) RebaseAndSignRef(ref string) error {
  433. return repo.runGitCommandInline("rebase", "-S", "-i", ref)
  434. }
  435. // ListCommits returns the list of commits reachable from the given ref.
  436. //
  437. // The generated list is in chronological order (with the oldest commit first).
  438. //
  439. // If the specified ref does not exist, then this method returns an empty result.
  440. func (repo *GitRepo) ListCommits(ref string) []string {
  441. var stdout bytes.Buffer
  442. var stderr bytes.Buffer
  443. if err := repo.runGitCommandWithIO(nil, &stdout, &stderr, "rev-list", "--reverse", ref); err != nil {
  444. return nil
  445. }
  446. byteLines := bytes.Split(stdout.Bytes(), []byte("\n"))
  447. var commits []string
  448. for _, byteLine := range byteLines {
  449. commits = append(commits, string(byteLine))
  450. }
  451. return commits
  452. }
  453. // ListCommitsBetween returns the list of commits between the two given revisions.
  454. //
  455. // The "from" parameter is the starting point (exclusive), and the "to"
  456. // parameter is the ending point (inclusive).
  457. //
  458. // The "from" commit does not need to be an ancestor of the "to" commit. If it
  459. // is not, then the merge base of the two is used as the starting point.
  460. // Admittedly, this makes calling these the "between" commits is a bit of a
  461. // misnomer, but it also makes the method easier to use when you want to
  462. // generate the list of changes in a feature branch, as it eliminates the need
  463. // to explicitly calculate the merge base. This also makes the semantics of the
  464. // method compatible with git's built-in "rev-list" command.
  465. //
  466. // The generated list is in chronological order (with the oldest commit first).
  467. func (repo *GitRepo) ListCommitsBetween(from, to string) ([]string, error) {
  468. out, err := repo.runGitCommand("rev-list", "--reverse", from+".."+to)
  469. if err != nil {
  470. return nil, err
  471. }
  472. if out == "" {
  473. return nil, nil
  474. }
  475. return strings.Split(out, "\n"), nil
  476. }
  477. // StoreBlob writes the given file to the repository and returns its hash.
  478. func (repo *GitRepo) StoreBlob(contents string) (string, error) {
  479. stdin := strings.NewReader(contents)
  480. var stdout bytes.Buffer
  481. var stderr bytes.Buffer
  482. args := []string{"hash-object", "-w", "-t", "blob", "--stdin"}
  483. err := repo.runGitCommandWithIO(stdin, &stdout, &stderr, args...)
  484. if err != nil {
  485. message := strings.TrimSpace(stderr.String())
  486. return "", fmt.Errorf("failure storing a git blob, %v: %q", err, message)
  487. }
  488. return strings.TrimSpace(stdout.String()), nil
  489. }
  490. // StoreTree writes the given file tree to the repository and returns its hash.
  491. func (repo *GitRepo) StoreTree(contents map[string]TreeChild) (string, error) {
  492. var lines []string
  493. for path, obj := range contents {
  494. objHash, err := obj.Store(repo)
  495. if err != nil {
  496. return "", err
  497. }
  498. mode := "040000"
  499. if obj.Type() == "blob" {
  500. mode = "100644"
  501. }
  502. line := fmt.Sprintf("%s %s %s\t%s", mode, obj.Type(), objHash, path)
  503. lines = append(lines, line)
  504. }
  505. stdin := strings.NewReader(strings.Join(lines, "\n"))
  506. var stdout bytes.Buffer
  507. var stderr bytes.Buffer
  508. args := []string{"mktree"}
  509. err := repo.runGitCommandWithIO(stdin, &stdout, &stderr, args...)
  510. if err != nil {
  511. message := strings.TrimSpace(stderr.String())
  512. return "", fmt.Errorf("failure storing a git tree, %v: %q", err, message)
  513. }
  514. return strings.TrimSpace(stdout.String()), nil
  515. }
  516. func (repo *GitRepo) readBlob(objHash string) (*Blob, error) {
  517. out, err := repo.runGitCommand("cat-file", "-p", objHash)
  518. if err != nil {
  519. return nil, fmt.Errorf("failure reading the file contents of %q: %v", objHash, err)
  520. }
  521. return &Blob{contents: out, savedHashes: map[Repo]string{repo: objHash}}, nil
  522. }
  523. func (repo *GitRepo) ReadTree(ref string) (*Tree, error) {
  524. return repo.readTreeWithHash(ref, "")
  525. }
  526. func (repo *GitRepo) readTreeWithHash(ref, hash string) (*Tree, error) {
  527. out, err := repo.runGitCommand("ls-tree", "--full-tree", ref)
  528. if err != nil {
  529. return nil, fmt.Errorf("failure listing the file contents of %q: %v", ref, err)
  530. }
  531. contents := make(map[string]TreeChild)
  532. if len(out) == 0 {
  533. // This is possible if the tree is empty
  534. return NewTree(contents), nil
  535. }
  536. for _, line := range strings.Split(out, "\n") {
  537. lineParts := strings.Split(line, "\t")
  538. if len(lineParts) != 2 {
  539. return nil, fmt.Errorf("malformed ls-tree output line: %q", line)
  540. }
  541. path := lineParts[1]
  542. lineParts = strings.Split(lineParts[0], " ")
  543. if len(lineParts) != 3 {
  544. return nil, fmt.Errorf("malformed ls-tree output line: %q", line)
  545. }
  546. objType := lineParts[1]
  547. objHash := lineParts[2]
  548. var child TreeChild
  549. if objType == "tree" {
  550. child, err = repo.readTreeWithHash(objHash, objHash)
  551. } else if objType == "blob" {
  552. child, err = repo.readBlob(objHash)
  553. } else {
  554. return nil, fmt.Errorf("unrecognized tree object type: %q", objType)
  555. }
  556. if err != nil {
  557. return nil, fmt.Errorf("failed to read a tree child object: %v", err)
  558. }
  559. contents[path] = child
  560. }
  561. t := NewTree(contents)
  562. t.savedHashes[repo] = hash
  563. return t, nil
  564. }
  565. // CreateCommit creates a commit object and returns its hash.
  566. func (repo *GitRepo) CreateCommit(details *CommitDetails) (string, error) {
  567. args := []string{"commit-tree", details.Tree, "-m", details.Summary}
  568. for _, parent := range details.Parents {
  569. args = append(args, "-p", parent)
  570. }
  571. var env []string
  572. if details.Author != "" {
  573. env = append(env, fmt.Sprintf("GIT_AUTHOR_NAME=%s", details.Author))
  574. }
  575. if details.AuthorEmail != "" {
  576. env = append(env, fmt.Sprintf("GIT_AUTHOR_EMAIL=%s", details.AuthorEmail))
  577. }
  578. if details.AuthorTime != "" {
  579. env = append(env, fmt.Sprintf("GIT_AUTHOR_DATE=%s", details.AuthorTime))
  580. }
  581. if details.Committer != "" {
  582. env = append(env, fmt.Sprintf("GIT_COMMITTER_NAME=%s", details.Committer))
  583. }
  584. if details.CommitterEmail != "" {
  585. env = append(env, fmt.Sprintf("GIT_COMMITTER_EMAIL=%s", details.CommitterEmail))
  586. }
  587. if details.Time != "" {
  588. env = append(env, fmt.Sprintf("GIT_COMMITTER_DATE=%s", details.Time))
  589. }
  590. return repo.runGitCommandWithEnv(env, args...)
  591. }
  592. // CreateCommitWithTree creates a commit object with the given tree and returns its hash.
  593. func (repo *GitRepo) CreateCommitWithTree(details *CommitDetails, t *Tree) (string, error) {
  594. treeHash, err := repo.StoreTree(t.Contents())
  595. if err != nil {
  596. return "", fmt.Errorf("failure storing a tree: %v", err)
  597. }
  598. details.Tree = treeHash
  599. return repo.CreateCommit(details)
  600. }
  601. // SetRef sets the commit pointed to by the specified ref to `newCommitHash`,
  602. // iff the ref currently points `previousCommitHash`.
  603. func (repo *GitRepo) SetRef(ref, newCommitHash, previousCommitHash string) error {
  604. args := []string{"update-ref", ref, newCommitHash}
  605. if previousCommitHash != "" {
  606. args = append(args, previousCommitHash)
  607. }
  608. _, err := repo.runGitCommand(args...)
  609. return err
  610. }
  611. // GetNotes uses the "git" command-line tool to read the notes from the given ref for a given revision.
  612. func (repo *GitRepo) GetNotes(notesRef, revision string) []Note {
  613. var notes []Note
  614. rawNotes, err := repo.runGitCommand("notes", "--ref", notesRef, "show", revision)
  615. if err != nil {
  616. // We just assume that this means there are no notes
  617. return nil
  618. }
  619. for _, line := range strings.Split(rawNotes, "\n") {
  620. notes = append(notes, Note([]byte(line)))
  621. }
  622. return notes
  623. }
  624. func stringsReader(s []*string) io.Reader {
  625. var subReaders []io.Reader
  626. for _, strPtr := range s {
  627. subReader := strings.NewReader(*strPtr)
  628. subReaders = append(subReaders, subReader, strings.NewReader("\n"))
  629. }
  630. return io.MultiReader(subReaders...)
  631. }
  632. // splitBatchCheckOutput parses the output of a 'git cat-file --batch-check=...' command.
  633. //
  634. // The output is expected to be formatted as a series of entries, with each
  635. // entry consisting of:
  636. // 1. The SHA1 hash of the git object being output, followed by a space.
  637. // 2. The git "type" of the object (commit, blob, tree, missing, etc), followed by a newline.
  638. //
  639. // To generate this format, make sure that the 'git cat-file' command includes
  640. // the argument '--batch-check=%(objectname) %(objecttype)'.
  641. //
  642. // The return value is a map from object hash to a boolean indicating if that object is a commit.
  643. func splitBatchCheckOutput(out *bytes.Buffer) (map[string]bool, error) {
  644. isCommit := make(map[string]bool)
  645. reader := bufio.NewReader(out)
  646. for {
  647. nameLine, err := reader.ReadString(byte(' '))
  648. if err == io.EOF {
  649. return isCommit, nil
  650. }
  651. if err != nil {
  652. return nil, fmt.Errorf("Failure while reading the next object name: %v", err)
  653. }
  654. nameLine = strings.TrimSuffix(nameLine, " ")
  655. typeLine, err := reader.ReadString(byte('\n'))
  656. if err != nil && err != io.EOF {
  657. return nil, fmt.Errorf("Failure while reading the next object type: %q - %v", nameLine, err)
  658. }
  659. typeLine = strings.TrimSuffix(typeLine, "\n")
  660. if typeLine == "commit" {
  661. isCommit[nameLine] = true
  662. }
  663. }
  664. }
  665. // splitBatchCatFileOutput parses the output of a 'git cat-file --batch=...' command.
  666. //
  667. // The output is expected to be formatted as a series of entries, with each
  668. // entry consisting of:
  669. // 1. The SHA1 hash of the git object being output, followed by a newline.
  670. // 2. The size of the object's contents in bytes, followed by a newline.
  671. // 3. The objects contents.
  672. //
  673. // To generate this format, make sure that the 'git cat-file' command includes
  674. // the argument '--batch=%(objectname)\n%(objectsize)'.
  675. func splitBatchCatFileOutput(out *bytes.Buffer) (map[string][]byte, error) {
  676. contentsMap := make(map[string][]byte)
  677. reader := bufio.NewReader(out)
  678. for {
  679. nameLine, err := reader.ReadString(byte('\n'))
  680. if strings.HasSuffix(nameLine, "\n") {
  681. nameLine = strings.TrimSuffix(nameLine, "\n")
  682. }
  683. if err == io.EOF {
  684. return contentsMap, nil
  685. }
  686. if err != nil {
  687. return nil, fmt.Errorf("Failure while reading the next object name: %v", err)
  688. }
  689. sizeLine, err := reader.ReadString(byte('\n'))
  690. if strings.HasSuffix(sizeLine, "\n") {
  691. sizeLine = strings.TrimSuffix(sizeLine, "\n")
  692. }
  693. if err != nil {
  694. return nil, fmt.Errorf("Failure while reading the next object size: %q - %v", nameLine, err)
  695. }
  696. size, err := strconv.Atoi(sizeLine)
  697. if err != nil {
  698. return nil, fmt.Errorf("Failure while parsing the next object size: %q - %v", nameLine, err)
  699. }
  700. contentBytes := make([]byte, size, size)
  701. readDest := contentBytes
  702. len := 0
  703. err = nil
  704. for err == nil && len < size {
  705. nextLen := 0
  706. nextLen, err = reader.Read(readDest)
  707. len += nextLen
  708. readDest = contentBytes[len:]
  709. }
  710. contentsMap[nameLine] = contentBytes
  711. if err == io.EOF {
  712. return contentsMap, nil
  713. }
  714. if err != nil {
  715. return nil, err
  716. }
  717. for bs, err := reader.Peek(1); err == nil && bs[0] == byte('\n'); bs, err = reader.Peek(1) {
  718. reader.ReadByte()
  719. }
  720. }
  721. }
  722. // notesMapping represents the association between a git object and the notes for that object.
  723. type notesMapping struct {
  724. ObjectHash *string
  725. NotesHash *string
  726. }
  727. // notesOverview represents a high-level overview of all the notes under a single notes ref.
  728. type notesOverview struct {
  729. NotesMappings []*notesMapping
  730. ObjectHashesReader io.Reader
  731. NotesHashesReader io.Reader
  732. }
  733. // notesOverview returns an overview of the git notes stored under the given ref.
  734. func (repo *GitRepo) notesOverview(notesRef string) (*notesOverview, error) {
  735. var stdout bytes.Buffer
  736. var stderr bytes.Buffer
  737. if err := repo.runGitCommandWithIO(nil, &stdout, &stderr, "notes", "--ref", notesRef, "list"); err != nil {
  738. return nil, err
  739. }
  740. var notesMappings []*notesMapping
  741. var objHashes []*string
  742. var notesHashes []*string
  743. outScanner := bufio.NewScanner(&stdout)
  744. for outScanner.Scan() {
  745. line := outScanner.Text()
  746. lineParts := strings.Split(line, " ")
  747. if len(lineParts) != 2 {
  748. return nil, fmt.Errorf("Malformed output line from 'git-notes list': %q", line)
  749. }
  750. objHash := &lineParts[1]
  751. notesHash := &lineParts[0]
  752. notesMappings = append(notesMappings, &notesMapping{
  753. ObjectHash: objHash,
  754. NotesHash: notesHash,
  755. })
  756. objHashes = append(objHashes, objHash)
  757. notesHashes = append(notesHashes, notesHash)
  758. }
  759. err := outScanner.Err()
  760. if err != nil && err != io.EOF {
  761. return nil, fmt.Errorf("Failure parsing the output of 'git-notes list': %v", err)
  762. }
  763. return &notesOverview{
  764. NotesMappings: notesMappings,
  765. ObjectHashesReader: stringsReader(objHashes),
  766. NotesHashesReader: stringsReader(notesHashes),
  767. }, nil
  768. }
  769. // getIsCommitMap returns a mapping of all the annotated objects that are commits.
  770. func (overview *notesOverview) getIsCommitMap(repo *GitRepo) (map[string]bool, error) {
  771. var stdout bytes.Buffer
  772. var stderr bytes.Buffer
  773. if err := repo.runGitCommandWithIO(overview.ObjectHashesReader, &stdout, &stderr, "cat-file", "--batch-check=%(objectname) %(objecttype)"); err != nil {
  774. return nil, fmt.Errorf("Failure performing a batch file check: %v", err)
  775. }
  776. isCommit, err := splitBatchCheckOutput(&stdout)
  777. if err != nil {
  778. return nil, fmt.Errorf("Failure parsing the output of a batch file check: %v", err)
  779. }
  780. return isCommit, nil
  781. }
  782. // getNoteContentsMap returns a mapping from all the notes hashes to their contents.
  783. func (overview *notesOverview) getNoteContentsMap(repo *GitRepo) (map[string][]byte, error) {
  784. var stdout bytes.Buffer
  785. var stderr bytes.Buffer
  786. if err := repo.runGitCommandWithIO(overview.NotesHashesReader, &stdout, &stderr, "cat-file", "--batch=%(objectname)\n%(objectsize)"); err != nil {
  787. return nil, fmt.Errorf("Failure performing a batch file read: %v", err)
  788. }
  789. noteContentsMap, err := splitBatchCatFileOutput(&stdout)
  790. if err != nil {
  791. return nil, fmt.Errorf("Failure parsing the output of a batch file read: %v", err)
  792. }
  793. return noteContentsMap, nil
  794. }
  795. // GetAllNotes reads the contents of the notes under the given ref for every commit.
  796. //
  797. // The returned value is a mapping from commit hash to the list of notes for that commit.
  798. //
  799. // This is the batch version of the corresponding GetNotes(...) method.
  800. func (repo *GitRepo) GetAllNotes(notesRef string) (map[string][]Note, error) {
  801. // This code is unfortunately quite complicated, but it needs to be so.
  802. //
  803. // Conceptually, this is equivalent to:
  804. // result := make(map[string][]Note)
  805. // for _, commit := range repo.ListNotedRevisions(notesRef) {
  806. // result[commit] = repo.GetNotes(notesRef, commit)
  807. // }
  808. // return result, nil
  809. //
  810. // However, that logic would require separate executions of the 'git'
  811. // command for every annotated commit. For a repo with 10s of thousands
  812. // of reviews, that would mean calling Cmd.Run(...) 10s of thousands of
  813. // times. That, in turn, would take so long that the tool would be unusable.
  814. //
  815. // This method avoids that by taking advantage of the 'git cat-file --batch="..."'
  816. // command. That allows us to use a single invocation of Cmd.Run(...) to
  817. // inspect multiple git objects at once.
  818. //
  819. // As such, regardless of the number of reviews in a repo, we can get all
  820. // of the notes using a total of three invocations of Cmd.Run(...):
  821. // 1. One to list all the annotated objects (and their notes hash)
  822. // 2. A second one to filter out all of the annotated objects that are not commits.
  823. // 3. A final one to get the contents of all of the notes blobs.
  824. overview, err := repo.notesOverview(notesRef)
  825. if err != nil {
  826. return nil, err
  827. }
  828. isCommit, err := overview.getIsCommitMap(repo)
  829. if err != nil {
  830. return nil, fmt.Errorf("Failure building the set of commit objects: %v", err)
  831. }
  832. noteContentsMap, err := overview.getNoteContentsMap(repo)
  833. if err != nil {
  834. return nil, fmt.Errorf("Failure building the mapping from notes hash to contents: %v", err)
  835. }
  836. commitNotesMap := make(map[string][]Note)
  837. for _, notesMapping := range overview.NotesMappings {
  838. if !isCommit[*notesMapping.ObjectHash] {
  839. continue
  840. }
  841. noteBytes := noteContentsMap[*notesMapping.NotesHash]
  842. byteSlices := bytes.Split(noteBytes, []byte("\n"))
  843. var notes []Note
  844. for _, slice := range byteSlices {
  845. notes = append(notes, Note(slice))
  846. }
  847. commitNotesMap[*notesMapping.ObjectHash] = notes
  848. }
  849. return commitNotesMap, nil
  850. }
  851. // AppendNote appends a note to a revision under the given ref.
  852. func (repo *GitRepo) AppendNote(notesRef, revision string, note Note) error {
  853. _, err := repo.runGitCommand("notes", "--ref", notesRef, "append", "-m", string(note), revision)
  854. return err
  855. }
  856. // ListNotedRevisions returns the collection of revisions that are annotated by notes in the given ref.
  857. func (repo *GitRepo) ListNotedRevisions(notesRef string) []string {
  858. var revisions []string
  859. notesListOut, err := repo.runGitCommand("notes", "--ref", notesRef, "list")
  860. if err != nil {
  861. return nil
  862. }
  863. notesList := strings.Split(notesListOut, "\n")
  864. for _, notePair := range notesList {
  865. noteParts := strings.SplitN(notePair, " ", 2)
  866. if len(noteParts) == 2 {
  867. objHash := noteParts[1]
  868. objType, err := repo.runGitCommand("cat-file", "-t", objHash)
  869. // If a note points to an object that we do not know about (yet), then err will not
  870. // be nil. We can safely just ignore those notes.
  871. if err == nil && objType == "commit" {
  872. revisions = append(revisions, objHash)
  873. }
  874. }
  875. }
  876. return revisions
  877. }
  878. // Remotes returns a list of the remotes.
  879. func (repo *GitRepo) Remotes() ([]string, error) {
  880. remotes, err := repo.runGitCommand("remote")
  881. if err != nil {
  882. return nil, err
  883. }
  884. remoteNames := strings.Split(remotes, "\n")
  885. var result []string
  886. for _, name := range remoteNames {
  887. result = append(result, strings.TrimSpace(name))
  888. }
  889. sort.Strings(result)
  890. return result, nil
  891. }
  892. // Fetch fetches from the given remote using the supplied refspecs.
  893. func (repo *GitRepo) Fetch(remote string, refspecs ...string) error {
  894. args := []string{"fetch", remote}
  895. args = append(args, refspecs...)
  896. return repo.runGitCommandInline(args...)
  897. }
  898. // PushNotes pushes git notes to a remote repo.
  899. func (repo *GitRepo) PushNotes(remote, notesRefPattern string) error {
  900. refspec := fmt.Sprintf("%s:%s", notesRefPattern, notesRefPattern)
  901. // The push is liable to fail if the user forgot to do a pull first, so
  902. // we treat errors as user errors rather than fatal errors.
  903. err := repo.runGitCommandInline("push", remote, refspec)
  904. if err != nil {
  905. return fmt.Errorf("Failed to push to the remote '%s': %v", remote, err)
  906. }
  907. return nil
  908. }
  909. // PushNotesAndArchive pushes the given notes and archive refs to a remote repo.
  910. func (repo *GitRepo) PushNotesAndArchive(remote, notesRefPattern, archiveRefPattern string) error {
  911. notesRefspec := fmt.Sprintf("%s:%s", notesRefPattern, notesRefPattern)
  912. archiveRefspec := fmt.Sprintf("%s:%s", archiveRefPattern, archiveRefPattern)
  913. err := repo.runGitCommandInline("push", remote, notesRefspec, archiveRefspec)
  914. if err != nil {
  915. return fmt.Errorf("Failed to push the local archive to the remote '%s': %v", remote, err)
  916. }
  917. return nil
  918. }
  919. func (repo *GitRepo) getRefHashes(refPattern string) (map[string]string, error) {
  920. if !strings.HasSuffix(refPattern, "/*") {
  921. return nil, fmt.Errorf("unsupported ref pattern %q", refPattern)
  922. }
  923. refPrefix := strings.TrimSuffix(refPattern, "*")
  924. showRef, err := repo.runGitCommand("show-ref")
  925. if err != nil {
  926. return nil, err
  927. }
  928. refsMap := make(map[string]string)
  929. for _, line := range strings.Split(showRef, "\n") {
  930. lineParts := strings.Split(line, " ")
  931. if len(lineParts) != 2 {
  932. return nil, fmt.Errorf("unexpected line in output of `git show-ref`: %q", line)
  933. }
  934. if strings.HasPrefix(lineParts[1], refPrefix) {
  935. refsMap[lineParts[1]] = lineParts[0]
  936. }
  937. }
  938. return refsMap, nil
  939. }
  940. func getRemoteNotesRef(remote, localNotesRef string) string {
  941. // Note: The pattern for remote notes deviates from that of remote heads and devtools,
  942. // because the git command line tool requires all notes refs to be located under the
  943. // "refs/notes/" prefix.
  944. //
  945. // Because of that, we make the remote refs a subset of the local refs instead of
  946. // a parallel tree, which is the pattern used for heads and devtools.
  947. //
  948. // E.G. ("refs/notes/..." -> "refs/notes/remotes/<remote>/...")
  949. // versus ("refs/heads/..." -> "refs/remotes/<remote>/...")
  950. relativeNotesRef := strings.TrimPrefix(localNotesRef, notesRefPrefix)
  951. return notesRefPrefix + "remotes/" + remote + "/" + relativeNotesRef
  952. }
  953. func getLocalNotesRef(remote, remoteNotesRef string) string {
  954. relativeNotesRef := strings.TrimPrefix(remoteNotesRef, notesRefPrefix+"remotes/"+remote+"/")
  955. return notesRefPrefix + relativeNotesRef
  956. }
  957. // MergeNotes merges in the remote's state of the notes reference into the
  958. // local repository's.
  959. func (repo *GitRepo) MergeNotes(remote, notesRefPattern string) error {
  960. remoteRefPattern := getRemoteNotesRef(remote, notesRefPattern)
  961. refsMap, err := repo.getRefHashes(remoteRefPattern)
  962. if err != nil {
  963. return err
  964. }
  965. for remoteRef := range refsMap {
  966. localRef := getLocalNotesRef(remote, remoteRef)
  967. if _, err := repo.runGitCommand("notes", "--ref", localRef, "merge", remoteRef, "-s", "cat_sort_uniq"); err != nil {
  968. return err
  969. }
  970. }
  971. return nil
  972. }
  973. // PullNotes fetches the contents of the given notes ref from a remote repo,
  974. // and then merges them with the corresponding local notes using the
  975. // "cat_sort_uniq" strategy.
  976. func (repo *GitRepo) PullNotes(remote, notesRefPattern string) error {
  977. remoteNotesRefPattern := getRemoteNotesRef(remote, notesRefPattern)
  978. fetchRefSpec := fmt.Sprintf("+%s:%s", notesRefPattern, remoteNotesRefPattern)
  979. err := repo.Fetch(remote, fetchRefSpec)
  980. if err != nil {
  981. return err
  982. }
  983. return repo.MergeNotes(remote, notesRefPattern)
  984. }
  985. func getRemoteDevtoolsRef(remote, devtoolsRefPattern string) string {
  986. relativeRef := strings.TrimPrefix(devtoolsRefPattern, devtoolsRefPrefix)
  987. return remoteDevtoolsRefPrefix + remote + "/" + relativeRef
  988. }
  989. func getLocalDevtoolsRef(remote, remoteDevtoolsRef string) string {
  990. relativeRef := strings.TrimPrefix(remoteDevtoolsRef, remoteDevtoolsRefPrefix+remote+"/")
  991. return devtoolsRefPrefix + relativeRef
  992. }
  993. // MergeArchives merges in the remote's state of the archives reference into
  994. // the local repository's.
  995. func (repo *GitRepo) MergeArchives(remote, archiveRefPattern string) error {
  996. remoteRefPattern := getRemoteDevtoolsRef(remote, archiveRefPattern)
  997. refsMap, err := repo.getRefHashes(remoteRefPattern)
  998. if err != nil {
  999. return err
  1000. }
  1001. for remoteRef := range refsMap {
  1002. localRef := getLocalDevtoolsRef(remote, remoteRef)
  1003. if err := repo.mergeArchives(localRef, remoteRef); err != nil {
  1004. return err
  1005. }
  1006. }
  1007. return nil
  1008. }
  1009. // FetchAndReturnNewReviewHashes fetches the notes "branches" and then susses
  1010. // out the IDs (the revision the review points to) of any new reviews, then
  1011. // returns that list of IDs.
  1012. //
  1013. // This is accomplished by determining which files in the notes tree have
  1014. // changed because the _names_ of these files correspond to the revisions they
  1015. // point to.
  1016. func (repo *GitRepo) FetchAndReturnNewReviewHashes(remote, notesRefPattern string, devtoolsRefPatterns ...string) ([]string, error) {
  1017. for _, refPattern := range devtoolsRefPatterns {
  1018. if !strings.HasPrefix(refPattern, devtoolsRefPrefix) {
  1019. return nil, fmt.Errorf("Unsupported devtools ref: %q", refPattern)
  1020. }
  1021. }
  1022. remoteNotesRefPattern := getRemoteNotesRef(remote, notesRefPattern)
  1023. notesFetchRefSpec := fmt.Sprintf("+%s:%s", notesRefPattern, remoteNotesRefPattern)
  1024. localDevtoolsRefPattern := devtoolsRefPrefix + "*"
  1025. remoteDevtoolsRefPattern := getRemoteDevtoolsRef(remote, localDevtoolsRefPattern)
  1026. devtoolsFetchRefSpec := fmt.Sprintf("+%s:%s", localDevtoolsRefPattern, remoteDevtoolsRefPattern)
  1027. // Prior to fetching, record the current state of the remote notes refs
  1028. priorRefHashes, err := repo.getRefHashes(remoteNotesRefPattern)
  1029. if err != nil {
  1030. return nil, fmt.Errorf("failure reading the existing ref hashes for the remote %q: %v", remote, err)
  1031. }
  1032. if err := repo.Fetch(remote, notesFetchRefSpec, devtoolsFetchRefSpec); err != nil {
  1033. return nil, fmt.Errorf("failure fetching from the remote %q: %v", remote, err)
  1034. }
  1035. // After fetching, record the updated state of the remote notes refs
  1036. updatedRefHashes, err := repo.getRefHashes(remoteNotesRefPattern)
  1037. if err != nil {
  1038. return nil, fmt.Errorf("failure reading the updated ref hashes for the remote %q: %v", remote, err)
  1039. }
  1040. // Now that we have our two lists, we need to merge them.
  1041. updatedReviewSet := make(map[string]struct{})
  1042. for ref, hash := range updatedRefHashes {
  1043. priorHash, ok := priorRefHashes[ref]
  1044. if priorHash == hash {
  1045. // Nothing has changed for this ref
  1046. continue
  1047. }
  1048. var notes string
  1049. var err error
  1050. if !ok {
  1051. // This is a new ref, so include every noted object
  1052. notes, err = repo.runGitCommand("ls-tree", "-r", "--name-only", hash)
  1053. } else {
  1054. notes, err = repo.runGitCommand("diff", "--name-only", priorHash, hash)
  1055. }
  1056. if err != nil {
  1057. return nil, err
  1058. }
  1059. // The name of the review matches the name of the notes tree entry, with slashes removed
  1060. reviews := strings.Split(strings.Replace(notes, "/", "", -1), "\n")
  1061. for _, review := range reviews {
  1062. updatedReviewSet[review] = struct{}{}
  1063. }
  1064. }
  1065. updatedReviews := make([]string, 0, len(updatedReviewSet))
  1066. for key, _ := range updatedReviewSet {
  1067. updatedReviews = append(updatedReviews, key)
  1068. }
  1069. return updatedReviews, nil
  1070. }
  1071. // PullNotesAndArchive fetches the contents of the notes and archives refs from
  1072. // a remote repo, and merges them with the corresponding local refs.
  1073. //
  1074. // For notes refs, we assume that every note can be automatically merged using
  1075. // the 'cat_sort_uniq' strategy (the git-appraise schemas fit that requirement),
  1076. // so we automatically merge the remote notes into the local notes.
  1077. //
  1078. // For "archive" refs, they are expected to be used solely for maintaining
  1079. // reachability of commits that are part of the history of any reviews,
  1080. // so we do not maintain any consistency with their tree objects. Instead,
  1081. // we merely ensure that their history graph includes every commit that we
  1082. // intend to keep.
  1083. func (repo *GitRepo) PullNotesAndArchive(remote, notesRefPattern, archiveRefPattern string) error {
  1084. if _, err := repo.FetchAndReturnNewReviewHashes(remote, notesRefPattern, archiveRefPattern); err != nil {
  1085. return fmt.Errorf("failure fetching from the remote %q: %v", remote, err)
  1086. }
  1087. if err := repo.MergeArchives(remote, archiveRefPattern); err != nil {
  1088. return fmt.Errorf("failure merging archives from the remote %q: %v", remote, err)
  1089. }
  1090. if err := repo.MergeNotes(remote, notesRefPattern); err != nil {
  1091. return fmt.Errorf("failure merging notes from the remote %q: %v", remote, err)
  1092. }
  1093. return nil
  1094. }
  1095. // Push pushes the given refs to a remote repo.
  1096. func (repo *GitRepo) Push(remote string, refSpecs ...string) error {
  1097. pushArgs := append([]string{"push", remote}, refSpecs...)
  1098. err := repo.runGitCommandInline(pushArgs...)
  1099. if err != nil {
  1100. return fmt.Errorf("Failed to push the local refs to the remote '%s': %v", remote, err)
  1101. }
  1102. return nil
  1103. }