Something went wrong. Try again.
Short-lived signing certificates for Git, issued down a chain of authority
Something went wrong. Try again.
2.2 kB · 68 lines
OCaml
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869open Result.Syntax
let err_msgf fmt = Fmt.kstr (fun s -> Error s) fmtlet path fs p = Eio.Path.(fs / Fpath.to_string p)
let of_env name s = match Fpath.of_string s with | Ok p -> Ok p | Error (`Msg m) -> err_msgf "%s: %s" name m
let file ~fs ~env = match env "GIT_CONFIG_GLOBAL" with | Some p when p <> "" -> of_env "GIT_CONFIG_GLOBAL" p | _ -> ( match env "HOME" with | None | Some "" -> Error "neither GIT_CONFIG_GLOBAL nor HOME names a Git configuration" | Some home -> let* home = of_env "HOME" home in let* xdg = match env "XDG_CONFIG_HOME" with | Some d when d <> "" -> of_env "XDG_CONFIG_HOME" d | _ -> Ok Fpath.(home / ".config") in let user = Fpath.(home / ".gitconfig") in let xdg = Fpath.(xdg / "git" / "config") in if (not (Eio.Path.is_file (path fs user))) && Eio.Path.is_file (path fs xdg) then Ok xdg else Ok user)
let set ~fs file vars = let p = path fs file in let current = match Eio.Path.load p with | s -> Ok s | exception Eio.Io (Eio.Fs.E (Not_found _), _) -> Ok "" | exception (Eio.Io _ as e) -> err_msgf "%a: %a" Fpath.pp file Eio.Exn.pp e in let msg r = Result.map_error (fun (`Msg m) -> Fmt.str "%a: %s" Fpath.pp file m) r in let* current = current in let* config = msg (Git.Config.of_string current) in let* config = List.fold_left (fun acc (name, value) -> let* acc = acc in let* section, key = msg (Git.Config.key name) in Ok (Git.Config.set acc ~section ~key ~value)) (Ok config) vars in Eio.Path.mkdirs ~exists_ok:true ~perm:0o755 (path fs (Fpath.parent file)); Eio.Path.save ~create:(`Or_truncate 0o644) p (Git.Config.to_string config); Ok ()
let sets_signing_key ~fs file = match Eio.Path.load (path fs file) with | exception Eio.Io _ -> false | s -> ( match Git.Config.of_string s with | Error _ -> false | Ok c -> Option.is_some (Git.Config.find c { name = "user"; subsection = None } "signingkey"))