Skip to content

Commit

Permalink
feat: add support for fetch particular branch/file, checkout specific…
Browse files Browse the repository at this point in the history
… file, fetch all
  • Loading branch information
naqvis committed Mar 28, 2023
1 parent 5b41437 commit 47325a3
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .ameba.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Lint/NotNil:
Enabled: false
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: git-repository
version: 1.3.0
version: 1.3.1

dependencies:
connect-proxy:
Expand Down
8 changes: 8 additions & 0 deletions src/git-repository/commands.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
Expand Down
20 changes: 20 additions & 0 deletions src/git-repository/generic.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion src/git-repository/interface.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 47325a3

Please sign in to comment.