Homebrew bottle builder and tap manager for OCaml monorepos
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Read git HEAD directly instead of shelling out to git rev-parse

Read .git/HEAD and follow the ref to get the commit hash. No
subprocess, no dependency on git being in PATH. Uses Bos.OS.File.read
which is already a dependency.

+19 -8
+19 -8
lib/homebrew.ml
··· 203 203 204 204 (* {1 Building} *) 205 205 206 + let git_head_short () = 207 + (* Read HEAD directly from .git without shelling out. *) 208 + let read_file path = 209 + match Bos.OS.File.read (Fpath.v path) with 210 + | Ok s -> Some (String.trim s) 211 + | Error _ -> None 212 + in 213 + match read_file ".git/HEAD" with 214 + | Some s when Astring.String.is_prefix ~affix:"ref: " s -> ( 215 + let ref_path = ".git/" ^ Astring.String.drop ~max:5 s in 216 + match read_file ref_path with 217 + | Some hex when String.length hex >= 7 -> String.sub hex 0 7 218 + | _ -> "unknown") 219 + | Some hex when String.length hex >= 7 -> String.sub hex 0 7 220 + | _ -> "unknown" 221 + 206 222 let version_string () = 207 223 let t = Unix.localtime (Unix.gettimeofday ()) in 208 - let date = Fmt.str "%04d%02d%02d" (1900 + t.tm_year) (1 + t.tm_mon) t.tm_mday in 209 - let hash = 210 - match 211 - Bos.OS.Cmd.run_out Bos.Cmd.(v "git" % "rev-parse" % "--short" % "HEAD") 212 - |> Bos.OS.Cmd.out_string 213 - with 214 - | Ok (h, _) -> String.trim h 215 - | Error _ -> "unknown" 224 + let date = 225 + Fmt.str "%04d%02d%02d" (1900 + t.tm_year) (1 + t.tm_mon) t.tm_mday 216 226 in 227 + let hash = git_head_short () in 217 228 Fmt.str "%s-%s" date hash 218 229 219 230 let exe config (bin : binary) =