Skip to content

Commit 0dc6c1d

Browse files
authored
Merge pull request #148 from phongulus/phong/longest-common-prefix-fix
Fix longest common prefix
2 parents 2ba9deb + d164e24 commit 0dc6c1d

File tree

4 files changed

+51
-15
lines changed

4 files changed

+51
-15
lines changed

lib/common.ml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,5 @@ let write_to_local_file ~data path =
6161
try Ok (Devkit.Files.save_as path (fun oc -> Printf.fprintf oc "%s" data))
6262
with exn -> fmt_error "%s" (Exn.to_string exn)
6363

64-
let longest_common_prefix xs =
65-
match xs with
66-
| [] -> ""
67-
| [ x ] -> x
68-
| x :: _ -> List.sort (Fun.flip String.compare) xs |> List.hd |> Stre.common_prefix x |> String.sub x 0
69-
7064
let sign_string_sha256 ~key ~basestring =
7165
Cstruct.of_string basestring |> Nocrypto.Hash.SHA256.hmac ~key:(Cstruct.of_string key) |> Hex.of_cstruct |> Hex.show

lib/slack_message.ml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,16 +133,16 @@ let month = function
133133
let condense_file_changes files =
134134
match files with
135135
| [ f ] -> sprintf "_modified `%s` (+%d-%d)_" (escape_mrkdwn f.filename) f.additions f.deletions
136-
| _ ->
137-
let rec drop_last = function
138-
| [ _ ] | [] -> [] (* Should raise when empty instead? *)
139-
| v :: vs -> v :: drop_last vs
136+
| [] -> "_no files modified_"
137+
| first_file :: fl ->
138+
let rec longest_prefix_of_two_lists l1 l2 =
139+
match l1, l2 with
140+
| e1 :: l1', e2 :: l2' when e1 = e2 -> e1 :: longest_prefix_of_two_lists l1' l2'
141+
| _ -> []
140142
in
141143
let prefix_path =
142-
List.map (fun f -> f.filename) files
143-
|> Common.longest_common_prefix
144-
|> String.split_on_char '/'
145-
|> drop_last
144+
List.map (fun f -> String.split_on_char '/' f.filename) fl
145+
|> List.fold_left longest_prefix_of_two_lists (String.split_on_char '/' first_file.filename)
146146
|> String.concat "/"
147147
in
148148
sprintf "modified %d files%s" (List.length files) (if prefix_path = "" then "" else sprintf " in `%s/`" prefix_path)

test/dune

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(executables
2-
(names test github_link_test)
2+
(names test github_link_test longest_prefix_test)
33
(libraries lib devkit devkit.core extlib lwt.unix
44
yojson)
55
(preprocess
@@ -27,3 +27,8 @@
2727
(alias runtest)
2828
(action
2929
(run ./github_link_test.exe)))
30+
31+
(rule
32+
(alias runtest)
33+
(action
34+
(run ./longest_prefix_test.exe)))

test/longest_prefix_test.ml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
open Lib
2+
open Github_t
3+
open Slack_message
4+
5+
let make_test_file filename =
6+
{
7+
sha = "something something";
8+
filename;
9+
status = "something something";
10+
additions = 5;
11+
deletions = 5;
12+
changes = 10;
13+
url = "test_link";
14+
}
15+
16+
let single_file = [ make_test_file "testdir1/testdir2/changed_file.txt" ]
17+
18+
let multiple_files_same_dir =
19+
List.map make_test_file
20+
[
21+
"testdir1/testdir2/changed_file2.txt";
22+
"testdir1/testdir2/changed_file3.txt";
23+
"testdir1/testdir2/changed_file1.txt";
24+
]
25+
26+
let multiple_files_common_root_dir =
27+
List.map make_test_file [ "testdir1/changed_file2.txt"; "backend/changed_file3.txt"; "changed_file1.txt" ]
28+
29+
let multiple_files_common_dir =
30+
List.map make_test_file
31+
[ "backend/something/changed_file2.txt"; "backend/something/changed_file3.txt"; "backend/some/changed_file1.txt" ]
32+
33+
let () =
34+
assert (condense_file_changes single_file = "_modified `testdir1/testdir2/changed_file.txt` (+5-5)_");
35+
assert (condense_file_changes multiple_files_same_dir = "modified 3 files in `testdir1/testdir2/`");
36+
assert (condense_file_changes multiple_files_common_root_dir = "modified 3 files");
37+
assert (condense_file_changes multiple_files_common_dir = "modified 3 files in `backend/`")

0 commit comments

Comments
 (0)