@@ -11,22 +11,49 @@ use std::{
1111use crate :: config:: { BranchName , CommitId } ;
1212
1313/// 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 > {
1515 log:: trace!( "$ git {}" , args. join( " " ) ) ;
1616 get_git_output ( & spawn_git ( & args, & ROOT ) ?, & args)
1717}
1818
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+
1949/// Create a commit with the given message
2050pub fn commit ( message : & str ) -> Result < String > {
2151 git ( [ "commit" , "--message" , & format ! ( "patchy: {message}" ) ] )
2252}
2353
2454/// 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] )
3057}
3158
3259/// Fetches the `remote_branch` as the name of `local_branch` from `url`
@@ -38,6 +65,32 @@ pub fn fetch_remote_branch(
3865 git ( [ "fetch" , url, & format ! ( "{remote_branch}:{local_branch}" ) ] )
3966}
4067
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+
4194/// `true` if there are unstaged changes
4295pub fn is_worktree_dirty ( ) -> bool {
4396 git ( [ "diff" , "--cached" , "--quiet" ] ) . is_err ( )
@@ -70,6 +123,18 @@ pub fn delete_remote_and_branch(remote: &str, branch: &BranchName) -> Result<()>
70123 Ok ( ( ) )
71124}
72125
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+
73138/// Resets the `branch` to the specified `commit`
74139pub fn reset_branch_to_commit ( branch : & BranchName , commit : & CommitId ) -> Result < String > {
75140 git ( [ "branch" , "--force" , branch. as_ref ( ) , commit. as_ref ( ) ] )
0 commit comments