@@ -59,12 +59,17 @@ impl From<HeadersV1> for HeadersV2 {
5959
6060const HEADERS_VERSION_FIELD : & str = "gitbutler-headers-version" ;
6161const HEADERS_CHANGE_ID_FIELD : & str = "gitbutler-change-id" ;
62- const HEADERS_CONFLICTED_FIELD : & str = "gitbutler-conflicted" ;
62+ /// The name of the header field that stores the amount of conflicted files.
63+ pub const HEADERS_CONFLICTED_FIELD : & str = "gitbutler-conflicted" ;
64+ const HEADERS_VERSION : & str = "2" ;
6365
64- impl From < HeadersV2 > for Vec < ( BString , BString ) > {
65- fn from ( hdr : HeadersV2 ) -> Self {
66+ impl From < & HeadersV2 > for Vec < ( BString , BString ) > {
67+ fn from ( hdr : & HeadersV2 ) -> Self {
6668 let mut out = vec ! [
67- ( BString :: from( HEADERS_VERSION_FIELD ) , BString :: from( "2" ) ) ,
69+ (
70+ BString :: from( HEADERS_VERSION_FIELD ) ,
71+ BString :: from( HEADERS_VERSION ) ,
72+ ) ,
6873 ( HEADERS_CHANGE_ID_FIELD . into( ) , hdr. change_id. clone( ) . into( ) ) ,
6974 ] ;
7075
@@ -91,6 +96,18 @@ pub enum TreeKind {
9196 AutoResolution ,
9297}
9398
99+ impl TreeKind {
100+ /// Return then name of the entry this tree would take in the 'meta' tree that captures cherry-pick conflicts.
101+ pub fn as_tree_entry_name ( & self ) -> & ' static str {
102+ match self {
103+ TreeKind :: Ours => ".conflict-side-0" ,
104+ TreeKind :: Theirs => ".conflict-side-1" ,
105+ TreeKind :: Base => ".conflict-base-0" ,
106+ TreeKind :: AutoResolution => ".auto-resolution" ,
107+ }
108+ }
109+ }
110+
94111/// Instantiation
95112impl < ' repo > Commit < ' repo > {
96113 /// Decode the object at `commit_id` and keep its data for later query.
@@ -103,11 +120,50 @@ impl<'repo> Commit<'repo> {
103120 }
104121}
105122
123+ impl Commit < ' _ > {
124+ /// Set this commit to use the given `headers`, completely replacing the ones it might currently have.
125+ pub fn set_headers ( & mut self , header : & HeadersV2 ) {
126+ for field in [
127+ HEADERS_VERSION_FIELD ,
128+ HEADERS_CHANGE_ID_FIELD ,
129+ HEADERS_CONFLICTED_FIELD ,
130+ ] {
131+ if let Some ( pos) = self . extra_headers ( ) . find_pos ( field) {
132+ self . extra_headers . remove ( pos) ;
133+ }
134+ }
135+
136+ self . extra_headers
137+ . extend ( Vec :: < ( BString , BString ) > :: from ( header) ) ;
138+ }
139+ }
140+
141+ impl std:: ops:: Deref for Commit < ' _ > {
142+ type Target = gix:: objs:: Commit ;
143+
144+ fn deref ( & self ) -> & Self :: Target {
145+ & self . inner
146+ }
147+ }
148+
149+ impl std:: ops:: DerefMut for Commit < ' _ > {
150+ fn deref_mut ( & mut self ) -> & mut Self :: Target {
151+ & mut self . inner
152+ }
153+ }
154+
155+ impl HeadersV2 {
156+ /// Return `true` if this commit contains a tree that is conflicted.
157+ pub fn is_conflicted ( & self ) -> bool {
158+ self . conflicted . is_some ( )
159+ }
160+ }
161+
106162/// Access
107163impl < ' repo > Commit < ' repo > {
108164 /// Return `true` if this commit contains a tree that is conflicted.
109165 pub fn is_conflicted ( & self ) -> bool {
110- self . headers ( ) . is_some_and ( |hdr| hdr. conflicted . is_some ( ) )
166+ self . headers ( ) . is_some_and ( |hdr| hdr. is_conflicted ( ) )
111167 }
112168
113169 /// Return the hash of *our* tree, even if this commit is conflicted.
@@ -127,12 +183,7 @@ impl<'repo> Commit<'repo> {
127183 . attach ( self . id . repo )
128184 . object ( ) ?
129185 . into_tree ( )
130- . find_entry ( match kind {
131- TreeKind :: Ours => ".conflict-side-0" ,
132- TreeKind :: Theirs => ".conflict-side-1" ,
133- TreeKind :: Base => ".conflict-base-0" ,
134- TreeKind :: AutoResolution => ".auto-resolution" ,
135- } )
186+ . find_entry ( kind. as_tree_entry_name ( ) )
136187 . with_context ( || format ! ( "Unexpected tree in conflicting commit {}" , self . id) ) ?
137188 . id ( ) ;
138189 Some ( our_tree)
@@ -156,7 +207,7 @@ impl<'repo> Commit<'repo> {
156207 if let Some ( header) = decoded. extra_headers ( ) . find ( HEADERS_VERSION_FIELD ) {
157208 let version = header. to_owned ( ) ;
158209
159- if version == "2" {
210+ if version == HEADERS_VERSION {
160211 let change_id = decoded. extra_headers ( ) . find ( HEADERS_CHANGE_ID_FIELD ) ?;
161212 let change_id = change_id. to_str ( ) . ok ( ) ?. to_string ( ) ;
162213
0 commit comments