Skip to content

Commit

Permalink
Merge pull request #20 from liquidata-inc/aaron/checkcommitters-local…
Browse files Browse the repository at this point in the history
…-mode

go/utils: Improve checkcommitters; add prepr.sh.
  • Loading branch information
Brian Hendriks authored Aug 8, 2019
2 parents 6e8d516 + 0a0fcf9 commit ed41e11
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 16 deletions.
1 change: 1 addition & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pipeline {
sh "go get golang.org/x/tools/cmd/goimports"
}
dir ("go") {
// Keep this in sync with //go/utils/prepr/prepr.sh.
sh "go get -mod=readonly ./..."
sh "./utils/repofmt/check_fmt.sh"
sh "./Godeps/verify.sh"
Expand Down
2 changes: 1 addition & 1 deletion go/utils/checkcommitters/check_pr.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ set -eo pipefail
script_dir=$(dirname "$0")
cd "$script_dir"

exec go run . $BRANCH_NAME $CHANGE_TARGET
exec go run . -pr $BRANCH_NAME $CHANGE_TARGET
48 changes: 48 additions & 0 deletions go/utils/checkcommitters/fix_committer.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/bin/bash

set -eo pipefail

force=
if [[ $# -gt 0 ]]; then
if [ "$1" = "-f" ]; then
force=`shift`
fi
fi

if [[ $# -ne 4 && $# -ne 2 ]]; then
echo "Usage: fix_committer.sh [-f] TARGET_BRANCH WRONG_EMAIL [RIGHT_NAME RIGHT_EMAIL]" 1>&2
echo " Example: fix_committer.sh master [email protected] \"Aaron Son\" \"[email protected]\"" 1>&2
echo " If RIGHT_NAME and RIGHT_EMAIL are ommitted, they are taken to be the current user from git config" 1>&2
exit 1
fi

script_dir=$(dirname "$0")
cd $script_dir/../../..

target=$1
wrongemail=$2
if [[ $# -eq 4 ]]; then
rightname=$3
rightemail=$4
else
rightname=$(git config --get user.name)
rightemail=$(git config --get user.email)
fi

mergebase=`git merge-base HEAD "remotes/origin/$target"`

exec git "$force" filter-branch --env-filter '
OLD_EMAIL="'"$wrongemail"'"
CORRECT_NAME="'"$rightname"'"
CORRECT_EMAIL="'"$rightemail"'"
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_COMMITTER_NAME="$CORRECT_NAME"
export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_AUTHOR_NAME="$CORRECT_NAME"
export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' ${mergebase}..HEAD
67 changes: 52 additions & 15 deletions go/utils/checkcommitters/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,65 @@ var AllowedCommitters = map[string]*struct{}{
// - This would be a place to enforce DCO or CLA requirements.

func main() {
if len(os.Args) != 3 {
fmt.Printf("Usage: checkcommitters SOURCE_BRANCH TARGET_BRANCH\n")
fmt.Printf(" SOURCE_BRANCH is the remotes/origin branch to be merged by the PR, for example PR-4.\n")
fmt.Printf(" CHANGE_TARGET is the target remotes/origin branch of the PR, for example master.\n")
fmt.Printf("This should be run from the git checkout workspace for the PR.\n")
if len(os.Args) < 2 {
PrintUsageAndExit()
}
if os.Args[1] == "-pr" {
if len(os.Args) != 4 {
PrintUsageAndExit()
}
HandleCheckAndExit(Check("remotes/origin/"+os.Args[2], "remotes/origin/"+os.Args[3]))
} else if os.Args[1] == "-dir" {
if len(os.Args) > 3 {
PrintUsageAndExit()
}
target := "master"
if len(os.Args) == 3 {
target = os.Args[2]
}
HandleCheckAndExit(Check("HEAD", "remotes/origin/"+target))
} else {
PrintUsageAndExit()
}
}

func HandleCheckAndExit(failed bool) {
if failed {
fmt.Printf("\nThis PR has non-whitelisted committers or authors.\n")
fmt.Printf("Please use ./utils/checkcommitters/fix_committer.sh to make\n")
fmt.Printf("all commits from a whitelisted committer and author.\n")
os.Exit(1)
}
mbc := exec.Command("git", "merge-base", "remotes/origin/"+os.Args[2], "remotes/origin/"+os.Args[1])
}

func PrintUsageAndExit() {
fmt.Printf("Usage: checkcommitters [-pr SOURCE_BRANCH TARGET_BRANCH | -dir TARGET_BRANCH\n")
fmt.Printf(" SOURCE_BRANCH is the remotes/origin branch to be merged by the PR, for example PR-4.\n")
fmt.Printf(" CHANGE_TARGET is the target remotes/origin branch of the PR, for example master.\n")
fmt.Printf("This should be run from the git checkout workspace for the PR.\n")
fmt.Printf("Example: checkcommitters -pr PR-4 master.\n")
fmt.Printf(" Will check that all commits from merge-base of PR-4 and remotes/origin/master HEAD conform.\n")
fmt.Printf("Example: checkcommitters -dir master\n")
fmt.Printf(" Will check that all commits from remotes/origin/master..HEAD conform.\n")
os.Exit(1)
}

func Check(source, target string) bool {
mbc := exec.Command("git", "merge-base", source, target)
mbco, err := mbc.CombinedOutput()
if err != nil {
log.Fatalf("Error running `git merge-base remotes/origin/%s remotes/origin/%s` to find merge parent: %v\n", os.Args[2], os.Args[1], err)
log.Fatalf("Error running `git merge-base %s %s` to find merge parent: %v\n", source, target, err)
}
base := strings.TrimSpace(string(mbco))
lc := exec.Command("git", "log", "--format=full", base+"..remotes/origin/"+os.Args[1])

return CheckFromBase(base, source)
}

func CheckFromBase(base string, source string) bool {
lc := exec.Command("git", "log", "--format=full", base+".."+source)
lco, err := lc.CombinedOutput()
if err != nil {
log.Fatalf("Error running `git log --format=full %s..remotes/origin/%s`: %v\n", base, os.Args[1], err)
log.Fatalf("Error running `git log --format=full %s..%s`: %v\n", base, source, err)
}

var failed bool
Expand All @@ -98,10 +140,5 @@ func main() {
}
lco = lco[n+1:]
}
if failed {
fmt.Printf("\n\nThis PR has non-whitelisted committers or authors.\n")
fmt.Printf("Please use git rebase or git filter-branch to ensure every commit\n")
fmt.Printf("is from a whitelisted committer and author.\n")
os.Exit(1)
}
return failed
}
2 changes: 2 additions & 0 deletions go/utils/copyrightshdrs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,8 @@ func CheckProto() bool {
fmt.Printf("ERROR: Wrong copyright header: %v\n", path)
failed = true
}
} else if strings.HasPrefix(path, "../proto/third_party") {
return filepath.SkipDir
}
return nil
})
Expand Down
21 changes: 21 additions & 0 deletions go/utils/prepr/prepr.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash

set -eo pipefail

script_dir=$(dirname "$0")
cd $script_dir/../..

target="master"
if [[ $# -eq 1 ]]; then
target="$1"
fi

# Keep this in sync with Jenkinsfile contents that
# are easy to evaluate locally and might commonly fail.

go get -mod=readonly ./...
./utils/repofmt/check_fmt.sh
./Godeps/verify.sh
go run ./utils/checkcommitters -dir "$target"
go vet -mod=readonly ./...
go run -mod=readonly ./utils/copyrightshdrs/

0 comments on commit ed41e11

Please sign in to comment.