Added the --gitrepo and --gitdiff switches to .check.rb#521
Conversation
There was a problem hiding this comment.
Code Review
This pull request adds command-line options (--gitdiff, --gitrepo, and --interval) to .check.rb to allow verifying files with changes between Git refs or checking a Git repository and its branch. Feedback includes improving check_git_repository with defensive checks, idiomatic array methods, and cleaner string formatting, as well as optimizing the script to avoid executing git ls-files twice when --gitdiff is disabled.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| def check_git_repository(gemname, tree, errinfo) # rubocop:disable Metrics/MethodLength | ||
| repo = tree["repository"] | ||
| branch = tree["branch"] || "master" | ||
| refs = git_get_remote_refs(repo) | ||
|
|
||
| if refs.nil? | ||
| errinfo.push <<~INFO | ||
| #{gemname}: unable to access the repository or locate a valid branch name. | ||
| INFO | ||
| elsif refs.any?("refs/heads/#{branch}") | ||
| # OK | ||
| elsif refs.any?("refs/tags/#{branch}") | ||
| errinfo.push <<~INFO | ||
| WARN: #{gemname}: instead of branches, the tag "#{branch}" is used. | ||
| INFO | ||
| else | ||
| candidates = refs.grep(%r{^refs/heads/}).join("\n").gsub(%r{^refs/heads/}, "").tr("\n", " ") | ||
| errinfo.push <<~INFO | ||
| #{gemname}: branch "#{branch}" is missing. | ||
| \tcandidates: #{candidates} | ||
| INFO | ||
| end | ||
| end |
There was a problem hiding this comment.
There are a few improvements we can make to check_git_repository:
- Defensive Programming: If
repositoryis missing or empty in the YAML file,repowill benilor empty. Callinggit_get_remote_refswithnilwill run a failing git command. We should guard against this and return early. - Idiomatic Membership Check: Use
Array#include?instead ofArray#any?with an argument for checking exact string matching. It is more standard and compatible. - Cleaner String Manipulation: Instead of joining the array with newlines, performing a regex replacement, and then translating newlines back to spaces, we can map over the array using
delete_prefixand join them with spaces directly.
def check_git_repository(gemname, tree, errinfo) # rubocop:disable Metrics/MethodLength
repo = tree["repository"]
return if repo.to_s.empty?
branch = tree["branch"] || "master"
refs = git_get_remote_refs(repo)
if refs.nil?
errinfo.push <<~INFO
#{gemname}: unable to access the repository or locate a valid branch name.
INFO
elsif refs.include?("refs/heads/#{branch}")
# OK
elsif refs.include?("refs/tags/#{branch}")
errinfo.push <<~INFO
WARN: #{gemname}: instead of branches, the tag "#{branch}" is used.
INFO
else
candidates = refs.grep(%r{\Arefs/heads/}).map { |r| r.delete_prefix("refs/heads/") }.join(" ")
errinfo.push <<~INFO
#{gemname}: branch "#{branch}" is missing.
\tcandidates: #{candidates}
INFO
end
end| if gitdiff | ||
| case ARGV.size | ||
| when 1, 2 | ||
| # do nothing | ||
| else | ||
| warn banner | ||
| exit 1 | ||
| end | ||
| cmd = %w[git diff -z --name-only --diff-filter=ACMR] | ||
| cmd.concat ARGV | ||
| cmd.concat %w[-- :^*/*] | ||
| gemfiles = run_and_split_nul(cmd) | ||
| else | ||
| gemfiles = run_and_split_nul(%w[git ls-files -z :^*/*]) | ||
| end | ||
|
|
||
| entries = run_and_split_nul(%w[git ls-files -z :^*/*]) |
There was a problem hiding this comment.
When --gitdiff is not specified, git ls-files is executed twice: once to populate gemfiles and once to populate entries. We can optimize this by running git ls-files once, storing it in entries, and then assigning gemfiles = entries in the else branch.
entries = run_and_split_nul(%w[git ls-files -z :^*/*])
if gitdiff
case ARGV.size
when 1, 2
# do nothing
else
warn banner
exit 1
end
cmd = %w[git diff -z --name-only --diff-filter=ACMR]
cmd.concat ARGV
cmd.concat %w[-- :^*/*]
gemfiles = run_and_split_nul(cmd)
else
gemfiles = entries
endThe purpose is to enable verification of the validity of Git repositories and branches. It actually accesses the URL specified by the "repository" in the ".gem" file and retrieves the branch and tag names. If the "branch" in the ".gem" file is not included in the retrieved reference name, it reports an error. Additionally, if the "branch" in the ".gem" file is actually a tag name, it also reports an error. Furthermore, the `.check.rb --interval` switch has been added. The `--gitrepo` switch is equivalent to web scraping from the perspective of the target server. Therefore, unlimited consecutive access may be restricted by the target server’s rate limits. This switch allows specifying the interval between consecutive accesses as a real number in seconds. The default value is 1.2 seconds.
The goal is to enable efficient verification of specific changes rather than verifying all files. Using `.check.rb --gitdiff base-ref head-ref` allows verifying only the files that have changed between Git commits.
The `.check.rb --gitrepo` switch now verifies the validity of the Git repository and branch specified by the ".gem" file. The actual targets for verification are the ".gem" files detected by the `.check.rb --gitdiff` switch. Although the logic is triggered by "push" events as well, the current workflow configuration is limited to pull requests only.
editorconfig-check は構文を理解せず、単純なインデントサイズしか確認しないためです。 ref. https://github.com/super-linter/super-linter/blob/c358755/README.md#configure-super-linter ref. https://github.com/editorconfig-checker/editorconfig-checker/blob/dbb0dbb/README.md#configuration
Overview: This allows checking against a Git repository and a specified branch. For pull requests, only modified or added “.gem” files are checked.