git-timecost/src/timecost/git_reader.cr

50 lines
1.3 KiB
Crystal

require "json"
require "./commit"
require "./author"
module TimeCost
class GitReader
property branches_filter : String
property author_filter : String
def initialize(@branches_filter="", @author_filter="")
end
def parse() : Array(Commit)
cmd_git = [
"git", "log", "--all",
"--date=iso", "--no-patch",
"--pretty=format:'{%n \"commit\": \"%H\",%n \"author\": { \"name\": \"%aN\", \"email\": \"%aE\" },%n \"date\": \"%ad\",%n \"message\": \"%f\",%n \"notes\": \"%N\"%n }'"
]
if (self.branches_filter)
cmd_git.concat ["--", self.branches_filter]
else
cmd_git.concat ["--all"]
end
cmd_jq = ["jq", "--slurp"]
# STDERR.puts cmd_git.join(" ")
# STDERR.puts cmd_jq.join(" ")
commit_json_str = %x{#{cmd_git.join(" ")} | #{cmd_jq.join(" ")}}
commit_json = Array(JSON::Any).from_json(commit_json_str)
commits = commit_json.map do |json|
author = Author.new(
name: json["author"]["name"].to_s,
email: json["author"]["email"].to_s,
)
commit = Commit.new(
commit_hash: json["commit"].to_s,
datetime: Time.parse!(json["date"].to_s, "%F %T %z"),
author: author,
message: json["message"].to_s,
)
end
return commits
end
end
end