Skip to content

Commit

Permalink
feat: track author and authors
Browse files Browse the repository at this point in the history
  • Loading branch information
3v0k4 committed Jul 17, 2023
1 parent 4e59916 commit 096015a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 77 deletions.
18 changes: 4 additions & 14 deletions lib/knapsack_pro/repository_adapters/git_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,11 @@ def commit_authors
{ commits: commits.to_i, author: KnapsackPro::MaskString.call(author) }
end

hashes = git_parentless_commit_hashes
.split("\n")

raise if (authors.empty? || hashes.empty?)
raise if authors.empty?

{
parentless_commit_hashes: hashes,
authors: authors,
}
authors
rescue Exception
nil
[]
end

def build_author
Expand All @@ -47,17 +41,13 @@ def build_author
private

def git_commit_authors
`git shortlog --summary --email --since "one month ago"`
`git fetch --shallow-since "1 month ago" >/dev/null 2>&1 && git shortlog --summary --email --since "one month ago"`
end

def git_build_author
`git log --format="%aN <%aE>" -1`
end

def git_parentless_commit_hashes
`git rev-list --max-parents=0 HEAD`
end

def working_dir
dir = KnapsackPro::Config::Env.project_dir
File.expand_path(dir)
Expand Down
71 changes: 8 additions & 63 deletions spec/knapsack_pro/repository_adapters/git_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,6 @@

describe '#commit_authors' do
it "returns the masked commit authors" do
allow_any_instance_of(KnapsackPro::RepositoryAdapters::GitAdapter).to receive(:git_parentless_commit_hashes).and_return([
"8354b6b219d1a7e38efadd34579d15e8bbd60f98\n",
"19f06dbb8e51d97543ddafe83e7a1d912b6b4538\n",
].join(""))

allow_any_instance_of(KnapsackPro::RepositoryAdapters::GitAdapter).to receive(:git_commit_authors).and_return([
" 5\t3v0k4 <riccardo@example.com>\n",
" 10\tArtur Nowak <artur@example.com>\n",
Expand All @@ -80,81 +75,31 @@

subject = KnapsackPro::RepositoryAdapters::GitAdapter.new

expect(subject.commit_authors).to eq({
parentless_commit_hashes: [
"8354b6b219d1a7e38efadd34579d15e8bbd60f98",
"19f06dbb8e51d97543ddafe83e7a1d912b6b4538",
],
authors: [
{ commits: 5, author: "3v0*4 <ri******@ex*****.co*>" },
{ commits: 10, author: "Ar*** No*** <ar***@ex*****.co*>" },
{ commits: 2, author: "Ri****** <ri******@ex*****.co*>" },
{ commits: 3, author: "sh**** <sh***@ex*****.co*>" },
]
})
expect(subject.commit_authors).to eq([
{ commits: 5, author: "3v0*4 <ri******@ex*****.co*>" },
{ commits: 10, author: "Ar*** No*** <ar***@ex*****.co*>" },
{ commits: 2, author: "Ri****** <ri******@ex*****.co*>" },
{ commits: 3, author: "sh**** <sh***@ex*****.co*>" },
])
end

context "when the authors command raises an exception" do
it "returns nil" do
allow_any_instance_of(KnapsackPro::RepositoryAdapters::GitAdapter).to receive(:git_parentless_commit_hashes).and_return([
"8354b6b219d1a7e38efadd34579d15e8bbd60f98\n",
"19f06dbb8e51d97543ddafe83e7a1d912b6b4538\n",
].join(""))

allow_any_instance_of(KnapsackPro::RepositoryAdapters::GitAdapter).to receive(:git_commit_authors).and_raise(Exception)

subject = KnapsackPro::RepositoryAdapters::GitAdapter.new

expect(subject.commit_authors).to eq nil
expect(subject.commit_authors).to eq []
end
end

context "when the authors command returns an empty string" do
it "returns nil" do
allow_any_instance_of(KnapsackPro::RepositoryAdapters::GitAdapter).to receive(:git_parentless_commit_hashes).and_return([
"8354b6b219d1a7e38efadd34579d15e8bbd60f98\n",
"19f06dbb8e51d97543ddafe83e7a1d912b6b4538\n",
].join(""))

allow_any_instance_of(KnapsackPro::RepositoryAdapters::GitAdapter).to receive(:git_commit_authors).and_return("")

subject = KnapsackPro::RepositoryAdapters::GitAdapter.new

expect(subject.commit_authors).to eq nil
end
end

context "when the hashes command raises an exception" do
it "returns nil" do
allow_any_instance_of(KnapsackPro::RepositoryAdapters::GitAdapter).to receive(:git_parentless_commit_hashes).and_raise(Exception)

allow_any_instance_of(KnapsackPro::RepositoryAdapters::GitAdapter).to receive(:git_commit_authors).and_return([
" 5\t3v0k4 <riccardo@example.com>\n",
" 10\tArtur Nowak <artur@example.com>\n",
" 2\tRiccardo <riccardo@example.com>\n",
" 3 \tshadre <shadi@example.com>\n",
].join(""))

subject = KnapsackPro::RepositoryAdapters::GitAdapter.new

expect(subject.commit_authors).to eq nil
end
end

context "when the hashes command returns an empty string" do
it "returns nil" do
allow_any_instance_of(KnapsackPro::RepositoryAdapters::GitAdapter).to receive(:git_parentless_commit_hashes).and_return("")

allow_any_instance_of(KnapsackPro::RepositoryAdapters::GitAdapter).to receive(:git_commit_authors).and_return([
" 5\t3v0k4 <riccardo@example.com>\n",
" 10\tArtur Nowak <artur@example.com>\n",
" 2\tRiccardo <riccardo@example.com>\n",
" 3 \tshadre <shadi@example.com>\n",
].join(""))

subject = KnapsackPro::RepositoryAdapters::GitAdapter.new

expect(subject.commit_authors).to eq nil
expect(subject.commit_authors).to eq []
end
end
end
Expand Down

0 comments on commit 096015a

Please sign in to comment.