func cmdSubmit()

in git-codereview/submit.go [14:75]


func cmdSubmit(args []string) {
	// NOTE: New flags should be added to the usage message below as well as doc.go.
	var interactive bool
	flags.BoolVar(&interactive, "i", false, "interactively select commits to submit")
	flags.Usage = func() {
		fmt.Fprintf(stderr(), "Usage: %s submit %s [-i | commit...]\n", progName, globalFlags)
		exit(2)
	}
	flags.Parse(args)
	if interactive && flags.NArg() > 0 {
		flags.Usage()
		exit(2)
	}

	b := CurrentBranch()
	var cs []*Commit
	if interactive {
		hashes := submitHashes(b)
		if len(hashes) == 0 {
			printf("nothing to submit")
			return
		}
		for _, hash := range hashes {
			cs = append(cs, b.CommitByRev("submit", hash))
		}
	} else if args := flags.Args(); len(args) >= 1 {
		for _, arg := range args {
			cs = append(cs, b.CommitByRev("submit", arg))
		}
	} else {
		cs = append(cs, b.DefaultCommit("submit", "must specify commit on command line or use submit -i"))
	}

	// No staged changes.
	// Also, no unstaged changes, at least for now.
	// This makes sure the sync at the end will work well.
	// We can relax this later if there is a good reason.
	checkStaged("submit")
	checkUnstaged("submit")

	// Submit the changes.
	var g *GerritChange
	for _, c := range cs {
		printf("submitting %s %s", c.ShortHash, c.Subject)
		g = submit(b, c)
	}

	// Sync client to revision that Gerrit committed, but only if we can do it cleanly.
	// Otherwise require user to run 'git sync' themselves (if they care).
	run("git", "fetch", "-q")
	if len(cs) == 1 && len(b.Pending()) == 1 {
		if err := runErr("git", "checkout", "-q", "-B", b.Name, g.CurrentRevision, "--"); err != nil {
			dief("submit succeeded, but cannot sync local branch\n"+
				"\trun 'git sync' to sync, or\n"+
				"\trun 'git branch -D %s; git change master; git sync' to discard local branch", b.Name)
		}
	} else {
		printf("submit succeeded; run 'git sync' to sync")
	}

	// Done! Change is submitted, branch is up to date, ready for new work.
}