1
1
use colored:: Colorize ;
2
- use git2:: { DiffFormat , DiffOptions , Repository , StatusOptions } ;
2
+ use git2:: { DiffFormat , DiffOptions , Oid , Repository , StatusOptions } ;
3
3
use normpath:: { BasePathBuf , PathExt } ;
4
4
use std:: { path:: Path , str} ;
5
5
6
- pub fn build_commands ( commit_message : String , include_push : bool ) -> Vec < Vec < String > > {
7
- let mut commit_message = commit_message;
6
+ use crate :: command_utils:: run_commands;
7
+
8
+ pub fn build_commands (
9
+ commit_message : & String ,
10
+ include_push : bool ,
11
+ files : & Vec < String > ,
12
+ ) -> Vec < Vec < String > > {
13
+ let mut commit_message = commit_message. clone ( ) ;
8
14
9
15
if commit_message. starts_with ( "\" " ) && commit_message. ends_with ( "\" " ) {
10
16
commit_message = commit_message[ 1 ..commit_message. len ( ) - 1 ] . to_owned ( ) ;
11
17
}
12
18
13
19
let mut commands: Vec < Vec < String > > = Vec :: new ( ) ;
14
- commands. push ( "git add ." . split ( ' ' ) . map ( |s| s. to_owned ( ) ) . collect ( ) ) ;
20
+ let mut add_command: Vec < String > = "git add"
21
+ . to_owned ( )
22
+ . split ( ' ' )
23
+ . map ( |s| s. to_owned ( ) )
24
+ . collect ( ) ;
25
+ add_command. extend ( paths_to_git_paths ( & files) ) ;
26
+ commands. push ( add_command) ;
15
27
let mut commit_command: Vec < String > = "git commit -m"
16
28
. to_owned ( )
17
29
. split ( ' ' )
@@ -23,6 +35,8 @@ pub fn build_commands(commit_message: String, include_push: bool) -> Vec<Vec<Str
23
35
commands. push ( "git push" . split ( ' ' ) . map ( |s| s. to_owned ( ) ) . collect ( ) ) ;
24
36
}
25
37
38
+ println ! ( "Commands: {:?}" , commands) ;
39
+
26
40
commands
27
41
}
28
42
@@ -148,4 +162,54 @@ impl Git {
148
162
149
163
Ok ( status_value)
150
164
}
165
+
166
+ pub fn add_all ( self : & Self , files : Option < & Vec < String > > ) {
167
+ self . repo
168
+ . index ( )
169
+ . unwrap ( )
170
+ . add_all (
171
+ paths_to_git_paths ( & files. unwrap_or ( & vec ! [ ] ) ) . iter ( ) ,
172
+ git2:: IndexAddOption :: DEFAULT ,
173
+ None ,
174
+ )
175
+ . unwrap ( ) ;
176
+ }
177
+
178
+ pub fn commit ( self : & Self , message : & String ) -> Result < Oid , git2:: Error > {
179
+ let mut index = self . repo . index ( ) . unwrap ( ) ;
180
+ let oid = index. write_tree ( ) . unwrap ( ) ;
181
+ let signature = self . repo . signature ( ) . unwrap ( ) ;
182
+ let parent_commit = self . repo . head ( ) . unwrap ( ) . peel_to_commit ( ) . unwrap ( ) ;
183
+ let tree = self . repo . find_tree ( oid) . unwrap ( ) ;
184
+ self . repo . commit (
185
+ Some ( "HEAD" ) ,
186
+ & signature,
187
+ & signature,
188
+ & message,
189
+ & tree,
190
+ & [ & parent_commit] ,
191
+ )
192
+ }
193
+
194
+ pub fn push ( self : & Self ) {
195
+ run_commands ( & vec ! [ vec![ "git push" . to_owned( ) ] ] ) ;
196
+ }
197
+ }
198
+
199
+ fn paths_to_git_paths ( paths : & Vec < String > ) -> Vec < String > {
200
+ if paths. is_empty ( ) {
201
+ return vec ! [ "." . to_owned( ) ] ;
202
+ }
203
+ paths
204
+ . iter ( )
205
+ . map ( |entry| Path :: new ( entry) )
206
+ . map ( |entry| {
207
+ entry
208
+ . into_iter ( )
209
+ . map ( |entry| entry. to_str ( ) . unwrap ( ) . to_owned ( ) )
210
+ . filter ( |entry| entry != & "." )
211
+ . collect :: < Vec < String > > ( )
212
+ . join ( "/" )
213
+ } )
214
+ . collect ( )
151
215
}
0 commit comments