devcam review: submit changes for review

Context: http://camlistore.org/issue/408

Change-Id: I587de65c9dc8d87c02541096e1f4f7765e644962
This commit is contained in:
mpl 2014-05-13 23:04:17 +02:00
parent 630ba8c68f
commit 4569a7f475
6 changed files with 151 additions and 23 deletions

View File

@ -93,6 +93,6 @@ $ ln -s ../../misc/pre-commit.githook pre-commit
Finally, submit your code to gerrit with:
$ ./misc/review
$ devcam review
Please update this file as appropriate.

View File

@ -148,6 +148,11 @@ func handleSignals(camliProc *os.Process) {
}
func checkCamliSrcRoot() {
args := flag.Args()
if len(args) > 0 && args[0] == "review" {
// exception for devcam review, which does its own check.
return
}
if _, err := os.Stat("make.go"); err != nil {
if !os.IsNotExist(err) {
log.Fatalf("Could not stat make.go: %v", err)
@ -198,7 +203,7 @@ func build(path string) error {
}
func main() {
checkCamliSrcRoot()
cmdmain.CheckCwd = checkCamliSrcRoot
// TODO(mpl): usage error is not really correct for devcam.
// See if I can reimplement it while still using cmdmain.Main().
cmdmain.Main()

126
dev/devcam/review.go Normal file
View File

@ -0,0 +1,126 @@
/*
Copyright 2014 The Camlistore Authors.
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.
*/
// This file adds the "review" subcommand to devcam, to send changes for peer review.
package main
import (
"bufio"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"camlistore.org/pkg/cmdmain"
)
var (
defaultHook = filepath.FromSlash("misc/commit-msg.githook")
hookFile = filepath.FromSlash(".git/hooks/commit-msg")
)
type reviewCmd struct{}
func init() {
cmdmain.RegisterCommand("review", func(flags *flag.FlagSet) cmdmain.CommandRunner {
return new(reviewCmd)
})
}
func (c *reviewCmd) Usage() {
fmt.Fprintf(cmdmain.Stderr, "Usage: devcam review\n")
}
func (c *reviewCmd) Describe() string {
return "Submit your git commits for review."
}
func (c *reviewCmd) RunCommand(args []string) error {
if len(args) > 0 {
return cmdmain.UsageError("too many arguments.")
}
goToCamliRoot()
c.checkHook()
gitPush()
return nil
}
func goToCamliRoot() {
prevDir, err := os.Getwd()
if err != nil {
log.Fatalf("could not get current directory: %v", err)
}
for {
if _, err := os.Stat(defaultHook); err == nil {
return
}
if err := os.Chdir(".."); err != nil {
log.Fatalf("Could not chdir: %v", err)
}
currentDir, err := os.Getwd()
if err != nil {
log.Fatalf("Could not get current directory: %v", err)
}
if currentDir == prevDir {
log.Fatal("Camlistore tree root not found. Run from within the Camlistore tree please.")
}
prevDir = currentDir
}
}
func (c *reviewCmd) checkHook() {
_, err := os.Stat(hookFile)
if err == nil {
return
}
if !os.IsNotExist(err) {
log.Fatal(err)
}
fmt.Fprintf(cmdmain.Stdout, "Presubmit hook to add Change-Id to commit messages is missing.\nNow automatically creating it at %v from %v\n\n", hookFile, defaultHook)
data, err := ioutil.ReadFile(defaultHook)
if err != nil {
log.Fatal(err)
}
if err := ioutil.WriteFile(hookFile, data, 0700); err != nil {
log.Fatal(err)
}
fmt.Fprintf(cmdmain.Stdout, "Amending last commit to add Change-Id.\nPlease re-save description without making changes.\n\n")
fmt.Fprintf(cmdmain.Stdout, "Press Enter to continue.\n")
if _, _, err := bufio.NewReader(cmdmain.Stdin).ReadLine(); err != nil {
log.Fatal(err)
}
cmd := exec.Command("git", []string{"commit", "--amend"}...)
cmd.Stdout = cmdmain.Stdout
cmd.Stderr = cmdmain.Stderr
if err := cmd.Run(); err != nil {
log.Fatal(err)
}
}
func gitPush() {
cmd := exec.Command("git",
[]string{"push", "https://camlistore.googlesource.com/camlistore", "HEAD:refs/for/master"}...)
cmd.Stdout = cmdmain.Stdout
cmd.Stderr = cmdmain.Stderr
if err := cmd.Run(); err != nil {
log.Fatalf("Could not git push: %v", err)
}
}

View File

@ -3,26 +3,19 @@
use strict;
use FindBin qw($Bin);
my $hook_file = "$Bin/../.git/hooks/commit-msg";
my $devcam_binpath = "";
my $gopath = $ENV{'GOPATH'};
unless (-e $hook_file) {
print <<EOF;
Presubmit hook to add Change-Id to commit messages is missing.
Copying to $hook_file.
EOF
system("cp", "-p", "$Bin/commit-msg.githook", $hook_file) and die "cp failed.\n";
print <<EOF;
Amending last commit to add Change-Id.
Please re-save description without making changes.
Press Enter to continue.
EOF
scalar(<STDIN>);
system("git", "commit", "--amend") and die "git commit --amend fail\n";
if (-e "$Bin/../bin/devcam") {
$devcam_binpath = "$Bin/../bin/devcam";
} elsif (-e "$gopath/bin/devcam") {
$devcam_binpath = "$gopath/bin/devcam";
} elsif (-e "$Bin\\..\\bin\\devcam.exe") {
$devcam_binpath = "$Bin\\..\\bin\\devcam.exe";
} elsif (-e "$gopath\\bin\\devcam.exe") {
$devcam_binpath = "$gopath\\bin\\devcam.exe";
} else {
die "devcam binary not found\n";
}
exec("git", "push", "https://camlistore.googlesource.com/camlistore", "HEAD:refs/for/master");
exec($devcam_binpath, "review");

View File

@ -47,6 +47,9 @@ var (
// ExitWithFailure determines whether the command exits
// with a non-zero exit status.
ExitWithFailure bool
// CheckCwd checks the current working directory, and possibly
// changes it, or aborts the run if needed.
CheckCwd = func() {}
)
var ErrUsage = UsageError("invalid command")
@ -233,6 +236,7 @@ func Main() {
setCommandLineOutput(Stderr)
}
flag.Parse()
CheckCwd()
args := flag.Args()
if *FlagVersion {

View File

@ -37,7 +37,7 @@
<ul>
<li>Create a topic branch, make some changes and commit away.</li>
<li>Before review, squash your changes into a single change.</li>
<li>Send for review with: <pre>./misc/review</pre></li>
<li>Send for review. See <a href="https://camlistore.googlesource.com/camlistore/+/master/HACKING">HACKING</a> for how to install devcam, and then use:<pre>devcam review</pre></li>
<li>Modify as necessary until change is merged. Amend your commit or squash to a single commit before sending for review again (be sure to keep the same <a href="http://gerrit.googlecode.com/svn/documentation/2.2.1/user-changeid.html">the Change-Id line</a>)</li>
</ul>
</ul>