github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/review/git-codereview/sync.go (about)

     1  // Copyright 2014 The Go Authors.  All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import "strings"
     8  
     9  func cmdSync(args []string) {
    10  	expectZeroArgs(args, "sync")
    11  
    12  	// Get current branch and commit ID for fixup after pull.
    13  	b := CurrentBranch()
    14  	var id string
    15  	if work := b.Pending(); len(work) > 0 {
    16  		id = work[0].ChangeID
    17  	}
    18  
    19  	// Don't sync with staged or unstaged changes.
    20  	// rebase is going to complain if we don't, and we can give a nicer error.
    21  	checkStaged("sync")
    22  	checkUnstaged("sync")
    23  
    24  	// Pull remote changes into local branch.
    25  	// We do this in one command so that people following along with 'git sync -v'
    26  	// see fewer commands to understand.
    27  	// We want to pull in the remote changes from the upstream branch
    28  	// and rebase the current pending commit (if any) on top of them.
    29  	// If there is no pending commit, the pull will do a fast-forward merge.
    30  	run("git", "pull", "-q", "-r", "origin", strings.TrimPrefix(b.OriginBranch(), "origin/"))
    31  
    32  	// If the change commit has been submitted,
    33  	// roll back change leaving any changes unstaged.
    34  	// Pull should have done this for us, but check just in case.
    35  	b = CurrentBranch() // discard any cached information
    36  	if len(b.Pending()) == 1 && b.Submitted(id) {
    37  		run("git", "reset", b.Branchpoint())
    38  	}
    39  }
    40  
    41  func checkStaged(cmd string) {
    42  	if HasStagedChanges() {
    43  		dief("cannot %s: staged changes exist\n"+
    44  			"\trun 'git status' to see changes\n"+
    45  			"\trun 'git-codereview change' to commit staged changes", cmd)
    46  	}
    47  }
    48  
    49  func checkUnstaged(cmd string) {
    50  	if HasUnstagedChanges() {
    51  		dief("cannot %s: unstaged changes exist\n"+
    52  			"\trun 'git status' to see changes\n"+
    53  			"\trun 'git stash' to save unstaged changes\n"+
    54  			"\trun 'git add' and 'git-codereview change' to commit staged changes", cmd)
    55  	}
    56  }