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

Closed
Show file tree
Hide file tree
Changes from 16 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
69 changes: 49 additions & 20 deletions common/lib/dependabot/file_fetchers/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ def commit
rescue Octokit::Conflict => e
raise unless e.message.include?("Repository is empty")
end

/# Returns the path to the shallow-cloned repo
sig { overridable.returns(String) }
def shallow_clone_repo_contents
end/

# Returns the path to the cloned repo
sig { overridable.returns(String) }
Expand Down Expand Up @@ -441,12 +446,11 @@ def codecommit_client
# INTERNAL METHODS (not for use by sub-classes) #
#################################################

sig do
sig {
params(path: String, fetch_submodules: T::Boolean, raise_errors: T::Boolean)
.returns(T::Array[OpenStruct])
end
def _fetch_repo_contents(path, fetch_submodules: false,
raise_errors: true)
.returns(T::Array[OpenStruct]) }

def _fetch_repo_contents(path, fetch_submodules: false, raise_errors: true)
path = path.gsub(" ", "%20")
provider, repo, tmp_path, commit =
_full_specification_for(path, fetch_submodules: fetch_submodules)
Expand Down Expand Up @@ -476,10 +480,10 @@ def _fetch_repo_contents(path, fetch_submodules: false,
retry
end

sig do
sig {
params(provider: String, repo: String, path: String, commit: String)
.returns(T::Array[OpenStruct])
end
.returns(T::Array[OpenStruct]) }

def _fetch_repo_contents_fully_specified(provider, repo, path, commit)
case provider
when "github"
Expand Down Expand Up @@ -831,8 +835,12 @@ def _clone_repo_contents(target_directory:)
" --recurse-submodules=on-demand"
end
# Need to fetch the commit due to the --depth 1 above.
SharedHelpers.run_shell_command("git fetch #{fetch_options.string} origin #{source.commit}")

if isLfsEnabled(path.to_s)
SharedHelpers.run_shell_command("git lfs install")
SharedHelpers.run_shell_command("git-lfs-fetch #{fetch_options.string} origin #{source.commit}")
else
SharedHelpers.run_shell_command("git fetch #{fetch_options.string} origin #{source.commit}")
end
reset_options = StringIO.new
reset_options << "--hard"
reset_options << if submodule_cloning_failed
Expand Down Expand Up @@ -861,20 +869,41 @@ 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 = isLfsEnabled(path) if lfsEnabled.nil?
SharedHelpers.run_shell_command("git-lfs-checkout") if lfsEnabled
commandString = getCommandString(path,lfsEnabled)
# eep commandString
SharedHelpers.run_shell_command(commandString).split("\n").filter_map do |line|
info = line.split

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

next path if type == DependencyFile::Mode::SUBMODULE
end
end
end
end
end
rescue SharedHelpers::HelperSubprocessFailed => spf
Dependabot.logger.warn("LFS is enabled in this repo. Please use an LFS enabled client") if lfsEnabled
Dependabot.logger.error(spf.message)
raise
end

sig { params(path: String).returns(T.nilable(T::Boolean)) }
def isLfsEnabled(path)
filepath = File.join(path,".gitattributes")
lfsEnabled = T.let(true, T::Boolean) if File.exist?(filepath) && File.readable?(filepath) && SharedHelpers.run_shell_command("cat #{filepath} | grep \"filter=lfs\"").include?("filter=lfs")
rescue
# this should not be needed, but I don't trust 'should'
lfsEnabled = T.let(false, T::Boolean)
end

sig { params(path: String, lfsEnabled: T.nilable(T::Boolean)).returns(String) }
def getCommandString(path,lfsEnabled)
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 = "cd #{path};git-lfs ls-files --stage"
return commandString
end

end
end
end
# rubocop:enable Metrics/ClassLength
25 changes: 22 additions & 3 deletions common/lib/dependabot/shared_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -374,12 +374,31 @@ def self.configure_git_to_use_https(host)

sig { params(path: String).void }
def self.reset_git_repo(path)
Dir.chdir(path) do
run_shell_command("git reset HEAD --hard")
run_shell_command("git clean -fx")
if isLfsEnabled(path)
Dir.chdir(path) do
run_shell_command("git-lfs-reset HEAD --hard")
rescue SharedHelpers::HelperSubprocessFailed
Dependabot.logger.warn("LFS is enabled in this repo. Please use an LFS enabled client")
run_shell_command("git clean -fx")
end
else
Dir.chdir(path) do
run_shell_command("git reset HEAD --hard")
run_shell_command("git clean -fx")
end
end
end

sig { params(path: String).returns(T.nilable(T::Boolean)) }
def self.isLfsEnabled(path)
filepath = File.join(path,".gitattributes")
lfsEnabled = T.let(true, T::Boolean) if File.exist?(filepath) && File.readable?(filepath) && SharedHelpers.run_shell_command("cat #{filepath} | grep \"filter=lfs\"").include?("filter=lfs")
rescue
# this should not be needed, but I don't trust 'should'
lfsEnabled = T.let(false, T::Boolean)
end


sig { returns(T::Array[String]) }
def self.find_safe_directories
# to preserve safe directories from global .gitconfig
Expand Down
12 changes: 12 additions & 0 deletions common/lib/dependabot/workspace/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Git < Base

USER = "dependabot[bot]"
EMAIL = T.let("#{USER}@users.noreply.github.com".freeze, String)
lfsEnabled = nil

sig { returns(String) }
attr_reader :initial_head_sha
Expand All @@ -22,6 +23,7 @@ def initialize(path)
super(path)
@initial_head_sha = T.let(head_sha, String)
configure_git
run_shell_command("git lfs install") if isLfsEnabled(path.to_s)
end

sig { returns(T::Boolean) }
Expand Down Expand Up @@ -168,6 +170,16 @@ def run_shell_command(*args, **kwargs)
def debug(message)
Dependabot.logger.debug("[workspace] #{message}")
end

sig { params(path: String).returns(T.nilable(T::Boolean)) }
def isLfsEnabled(path)
filepath = File.join(path,".gitattributes")
lfsEnabled = T.let(true, T::Boolean) if File.exist?(filepath) && File.readable?(filepath) && SharedHelpers.run_shell_command("cat #{filepath} | grep \"filter=lfs\"").include?("filter=lfs")
rescue
# this should not be needed, but I don't trust 'should'
lfsEnabled = T.let(false, T::Boolean)
end

end
end
end
2 changes: 1 addition & 1 deletion common/spec/dependabot/file_fetchers/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1643,7 +1643,7 @@ def fetch_files
end
end

context "when a retryable error occurs" do
context "when a retryable error occurs", focus: true do
let(:retryable_error) do
proc {
raise Dependabot::SharedHelpers::HelperSubprocessFailed.new(
Expand Down
Loading