-
Notifications
You must be signed in to change notification settings - Fork 0
Add quickgogen command to gogen touched directories/files #75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
822d744
b6dd362
4469e7c
c5b8346
5083bdb
a5c15cc
40a0261
5a8ba19
fee046e
bd6b9a8
4702597
3a15d1b
d6dcf1d
affd5e3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../scripts/dev/quickgogen.sh |
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -11,6 +11,22 @@ function get_current_branch() { | |||||||||
echo "${ref#refs/heads/}" | ||||||||||
} | ||||||||||
|
||||||||||
# Gets the main branch or dies. | ||||||||||
function get_main_branch_or_die() { | ||||||||||
# From https://stackoverflow.com/questions/28666357/git-how-to-get-default-branch#comment92366240_50056710 | ||||||||||
local main_branch | ||||||||||
main_branch="$(git remote show origin | grep "HEAD branch" | sed 's/.*: //')" | ||||||||||
[[ -n "${main_branch}" ]] || die "Failed to get main branch" | ||||||||||
echo "${main_branch}" | ||||||||||
} | ||||||||||
|
||||||||||
# Gets the diffbase off of the remote's main branch or dies. | ||||||||||
function get_diffbase_or_die() { | ||||||||||
diffbase="$(git merge-base HEAD "origin/${main_branch}")" | ||||||||||
[[ $? -eq 0 ]] || die "Failed to determine diffbase" | ||||||||||
Comment on lines
+25
to
+26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
echo "${diffbase}" | ||||||||||
} | ||||||||||
|
||||||||||
function get_remote() { | ||||||||||
local remote | ||||||||||
remote="$(git config --get remote.origin.url)" | ||||||||||
|
@@ -67,3 +83,13 @@ function branch_exists() { | |||||||||
local branch="$1" | ||||||||||
git show-ref --verify --quiet "refs/heads/$branch" | ||||||||||
} | ||||||||||
|
||||||||||
# TODO(do-not-merge): How return multi-line output properly? | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assume this works now? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure what this referred to |
||||||||||
# Requires $gitroot. | ||||||||||
# Returns the list of .go file paths in $gitroot (with $gitroot prefix) | ||||||||||
# that have a comment saying they are generated files. | ||||||||||
function quick_get_generated_files { | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
echo "$(git -C "$gitroot" \ | ||||||||||
grep -l '^// Code generated by .* DO NOT EDIT\.' -- '*.go' | \ | ||||||||||
sed -e "s@^@${gitroot}/@")" | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer
Suggested change
You never know who might depend on using |
||||||||||
} | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add new line |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,64 @@ | ||||||
#!/usr/bin/env bash | ||||||
|
||||||
# Usage: Runs gogen for all Go files that have changed between the current code and master. | ||||||
|
||||||
SCRIPT="$(python -c 'import os, sys; print(os.path.realpath(sys.argv[1]))' "${BASH_SOURCE[0]}")" | ||||||
source "$(dirname "$SCRIPT")/../../lib/common.sh" | ||||||
source "$(dirname "$SCRIPT")/../../lib/git.sh" | ||||||
|
||||||
gitroot="$(git rev-parse --show-toplevel)" | ||||||
[[ $? -eq 0 ]] || die "Current directory is not a git repository." | ||||||
|
||||||
if [[ -f "${gitroot}/go.mod" ]]; then | ||||||
export GO111MODULE=on | ||||||
fi | ||||||
|
||||||
# Various code generation helpers are expected to be in the PATH when called by go generate. | ||||||
export PATH="$PATH:${gitroot}/tools/generate-helpers" | ||||||
|
||||||
main_branch="$(get_main_branch_or_die)" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, |
||||||
diffbase="$(get_diffbase_or_die)" | ||||||
|
||||||
generated_files="$(git -C "$gitroot" grep -l '^// Code generated by .* DO NOT EDIT\.' -- '*.go' | sed -e "s@^@${gitroot}/@")" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See above wrt |
||||||
|
||||||
IFS=$'\n' read -d '' -r -a changed_files < <( | ||||||
{ | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might've been inconsistent in the scripts before, but here you're using tab indent vs 2 spaces in the previous file. Imho 2 spaces is much easier on the eyes. We typically only use tabs where prescribed by the language (Go, Makefile). Quite a shame, given that tab indent is way superior to space indent, but eh... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lol, looks like I asked you to use this indent back in August? :P |
||||||
git diff "$diffbase" --name-status . | | ||||||
sed -n -E -e "s@^[AM][[:space:]]+|^R[^[:space:]]*[[:space:]]+[^[:space:]]+[[:space:]]+@${gitroot}/@p" ; | ||||||
echo "$generated_files" ; echo "$generated_files" | ||||||
} | sort | uniq -u) || true | ||||||
|
||||||
function private_gogen() { | ||||||
local status=0 | ||||||
local changed_files=("$@") | ||||||
local gofiles | ||||||
|
||||||
IFS=$'\n' read -d '' -r -a gofiles < <( | ||||||
printf '%s\n' "${changed_files[@]}" | | ||||||
grep '\.go$' | ||||||
) | ||||||
[[ "${#gofiles[@]}" == 0 ]] && return 0 | ||||||
|
||||||
IFS=$'\n' read -d '' -r -a godirs < <( | ||||||
for f in "${gofiles[@]}"; do dirname "$f"; done | | ||||||
sort | uniq) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
slightly more concise, and just as POSIX-compliant |
||||||
|
||||||
einfo "Running go generate..." | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. indent changes again There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||||||
for dir in "${godirs[@]}"; do | ||||||
einfo "...Generating for ${dir}" | ||||||
( cd "$dir" && go generate "./" ) && (( status == 0 )) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was going to remark that this won't work due to |
||||||
status=$? | ||||||
done | ||||||
|
||||||
return "${status}" | ||||||
} | ||||||
|
||||||
[[ "${#changed_files[@]}" -eq 0 ]] && { ewarn "No relevant changes found in current directory."; exit 0; } | ||||||
status=0 | ||||||
|
||||||
private_gogen "${changed_files[@]}" && (( status == 0 )) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||
status=$? | ||||||
|
||||||
[[ "${status}" -eq 0 ]] && exit 0 | ||||||
efatal "An error occurred while generating files" | ||||||
exit 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you want to
|| die
here as well? (Yes, doing||
after an assignment is perfectly legal)