lib/extract_repo.rb in extract-repo-0.0.2 vs lib/extract_repo.rb in extract-repo-0.0.3
- old
+ new
@@ -1,31 +1,44 @@
require "pry"
require "English"
require "foobara/all"
+# TODO: allow extracting from a local repo and default to that repo as "."
class ExtractRepo < Foobara::Command
inputs do
repo_url :string, :required
paths [:string], :required
- output_path :string, default: "/#{Dir.home}/tmp/extract"
+ output_path :string, default: "#{Dir.home}/tmp/extract"
+ delete_extracted :boolean, default: false
end
- attr_accessor :file_paths
+ attr_accessor :file_paths, :absolute_repo_path
def execute
+ determine_absolute_repo_path
mk_extract_dir
rm_old_repo
clone_repo
remove_origin
remove_tags
determine_paths
determine_historic_paths
filter_repo
remove_replaces
+
+ delete_extracted_paths if delete_extracted?
end
+ def determine_absolute_repo_path
+ self.absolute_repo_path = if local_repository?
+ File.absolute_path(repo_url)
+ else
+ repo_url
+ end
+ end
+
def chdir(dir, &)
Dir.chdir(File.expand_path(dir), &)
end
def repo_dir
@@ -44,14 +57,18 @@
sh "rm -rf #{repo_dir}"
end
def clone_repo
chdir output_path do
- sh "git clone #{repo_url}"
+ sh "git clone #{absolute_repo_path}"
end
end
+ def local_repository?
+ !URI.parse(repo_url).scheme
+ end
+
def remove_origin
chdir repo_dir do
sh "git remote rm origin"
end
end
@@ -65,11 +82,14 @@
def remove_replaces
chdir repo_dir do
replace_sha1s = sh "git replace -l", silent: true
replace_sha1s = replace_sha1s.chomp.split("\n")
replace_sha1s.each do |replace_sha1|
+ # It seems like some versions of git do not create replace markers when doing this
+ # :nocov:
sh "git replace -d #{replace_sha1}"
+ # :nocov:
end
end
end
def determine_paths
@@ -124,9 +144,24 @@
def filter_repo
chdir repo_dir do
path_args = file_paths.map { |path| "--path #{path}" }.join(" ")
sh "git-filter-repo #{path_args} --force --prune-degenerate always"
end
+ end
+
+ # This feels dangerous however it is opt-in at least
+ def delete_extracted_paths
+ return unless local_repository?
+
+ Dir.chdir absolute_repo_path do
+ paths.each do |path|
+ FileUtils.rm_r path
+ end
+ end
+ end
+
+ def delete_extracted?
+ delete_extracted
end
def sh(cmd, dry_run: false, silent: false)
unless silent
puts cmd