File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -11,6 +11,22 @@ function get_current_branch() {
1111 echo " ${ref# refs/ heads/ } "
1212}
1313
14+ # Gets the main branch or dies.
15+ function get_main_branch_or_die() {
16+ # From https://stackoverflow.com/questions/28666357/git-how-to-get-default-branch#comment92366240_50056710
17+ local main_branch
18+ main_branch=" $( git remote show origin | grep " HEAD branch" | sed ' s/.*: //' ) "
19+ [[ -n " ${main_branch} " ]] || die " Failed to get main branch"
20+ echo " ${main_branch} "
21+ }
22+
23+ # Gets the diffbase off of the remote's main branch or dies.
24+ function get_diffbase_or_die() {
25+ diffbase=" $( git merge-base HEAD " origin/${main_branch} " ) "
26+ [[ $? -eq 0 ]] || die " Failed to determine diffbase"
27+ echo " ${diffbase} "
28+ }
29+
1430function get_remote() {
1531 local remote
1632 remote=" $( git config --get remote.origin.url) "
@@ -67,3 +83,13 @@ function branch_exists() {
6783 local branch=" $1 "
6884 git show-ref --verify --quiet " refs/heads/$branch "
6985}
86+
87+ # TODO(do-not-merge): How return multi-line output properly?
88+ # Requires $gitroot.
89+ # Returns the list of .go file paths in $gitroot (with $gitroot prefix)
90+ # that have a comment saying they are generated files.
91+ function quick_get_generated_files {
92+ echo " $( git -C " $gitroot " \
93+ grep -l ' ^// Code generated by .* DO NOT EDIT\.' -- ' *.go' | \
94+ sed -e " s@^@${gitroot} /@" ) "
95+ }
Original file line number Diff line number Diff line change 44
55SCRIPT=" $( python -c ' import os, sys; print(os.path.realpath(sys.argv[1]))' " ${BASH_SOURCE[0]} " ) "
66source " $( dirname " $SCRIPT " ) /../../lib/common.sh"
7+ source " $( dirname " $SCRIPT " ) /../../lib/git.sh"
78
89gitroot=" $( git rev-parse --show-toplevel) "
910[[ $? -eq 0 ]] || die " Current directory is not a git repository."
1516# Various code generation helpers are expected to be in the PATH when called by go generate.
1617export PATH=" $PATH :${gitroot} /tools/generate-helpers"
1718
18- # From https://stackoverflow.com/questions/28666357/git-how-to-get-default-branch#comment92366240_50056710
19- main_branch=" $( git remote show origin | grep " HEAD branch" | sed ' s/.*: //' ) "
20- [[ -n " ${main_branch} " ]] || die " Failed to get main branch"
21-
22- diffbase=" $( git merge-base HEAD " origin/${main_branch} " ) "
23- [[ $? -eq 0 ]] || die " Failed to determine diffbase"
19+ main_branch=" $( get_main_branch_or_die) "
20+ diffbase=" $( get_diffbase_or_die) "
2421
2522generated_files=" $( git -C " $gitroot " grep -l ' ^// Code generated by .* DO NOT EDIT\.' -- ' *.go' | sed -e " s@^@${gitroot} /@" ) "
2623
@@ -57,7 +54,6 @@ function private_gogen() {
5754}
5855
5956[[ " ${# changed_files[@]} " -eq 0 ]] && { ewarn " No relevant changes found in current directory." ; exit 0; }
60-
6157status=0
6258
6359private_gogen " ${changed_files[@]} " && (( status == 0 ))
Original file line number Diff line number Diff line change 88SCRIPT=" $( python -c ' import os, sys; print(os.path.realpath(sys.argv[1]))' " ${BASH_SOURCE[0]} " ) "
99source " $( dirname " $SCRIPT " ) /../../lib/common.sh"
1010source " $( dirname " $SCRIPT " ) /../../setup/packages.sh"
11+ source " $( dirname " $SCRIPT " ) /../../lib/git.sh"
1112
1213check_dependencies
1314
@@ -213,12 +214,8 @@ if [[ -f "${gitroot}/go.mod" ]]; then
213214 export GO111MODULE=on
214215fi
215216
216- # https://stackoverflow.com/a/44750379
217- main_branch=" $( git symbolic-ref refs/remotes/origin/HEAD | sed ' s@^refs/remotes/origin/@@' ) "
218- [[ -n " ${main_branch} " ]] || die " Failed to get main branch"
219-
220- diffbase=" $( git merge-base HEAD " origin/${main_branch} " ) "
221- [[ $? -eq 0 ]] || die " Failed to determine diffbase"
217+ main_branch=$( get_main_branch_or_die)
218+ diffbase=$( get_diffbase_or_die)
222219
223220generated_files=" $( git -C " $gitroot " grep -l ' ^// Code generated by .* DO NOT EDIT\.' -- ' *.go' | sed -e " s@^@${gitroot} /@" ) "
224221
You can’t perform that action at this time.
0 commit comments