-
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?
Conversation
misberner
left a comment
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.
I like it! However, wdyt about the following: rather than basing this on top of the smart-branch tool and marker commits, follow the quickstyle model of identifying where the current branch diverged from master, and use that as a basis for "what has changed"?
In effect, I think the differences would be:
quickgogenwould work for people not usingsmart-branch,smart-gogenwould not- If you have multiple smart branches stacked on top of each other,
smart-gogenwould only consider files changed in the top branch,quickgogenwould consider all files since master - For somebody using
smart-branchwith only a single branch on top of master, the behavior would be the same.
WDYT?
scripts/dev/quickgogen.sh
Outdated
| IFS=$'\n' read -d '' -r -a gofiles < <( | ||
| printf '%s\n' "${changed_files[@]}" | | ||
| grep '\.go$' | ||
| ) |
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.
This might be a quickstyle artifact, but the indent changes here
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.
I'm not sure which part you're referring to, sorry if I'm missing something obvious. Do you mean the file redirect subshell (is this the right term?) shouldn't be indented? This line has two spaces, which matches the opening IFS= line
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.
Ahhh never mind, I see now - tabs v spaces. Judging from quickstyle we prefer tabs over spaces.
FWIW quickstyle didn't do anything about this when run, and nothing in goland highlighted the difference to me. If there's an option I can turn on somewhere for quickstyle I'll try to find one, same for goland.
| for f in "${gofiles[@]}"; do dirname "$f"; done | | ||
| sort | uniq) | ||
|
|
||
| einfo "Running go generate..." |
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.
indent changes again
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.
Done
scripts/dev/quickgogen.sh
Outdated
|
|
||
| [[ "${status}" -eq 0 ]] && exit 0 | ||
| efatal "An error occurred while generating files" | ||
| exit 1 No newline at end of file |
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.
add a newline at the end of file
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.
Done
scripts/dev/quickgogen.sh
Outdated
| einfo "Running go generate..." | ||
| 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 comment
The reason will be displayed to describe this comment to others. Learn more.
This probably won't work because the PATH setting is missing. You need to copy over the
export PATH="$PATH:${gitroot}/tools/generate-helpers"
from gogen.sh.
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.
Good call; done.
scripts/dev/quickgogen.sh
Outdated
| fi | ||
|
|
||
| # From https://stackoverflow.com/questions/28666357/git-how-to-get-default-branch#comment92366240_50056710 | ||
| main_branch="$(git remote show origin | grep "HEAD branch" | sed 's/.*: //')" |
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.
Can we reuse some of this stuff between quickgogen and quickstyle?
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.
To be honest my bash-fu is quite weak and the first two times I tried this it came out squirrely.
I've moved main_branch and diffbase to git.sh, they return single strings and seem easy enough.
I don't know e.g. how to move something like the processing of changed_files there, because as soon as I echo the variable to return it (or print it out) I think the new lines are going to be converted to spaces, correct?
I welcome the opportunity to learn here. How can I capture multi-line string output from a function call in bash?
…t_generated_files unclear - multiline output
8aadcce to
3a15d1b
Compare
ebensh
left a comment
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.
I'd like to either review and finish this or let it die. Please let me know your preference @viswajithiii and @misberner - is it even worth cleaning this up?
I do think it's valuable. I'll try to make another pass today or on Monday. |
| 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Add new line
| 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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure what this referred to
| 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/.*: //')" |
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)
| diffbase="$(git merge-base HEAD "origin/${main_branch}")" | ||
| [[ $? -eq 0 ]] || die "Failed to determine diffbase" |
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.
| diffbase="$(git merge-base HEAD "origin/${main_branch}")" | |
| [[ $? -eq 0 ]] || die "Failed to determine diffbase" | |
| local diffbase | |
| diffbase="$(git merge-base HEAD "origin/${main_branch}")" || die "Failed to determine diffbase" |
| # 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 comment
The reason will be displayed to describe this comment to others. Learn more.
| function quick_get_generated_files { | |
| function quick_get_generated_files() { |
| 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure what this referred to
| function quick_get_generated_files { | ||
| 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 comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer
| sed -e "s@^@${gitroot}/@")" | |
| awk -v PREFIX="${gitroot}/" '{print PREFIX $0}')" |
You never know who might depend on using @ as a character in their directory names..
| main_branch="$(get_main_branch_or_die)" | ||
| 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 comment
The reason will be displayed to describe this comment to others. Learn more.
See above wrt awk instead of sed
|
|
||
| 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 comment
The reason will be displayed to describe this comment to others. Learn more.
| sort | uniq) | |
| sort -u) |
slightly more concise, and just as POSIX-compliant
| einfo "Running go generate..." | ||
| 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 comment
The reason will be displayed to describe this comment to others. Learn more.
I was going to remark that this won't work due to $PATH, and then realized that the export PATH= line is present above, and that private_gogen is derived from gogen.sh. Given that gogen is part of the workflow repo, I wonder if the whole script could be simplified slightly if we were to just rely on it? We won't save much, but for consistency it would be nice to just delegate to gogen <list of dirs with changed go files that aren't generated> at the end
| [[ "${#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 comment
The reason will be displayed to describe this comment to others. Learn more.
status is always 0 here
Co-authored-by: Viswajith Venugopal <[email protected]>
Tested:
Checked
roxhelp --list-allto confirm the expected comment was listed next to the commandRan the command from the root of rox repo after making changes to a datastore file, checked that the correct mocks were re-generated.