blob: 5984df3582e99d3120df9251ae21b497433b08ef [file] [log] [blame]
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import "strings"
func doSync(args []string) {
expectZeroArgs(args, "sync")
// Get current branch and commit ID for fixup after pull.
b := CurrentBranch()
id := b.ChangeID()
// Don't sync with staged or unstaged changes.
// rebase is going to complain if we don't, and we can give a nicer error.
// Pull remote changes into local branch.
// We do this in one command so that people following along with 'git sync -v'
// see fewer commands to understand.
// We want to pull in the remote changes from the upstream branch
// and rebase the current pending commit (if any) on top of them.
// If there is no pending commit, the pull will do a fast-forward merge.
run("git", "pull", "-q", "-r", "origin", strings.TrimPrefix(b.OriginBranch(), "origin/"))
// If the change commit has been submitted,
// roll back change leaving any changes unstaged.
// Pull should have done this for us, but check just in case.
if b.Submitted(id) && b.HasPendingCommit() {
run("git", "reset", "HEAD^")
func checkStaged(cmd string) {
if HasStagedChanges() {
dief("cannot %s: staged changes exist\n"+
"\trun 'git status' to see changes\n"+
"\trun 'git-review change' to commit staged changes", cmd)
func checkUnstaged(cmd string) {
if HasUnstagedChanges() {
dief("cannot %s: unstaged changes exist\n"+
"\trun 'git status' to see changes\n"+
"\trun 'git stash' to save unstaged changes\n"+
"\trun 'git add' and 'git-review change' to commit staged changes", cmd)