@@ -11,22 +11,49 @@ use std::{
11
11
use crate :: config:: { BranchName , CommitId } ;
12
12
13
13
/// Run `git` with the given arguments, and get its output
14
- pub fn git < const N : usize > ( args : [ & str ; N ] ) -> Result < String > {
14
+ fn git < const N : usize > ( args : [ & str ; N ] ) -> Result < String > {
15
15
log:: trace!( "$ git {}" , args. join( " " ) ) ;
16
16
get_git_output ( & spawn_git ( & args, & ROOT ) ?, & args)
17
17
}
18
18
19
+ /// Add the file
20
+ pub fn add ( file : & str ) -> Result < String > {
21
+ git ( [ "add" , file] )
22
+ }
23
+
24
+ /// Retrieve message of the last commit
25
+ pub fn last_commit_message ( ) -> Result < String > {
26
+ git ( [ "log" , "--format=%B" , "--max-count=1" ] )
27
+ }
28
+
29
+ /// Retrieve message of specific commit
30
+ pub fn get_message_of_commit ( commit : & str ) -> Result < String > {
31
+ git ( [ "log" , "--format=%B" , "--max-count=1" , commit] )
32
+ }
33
+
34
+ /// Merge the branch into the current one
35
+ pub fn merge ( branch : & str ) -> Result < String > {
36
+ git ( [ "merge" , "--squash" , branch] )
37
+ }
38
+
39
+ /// Remote the given remote
40
+ pub fn remove_remote ( remote : & str ) -> Result < String > {
41
+ git ( [ "remote" , "remove" , remote] )
42
+ }
43
+
44
+ /// Checkout the commit
45
+ pub fn checkout ( object : & str ) -> Result < String > {
46
+ git ( [ "checkout" , object] )
47
+ }
48
+
19
49
/// Create a commit with the given message
20
50
pub fn commit ( message : & str ) -> Result < String > {
21
51
git ( [ "commit" , "--message" , & format ! ( "patchy: {message}" ) ] )
22
52
}
23
53
24
54
/// Fetch remote `url` to local `name`
25
- pub fn try_remote_add ( name : & str , url : & str ) -> Result < String > {
26
- git ( [ "remote" , "add" , name, url] ) . inspect_err ( |_| {
27
- // try remove the remote just in case
28
- let _ = git ( [ "remote" , "remove" , name] ) ;
29
- } )
55
+ pub fn add_remote ( name : & str , url : & str ) -> Result < String > {
56
+ git ( [ "remote" , "add" , name, url] )
30
57
}
31
58
32
59
/// Fetches the `remote_branch` as the name of `local_branch` from `url`
@@ -38,6 +65,32 @@ pub fn fetch_remote_branch(
38
65
git ( [ "fetch" , url, & format ! ( "{remote_branch}:{local_branch}" ) ] )
39
66
}
40
67
68
+ /// Formats the commit as a `patch` and saves it to the specified path
69
+ pub fn save_commit_as_patch ( commit : & CommitId , output_path : & str ) -> Result < String > {
70
+ git ( [
71
+ "format-patch" ,
72
+ "-1" ,
73
+ commit. as_ref ( ) ,
74
+ "--output" ,
75
+ output_path,
76
+ ] )
77
+ }
78
+
79
+ /// Obtain the URL for a remote
80
+ pub fn get_remote_url ( remote : & str ) -> Result < String > {
81
+ git ( [ "remote" , "get-url" , remote] )
82
+ }
83
+
84
+ /// Apply a `patch` as a commit
85
+ pub fn apply_patch ( filename : & Path ) -> Result < ( ) > {
86
+ if let Err ( err) = git ( [ "am" , "--keep-cr" , "--signoff" , & filename. to_string_lossy ( ) ] ) {
87
+ git ( [ "am" , "--abort" ] ) ?;
88
+ return Err ( err) ;
89
+ }
90
+
91
+ Ok ( ( ) )
92
+ }
93
+
41
94
/// `true` if there are unstaged changes
42
95
pub fn is_worktree_dirty ( ) -> bool {
43
96
git ( [ "diff" , "--cached" , "--quiet" ] ) . is_err ( )
@@ -70,6 +123,18 @@ pub fn delete_remote_and_branch(remote: &str, branch: &BranchName) -> Result<()>
70
123
Ok ( ( ) )
71
124
}
72
125
126
+ /// Create a `branch` and check it out
127
+ pub fn create_branch ( branch : & str ) -> Result < String > {
128
+ git ( [ "switch" , "--create" , branch] )
129
+ }
130
+
131
+ /// forcefully renames the branch we are currently on into the branch specified
132
+ /// by the user. WARNING: this is a destructive action which erases the
133
+ /// branch name if it conflicts
134
+ pub fn rename_branch ( old : & str , new : & str ) -> Result < String > {
135
+ git ( [ "branch" , "--move" , "--force" , old, new] )
136
+ }
137
+
73
138
/// Resets the `branch` to the specified `commit`
74
139
pub fn reset_branch_to_commit ( branch : & BranchName , commit : & CommitId ) -> Result < String > {
75
140
git ( [ "branch" , "--force" , branch. as_ref ( ) , commit. as_ref ( ) ] )
0 commit comments