@@ -63,36 +63,29 @@ def clone(repository, name, opts = {})
63
63
64
64
## READ COMMANDS ##
65
65
66
+ def log_commits ( opts = { } )
67
+ arr_opts = log_common_options ( opts )
66
68
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 )
77
72
78
73
command_lines ( 'log' , arr_opts , true ) . map { |l | l . split . first }
79
74
end
80
75
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'
84
80
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 )
92
83
93
84
full_log = command_lines ( 'log' , arr_opts , true )
94
85
process_commit_data ( full_log )
95
86
end
87
+
88
+
96
89
97
90
def revparse ( string )
98
91
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 = {})
272
265
273
266
# compares the index and the working directory
274
267
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' )
283
269
end
284
270
285
271
# compares the index and the repository
286
272
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 )
295
274
end
296
275
297
276
def ls_files ( location = nil )
@@ -699,6 +678,59 @@ def command(cmd, opts = [], chdir = true, redirect = '', &block)
699
678
end
700
679
out
701
680
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
702
734
703
735
def run_command ( git_cmd , &block )
704
736
if block_given?
0 commit comments