Skip to content

Commit eb92d38

Browse files
committed
Adjust branch option to specify the branch to collect statistics on
Change: * The function of the branch CLI flag to specify the specific branch we want to collect statistics on. This differs from the original function since we use to be able to collect all commits on a set of branches. Rugged only allows us to walk through from the HEAD of one branch at a time (we might be able to expand on this by doing multiple walks from the HEAD of each branch). Refactor: * spec_helper to require git_statistics since it will include CLI class and by default it will also include the initialize file * The way we pass the branch into the collection method (pass it within the options hash) Add: * Default branch constant in CLI class
1 parent d4cb2d2 commit eb92d38

File tree

4 files changed

+17
-13
lines changed

4 files changed

+17
-13
lines changed

lib/git_statistics.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ module GitStatistics
44
class CLI
55
attr_reader :repository, :options
66

7+
DEFAULT_BRANCH = "master"
8+
79
def initialize(dir)
810
repository_location = dir.nil? ? Rugged::Repository.discover(Dir.pwd) : Rugged::Repository.discover(dir)
911
@repository = Rugged::Repository.new(repository_location)
@@ -16,7 +18,7 @@ def initialize(dir)
1618
update: false,
1719
sort: "commits",
1820
top: 0,
19-
branch: false,
21+
branch: DEFAULT_BRANCH,
2022
verbose: false,
2123
debug: false,
2224
limit: 100
@@ -42,7 +44,7 @@ def collect_and_only_update
4244

4345
if file_count >= 0
4446
time_since = Utilities.get_modified_time(commits_directory + "#{file_count}.json").to_s
45-
@collector.collect(options.branch, {:time_since => time_since})
47+
@collector.collect({:branch => options.branch, :time_since => time_since})
4648
@collected = true
4749
end
4850
end
@@ -59,7 +61,7 @@ def output_results
5961

6062
def fresh_collect!
6163
@collector = Collector.new(repository, options.limit, true, options.pretty)
62-
@collector.collect(options.branch)
64+
@collector.collect({:branch => options.branch})
6365
end
6466

6567
def parse_options
@@ -83,8 +85,8 @@ def parse_options
8385
opt.on "-t", "--top N", Float,"Show the top N authors in results" do |value|
8486
options.top = value
8587
end
86-
opt.on "-b", "--branch", "Use current branch for statistics (otherwise all branches)" do
87-
options.branch = true
88+
opt.on "-b", "--branch BRANCH", "Use the specified branch for statistics (otherwise the master branch is used)" do |branch|
89+
options.branch = branch
8890
end
8991
opt.on "-v", "--verbose", "Verbose output (shows INFO level log statements)" do
9092
options.verbose = true

lib/git_statistics/collector.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ def initialize(repo, limit, fresh, pretty)
99
@commits = Commits.new(@commits_path, fresh, limit, pretty)
1010
end
1111

12-
def collect(branch, options = {})
12+
def collect(options = {})
13+
branch = options[:branch] ? options[:branch] : CLI::DEFAULT_BRANCH
14+
branch_head = Rugged::Branch.lookup(repo, branch).tip
15+
1316
walker = Rugged::Walker.new(repo)
14-
master_head = Rugged::Branch.lookup(repo, "master").tip
17+
walker.push(branch_head)
1518

16-
walker.push(master_head)
1719
walker.each_with_index do |commit, count|
1820
if valid_commit?(commit, options)
1921
extract_commit(commit, count + 1)

spec/collector_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
let(:collector) {Collector.new(repo, limit, fresh, pretty)}
1010

1111
describe "#collect" do
12-
let(:branch) {""}
12+
let(:branch) {CLI::DEFAULT_BRANCH}
1313
let(:email) {false}
1414
let(:merge) {true}
1515
let(:time_since) {"Tue Sep 24 14:15:44 2012 -0400"}
1616
let(:time_until) {"Tue Sep 26 14:45:05 2012 -0400"}
1717
let(:author) {"Kevin Jalbert"}
1818

1919
let(:setup) {
20-
collector.collect(branch, {:time_since => time_since, :time_until => time_until})
20+
collector.collect({:branch => branch, :time_since => time_since, :time_until => time_until})
2121
collector.commits.calculate_statistics(email, merge)
2222
@subject = collector.commits.stats[author]
2323
}

spec/spec_helper.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111

1212
Dir.glob(spec_dir + 'support/**/*.rb') {|file| require file}
1313

14-
require 'git_statistics/initialize'
14+
require 'git_statistics'
1515

1616
GIT_REPO = Rugged::Repository.new(Rugged::Repository.discover(home_dir.to_s))
1717

1818
def fixture(file)
19-
GitStatistics::PipeStub.new(file)
19+
PipeStub.new(file)
2020
end
2121

2222
def setup_commits(commits, file_load, file_save, pretty)
@@ -30,7 +30,7 @@ def setup_commits(commits, file_load, file_save, pretty)
3030
RSpec.configure do |config|
3131
config.before do
3232
%w[debug info warn error fatal].each do |level|
33-
GitStatistics::Log.stub(level)
33+
Log.stub(level)
3434
end
3535
end
3636
end

0 commit comments

Comments
 (0)