Skip to content

Commit 1175e04

Browse files
Removing duplications
1 parent 2d06008 commit 1175e04

File tree

1 file changed

+68
-36
lines changed

1 file changed

+68
-36
lines changed

lib/git/lib.rb

Lines changed: 68 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -63,36 +63,29 @@ def clone(repository, name, opts = {})
6363

6464
## READ COMMANDS ##
6565

66+
def log_commits(opts={})
67+
arr_opts = log_common_options(opts)
6668

67-
def log_commits(opts = {})
68-
arr_opts = ['--pretty=oneline']
69-
arr_opts << "-#{opts[:count]}" if opts[:count]
70-
arr_opts << "--since=#{opts[:since]}" if opts[:since].is_a? String
71-
arr_opts << "--until=#{opts[:until]}" if opts[:until].is_a? String
72-
arr_opts << "--grep=#{opts[:grep]}" if opts[:grep].is_a? String
73-
arr_opts << "--author=#{opts[:author]}" if opts[:author].is_a? String
74-
arr_opts << "#{opts[:between][0].to_s}..#{opts[:between][1].to_s}" if (opts[:between] && opts[:between].size == 2)
75-
arr_opts << opts[:object] if opts[:object].is_a? String
76-
arr_opts << '--' << opts[:path_limiter] if opts[:path_limiter].is_a? String
69+
arr_opts << '--pretty=oneline'
70+
71+
arr_opts += log_path_options(opts)
7772

7873
command_lines('log', arr_opts, true).map { |l| l.split.first }
7974
end
8075

81-
def full_log_commits(opts = {})
82-
arr_opts = ['--pretty=raw']
83-
arr_opts << "-#{opts[:count]}" if opts[:count]
76+
def full_log_commits(opts={})
77+
arr_opts = log_common_options(opts)
78+
79+
arr_opts << '--pretty=raw'
8480
arr_opts << "--skip=#{opts[:skip]}" if opts[:skip]
85-
arr_opts << "--since=#{opts[:since]}" if opts[:since].is_a? String
86-
arr_opts << "--until=#{opts[:until]}" if opts[:until].is_a? String
87-
arr_opts << "--grep=#{opts[:grep]}" if opts[:grep].is_a? String
88-
arr_opts << "--author=#{opts[:author]}" if opts[:author].is_a? String
89-
arr_opts << "#{opts[:between][0].to_s}..#{opts[:between][1].to_s}" if (opts[:between] && opts[:between].size == 2)
90-
arr_opts << opts[:object] if opts[:object].is_a? String
91-
arr_opts << '--' << opts[:path_limiter] if opts[:path_limiter].is_a? String
81+
82+
arr_opts += log_path_options(opts)
9283

9384
full_log = command_lines('log', arr_opts, true)
9485
process_commit_data(full_log)
9586
end
87+
88+
9689

9790
def revparse(string)
9891
return string if string =~ /[A-Fa-f0-9]{40}/ # passing in a sha - just no-op it
@@ -272,26 +265,12 @@ def diff_stats(obj1 = 'HEAD', obj2 = nil, opts = {})
272265

273266
# compares the index and the working directory
274267
def diff_files
275-
hsh = {}
276-
command_lines('diff-files').each do |line|
277-
(info, file) = line.split("\t")
278-
(mode_src, mode_dest, sha_src, sha_dest, type) = info.split
279-
hsh[file] = {:path => file, :mode_file => mode_src.to_s[1, 7], :mode_index => mode_dest,
280-
:sha_file => sha_src, :sha_index => sha_dest, :type => type}
281-
end
282-
hsh
268+
diff_as_hash('diff-files')
283269
end
284270

285271
# compares the index and the repository
286272
def diff_index(treeish)
287-
hsh = {}
288-
command_lines('diff-index', treeish).each do |line|
289-
(info, file) = line.split("\t")
290-
(mode_src, mode_dest, sha_src, sha_dest, type) = info.split
291-
hsh[file] = {:path => file, :mode_repo => mode_src.to_s[1, 7], :mode_index => mode_dest,
292-
:sha_repo => sha_src, :sha_index => sha_dest, :type => type}
293-
end
294-
hsh
273+
diff_as_hash('diff-index', treeish)
295274
end
296275

297276
def ls_files(location=nil)
@@ -699,6 +678,59 @@ def command(cmd, opts = [], chdir = true, redirect = '', &block)
699678
end
700679
out
701680
end
681+
682+
# Takes the diff command line output (as Array) and parse it into a Hash
683+
#
684+
# @param [String] diff_command the diff commadn to be used
685+
# @param [Array] opts the diff options to be used
686+
# @return [Hash] the diff as Hash
687+
def diff_as_hash(diff_command, opts=[])
688+
command_lines(diff_command, opts).inject({}) do |memo, line|
689+
info, file = line.split("\t")
690+
mode_src, mode_dest, sha_src, sha_dest, type = info.split
691+
692+
memo[file] = {
693+
:mode_index => mode_dest,
694+
:mode_repo => mode_src.to_s[1, 7],
695+
:path => file,
696+
:sha_repo => sha_src,
697+
:sha_index => sha_dest,
698+
:type => type
699+
}
700+
701+
memo
702+
end
703+
end
704+
705+
# Returns an array holding the common options for the log commands
706+
#
707+
# @param [Hash] opts the given options
708+
# @return [Array] the set of common options that the log command will use
709+
def log_common_options(opts)
710+
arr_opts = []
711+
712+
arr_opts << "-#{opts[:count]}" if opts[:count]
713+
arr_opts << "--since=#{opts[:since]}" if opts[:since].is_a? String
714+
arr_opts << "--until=#{opts[:until]}" if opts[:until].is_a? String
715+
arr_opts << "--grep=#{opts[:grep]}" if opts[:grep].is_a? String
716+
arr_opts << "--author=#{opts[:author]}" if opts[:author].is_a? String
717+
arr_opts << "#{opts[:between][0].to_s}..#{opts[:between][1].to_s}" if (opts[:between] && opts[:between].size == 2)
718+
719+
arr_opts
720+
end
721+
722+
# Retrurns an array holding path options for the log commands
723+
#
724+
# @param [Hash] opts the given options
725+
# @return [Array] the set of path options that the log command will use
726+
def log_path_options(opts)
727+
arr_opts = []
728+
729+
arr_opts << opts[:object] if opts[:object].is_a? String
730+
arr_opts << '--' << opts[:path_limiter] if opts[:path_limiter].is_a? String
731+
732+
arr_opts
733+
end
702734

703735
def run_command(git_cmd, &block)
704736
if block_given?

0 commit comments

Comments
 (0)