summary refs log blame commit diff
path: root/third_party/go/git-appraise/review/review.go
blob: a23dd17bf7988dd3ce1dbb3482a413e7a39cfd91 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
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
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772



































































































































































































































































































































































































































































































































































































































































































































































































                                                                                                                                 
/*
Copyright 2015 Google Inc. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Package review contains the data structures used to represent code reviews.
package review

import (
	"bytes"
	"encoding/json"
	"fmt"
	"sort"

	"github.com/google/git-appraise/repository"
	"github.com/google/git-appraise/review/analyses"
	"github.com/google/git-appraise/review/ci"
	"github.com/google/git-appraise/review/comment"
	"github.com/google/git-appraise/review/gpg"
	"github.com/google/git-appraise/review/request"
)

const archiveRef = "refs/devtools/archives/reviews"

// CommentThread represents the tree-based hierarchy of comments.
//
// The Resolved field represents the aggregate status of the entire thread. If
// it is set to false, then it indicates that there is an unaddressed comment
// in the thread. If it is unset, then that means that the root comment is an
// FYI only, and that there are no unaddressed comments. If it is set to true,
// then that means that there are no unaddressed comments, and that the root
// comment has its resolved bit set to true.
type CommentThread struct {
	Hash     string             `json:"hash,omitempty"`
	Comment  comment.Comment    `json:"comment"`
	Original *comment.Comment   `json:"original,omitempty"`
	Edits    []*comment.Comment `json:"edits,omitempty"`
	Children []CommentThread    `json:"children,omitempty"`
	Resolved *bool              `json:"resolved,omitempty"`
	Edited   bool               `json:"edited,omitempty"`
}

// Summary represents the high-level state of a code review.
//
// This high-level state corresponds to the data that can be quickly read
// directly from the repo, so other methods that need to operate on a lot
// of reviews (such as listing the open reviews) should prefer operating on
// the summary rather than the details.
//
// Review summaries have two status fields which are orthogonal:
// 1. Resolved indicates if a reviewer has accepted or rejected the change.
// 2. Submitted indicates if the change has been incorporated into the target.
type Summary struct {
	Repo        repository.Repo   `json:"-"`
	Revision    string            `json:"revision"`
	Request     request.Request   `json:"request"`
	AllRequests []request.Request `json:"-"`
	Comments    []CommentThread   `json:"comments,omitempty"`
	Resolved    *bool             `json:"resolved,omitempty"`
	Submitted   bool              `json:"submitted"`
}

// Review represents the entire state of a code review.
//
// This extends Summary to also include a list of reports for both the
// continuous integration status, and the static analysis runs. Those reports
// correspond to either the current commit in the review ref (for pending
// reviews), or to the last commented-upon commit (for submitted reviews).
type Review struct {
	*Summary
	Reports  []ci.Report       `json:"reports,omitempty"`
	Analyses []analyses.Report `json:"analyses,omitempty"`
}

type commentsByTimestamp []*comment.Comment

// Interface methods for sorting comment threads by timestamp
func (cs commentsByTimestamp) Len() int      { return len(cs) }
func (cs commentsByTimestamp) Swap(i, j int) { cs[i], cs[j] = cs[j], cs[i] }
func (cs commentsByTimestamp) Less(i, j int) bool {
	return cs[i].Timestamp < cs[j].Timestamp
}

type byTimestamp []CommentThread

// Interface methods for sorting comment threads by timestamp
func (threads byTimestamp) Len() int      { return len(threads) }
func (threads byTimestamp) Swap(i, j int) { threads[i], threads[j] = threads[j], threads[i] }
func (threads byTimestamp) Less(i, j int) bool {
	return threads[i].Comment.Timestamp < threads[j].Comment.Timestamp
}

type requestsByTimestamp []request.Request

// Interface methods for sorting review requests by timestamp
func (requests requestsByTimestamp) Len() int { return len(requests) }
func (requests requestsByTimestamp) Swap(i, j int) {
	requests[i], requests[j] = requests[j], requests[i]
}
func (requests requestsByTimestamp) Less(i, j int) bool {
	return requests[i].Timestamp < requests[j].Timestamp
}

type summariesWithNewestRequestsFirst []Summary

// Interface methods for sorting review summaries in reverse chronological order
func (summaries summariesWithNewestRequestsFirst) Len() int { return len(summaries) }
func (summaries summariesWithNewestRequestsFirst) Swap(i, j int) {
	summaries[i], summaries[j] = summaries[j], summaries[i]
}
func (summaries summariesWithNewestRequestsFirst) Less(i, j int) bool {
	return summaries[i].Request.Timestamp > summaries[j].Request.Timestamp
}

// updateThreadsStatus calculates the aggregate status of a sequence of comment threads.
//
// The aggregate status is the conjunction of all of the non-nil child statuses.
//
// This has the side-effect of setting the "Resolved" field of all descendant comment threads.
func updateThreadsStatus(threads []CommentThread) *bool {
	sort.Stable(byTimestamp(threads))
	noUnresolved := true
	var result *bool
	for i := range threads {
		thread := &threads[i]
		thread.updateResolvedStatus()
		if thread.Resolved != nil {
			noUnresolved = noUnresolved && *thread.Resolved
			result = &noUnresolved
		}
	}
	return result
}

// updateResolvedStatus calculates the aggregate status of a single comment thread,
// and updates the "Resolved" field of that thread accordingly.
func (thread *CommentThread) updateResolvedStatus() {
	resolved := updateThreadsStatus(thread.Children)
	if resolved == nil {
		thread.Resolved = thread.Comment.Resolved
		return
	}

	if !*resolved {
		thread.Resolved = resolved
		return
	}

	if thread.Comment.Resolved == nil || !*thread.Comment.Resolved {
		thread.Resolved = nil
		return
	}

	thread.Resolved = resolved
}

// Verify verifies the signature on a comment.
func (thread *CommentThread) Verify() error {
	err := gpg.Verify(&thread.Comment)
	if err != nil {
		hash, _ := thread.Comment.Hash()
		return fmt.Errorf("verification of comment [%s] failed: %s", hash, err)
	}
	for _, child := range thread.Children {
		err = child.Verify()
		if err != nil {
			return err
		}
	}
	return nil
}

// mutableThread is an internal-only data structure used to store partially constructed comment threads.
type mutableThread struct {
	Hash     string
	Comment  comment.Comment
	Edits    []*comment.Comment
	Children []*mutableThread
}

// fixMutableThread is a helper method to finalize a mutableThread struct
// (partially constructed comment thread) as a CommentThread struct
// (fully constructed comment thread).
func fixMutableThread(mutableThread *mutableThread) CommentThread {
	var children []CommentThread
	edited := len(mutableThread.Edits) > 0
	for _, mutableChild := range mutableThread.Children {
		child := fixMutableThread(mutableChild)
		if (!edited) && child.Edited {
			edited = true
		}
		children = append(children, child)
	}
	comment := &mutableThread.Comment
	if len(mutableThread.Edits) > 0 {
		sort.Stable(commentsByTimestamp(mutableThread.Edits))
		comment = mutableThread.Edits[len(mutableThread.Edits)-1]
	}

	return CommentThread{
		Hash:     mutableThread.Hash,
		Comment:  *comment,
		Original: &mutableThread.Comment,
		Edits:    mutableThread.Edits,
		Children: children,
		Edited:   edited,
	}
}

// This function builds the comment thread tree from the log-based list of comments.
//
// Since the comments can be processed in any order, this uses an internal mutable
// data structure, and then converts it to the proper CommentThread structure at the end.
func buildCommentThreads(commentsByHash map[string]comment.Comment) []CommentThread {
	threadsByHash := make(map[string]*mutableThread)
	for hash, comment := range commentsByHash {
		thread, ok := threadsByHash[hash]
		if !ok {
			thread = &mutableThread{
				Hash:    hash,
				Comment: comment,
			}
			threadsByHash[hash] = thread
		}
	}
	var rootHashes []string
	for hash, thread := range threadsByHash {
		if thread.Comment.Original != "" {
			original, ok := threadsByHash[thread.Comment.Original]
			if ok {
				original.Edits = append(original.Edits, &thread.Comment)
			}
		} else if thread.Comment.Parent == "" {
			rootHashes = append(rootHashes, hash)
		} else {
			parent, ok := threadsByHash[thread.Comment.Parent]
			if ok {
				parent.Children = append(parent.Children, thread)
			}
		}
	}
	var threads []CommentThread
	for _, hash := range rootHashes {
		threads = append(threads, fixMutableThread(threadsByHash[hash]))
	}
	return threads
}

// loadComments reads in the log-structured sequence of comments for a review,
// and then builds the corresponding tree-structured comment threads.
func (r *Summary) loadComments(commentNotes []repository.Note) []CommentThread {
	commentsByHash := comment.ParseAllValid(commentNotes)
	return buildCommentThreads(commentsByHash)
}

func getSummaryFromNotes(repo repository.Repo, revision string, requestNotes, commentNotes []repository.Note) (*Summary, error) {
	requests := request.ParseAllValid(requestNotes)
	if requests == nil {
		return nil, fmt.Errorf("Could not find any review requests for %q", revision)
	}
	sort.Stable(requestsByTimestamp(requests))
	reviewSummary := Summary{
		Repo:        repo,
		Revision:    revision,
		Request:     requests[len(requests)-1],
		AllRequests: requests,
	}
	reviewSummary.Comments = reviewSummary.loadComments(commentNotes)
	reviewSummary.Resolved = updateThreadsStatus(reviewSummary.Comments)
	return &reviewSummary, nil
}

// GetSummary returns the summary of the code review specified by its revision
// and the references which contain that reviews summary and comments.
//
// If no review request exists, the returned review summary is nil.
func GetSummaryViaRefs(repo repository.Repo, requestRef, commentRef,
	revision string) (*Summary, error) {

	if err := repo.VerifyCommit(revision); err != nil {
		return nil, fmt.Errorf("Could not find a commit named %q", revision)
	}
	requestNotes := repo.GetNotes(requestRef, revision)
	commentNotes := repo.GetNotes(commentRef, revision)
	summary, err := getSummaryFromNotes(repo, revision, requestNotes, commentNotes)
	if err != nil {
		return nil, err
	}
	currentCommit := revision
	if summary.Request.Alias != "" {
		currentCommit = summary.Request.Alias
	}

	if !summary.IsAbandoned() {
		submitted, err := repo.IsAncestor(currentCommit, summary.Request.TargetRef)
		if err != nil {
			return nil, err
		}
		summary.Submitted = submitted
	}
	return summary, nil
}

// GetSummary returns the summary of the specified code review.
//
// If no review request exists, the returned review summary is nil.
func GetSummary(repo repository.Repo, revision string) (*Summary, error) {
	return GetSummaryViaRefs(repo, request.Ref, comment.Ref, revision)
}

// Details returns the detailed review for the given summary.
func (r *Summary) Details() (*Review, error) {
	review := Review{
		Summary: r,
	}
	currentCommit, err := review.GetHeadCommit()
	if err == nil {
		review.Reports = ci.ParseAllValid(review.Repo.GetNotes(ci.Ref, currentCommit))
		review.Analyses = analyses.ParseAllValid(review.Repo.GetNotes(analyses.Ref, currentCommit))
	}
	return &review, nil
}

// IsAbandoned returns whether or not the given review has been abandoned.
func (r *Summary) IsAbandoned() bool {
	return r.Request.TargetRef == ""
}

// IsOpen returns whether or not the given review is still open (neither submitted nor abandoned).
func (r *Summary) IsOpen() bool {
	return !r.Submitted && !r.IsAbandoned()
}

// Verify returns whether or not a summary's comments are a) signed, and b)
/// that those signatures are verifiable.
func (r *Summary) Verify() error {
	err := gpg.Verify(&r.Request)
	if err != nil {
		return fmt.Errorf("couldn't verify request targeting: %q: %s",
			r.Request.TargetRef, err)
	}
	for _, thread := range r.Comments {
		err := thread.Verify()
		if err != nil {
			return err
		}
	}
	return nil
}

// Get returns the specified code review.
//
// If no review request exists, the returned review is nil.
func Get(repo repository.Repo, revision string) (*Review, error) {
	summary, err := GetSummary(repo, revision)
	if err != nil {
		return nil, err
	}
	if summary == nil {
		return nil, nil
	}
	return summary.Details()
}

func getIsSubmittedCheck(repo repository.Repo) func(ref, commit string) bool {
	refCommitsMap := make(map[string]map[string]bool)

	getRefCommitsMap := func(ref string) map[string]bool {
		commitsMap, ok := refCommitsMap[ref]
		if ok {
			return commitsMap
		}
		commitsMap = make(map[string]bool)
		for _, commit := range repo.ListCommits(ref) {
			commitsMap[commit] = true
		}
		refCommitsMap[ref] = commitsMap
		return commitsMap
	}

	return func(ref, commit string) bool {
		return getRefCommitsMap(ref)[commit]
	}
}

func unsortedListAll(repo repository.Repo) []Summary {
	reviewNotesMap, err := repo.GetAllNotes(request.Ref)
	if err != nil {
		return nil
	}
	discussNotesMap, err := repo.GetAllNotes(comment.Ref)
	if err != nil {
		return nil
	}

	isSubmittedCheck := getIsSubmittedCheck(repo)
	var reviews []Summary
	for commit, notes := range reviewNotesMap {
		summary, err := getSummaryFromNotes(repo, commit, notes, discussNotesMap[commit])
		if err != nil {
			continue
		}
		if !summary.IsAbandoned() {
			summary.Submitted = isSubmittedCheck(summary.Request.TargetRef, summary.getStartingCommit())
		}
		reviews = append(reviews, *summary)
	}
	return reviews
}

// ListAll returns all reviews stored in the git-notes.
func ListAll(repo repository.Repo) []Summary {
	reviews := unsortedListAll(repo)
	sort.Stable(summariesWithNewestRequestsFirst(reviews))
	return reviews
}

// ListOpen returns all reviews that are not yet incorporated into their target refs.
func ListOpen(repo repository.Repo) []Summary {
	var openReviews []Summary
	for _, review := range unsortedListAll(repo) {
		if review.IsOpen() {
			openReviews = append(openReviews, review)
		}
	}
	sort.Stable(summariesWithNewestRequestsFirst(openReviews))
	return openReviews
}

// GetCurrent returns the current, open code review.
//
// If there are multiple matching reviews, then an error is returned.
func GetCurrent(repo repository.Repo) (*Review, error) {
	reviewRef, err := repo.GetHeadRef()
	if err != nil {
		return nil, err
	}
	var matchingReviews []Summary
	for _, review := range ListOpen(repo) {
		if review.Request.ReviewRef == reviewRef {
			matchingReviews = append(matchingReviews, review)
		}
	}
	if matchingReviews == nil {
		return nil, nil
	}
	if len(matchingReviews) != 1 {
		return nil, fmt.Errorf("There are %d open reviews for the ref \"%s\"", len(matchingReviews), reviewRef)
	}
	return matchingReviews[0].Details()
}

// GetBuildStatusMessage returns a string of the current build-and-test status
// of the review, or "unknown" if the build-and-test status cannot be determined.
func (r *Review) GetBuildStatusMessage() string {
	statusMessage := "unknown"
	ciReport, err := ci.GetLatestCIReport(r.Reports)
	if err != nil {
		return fmt.Sprintf("unknown: %s", err)
	}
	if ciReport != nil {
		statusMessage = fmt.Sprintf("%s (%q)", ciReport.Status, ciReport.URL)
	}
	return statusMessage
}

// GetAnalysesNotes returns all of the notes from the most recent static
// analysis run recorded in the git notes.
func (r *Review) GetAnalysesNotes() ([]analyses.Note, error) {
	latestAnalyses, err := analyses.GetLatestAnalysesReport(r.Analyses)
	if err != nil {
		return nil, err
	}
	if latestAnalyses == nil {
		return nil, fmt.Errorf("No analyses available")
	}
	return latestAnalyses.GetNotes()
}

// GetAnalysesMessage returns a string summarizing the results of the
// most recent static analyses.
func (r *Review) GetAnalysesMessage() string {
	latestAnalyses, err := analyses.GetLatestAnalysesReport(r.Analyses)
	if err != nil {
		return err.Error()
	}
	if latestAnalyses == nil {
		return "No analyses available"
	}
	status := latestAnalyses.Status
	if status != "" && status != analyses.StatusNeedsMoreWork {
		return status
	}
	analysesNotes, err := latestAnalyses.GetNotes()
	if err != nil {
		return err.Error()
	}
	if analysesNotes == nil {
		return "passed"
	}
	return fmt.Sprintf("%d warnings\n", len(analysesNotes))
	// TODO(ojarjur): Figure out the best place to display the actual notes
}

func prettyPrintJSON(jsonBytes []byte) (string, error) {
	var prettyBytes bytes.Buffer
	err := json.Indent(&prettyBytes, jsonBytes, "", "  ")
	if err != nil {
		return "", err
	}
	return prettyBytes.String(), nil
}

// GetJSON returns the pretty printed JSON for a review summary.
func (r *Summary) GetJSON() (string, error) {
	jsonBytes, err := json.Marshal(*r)
	if err != nil {
		return "", err
	}
	return prettyPrintJSON(jsonBytes)
}

// GetJSON returns the pretty printed JSON for a review.
func (r *Review) GetJSON() (string, error) {
	jsonBytes, err := json.Marshal(*r)
	if err != nil {
		return "", err
	}
	return prettyPrintJSON(jsonBytes)
}

// findLastCommit returns the later (newest) commit from the union of the provided commit
// and all of the commits that are referenced in the given comment threads.
func (r *Review) findLastCommit(startingCommit, latestCommit string, commentThreads []CommentThread) string {
	isLater := func(commit string) bool {
		if err := r.Repo.VerifyCommit(commit); err != nil {
			return false
		}
		if t, e := r.Repo.IsAncestor(latestCommit, commit); e == nil && t {
			return true
		}
		if t, e := r.Repo.IsAncestor(startingCommit, commit); e == nil && !t {
			return false
		}
		if t, e := r.Repo.IsAncestor(commit, latestCommit); e == nil && t {
			return false
		}
		ct, err := r.Repo.GetCommitTime(commit)
		if err != nil {
			return false
		}
		lt, err := r.Repo.GetCommitTime(latestCommit)
		if err != nil {
			return true
		}
		return ct > lt
	}
	updateLatest := func(commit string) {
		if commit == "" {
			return
		}
		if isLater(commit) {
			latestCommit = commit
		}
	}
	for _, commentThread := range commentThreads {
		comment := commentThread.Comment
		if comment.Location != nil {
			updateLatest(comment.Location.Commit)
		}
		updateLatest(r.findLastCommit(startingCommit, latestCommit, commentThread.Children))
	}
	return latestCommit
}

func (r *Summary) getStartingCommit() string {
	if r.Request.Alias != "" {
		return r.Request.Alias
	}
	return r.Revision
}

// GetHeadCommit returns the latest commit in a review.
func (r *Review) GetHeadCommit() (string, error) {
	currentCommit := r.getStartingCommit()
	if r.Request.ReviewRef == "" {
		return currentCommit, nil
	}

	if r.Submitted {
		// The review has already been submitted.
		// Go through the list of comments and find the last commented upon commit.
		return r.findLastCommit(currentCommit, currentCommit, r.Comments), nil
	}

	// It is possible that the review ref is no longer an ancestor of the starting
	// commit (e.g. if a rebase left us in a detached head), in which case we have to
	// find the head commit without using it.
	useReviewRef, err := r.Repo.IsAncestor(currentCommit, r.Request.ReviewRef)
	if err != nil {
		return "", err
	}
	if useReviewRef {
		return r.Repo.ResolveRefCommit(r.Request.ReviewRef)
	}

	return r.findLastCommit(currentCommit, currentCommit, r.Comments), nil
}

// GetBaseCommit returns the commit against which a review should be compared.
func (r *Review) GetBaseCommit() (string, error) {
	if !r.IsOpen() {
		if r.Request.BaseCommit != "" {
			return r.Request.BaseCommit, nil
		}

		// This means the review has been submitted, but did not specify a base commit.
		// In this case, we have to treat the last parent commit as the base. This is
		// usually what we want, since merging a target branch into a feature branch
		// results in the previous commit to the feature branch being the first parent,
		// and the latest commit to the target branch being the second parent.
		return r.Repo.GetLastParent(r.Revision)
	}

	targetRefHead, err := r.Repo.ResolveRefCommit(r.Request.TargetRef)
	if err != nil {
		return "", err
	}
	leftHandSide := targetRefHead
	rightHandSide := r.Revision
	if r.Request.ReviewRef != "" {
		if reviewRefHead, err := r.Repo.ResolveRefCommit(r.Request.ReviewRef); err == nil {
			rightHandSide = reviewRefHead
		}
	}

	return r.Repo.MergeBase(leftHandSide, rightHandSide)
}

// ListCommits lists the commits included in a review.
func (r *Review) ListCommits() ([]string, error) {
	baseCommit, err := r.GetBaseCommit()
	if err != nil {
		return nil, err
	}
	headCommit, err := r.GetHeadCommit()
	if err != nil {
		return nil, err
	}
	return r.Repo.ListCommitsBetween(baseCommit, headCommit)
}

// GetDiff returns the diff for a review.
func (r *Review) GetDiff(diffArgs ...string) (string, error) {
	var baseCommit, headCommit string
	baseCommit, err := r.GetBaseCommit()
	if err == nil {
		headCommit, err = r.GetHeadCommit()
	}
	if err == nil {
		return r.Repo.Diff(baseCommit, headCommit, diffArgs...)
	}
	return "", err
}

// AddComment adds the given comment to the review.
func (r *Review) AddComment(c comment.Comment) error {
	commentNote, err := c.Write()
	if err != nil {
		return err
	}

	r.Repo.AppendNote(comment.Ref, r.Revision, commentNote)
	return nil
}

// Rebase performs an interactive rebase of the review onto its target ref.
//
// If the 'archivePrevious' argument is true, then the previous head of the
// review will be added to the 'refs/devtools/archives/reviews' ref prior
// to being rewritten. That ensures the review history is kept from being
// garbage collected.
func (r *Review) Rebase(archivePrevious bool) error {
	if archivePrevious {
		orig, err := r.GetHeadCommit()
		if err != nil {
			return err
		}
		if err := r.Repo.ArchiveRef(orig, archiveRef); err != nil {
			return err
		}
	}
	if err := r.Repo.SwitchToRef(r.Request.ReviewRef); err != nil {
		return err
	}

	err := r.Repo.RebaseRef(r.Request.TargetRef)
	if err != nil {
		return err
	}

	alias, err := r.Repo.GetCommitHash("HEAD")
	if err != nil {
		return err
	}
	r.Request.Alias = alias
	newNote, err := r.Request.Write()
	if err != nil {
		return err
	}
	return r.Repo.AppendNote(request.Ref, r.Revision, newNote)
}

// RebaseAndSign performs an interactive rebase of the review onto its
// target ref. It signs the result of the rebase as well as (re)signs
// the review request itself.
//
// If the 'archivePrevious' argument is true, then the previous head of the
// review will be added to the 'refs/devtools/archives/reviews' ref prior
// to being rewritten. That ensures the review history is kept from being
// garbage collected.
func (r *Review) RebaseAndSign(archivePrevious bool) error {
	if archivePrevious {
		orig, err := r.GetHeadCommit()
		if err != nil {
			return err
		}
		if err := r.Repo.ArchiveRef(orig, archiveRef); err != nil {
			return err
		}
	}
	if err := r.Repo.SwitchToRef(r.Request.ReviewRef); err != nil {
		return err
	}

	err := r.Repo.RebaseAndSignRef(r.Request.TargetRef)
	if err != nil {
		return err
	}

	alias, err := r.Repo.GetCommitHash("HEAD")
	if err != nil {
		return err
	}
	r.Request.Alias = alias

	key, err := r.Repo.GetUserSigningKey()
	if err != nil {
		return err
	}
	err = gpg.Sign(key, &r.Request)
	if err != nil {
		return err
	}

	newNote, err := r.Request.Write()
	if err != nil {
		return err
	}
	return r.Repo.AppendNote(request.Ref, r.Revision, newNote)
}