Skip to content

Commit

Permalink
feat: add file listings
Browse files Browse the repository at this point in the history
  • Loading branch information
stakach committed Apr 15, 2024
1 parent 47325a3 commit d8439fd
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 3 deletions.
24 changes: 24 additions & 0 deletions spec/file_list_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require "./spec_helper"

module GitRepository
describe GitRepository::Generic do
client = Generic.new("https://github.com/PlaceOS/drivers")

it "should list files in the repository" do
# all files
files = client.file_list
files.includes?("drivers/ubipark/api.cr").should be_true

# files from an earlier commit
files = client.file_list("c0536e9c7b5bfe6e40e850717f377846090f50ea")
files.includes?("drivers/ubipark/api.cr").should be_false

# files filtered by folder
files.includes?("shard.yml").should be_true

files = client.file_list(path: "drivers/")
files.includes?("drivers/ubipark/api.cr").should be_true
files.includes?("shard.yml").should be_false
end
end
end
2 changes: 1 addition & 1 deletion src/git-repository/adapters/dev-azure.cr
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class GitRepository::Adapters::DevAzure < GitRepository::Generic
commits.concat Commits.from_json(response.body).to_commits
commits.uniq!(&.hash)
end
commits.sort { |a, b| b.time <=> a.time }
commits.sort { |ref_a, ref_b| ref_b.time <=> ref_a.time }
in String, Nil
params["searchCriteria.itemPath"] = "/#{file}" if file
uri.query_params = params
Expand Down
21 changes: 19 additions & 2 deletions src/git-repository/commands.cr
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,20 @@ struct GitRepository::Commands
end

# clones just the repository history
def clone_logs(repository_uri : String, branch : String, depth : Int? = 50)
args = [repository_uri, "-b", branch]
def clone_logs(repository_uri : String, branch : String? = nil, depth : Int? = 50)
args = [repository_uri]
args.concat({"-b", branch.to_s}) if branch
args.concat({"--depth", depth.to_s}) if depth
# bare repo, no file data, quiet clone, . = clone into current directory
args.concat({"--bare", "--filter=blob:none", "-q", "."})
run_git("clone", args)
end

# clones the git data for the list of files
def clone_file_tree(repository_uri : String)
run_git("clone", {"--filter=blob:none", "--no-checkout", "--depth", "1", repository_uri, "-q", "."})
end

# pull latest logs
def pull_logs
run_git("fetch", {"origin", "+refs/heads/*:refs/heads/*", "--prune"})
Expand All @@ -62,6 +68,17 @@ struct GitRepository::Commands
commits(file, depth)
end

# list files in the repositories
def list_files(path : String? = nil) : Array(String)
args = ["--name-only", "-r", "HEAD"]
args << path.to_s if path
stdout = run_git("ls-tree", args)
stdout.tap(&.rewind)
.each_line
.reject(&.empty?)
.to_a
end

LOG_FORMAT = "format:%H%n%cI%n%an%n%s%n<--%n%n-->"

# grab commits from cached repository
Expand Down
12 changes: 12 additions & 0 deletions src/git-repository/generic.cr
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,16 @@ class GitRepository::Generic < GitRepository::Interface
git.commits(depth: 1).first
end
end

def file_list(ref : String? = nil, path : String? = nil) : Array(String)
create_temp_folder do |temp_folder|
git = Commands.new(temp_folder)
git.clone_file_tree(@repository)
if ref
git.fetch ref # git fetch origin branch
git.checkout ref # git checkout branch
end
git.list_files path # git ls-tree --name-only -r HEAD
end
end
end
1 change: 1 addition & 0 deletions src/git-repository/interface.cr
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ abstract class GitRepository::Interface
abstract def commits(branch : String, depth : Int? = 50) : Array(Commit)
abstract def commits(branch : String, file : String | Enumerable(String), depth : Int? = 50) : Array(Commit)
abstract def fetch_commit(ref : String, download_to_path : String | Path) : Commit
abstract def file_list(ref : String? = nil, path : String? = nil) : Array(String)

module TempFolders
def create_temp_folder
Expand Down

0 comments on commit d8439fd

Please sign in to comment.