@@ -78,9 +78,11 @@ fn find_unapply_base(
7878 filtered : git2:: Oid ,
7979) -> super :: JoshResult < git2:: Oid > {
8080 if contained_in == git2:: Oid :: zero ( ) {
81+ tracing:: info!( "contained in zero" , ) ;
8182 return Ok ( git2:: Oid :: zero ( ) ) ;
8283 }
8384 if let Some ( original) = bm. get ( & filtered) {
85+ tracing:: info!( "Found in bm" , ) ;
8486 return Ok ( * original) ;
8587 }
8688 let contained_in_commit = transaction. repo ( ) . find_commit ( contained_in) ?;
@@ -96,10 +98,12 @@ fn find_unapply_base(
9698 let original = transaction. repo ( ) . find_commit ( original?) ?;
9799 if filtered == filter:: apply_to_commit ( filter, & original, transaction) ? {
98100 bm. insert ( filtered, original. id ( ) ) ;
101+ tracing:: info!( "found original properly" , ) ;
99102 return Ok ( original. id ( ) ) ;
100103 }
101104 }
102105
106+ tracing:: info!( "Didn't find original" , ) ;
103107 Ok ( git2:: Oid :: zero ( ) )
104108}
105109
@@ -199,6 +203,71 @@ fn all_equal(a: git2::Parents, b: &[&git2::Commit]) -> bool {
199203 true
200204}
201205
206+ fn find_oldest_similar_commit (
207+ transaction : & cache:: Transaction ,
208+ filter : filter:: Filter ,
209+ unfiltered : git2:: Oid ,
210+ ) -> super :: JoshResult < git2:: Oid > {
211+ let walk = {
212+ let mut walk = transaction. repo ( ) . revwalk ( ) ?;
213+ walk. set_sorting ( git2:: Sort :: TOPOLOGICAL ) ?;
214+ walk. push ( unfiltered) ?;
215+ walk
216+ } ;
217+ tracing:: info!( "oldest similar?" ) ;
218+ let unfiltered_commit = transaction. repo ( ) . find_commit ( unfiltered) ?;
219+ let filtered = filter:: apply_to_commit ( filter, & unfiltered_commit, transaction) ?;
220+ let mut prev_rev = unfiltered;
221+ for rev in walk {
222+ let rev = rev?;
223+ tracing:: info!( "next" ) ;
224+ let rev_commit = transaction. repo ( ) . find_commit ( rev) ?;
225+ if filtered != filter:: apply_to_commit ( filter, & rev_commit, transaction) ? {
226+ tracing:: info!( "diff! {}" , prev_rev) ;
227+ return Ok ( prev_rev) ;
228+ }
229+ prev_rev = rev;
230+ }
231+ tracing:: info!( "bottom" ) ;
232+ return Ok ( unfiltered) ;
233+ }
234+
235+ fn find_new_branch_base (
236+ transaction : & cache:: Transaction ,
237+ bm : & mut std:: collections:: HashMap < git2:: Oid , git2:: Oid > ,
238+ filter : filter:: Filter ,
239+ contained_in : git2:: Oid ,
240+ filtered : git2:: Oid ,
241+ ) -> super :: JoshResult < git2:: Oid > {
242+ let walk = {
243+ let mut walk = transaction. repo ( ) . revwalk ( ) ?;
244+ walk. set_sorting ( git2:: Sort :: TOPOLOGICAL ) ?;
245+ walk. push ( filtered) ?;
246+ walk
247+ } ;
248+ tracing:: info!( "new branch base?" ) ;
249+
250+ for rev in walk {
251+ let rev = rev?;
252+ if let Ok ( base) = find_unapply_base ( transaction, bm, filter, contained_in, rev) {
253+ if base != git2:: Oid :: zero ( ) {
254+ tracing:: info!( "new branch base: {:?}" , base) ;
255+ let base =
256+ if let Ok ( new_base) = find_oldest_similar_commit ( transaction, filter, base) {
257+ new_base
258+ } else {
259+ base
260+ } ;
261+ tracing:: info!( "inserting in bm {}, {}" , rev, base) ;
262+ bm. insert ( rev, base) ;
263+ return Ok ( rev) ;
264+ }
265+ }
266+ }
267+ tracing:: info!( "new branch base not found" ) ;
268+ return Ok ( git2:: Oid :: zero ( ) ) ;
269+ }
270+
202271#[ tracing:: instrument( skip( transaction) ) ]
203272pub fn unapply_filter (
204273 transaction : & cache:: Transaction ,
@@ -213,12 +282,30 @@ pub fn unapply_filter(
213282 let mut bm = std:: collections:: HashMap :: new ( ) ;
214283 let mut ret = original_target;
215284
285+ let old = if old == git2:: Oid :: zero ( ) {
286+ match find_new_branch_base ( transaction, & mut bm, filterobj, original_target, new) {
287+ Ok ( res) => {
288+ tracing:: info!( "No error, branch base {} " , res) ;
289+ res
290+ }
291+ Err ( _) => {
292+ tracing:: info!( "Error in new branch base" ) ;
293+ old
294+ }
295+ }
296+ } else {
297+ tracing:: info!( "Old not zero" ) ;
298+ old
299+ } ;
300+
301+ tracing:: info!( "before walk" ) ;
302+
216303 let walk = {
217304 let mut walk = transaction. repo ( ) . revwalk ( ) ?;
218305 walk. set_sorting ( git2:: Sort :: REVERSE | git2:: Sort :: TOPOLOGICAL ) ?;
219306 walk. push ( new) ?;
220307 if walk. hide ( old) . is_ok ( ) {
221- tracing:: trace !( "walk: hidden {}" , old) ;
308+ tracing:: info !( "walk: hidden {}" , old) ;
222309 } else {
223310 tracing:: warn!( "walk: can't hide" ) ;
224311 }
@@ -230,6 +317,7 @@ pub fn unapply_filter(
230317
231318 let s = tracing:: span!( tracing:: Level :: TRACE , "walk commit" , ?rev) ;
232319 let _e = s. enter ( ) ;
320+ tracing:: info!( "walk commit: {:?}" , rev) ;
233321
234322 let module_commit = transaction. repo ( ) . find_commit ( rev) ?;
235323
0 commit comments