Skip to content

Commit 3a15d1b

Browse files
committed
Try to increase code reuse - main_branch and diffbase added, quick_get_generated_files unclear - multiline output
1 parent 4702597 commit 3a15d1b

3 files changed

Lines changed: 32 additions & 13 deletions

File tree

lib/git.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
1430
function 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+
}

scripts/dev/quickgogen.sh

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
SCRIPT="$(python -c 'import os, sys; print(os.path.realpath(sys.argv[1]))' "${BASH_SOURCE[0]}")"
66
source "$(dirname "$SCRIPT")/../../lib/common.sh"
7+
source "$(dirname "$SCRIPT")/../../lib/git.sh"
78

89
gitroot="$(git rev-parse --show-toplevel)"
910
[[ $? -eq 0 ]] || die "Current directory is not a git repository."
@@ -15,12 +16,8 @@ fi
1516
# Various code generation helpers are expected to be in the PATH when called by go generate.
1617
export 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

2522
generated_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-
6157
status=0
6258

6359
private_gogen "${changed_files[@]}" && (( status == 0 ))

scripts/dev/quickstyle.sh

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
SCRIPT="$(python -c 'import os, sys; print(os.path.realpath(sys.argv[1]))' "${BASH_SOURCE[0]}")"
99
source "$(dirname "$SCRIPT")/../../lib/common.sh"
1010
source "$(dirname "$SCRIPT")/../../setup/packages.sh"
11+
source "$(dirname "$SCRIPT")/../../lib/git.sh"
1112

1213
check_dependencies
1314

@@ -213,12 +214,8 @@ if [[ -f "${gitroot}/go.mod" ]]; then
213214
export GO111MODULE=on
214215
fi
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

223220
generated_files="$(git -C "$gitroot" grep -l '^// Code generated by .* DO NOT EDIT\.' -- '*.go' | sed -e "s@^@${gitroot}/@")"
224221

0 commit comments

Comments
 (0)