Skip to content

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

Open
wants to merge 14 commits into
base: master
Choose a base branch
from

Conversation

ebensh
Copy link

@ebensh ebensh commented Aug 3, 2021

Tested:

Checked roxhelp --list-all to confirm the expected comment was listed next to the command
Ran the command from the root of rox repo after making changes to a datastore file, checked that the correct mocks were re-generated.

@ebensh ebensh requested a review from misberner August 3, 2021 13:02
Copy link
Contributor

@misberner misberner left a 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:

  • quickgogen would work for people not using smart-branch, smart-gogen would not
  • If you have multiple smart branches stacked on top of each other, smart-gogen would only consider files changed in the top branch, quickgogen would consider all files since master
  • For somebody using smart-branch with only a single branch on top of master, the behavior would be the same.

WDYT?

@ebensh ebensh changed the title Add smart-gogen command to gogen touched directories/files Add quickgogen command to gogen touched directories/files Aug 4, 2021
@ebensh ebensh requested a review from misberner August 6, 2021 12:51
IFS=$'\n' read -d '' -r -a gofiles < <(
printf '%s\n' "${changed_files[@]}" |
grep '\.go$'
)
Copy link
Contributor

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

Copy link
Author

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

Copy link
Author

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..."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indent changes again

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


[[ "${status}" -eq 0 ]] && exit 0
efatal "An error occurred while generating files"
exit 1
Copy link
Contributor

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

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

einfo "Running go generate..."
for dir in "${godirs[@]}"; do
einfo "...Generating for ${dir}"
( cd "$dir" && go generate "./" ) && (( status == 0 ))
Copy link
Contributor

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.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call; done.

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/.*: //')"
Copy link
Contributor

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?

Copy link
Author

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?

Copy link
Author

@ebensh ebensh left a 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?

@misberner
Copy link
Contributor

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}/@")"
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add new line

@@ -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?
Copy link
Contributor

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?

Copy link
Contributor

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/.*: //')"
Copy link
Contributor

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)

Comment on lines +25 to +26
diffbase="$(git merge-base HEAD "origin/${main_branch}")"
[[ $? -eq 0 ]] || die "Failed to determine diffbase"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function quick_get_generated_files {
function quick_get_generated_files() {

@@ -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?
Copy link
Contributor

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}/@")"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer

Suggested change
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}/@")"
Copy link
Contributor

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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 ))
Copy link
Contributor

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 ))
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants