// Validate that the user's request to rebase a review makes sense. // // This checks both that the request is well formed, and that the // corresponding review is in a state where rebasing is appropriate. func validateRebaseRequest(repo repository.Repo, args []string) (*review.Review, error) { var r *review.Review var err error if len(args) > 1 { return nil, errors.New("Only rebasing a single review is supported.") } if len(args) == 1 { r, err = review.Get(repo, args[0]) } else { r, err = review.GetCurrent(repo) } if err != nil { return nil, fmt.Errorf("Failed to load the review: %v\n", err) } if r == nil { return nil, errors.New("There is no matching review.") } if r.Submitted { return nil, errors.New("The review has already been submitted.") } target := r.Request.TargetRef if err := repo.VerifyGitRef(target); err != nil { return nil, err } return r, nil }
// showReview prints the current code review. func showReview(args []string) error { showFlagSet.Parse(args) args = showFlagSet.Args() var r *review.Review var err error if len(args) > 1 { return errors.New("Only showing a single review is supported.") } if len(args) == 1 { r = review.Get(args[0]) } else { r, err = review.GetCurrent() } if err != nil { return fmt.Errorf("Failed to load the review: %v\n", err) } if r == nil { return errors.New("There is no matching review.") } if *showJsonOutput { return r.PrintJson() } return r.PrintDetails() }
// commentOnReview adds a comment to the current code review. func commentOnReview(repo repository.Repo, args []string) error { commentFlagSet.Parse(args) args = commentFlagSet.Args() if *commentLgtm && *commentNmw { return errors.New("You cannot combine the flags -lgtm and -nmw.") } if *commentLine != 0 && *commentFile == "" { return errors.New("Specifying a line number with the -l flag requires that you also specify a file name with the -f flag.") } var r *review.Review var err error if len(args) > 1 { return errors.New("Only accepting a single review is supported.") } if len(args) == 1 { r = review.Get(repo, args[0]) } else { r, err = review.GetCurrent(repo) } if err != nil { return fmt.Errorf("Failed to load the review: %v\n", err) } if r == nil { return errors.New("There is no matching review.") } commentedUponCommit, err := r.GetHeadCommit() if err != nil { return err } location := comment.Location{ Commit: commentedUponCommit, } if *commentFile != "" { location.Path = *commentFile if *commentLine != 0 { location.Range = &comment.Range{ StartLine: uint32(*commentLine), } } } c := comment.New(repo.GetUserEmail(), *commentMessage) c.Location = &location c.Parent = *commentParent if *commentLgtm || *commentNmw { resolved := *commentLgtm c.Resolved = &resolved } return r.AddComment(c) }
// rejectReview adds an NMW comment to the current code review. func rejectReview(repo repository.Repo, args []string) error { rejectFlagSet.Parse(args) args = rejectFlagSet.Args() var r *review.Review var err error if len(args) > 1 { return errors.New("Only rejecting a single review is supported.") } if len(args) == 1 { r, err = review.Get(repo, args[0]) } else { r, err = review.GetCurrent(repo) } if err != nil { return fmt.Errorf("Failed to load the review: %v\n", err) } if r == nil { return errors.New("There is no matching review.") } if *rejectMessageFile != "" && *rejectMessage == "" { *rejectMessage, err = input.FromFile(*rejectMessageFile) if err != nil { return err } } if *rejectMessageFile == "" && *rejectMessage == "" { *rejectMessage, err = input.LaunchEditor(repo, commentFilename) if err != nil { return err } } rejectedCommit, err := r.GetHeadCommit() if err != nil { return err } location := comment.Location{ Commit: rejectedCommit, } resolved := false userEmail, err := repo.GetUserEmail() if err != nil { return err } c := comment.New(userEmail, *rejectMessage) c.Location = &location c.Resolved = &resolved return r.AddComment(c) }
// commentOnReview adds a comment to the current code review. func commentOnReview(args []string) error { commentFlagSet.Parse(args) args = commentFlagSet.Args() if *lgtm && *nmw { return errors.New("You cannot combine the flags -lgtm and -nmw.") } r, err := review.GetCurrent() if err != nil { return fmt.Errorf("Failed to load the current review: %v\n", err) } if r == nil { return errors.New("There is no current review.") } commentedUponCommit := repository.GetCommitHash(r.Request.ReviewRef) location := comment.Location{ Commit: commentedUponCommit, } if len(args) > 0 { location.Path = args[0] if len(args) > 1 { startLine, err := strconv.ParseUint(args[1], 0, 32) if err != nil { return err } location.Range = &comment.Range{ StartLine: uint32(startLine), } } } c := comment.New(*commentMessage) c.Location = &location c.Parent = *parent if *lgtm || *nmw { resolved := *lgtm c.Resolved = &resolved } return r.AddComment(c) }
// acceptReview adds an LGTM comment to the current code review. func acceptReview(args []string) error { acceptFlagSet.Parse(args) r, err := review.GetCurrent() if err != nil { return fmt.Errorf("Failed to load the current review: %v\n", err) } if r == nil { return errors.New("There is no current review.") } acceptedCommit := repository.GetCommitHash(r.Request.ReviewRef) location := comment.Location{ Commit: acceptedCommit, } resolved := true c := comment.New(*acceptMessage) c.Location = &location c.Resolved = &resolved return r.AddComment(c) }
// acceptReview adds an LGTM comment to the current code review. func acceptReview(repo repository.Repo, args []string) error { acceptFlagSet.Parse(args) args = acceptFlagSet.Args() var r *review.Review var err error if len(args) > 1 { return errors.New("Only accepting a single review is supported.") } if len(args) == 1 { r, err = review.Get(repo, args[0]) } else { r, err = review.GetCurrent(repo) } if err != nil { return fmt.Errorf("Failed to load the review: %v\n", err) } if r == nil { return errors.New("There is no matching review.") } acceptedCommit, err := r.GetHeadCommit() if err != nil { return err } location := comment.Location{ Commit: acceptedCommit, } resolved := true userEmail, err := repo.GetUserEmail() if err != nil { return err } c := comment.New(userEmail, *acceptMessage) c.Location = &location c.Resolved = &resolved return r.AddComment(c) }
// Submit the current code review request. // // The "args" parameter contains all of the command line arguments that followed the subcommand. func submitReview(args []string) error { submitFlagSet.Parse(args) if *submitMerge && *submitRebase { return errors.New("Only one of --merge or --rebase is allowed.") } r, err := review.GetCurrent() if err != nil { return err } if r == nil { return errors.New("There is nothing to submit") } if !*submitTBR && (r.Resolved == nil || !*r.Resolved) { return errors.New("Not submitting as the review has not yet been accepted.") } target := r.Request.TargetRef source := r.Request.ReviewRef repository.VerifyGitRefOrDie(target) repository.VerifyGitRefOrDie(source) if !repository.IsAncestor(target, source) { return errors.New("Refusing to submit a non-fast-forward review. First merge the target ref.") } repository.SwitchToRef(target) if *submitMerge { repository.MergeRef(source, false) } else if *submitRebase { repository.RebaseRef(source) } else { repository.MergeRef(source, true) } return nil }
// showReview prints the current code review. func showReview(repo repository.Repo, args []string) error { showFlagSet.Parse(args) args = showFlagSet.Args() if *showDiffOptions != "" && !*showDiffOutput { return errors.New("The --diff-opts flag can only be used if the --diff flag is set.") } var r *review.Review var err error if len(args) > 1 { return errors.New("Only showing a single review is supported.") } if len(args) == 1 { r, err = review.Get(repo, args[0]) } else { r, err = review.GetCurrent(repo) } if err != nil { return fmt.Errorf("Failed to load the review: %v\n", err) } if r == nil { return errors.New("There is no matching review.") } if *showJSONOutput { return output.PrintJSON(r) } if *showDiffOutput { var diffArgs []string if *showDiffOptions != "" { diffArgs = strings.Split(*showDiffOptions, ",") } return output.PrintDiff(r, diffArgs...) } return output.PrintDetails(r) }
// Submit the current code review request. // // The "args" parameter contains all of the command line arguments that followed the subcommand. func submitReview(repo repository.Repo, args []string) error { submitFlagSet.Parse(args) args = submitFlagSet.Args() if *submitMerge && *submitRebase { return errors.New("Only one of --merge or --rebase is allowed.") } var r *review.Review var err error if len(args) > 1 { return errors.New("Only accepting a single review is supported.") } if len(args) == 1 { r, err = review.Get(repo, args[0]) } else { r, err = review.GetCurrent(repo) } if err != nil { return fmt.Errorf("Failed to load the review: %v\n", err) } if r == nil { return errors.New("There is no matching review.") } if r.Submitted { return errors.New("The review has already been submitted.") } if !*submitTBR && (r.Resolved == nil || !*r.Resolved) { return errors.New("Not submitting as the review has not yet been accepted.") } target := r.Request.TargetRef if err := repo.VerifyGitRef(target); err != nil { return err } source, err := r.GetHeadCommit() if err != nil { return err } isAncestor, err := repo.IsAncestor(target, source) if err != nil { return err } if !isAncestor { return errors.New("Refusing to submit a non-fast-forward review. First merge the target ref.") } if !(*submitRebase || *submitMerge || *submitFastForward) { submitStrategy, err := repo.GetSubmitStrategy() if err != nil { return err } if submitStrategy == "merge" && !*submitRebase && !*submitFastForward { *submitMerge = true } if submitStrategy == "rebase" && !*submitMerge && !*submitFastForward { *submitRebase = true } if submitStrategy == "fast-forward" && !*submitRebase && !*submitMerge { *submitFastForward = true } } if *submitRebase { if err := r.Rebase(*submitArchive); err != nil { return err } source, err = r.GetHeadCommit() if err != nil { return err } } if err := repo.SwitchToRef(target); err != nil { return err } if *submitMerge { submitMessage := fmt.Sprintf("Submitting review %.12s", r.Revision) return repo.MergeRef(source, false, submitMessage, r.Request.Description) } else { return repo.MergeRef(source, true) } }
// commentOnReview adds a comment to the current code review. func commentOnReview(repo repository.Repo, args []string) error { commentFlagSet.Parse(args) args = commentFlagSet.Args() var r *review.Review var err error if len(args) > 1 { return errors.New("Only accepting a single review is supported.") } if len(args) == 1 { r, err = review.Get(repo, args[0]) } else { r, err = review.GetCurrent(repo) } if err != nil { return fmt.Errorf("Failed to load the review: %v\n", err) } if r == nil { return errors.New("There is no matching review.") } if *commentLgtm && *commentNmw { return errors.New("You cannot combine the flags -lgtm and -nmw.") } if *commentLine != 0 && *commentFile == "" { return errors.New("Specifying a line number with the -l flag requires that you also specify a file name with the -f flag.") } if *commentParent != "" && !commentHashExists(*commentParent, r.Comments) { return errors.New("There is no matching parent comment.") } if *commentMessage == "" { editor, err := repo.GetCoreEditor() if err != nil { return fmt.Errorf("Unable to detect default git editor: %v\n", err) } path := fmt.Sprintf("%s/.git/%s", repo.GetPath(), commentFilename) cmd := exec.Command(editor, path) cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr err = cmd.Start() if err != nil { return fmt.Errorf("Unable to start editor: %v\n", err) } err = cmd.Wait() if err != nil { return fmt.Errorf("Editing finished with error: %v\n", err) } comment, err := ioutil.ReadFile(path) if err != nil { os.Remove(path) return fmt.Errorf("Error reading comment file: %v\n", err) } *commentMessage = string(comment) os.Remove(path) } commentedUponCommit, err := r.GetHeadCommit() if err != nil { return err } location := comment.Location{ Commit: commentedUponCommit, } if *commentFile != "" { location.Path = *commentFile if *commentLine != 0 { location.Range = &comment.Range{ StartLine: uint32(*commentLine), } } } userEmail, err := repo.GetUserEmail() if err != nil { return err } c := comment.New(userEmail, *commentMessage) c.Location = &location c.Parent = *commentParent if *commentLgtm || *commentNmw { resolved := *commentLgtm c.Resolved = &resolved } return r.AddComment(c) }
// rejectReview adds an NMW comment to the current code review. func rejectReview(repo repository.Repo, args []string) error { rejectFlagSet.Parse(args) args = rejectFlagSet.Args() var r *review.Review var err error if len(args) > 1 { return errors.New("Only rejecting a single review is supported.") } if len(args) == 1 { r, err = review.Get(repo, args[0]) } else { r, err = review.GetCurrent(repo) } if err != nil { return fmt.Errorf("Failed to load the review: %v\n", err) } if r == nil { return errors.New("There is no matching review.") } if *rejectMessage == "" { editor, err := repo.GetCoreEditor() if err != nil { return fmt.Errorf("Unable to detect default git editor: %v\n", err) } path := fmt.Sprintf("%s/.git/%s", repo.GetPath(), rejectFilename) cmd := exec.Command(editor, path) cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr err = cmd.Start() if err != nil { return fmt.Errorf("Unable to start editor: %v\n", err) } err = cmd.Wait() if err != nil { return fmt.Errorf("Editing finished with error: %v\n", err) } comment, err := ioutil.ReadFile(path) if err != nil { os.Remove(path) return fmt.Errorf("Error reading comment file: %v\n", err) } *rejectMessage = string(comment) os.Remove(path) } rejectedCommit, err := r.GetHeadCommit() if err != nil { return err } location := comment.Location{ Commit: rejectedCommit, } resolved := false userEmail, err := repo.GetUserEmail() if err != nil { return err } c := comment.New(userEmail, *rejectMessage) c.Location = &location c.Resolved = &resolved return r.AddComment(c) }