forked from perkeep/perkeep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
review.go
126 lines (109 loc) · 3.13 KB
/
review.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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)
}
}