(* Copyright (c) 2024-2026 Thomas Gazagnaire Permission to use, copy, modify, and distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. *) (* git worktree remove and its refusals, compared with native Git on twin repositories. *) module R = Git_eio.Repository module W = Git_eio.Worktree let quoted path = Filename.quote (Fpath.to_string path) let fixture ~fs root name = let dir = Fpath.(root / name) in Eio.Path.mkdir ~perm:0o755 Eio.Path.(fs / Fpath.to_string dir); Test_helpers.git ~dir "init -q -b main"; let dir = Fpath.v (String.trim (Test_helpers.git_output ~dir "rev-parse --show-toplevel")) in Test_helpers.git ~dir "config user.name Fixture"; Test_helpers.git ~dir "config user.email fixture@example.invalid"; Test_helpers.save_work ~fs ~root:dir "tracked" "committed\n"; Test_helpers.git ~dir "add tracked"; Test_helpers.git ~dir "commit -qm initial"; let path = Fpath.(dir / "linked") in Test_helpers.git ~dir ("worktree add -q -b owned " ^ quoted path); (dir, path) let result_ok = function Ok _ -> true | Error _ -> false let initial ~sw ~fs ~dir ~path = let repo = R.open_repo ~sw ~fs dir in let manager = R.worktree repo in Alcotest.(check bool) "library sees the initial registration" true (W.exists manager ~path); let inspected = Test_helpers.or_fail "inspect initial registration" (W.inspect manager ~path) in Alcotest.(check bool) "inspection names the selected path" true (Fpath.equal inspected.path path); Alcotest.(check (option string)) "inspection names the owned branch" (Some "owned") inspected.branch; let linked = R.open_repo ~sw ~fs path in Alcotest.(check (option Test_helpers.hash)) "inspection matches actual HEAD" (Some inspected.head) (Test_helpers.or_fail "head" (R.head linked)); ignore (Test_helpers.or_fail "validate registration backlink and lock" (W.lock_reason manager ~path)); (repo, manager) (* git worktree remove: one --force for [force], a second for [unlock]. *) let reference ~force ~unlock ~dir ~path ~expected ~reason = let flags = (if force then "-f " else "") ^ if unlock then "-f " else "" in let reference = Test_helpers.git_result ~dir ("worktree remove " ^ flags ^ quoted path) in Alcotest.(check bool) "native Git establishes the fixture outcome" expected (result_ok reference); (match reference with | Ok _ -> () | Error text -> Alcotest.(check bool) "native Git names the intended refusal" true (Astring.String.is_infix ~affix:reason text)); reference let check_refusal ~path ~reason actual = match actual with | Ok () -> () | Error (`Msg text) -> Alcotest.(check bool) "library names the selected path" true (Astring.String.is_infix ~affix:(Fpath.to_string path) text); Alcotest.(check bool) "library names the intended refusal" true (Astring.String.is_infix ~affix:reason text) let compare ?(force = false) ?(unlock = false) ~expected ~reason edit = Test_helpers.with_temp_repo @@ fun sw fs root -> let native, native_path = fixture ~fs root "native" in let library, library_path = fixture ~fs root "library" in edit ~fs ~dir:native ~path:native_path; edit ~fs ~dir:library ~path:library_path; let before path = ( Test_helpers.git_output ~dir:path "ls-files --stage", Test_helpers.read_worktree ~fs ~root:path "tracked", Test_helpers.read_worktree ~fs ~root:path "extra", Test_helpers.read_worktree ~fs ~root:path "extra/content", Test_helpers.read_worktree ~fs ~root:path "vendor/content" ) in let native_before = before native_path in let library_before = before library_path in Alcotest.(check bool) "twins begin with the same content and index" true (native_before = library_before); let native_ref = Test_helpers.git_output ~dir:native "rev-parse refs/heads/owned" in let library_ref = Test_helpers.git_output ~dir:library "rev-parse refs/heads/owned" in let repo, manager = initial ~sw ~fs ~dir:library ~path:library_path in let reference = reference ~force ~unlock ~dir:native ~path:native_path ~expected ~reason in let actual = W.remove ~unlock manager ~path:library_path ~force in check_refusal ~path:library_path ~reason actual; let removed = result_ok reference in Alcotest.(check bool) "removal agrees with git" removed (result_ok actual); let present path = Eio.Path.kind ~follow:false Eio.Path.(fs / Fpath.to_string path) <> `Not_found in Alcotest.(check bool) "path presence agrees with git" (present native_path) (present library_path); Alcotest.(check bool) "registration presence agrees with git" (not removed) (W.exists (R.worktree repo) ~path:library_path); Alcotest.(check bool) "branch remains" true (Test_helpers.or_fail "read owned" (R.read_ref repo "refs/heads/owned") <> None); Alcotest.(check string) "native branch is unchanged" native_ref (Test_helpers.git_output ~dir:native "rev-parse refs/heads/owned"); Alcotest.(check string) "library branch is unchanged" library_ref (Test_helpers.git_output ~dir:library "rev-parse refs/heads/owned"); if not removed then begin Alcotest.(check bool) "git preserves bytes and index" true (before native_path = native_before); Alcotest.(check bool) "library preserves bytes and index" true (before library_path = library_before) end let clean () = compare ~expected:true ~reason:"" (fun ~fs:_ ~dir:_ ~path:_ -> ()) let staged () = compare ~expected:false ~reason:"modified or untracked files" (fun ~fs ~dir:_ ~path -> Test_helpers.save_work ~fs ~root:path "tracked" "staged work\n"; Test_helpers.git ~dir:path "add tracked") let unstaged () = compare ~expected:false ~reason:"modified or untracked files" (fun ~fs ~dir:_ ~path -> Test_helpers.save_work ~fs ~root:path "tracked" "unstaged work\n") let untracked () = compare ~expected:false ~reason:"modified or untracked files" (fun ~fs ~dir:_ ~path -> Test_helpers.save_work ~fs ~root:path "extra" "untracked work\n") let nested () = compare ~expected:false ~reason:"modified or untracked files" (fun ~fs ~dir:_ ~path -> let nested = Fpath.(path / "extra") in Eio.Path.mkdir ~perm:0o755 Eio.Path.(fs / Fpath.to_string nested); Test_helpers.git ~dir:nested "init -q"; Test_helpers.save_work ~fs ~root:nested "content" "nested user work\n") let locked () = compare ~expected:false ~reason:"locked" (fun ~fs:_ ~dir ~path -> Test_helpers.git ~dir ("worktree lock --reason held " ^ quoted path)) let forced_unstaged () = compare ~force:true ~expected:true ~reason:"" (fun ~fs ~dir:_ ~path -> Test_helpers.save_work ~fs ~root:path "tracked" "unstaged work\n"; Test_helpers.save_work ~fs ~root:path "extra" "untracked work\n") let forced_locked () = compare ~force:true ~expected:false ~reason:"locked" (fun ~fs:_ ~dir ~path -> Test_helpers.git ~dir ("worktree lock --reason held " ^ quoted path)) let unlocked () = compare ~force:true ~unlock:true ~expected:true ~reason:"" (fun ~fs:_ ~dir ~path -> Test_helpers.git ~dir ("worktree lock --reason held " ^ quoted path)) let submodule () = compare ~expected:false ~reason:"submodules" (fun ~fs ~dir ~path -> let shared = Fpath.(parent dir / "submodule-source") in if Eio.Path.kind ~follow:false Eio.Path.(fs / Fpath.to_string shared) = `Not_found then begin Eio.Path.mkdir ~perm:0o755 Eio.Path.(fs / Fpath.to_string shared); Test_helpers.git ~dir:shared "init -q -b main"; Test_helpers.git ~dir:shared "config user.name Fixture"; Test_helpers.git ~dir:shared "config user.email fixture@example.invalid"; Test_helpers.save_work ~fs ~root:shared "content" "submodule work\n"; Test_helpers.git ~dir:shared "add content"; Test_helpers.git ~dir:shared "commit -qm initial" end; let vendor = Fpath.(path / "vendor") in Test_helpers.git ~dir:path ("clone -q --no-local " ^ quoted shared ^ " " ^ quoted vendor); let head = String.trim (Test_helpers.git_output ~dir:vendor "rev-parse HEAD") in Test_helpers.git ~dir:path ("update-index --add --cacheinfo 160000," ^ head ^ ",vendor"); Test_helpers.git ~dir:path "commit -qm submodule") let suite = ( "worktree_remove", [ Alcotest.test_case "clean removal" `Quick clean; Alcotest.test_case "staged work refusal" `Quick staged; Alcotest.test_case "populated submodule refusal" `Quick submodule; Alcotest.test_case "locked worktree refusal" `Quick locked; Alcotest.test_case "forced removal of changed work" `Quick forced_unstaged; Alcotest.test_case "forced locked worktree refusal" `Quick forced_locked; Alcotest.test_case "doubly forced removal of a locked worktree" `Quick unlocked; Alcotest.test_case "untracked work refusal" `Quick untracked; Alcotest.test_case "untracked nested repository refusal" `Quick nested; Alcotest.test_case "unstaged work refusal" `Quick unstaged; ] )