File tree Expand file tree Collapse file tree 2 files changed +46
-0
lines changed Expand file tree Collapse file tree 2 files changed +46
-0
lines changed Original file line number Diff line number Diff line change @@ -197,6 +197,27 @@ pub fn squash(
197
197
Ok ( new_id)
198
198
}
199
199
200
+ /// Reword `head_id`s commit
201
+ pub fn reword (
202
+ repo : & git2:: Repository ,
203
+ head_id : git2:: Oid ,
204
+ msg : & str ,
205
+ ) -> Result < git2:: Oid , git2:: Error > {
206
+ let old_commit = repo. find_commit ( head_id) ?;
207
+ let parents = old_commit. parents ( ) . collect :: < Vec < _ > > ( ) ;
208
+ let parents = parents. iter ( ) . collect :: < Vec < _ > > ( ) ;
209
+ let tree = repo. find_tree ( old_commit. tree_id ( ) ) ?;
210
+ let new_id = repo. commit (
211
+ None ,
212
+ & old_commit. author ( ) ,
213
+ & old_commit. committer ( ) ,
214
+ msg,
215
+ & tree,
216
+ & parents,
217
+ ) ?;
218
+ Ok ( new_id)
219
+ }
220
+
200
221
// From git2 crate
201
222
#[ cfg( unix) ]
202
223
fn bytes2path ( b : & [ u8 ] ) -> & std:: path:: Path {
Original file line number Diff line number Diff line change @@ -88,3 +88,28 @@ fn squash_clean() {
88
88
89
89
temp. close ( ) . unwrap ( ) ;
90
90
}
91
+
92
+ #[ test]
93
+ fn reword ( ) {
94
+ let temp = assert_fs:: TempDir :: new ( ) . unwrap ( ) ;
95
+ let plan = git_fixture:: Dag :: load ( std:: path:: Path :: new ( "tests/fixtures/branches.yml" ) ) . unwrap ( ) ;
96
+ plan. run ( temp. path ( ) ) . unwrap ( ) ;
97
+
98
+ let repo = git2:: Repository :: discover ( temp. path ( ) ) . unwrap ( ) ;
99
+
100
+ {
101
+ assert ! ( !git2_ext:: ops:: is_dirty( & repo) ) ;
102
+
103
+ let feature2 = repo
104
+ . find_branch ( "feature2" , git2:: BranchType :: Local )
105
+ . unwrap ( ) ;
106
+ let feature2_id = feature2. get ( ) . target ( ) . unwrap ( ) ;
107
+
108
+ let new_id = git2_ext:: ops:: reword ( & repo, feature2_id, "New message" ) . unwrap ( ) ;
109
+
110
+ println ! ( "{:#?}" , new_id) ;
111
+ assert ! ( !git2_ext:: ops:: is_dirty( & repo) ) ;
112
+ }
113
+
114
+ temp. close ( ) . unwrap ( ) ;
115
+ }
You can’t perform that action at this time.
0 commit comments