Skip to content

Commit

Permalink
Merge pull request #7411 from dependabot/deivid-rodriguez/bundler-sym…
Browse files Browse the repository at this point in the history
…links

Fix fetching files in symlinked folders
  • Loading branch information
deivid-rodriguez authored Aug 4, 2023
2 parents 5a45f2f + 6770cc3 commit c21e69e
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 8 deletions.
2 changes: 1 addition & 1 deletion bin/dry-run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ def show_diff(original_file, updated_file)
removed_lines = diff.count { |line| line.start_with?("-") }

puts
puts " ± #{original_file.name}"
puts " ± #{original_file.realpath}"
puts " ~~~"
puts diff.map { |line| " " + line }.join
puts " ~~~"
Expand Down
6 changes: 5 additions & 1 deletion common/lib/dependabot/dependency_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def initialize(name:, content:, directory: "/", type: "file",
@type = type

begin
@mode = File.stat((symlink_target || path).sub(%r{^/}, "")).mode.to_s(8)
@mode = File.stat(realpath).mode.to_s(8)
rescue StandardError
@mode = mode
end
Expand Down Expand Up @@ -76,6 +76,10 @@ def path
Pathname.new(File.join(directory, name)).cleanpath.to_path
end

def realpath
(symlink_target || path).sub(%r{^/}, "")
end

def ==(other)
return false unless other.instance_of?(self.class)

Expand Down
19 changes: 17 additions & 2 deletions common/lib/dependabot/file_fetchers/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,19 +159,34 @@ def fetch_file_from_host(filename, type: "file", fetch_submodules: false)

path = Pathname.new(File.join(directory, filename)).cleanpath.to_path
content = _fetch_file_content(path, fetch_submodules: fetch_submodules)
type = "symlink" if @linked_paths.key?(path.gsub(%r{^/}, ""))
clean_path = path.gsub(%r{^/}, "")

linked_path = symlinked_subpath(clean_path)
type = "symlink" if linked_path
symlink_target = clean_path.sub(linked_path, @linked_paths.dig(linked_path, :path)) if type == "symlink"

DependencyFile.new(
name: Pathname.new(filename).cleanpath.to_path,
directory: directory,
type: type,
content: content,
symlink_target: @linked_paths.dig(path.gsub(%r{^/}, ""), :path)
symlink_target: symlink_target
)
rescue *CLIENT_NOT_FOUND_ERRORS
raise Dependabot::DependencyFileNotFound, path
end

# Finds the first subpath in path that is a symlink
def symlinked_subpath(path)
subpaths(path).find { |subpath| @linked_paths.key?(subpath) }
end

# Given a "foo/bar/baz" path, returns ["foo", "foo/bar", "foo/bar/baz"]
def subpaths(path)
components = path.split("/")
components.map { |component| components[0..components.index(component)].join("/") }
end

def repo_contents(dir: ".", ignore_base_directory: false,
raise_errors: true, fetch_submodules: false)
dir = File.join(directory, dir) unless ignore_base_directory
Expand Down
3 changes: 1 addition & 2 deletions common/lib/dependabot/pull_request_creator/github.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,7 @@ def create_tree
end

{
path: (file.symlink_target ||
file.path).sub(%r{^/}, ""),
path: file.realpath,
mode: (file.mode || "100644"),
type: "blob"
}.merge(content)
Expand Down
3 changes: 1 addition & 2 deletions common/lib/dependabot/pull_request_updater/github.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,7 @@ def create_tree
end

{
path: (file.symlink_target ||
file.path).sub(%r{^/}, ""),
path: file.realpath,
mode: "100644",
type: "blob"
}.merge(content)
Expand Down

0 comments on commit c21e69e

Please sign in to comment.