Skip to content
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

Fix dependency constraint on ruby-git #1436

Merged
merged 5 commits into from
May 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
## master

* Update Octokit dependency to version 6.0 - [@spencertransier](https://github.com/spencertransier) [#1437](https://github.com/danger/danger/pull/1437)
<!-- Your comment above here -->
* Fixes dependency constraint issue on `ruby-git` - [@ainame](https://github.com/ainame) [#1436](https://github.com/danger/danger/pull/1436)

## 9.3.0

Expand Down
2 changes: 1 addition & 1 deletion danger.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Gem::Specification.new do |spec|
spec.add_runtime_dependency "cork", "~> 0.1"
spec.add_runtime_dependency "faraday", ">= 0.9.0", "< 3.0"
spec.add_runtime_dependency "faraday-http-cache", "~> 2.0"
spec.add_runtime_dependency "git", "~> 1.13.0"
spec.add_runtime_dependency "git", "~> 1.13"
spec.add_runtime_dependency "kramdown", "~> 2.3"
spec.add_runtime_dependency "kramdown-parser-gfm", "~> 1.0"
spec.add_runtime_dependency "no_proxy_fix"
Expand Down
30 changes: 23 additions & 7 deletions lib/danger/scm_source/git_repo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,9 @@ class GitRepo

def diff_for_folder(folder, from: "master", to: "HEAD", lookup_top_level: false)
self.folder = folder
git_top_level = folder
if lookup_top_level
Dir.chdir(folder) do
git_top_level = exec("rev-parse --show-toplevel")
end
end
repo = Git.open git_top_level
git_top_level = find_git_top_level_if_needed!(folder, lookup_top_level)
Copy link
Contributor Author

@ainame ainame Apr 27, 2023

Choose a reason for hiding this comment

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

I extracted logic to raise an error to keep the compatibility, but the behaviour should be the same as before, apart from the error message.


repo = Git.open(git_top_level)

ensure_commitish_exists!(from)
ensure_commitish_exists!(to)
Expand Down Expand Up @@ -168,6 +164,26 @@ def commits_in_branch_count(from, to)
def commit_is_ref?(commit)
/[a-f0-9]{5,40}/ !~ commit
end

def find_git_top_level_if_needed!(folder, lookup_top_level)
git_top_level = Dir.chdir(folder) { exec("rev-parse --show-toplevel") }
return git_top_level if lookup_top_level

# To keep backward compatibility, `GitRepo#diff_for_folder` expects folder
# to be top level git directory or requires `true` to `lookup_top_level`.
# https://github.com/danger/danger/pull/1178
return folder if compare_path(git_top_level, folder)

message = "#{folder} is not the top level git directory. Pass correct path or `true` to `lookup_top_level` option for `diff_for_folder`"
raise ArgumentError, message
end

# Compare given paths as realpath. Return true if both are same.
# `git rev-parse --show-toplevel` returns a path resolving symlink. In rspec, given path can
# be a temporary directory's path created under a symlinked directory `/var`.
def compare_path(path1, path2)
Pathname.new(path1).realdirpath == Pathname.new(path2).realdirpath
end
end
end

Expand Down
6 changes: 4 additions & 2 deletions spec/lib/danger/scm_source/git_repo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,16 @@
@dm = testing_dangerfile
expect do
@dm.env.scm.diff_for_folder(dir + "/subdir")
end.to raise_error(ArgumentError, /path does not exist/)
end.to raise_error(ArgumentError, /is not the top level git directory/)
Copy link
Contributor Author

@ainame ainame Apr 26, 2023

Choose a reason for hiding this comment

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

Previously ArgumentError was caused by Git.open called at git_repo.rb L20 when the path isn't a top-level git directory. But from git gem v1.15.0, Git.open won't cause the error any more.

My change maintains the current intention to keep the backward compatibility but gives a better error message to educate users.

#1178

end
end

it "looks up the top level git folder when requested" do
with_git_repo do |dir|
@dm = testing_dangerfile
@dm.env.scm.diff_for_folder(dir + "/subdir", lookup_top_level: true)
Copy link
Contributor Author

@ainame ainame Apr 27, 2023

Choose a reason for hiding this comment

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

The previous spec didn't assert anything. I made the expectation explicit.

expect do
@dm.env.scm.diff_for_folder(dir + "/subdir", lookup_top_level: true)
end.not_to raise_error
end
end
end
Expand Down