Something went wrong. Try again.
Git format in pure OCaml
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138(* Copyright (c) 2026 Thomas Gazagnaire <thomas@gazagnaire.org>
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. *)
module Hash = Object_hash
type from = Branch of string | Commit of Hash.t
type t = | Merge | Am | Rebase of { interactive : bool; branch : from option; onto : from option } | Cherry_pick | Revert | Bisect of { from : from option }
let pp_from ppf = function | Branch name -> Fmt.string ppf name | Commit h -> Fmt.string ppf (Hash.to_hex h)
let pp ppf = function | Merge -> Fmt.string ppf "merge" | Am -> Fmt.string ppf "am" | Rebase { interactive; branch; onto = _ } -> Fmt.pf ppf "%srebase%a" (if interactive then "interactive " else "") Fmt.(option (any " of " ++ pp_from)) branch | Cherry_pick -> Fmt.string ppf "cherry-pick" | Revert -> Fmt.string ppf "revert" | Bisect { from } -> Fmt.pf ppf "bisect%a" Fmt.(option (any " from " ++ pp_from)) from
let hash s = try Some (Hash.of_hex s) with Invalid_argument _ -> None
let trim_newline s = if String.ends_with ~suffix:"\n" s then String.sub s 0 (String.length s - 1) else s
(* Every trailing newline, as get_branch strips them. *)let rec trim_newlines s = if String.ends_with ~suffix:"\n" s then trim_newlines (trim_newline s) else s
(* wt-status.c, get_branch: nothing from an empty file, a ref, a commit, the rebase of a detached HEAD (nothing), or the short name of a branch, which BISECT_START holds. *)let from read name = Option.bind (read name) @@ fun s -> let s = trim_newlines s in if String.equal s "" then None else if String.starts_with ~prefix:"refs/" s then Some (Branch s) else match hash s with | Some h -> Some (Commit h) | None -> if String.equal s "detached HEAD" then None else Some (Branch ("refs/heads/" ^ s))
(* wt-status.c, wt_status_check_rebase. *)let rebase ~exists ~read = if exists "rebase-apply" then if exists "rebase-apply/applying" then Some Am else Some (Rebase { interactive = false; branch = from read "rebase-apply/head-name"; onto = from read "rebase-apply/onto"; }) else if exists "rebase-merge" then Some (Rebase { interactive = exists "rebase-merge/interactive"; branch = from read "rebase-merge/head-name"; onto = from read "rebase-merge/onto"; }) else None
(* A pseudoref that names a commit, as repo_get_oid reads it. *)let names_commit read name = match read name with | None -> false | Some s -> Option.is_some (hash (trim_newline s))
(* sequencer.c, sequencer_get_last_command: the first command of sequencer/todo, pick (or p) or revert, followed by a blank. *)let last_command read = let blank c = c = ' ' || c = '\t' in Option.bind (read "sequencer/todo") @@ fun todo -> let todo = String.trim todo in let n = String.length todo in let rec word i = if i < n && not (blank todo.[i]) then word (i + 1) else i in let i = word 0 in if i >= n then None else match String.sub todo 0 i with | "pick" | "p" -> Some `Pick | "revert" -> Some `Revert | _ -> None
let is_cherry_pick = function Cherry_pick -> true | _ -> false
(* wt-status.c, wt_status_get_state. *)let v ~exists ~read = let rebase = rebase ~exists ~read in let main = if exists "MERGE_HEAD" then Option.to_list rebase @ [ Merge ] else match rebase with | Some r -> [ r ] | None -> if names_commit read "CHERRY_PICK_HEAD" then [ Cherry_pick ] else [] in let revert = names_commit read "REVERT_HEAD" in let sequencer = match last_command read with | Some `Pick when not (List.exists is_cherry_pick main) -> [ Cherry_pick ] | Some `Revert when not revert -> [ Revert ] | Some (`Pick | `Revert) | None -> [] in let bisect = if exists "BISECT_LOG" then [ Bisect { from = from read "BISECT_START" } ] else [] in main @ sequencer @ (if revert then [ Revert ] else []) @ bisect