@@ -19,26 +19,38 @@ def run
1919
2020 Database . connect ( repo . git_dir )
2121
22- from_sha = @options [ :from ]
23- to_sha = @options [ :to ] || repo . head_sha
22+ from_ref = @options [ :from ]
23+ to_ref = @options [ :to ] || "HEAD"
24+
25+ unless from_ref
26+ $stderr. puts "Usage: git pkgs diff --from=REF [--to=REF]"
27+ exit 1
28+ end
29+
30+ # Resolve git refs (like HEAD~10) to SHAs
31+ from_sha = repo . rev_parse ( from_ref )
32+ to_sha = repo . rev_parse ( to_ref )
2433
2534 unless from_sha
26- $stderr. puts "Usage: git pkgs diff --from=SHA [--to=SHA] "
35+ $stderr. puts "Could not resolve ' #{ from_ref } ' "
2736 exit 1
2837 end
2938
30- from_commit = Models ::Commit . find_by ( sha : from_sha ) ||
31- Models ::Commit . where ( "sha LIKE ?" , "#{ from_sha } %" ) . first
32- to_commit = Models ::Commit . find_by ( sha : to_sha ) ||
33- Models ::Commit . where ( "sha LIKE ?" , "#{ to_sha } %" ) . first
39+ unless to_sha
40+ $stderr. puts "Could not resolve '#{ to_ref } '"
41+ exit 1
42+ end
43+
44+ from_commit = find_or_create_commit ( repo , from_sha )
45+ to_commit = find_or_create_commit ( repo , to_sha )
3446
3547 unless from_commit
36- $stderr. puts "Commit '#{ from_sha } ' not found in database "
48+ $stderr. puts "Commit '#{ from_sha [ 0 .. 7 ] } ' not found"
3749 exit 1
3850 end
3951
4052 unless to_commit
41- $stderr. puts "Commit '#{ to_sha } ' not found in database "
53+ $stderr. puts "Commit '#{ to_sha [ 0 .. 7 ] } ' not found"
4254 exit 1
4355 end
4456
@@ -98,17 +110,38 @@ def run
98110 puts "Summary: +#{ added . map ( &:name ) . uniq . count } -#{ removed . map ( &:name ) . uniq . count } ~#{ modified . map ( &:name ) . uniq . count } "
99111 end
100112
113+ def find_or_create_commit ( repo , sha )
114+ commit = Models ::Commit . find_by ( sha : sha ) ||
115+ Models ::Commit . where ( "sha LIKE ?" , "#{ sha } %" ) . first
116+ return commit if commit
117+
118+ # Lazily insert commit if it exists in git but not in database
119+ rugged_commit = repo . lookup ( sha )
120+ return nil unless rugged_commit
121+
122+ Models ::Commit . create! (
123+ sha : rugged_commit . oid ,
124+ message : rugged_commit . message ,
125+ author_name : rugged_commit . author [ :name ] ,
126+ author_email : rugged_commit . author [ :email ] ,
127+ committed_at : rugged_commit . time ,
128+ has_dependency_changes : false
129+ )
130+ rescue Rugged ::OdbError
131+ nil
132+ end
133+
101134 def parse_options
102135 options = { }
103136
104137 parser = OptionParser . new do |opts |
105- opts . banner = "Usage: git pkgs diff --from=SHA [--to=SHA ] [options]"
138+ opts . banner = "Usage: git pkgs diff --from=REF [--to=REF ] [options]"
106139
107- opts . on ( "-f" , "--from=SHA " , "Start commit (required)" ) do |v |
140+ opts . on ( "-f" , "--from=REF " , "Start commit (required)" ) do |v |
108141 options [ :from ] = v
109142 end
110143
111- opts . on ( "-t" , "--to=SHA " , "End commit (default: HEAD)" ) do |v |
144+ opts . on ( "-t" , "--to=REF " , "End commit (default: HEAD)" ) do |v |
112145 options [ :to ] = v
113146 end
114147
0 commit comments