• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mbarbin / vcs / 17

09 Apr 2024 05:05PM UTC coverage: 99.719% (-0.2%) from 99.944%
17

push

github

mbarbin
Clarify numstat behavior on unexpected output

8 of 13 new or added lines in 2 files covered. (61.54%)

1775 of 1780 relevant lines covered (99.72%)

24.76 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

78.26
/lib/git_cli/src/num_status.ml
1
(*******************************************************************************)
2
(*  Vcs - a versatile OCaml library for Git interaction                        *)
3
(*  Copyright (C) 2024 Mathieu Barbin <mathieu.barbin@gmail.com>               *)
4
(*                                                                             *)
5
(*  This file is part of Vcs.                                                  *)
6
(*                                                                             *)
7
(*  Vcs is free software; you can redistribute it and/or modify it under       *)
8
(*  the terms of the GNU Lesser General Public License as published by the     *)
9
(*  Free Software Foundation either version 3 of the License, or any later     *)
10
(*  version, with the LGPL-3.0 Linking Exception.                              *)
11
(*                                                                             *)
12
(*  Vcs is distributed in the hope that it will be useful, but WITHOUT ANY     *)
13
(*  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS  *)
14
(*  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License and    *)
15
(*  the file `NOTICE.md` at the root of this repository for more details.      *)
16
(*                                                                             *)
17
(*  You should have received a copy of the GNU Lesser General Public License   *)
18
(*  and the LGPL-3.0 Linking Exception along with this library. If not, see    *)
19
(*  <http://www.gnu.org/licenses/> and <https://spdx.org>, respectively.       *)
20
(*******************************************************************************)
21

22
module Status_code = struct
NEW
23
  type t =
×
NEW
24
    | Dash
×
NEW
25
    | Num of int
×
NEW
26
    | Other of string
×
27
  [@@deriving sexp_of]
28

29
  let parse = function
30
    | "-" -> Dash
10✔
31
    | status ->
298✔
32
      (match Int.of_string_opt status with
33
       | Some n when n >= 0 -> Num n
294✔
34
       | Some _ | None -> Other status)
2✔
35
  ;;
36
end
37

38
let parse_line_exn ~line : Vcs.Num_status.Change.t =
39
  match String.split line ~on:'\t' with
158✔
40
  | [] -> assert false
41
  | [ _ ] | [ _; _ ] | _ :: _ :: _ :: _ :: _ ->
1✔
42
    raise_s [%sexp "Unexpected output from git diff", (line : string)]
43
  | [ insertions; deletions; munged_path ] ->
154✔
44
    { Vcs.Num_status.Change.key = Munged_path.parse_exn munged_path
150✔
45
    ; num_stat =
46
        (match Status_code.parse insertions, Status_code.parse deletions with
154✔
47
         | Dash, Dash -> Binary_file
4✔
48
         | Num insertions, Num deletions -> Num_lines_in_diff { insertions; deletions }
146✔
NEW
49
         | _, _ -> raise_s [%sexp "Unexpected output from git diff", (line : string)])
×
50
    }
51
;;
52

53
let parse_lines_exn ~lines = List.map lines ~f:(fun line -> parse_line_exn ~line)
8✔
54

55
module Make (Runtime : Runtime.S) = struct
56
  type t = Runtime.t
57

58
  let diff t ~repo_root ~(changed : Vcs.Name_status.Changed.t) =
59
    let changed_param =
5✔
60
      match changed with
61
      | Between { src; dst } ->
5✔
62
        Printf.sprintf "%s..%s" (src |> Vcs.Rev.to_string) (dst |> Vcs.Rev.to_string)
5✔
63
    in
64
    Runtime.git
65
      t
66
      ~cwd:(repo_root |> Vcs.Repo_root.to_absolute_path)
5✔
67
      ~args:[ "diff"; "--numstat"; changed_param ]
68
      ~f:(fun output ->
69
        let%bind stdout = Vcs.Git.exit0_and_stdout output in
5✔
70
        Or_error.try_with (fun () -> parse_lines_exn ~lines:(String.split_lines stdout)))
5✔
71
  ;;
72
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc