From 47325a3ec5fbcf1e84c9b266bd92c5744b885d56 Mon Sep 17 00:00:00 2001 From: Ali Naqvi Date: Tue, 28 Mar 2023 10:50:23 +0800 Subject: [PATCH] feat: add support for fetch particular branch/file, checkout specific file, fetch all --- .ameba.yml | 2 ++ shard.yml | 2 +- src/git-repository/commands.cr | 8 ++++++++ src/git-repository/generic.cr | 20 ++++++++++++++++++++ src/git-repository/interface.cr | 2 +- 5 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 .ameba.yml diff --git a/.ameba.yml b/.ameba.yml new file mode 100644 index 0000000..b263235 --- /dev/null +++ b/.ameba.yml @@ -0,0 +1,2 @@ + Lint/NotNil: + Enabled: false \ No newline at end of file diff --git a/shard.yml b/shard.yml index da57914..ec31674 100644 --- a/shard.yml +++ b/shard.yml @@ -1,5 +1,5 @@ name: git-repository -version: 1.3.0 +version: 1.3.1 dependencies: connect-proxy: diff --git a/src/git-repository/commands.cr b/src/git-repository/commands.cr index 83954d0..5fd817e 100644 --- a/src/git-repository/commands.cr +++ b/src/git-repository/commands.cr @@ -25,10 +25,18 @@ struct GitRepository::Commands run_git("fetch", {"--depth", "1", "origin", branch}) end + def fetch_all(branch : String) + run_git("fetch", {"origin", branch}) + end + def checkout(branch : String) run_git("checkout", {branch}) end + def checkout(branch : String, file : String) + run_git("checkout", {branch, "--", file}) + end + def reset run_git("reset", {"--hard"}) run_git("clean", {"-fd", "-fx"}) diff --git a/src/git-repository/generic.cr b/src/git-repository/generic.cr index 416e5f3..1c65326 100644 --- a/src/git-repository/generic.cr +++ b/src/git-repository/generic.cr @@ -125,4 +125,24 @@ class GitRepository::Generic < GitRepository::Interface git.commits(depth: 1).first end end + + def fetch_commit(branch : String, commit : String, source_file : String, download_to_path : String | Path) : Commit + download_to = download_to_path.to_s + + # download the commit + create_temp_folder do |temp_folder| + git = Commands.new(temp_folder) + git.init + git.add_origin @repository + git.fetch_all branch # git fetch origin branch + git.checkout branch # git checkout branch or FETCH_HEAD + git.checkout commit, source_file # git checkout FETCH_HEAD or sha1 -- source_file + + move_into_place(temp_folder, download_to) + + # grab the current commit hash + git.path = download_to + git.commits(depth: 1).first + end + end end diff --git a/src/git-repository/interface.cr b/src/git-repository/interface.cr index d94550d..eec2ae0 100644 --- a/src/git-repository/interface.cr +++ b/src/git-repository/interface.cr @@ -34,7 +34,7 @@ abstract class GitRepository::Interface temp_folder end - def create_temp_folder + def create_temp_folder(&) temp_folder = create_temp_folder begin yield temp_folder