From 1fcc282e27681affc222e9997c9ce33f9de9c3b0 Mon Sep 17 00:00:00 2001 From: l0ng-ai <24760907+l0ng-ai@users.noreply.github.com> Date: Sun, 23 Aug 2026 23:16:37 +0800 Subject: [PATCH] test(diff): a ref name is not an object id however long it is MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `short_rev` cuts a rev to eight characters only when it is forty-plus characters *and* all hex. Every case in its test is shorter than forty, so the length half decides them alone and the hex half never gets a say. A ref name that long is the only input that asks it anything, and they exist: `origin/feature/a-thoroughly-descriptive-branch-name` is fifty-one. `DiffSource::Range` puts two revs in one header, so losing the hex test turns a branch comparison into "origin/…origin/". Also pinned: an oid-length string with a single non-hex character is still a name, which is the boundary the `all` is there to draw. --- src/ui/diff_overlay.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/ui/diff_overlay.rs b/src/ui/diff_overlay.rs index 9e878979..3cb62743 100644 --- a/src/ui/diff_overlay.rs +++ b/src/ui/diff_overlay.rs @@ -1738,6 +1738,24 @@ mod tests { "half a ref name says less than the whole of it" ); assert_eq!(short_rev("3f2a1b9"), "3f2a1b9", "already short"); + + // Long enough to be an oid, and not one. Every case above is under + // forty characters, so `len() >= 40` decides them on its own and the + // hex test never gets a say — a ref name this long is the only input + // that asks it anything. `DiffSource::Range` puts two of these in one + // header, and cutting them to eight characters leaves "origin/…origin/". + let long_ref = "origin/feature/a-thoroughly-descriptive-branch-name"; + assert!(long_ref.len() >= 40, "the fixture has to be oid-length"); + assert_eq!( + short_rev(long_ref), + long_ref, + "a ref name is not an object id however long it is" + ); + + // And an oid-length string that is *nearly* hex is still a name. + let nearly = "3f2a1b9c8d7e6f5a4b3c2d1e0f9a8b7c6d5e4f3z"; + assert_eq!(nearly.len(), 40); + assert_eq!(short_rev(nearly), nearly, "one non-hex character is enough"); } #[test]