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

Pushing changes in progress for git-lfs support adding to entire ecosystem. #9766

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
28 changes: 22 additions & 6 deletions common/lib/dependabot/file_fetchers/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -861,19 +861,35 @@ def decode_binary_string(str)

sig { params(path: String).returns(T::Array[String]) }
def find_submodules(path)
SharedHelpers.run_shell_command(
<<~CMD
git -C #{path} ls-files --stage
CMD
).split("\n").filter_map do |line|
lfsEnabled = true
commandString = getCommandString(path,lfsEnabled)
/debugger
p commandString/
SharedHelpers.run_shell_command(commandString
).split("\n").filter_map do |line|
Copy link
Member

Choose a reason for hiding this comment

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

I think this could be simplified a bit without the helper methods and by using Dir.chdir

Suggested change
lfsEnabled = true
commandString = getCommandString(path,lfsEnabled)
/debugger
p commandString/
SharedHelpers.run_shell_command(commandString
).split("\n").filter_map do |line|
lfs_enabled = # ...
git_bin = lfs_enabled ? "git-lfs" : "git"
Dir.chdir(path) do
SharedHelpers.run_shell_command("#{git_bin} ls-files --stage")
end.split("\n").filter_map do |line|

info = line.split

type = info.first
path = T.must(info.last)

debugger
next path if type == DependencyFile::Mode::SUBMODULE
end
end

def getCommandString(path,lfsEnabled)
#the HEREDOC command will see any stray spaces.
return "git -C #{path} ls-files --stage" unless lfsEnabled
Dependabot.logger.warn("LFS is enabled in this repo. Please use an LFS enabled client")
commandString = "CWD=\"#{__dir__}\";cd #{path};git-lfs ls-files --stage;cd $CWD"
Copy link
Member

Choose a reason for hiding this comment

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

I suggested something a little simpler above, but just adding a couple notes:

  • __dir__ is the name of the directory that contains this source code file, not the current working directory of the process
  • Rather than setting an environment variable for the directory, this is a good use case for Dir.chdir. This method changes into the given directory, runs whatever is inside block, and then changes back to the original directory.

# return "\"CWD=`pwd`;cd #{path};#git-lfs ls-files --stage;cd $CWD\""
return commandString
end

def getGitCommand(lfsEnabled)
return "git" unless !!lfsEnabled
Dependabot.logger.warn("LFS is enabled in this repo. Please use an LFS enabled client")
return "git-lfs"
end
end
end
end
Expand Down
36 changes: 36 additions & 0 deletions common/spec/dependabot/file_fetchers/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,42 @@ def fetch_files
before do
allow(file_fetcher_instance).to receive(:commit).and_return("sha")
end
#start of lfs testing
context "with data stored in git-lfs" do
# debugger
# let(:source) do
# Dependabot::Source.new(
# provider: "github",
# repo: repo,
# directory: directory
# )
# end
let(:url) { "https://api.github.com/repos/dependabot-fixtures/dependabot-yarn-lfs-fixture/contents/" }
let(:repo) { "dependabot-fixtures/dependabot-yarn-lfs-fixture" }
let(:repo_contents_path) { Dir.mktmpdir }
after { FileUtils.rm_rf(repo_contents_path) }

let(:file_fetcher_instance) do
described_class.new(source: source, credentials: credentials, repo_contents_path: repo_contents_path)
end

it "pulls files from lfs after cloning" do
# Calling #files triggers the clone
expect(file_fetcher_instance.files.map(&:name)).to contain_exactly("package.json", "yarn.lock", ".yarnrc.yml")
expect(
File.read(
File.join(repo_contents_path, ".yarn", "releases", "yarn-3.2.4.cjs")
)
).to start_with("#!/usr/bin/env node")

# LFS files not needed by dependabot are not pulled
expect(
File.read(
File.join(repo_contents_path, ".pnp.cjs")
)
).to start_with("version https://git-lfs.github.com/spec/v1")
end
end

Copy link
Member

Choose a reason for hiding this comment

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

Can we have unit tests covering these new scenarios?

context "with a GitHub source" do
its(:length) { is_expected.to eq(1) }
Expand Down
Loading