Installs pre-commit hooks for OCaml projects that run dune fmt automatically
1
fork

Configure Feed

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

precommit: Tidy code with idiomatic patterns

- Use pattern match for gitdir parsing instead of String.sub
- Use List.filter_map instead of filter + map
- Simplify empty string check (s <> "" vs String.length)

+9 -9
+9 -9
lib/precommit.ml
··· 98 98 (* .git is a file pointing to the real git dir (worktree) *) 99 99 let content = read_file ~fs git_dir_path in 100 100 let line = String.trim (List.hd (String.split_on_char '\n' content)) in 101 - if String.length line > 8 && String.sub line 0 8 = "gitdir: " then 102 - String.sub line 8 (String.length line - 8) 103 - else git_dir_path 101 + match String.split_on_char ' ' line with 102 + | "gitdir:" :: rest -> String.concat " " rest 103 + | _ -> git_dir_path 104 104 105 105 let init_in_dir ~fs ~dry_run dir = 106 106 let dune_project = Filename.concat dir "dune-project" in ··· 179 179 180 180 let list_subdirs ~fs dir = 181 181 Eio.Path.read_dir Eio.Path.(fs / dir) 182 - |> List.filter (fun name -> 183 - let path = Filename.concat dir name in 184 - is_directory ~fs path && name.[0] <> '.') 185 - |> List.map (fun name -> Filename.concat dir name) 182 + |> List.filter_map (fun name -> 183 + if name.[0] = '.' then None 184 + else 185 + let path = Filename.concat dir name in 186 + if is_directory ~fs path then Some path else None) 186 187 |> List.sort String.compare 187 188 188 189 let run_in_dir ~process_mgr ~fs dir cmd = ··· 191 192 Eio.Process.parse_out process_mgr Eio.Buf_read.take_all ~cwd 192 193 [ "/bin/sh"; "-c"; cmd ] 193 194 in 194 - output |> String.split_on_char '\n' 195 - |> List.filter (fun s -> String.length s > 0) 195 + output |> String.split_on_char '\n' |> List.filter (fun s -> s <> "") 196 196 197 197 type ai_commit = { hash : string; subject : string } 198 198