-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplumbing.ml
509 lines (406 loc) · 17.8 KB
/
plumbing.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
(** Raw interface to git system commands *)
module type Plumbing = sig
(** Representation of raw git commands.
This module represents the calling of calling git commands with
any command line arguments *)
(** The representation of the result of executing a git command *)
type result
(** [make result o e] is a result where [o] is the lines of stdout and
[e] is the lines of stderr *)
val make_result : string list -> string list -> string list -> result
(** [get_stdout r] is the lines of stdout *)
val get_stdout : result -> string list
(** [get_stdout r] is the lines of stderr *)
val get_stderr : result -> string list
(** [get_out r] is the lines of of both stdout and stderr in the order
that they were sent to their respective streams. For example, if
data was written to stdout, then stderr, and then stdout, again,
then [get_out r] follows that same order. *)
val get_out : result -> string list
(** [init args] calls git init with arguments [args] *)
val init : string array -> result
(** [push] calls git push with arguments [args]*)
val push : string array -> result
(** [pull args] calls git pull with arguments [args] *)
val pull : string array -> result
(** [hash_object args] calls git hash-object with arguments [args] and
is the output to standard output *)
val hash_object : string array -> result
(** [cat_file args] calls git cat-file with arguments [args] *)
val cat_file : string array -> result
(** [update_index args] calls git update-index with arguments [args] *)
val update_index : string array -> result
(** [write_tree args] calls git write-tree with arguments [args] *)
val write_tree : string array -> result
(** [read_tree args] calls git read-tree with arguments [args] *)
val read_tree : string array -> result
(** [commit_tree args] calls git commit-tree with arguments [args] *)
val commit_tree : string array -> result
(** [log args] calls git log with arguments [args] *)
val log : string array -> result
(** [add args] calls git add with arguments [args] *)
val add : string array -> result
(** [restore args] calls git restore with arguments [args] *)
val restore : string array -> result
(** [commit] calls git commit with arguments [args] *)
val commit : string array -> result
(** [show args] calls git show with arguments [args] *)
val show : string array -> result
(** [diff args] calls git diff with arguments [args] *)
val diff : string array -> result
(** [revparse args] calls git rev-parse with arguments [args] *)
val revparse : string array -> result
(** [status args] calls git status with arguments [args] *)
val status : string array -> result
(** [head args] calls git symbolic-ref HEAD with arguments [args]*)
val head : string array -> result
(** [checkout args] calls git checkout with arguments [args]*)
val checkout : string array -> result
(** [branch args] calls git branch with arguments [args] *)
val branch : string array -> result
(** [stash args] calls git stash with arguments [args] *)
val stash : string array -> result
(** [reset args] calls git reset with arguments [args] *)
val reset : string array -> result
(** [git args] calls git with arguments [args] *)
val git : string array -> result
end
module type PlumbingWithSet = sig
include Plumbing
(**[set_log_data out err out_and_err changes the log to a result containing \[out\], \[err\], and \[out_and_err\]]*)
val set_log_data : string list -> string list -> string list -> unit
(**[set_status_data out err out_and_err] changes the status to a
result containing [out], [err], and [out_and_err]*)
val set_status_data :
string list -> string list -> string list -> unit
(**[set_head_data out err out_and_err] changes the head to a result
containing [out], [err], and [out_and_err]*)
val set_head_data : string list -> string list -> string list -> unit
end
(** The [Plumbing] used to run OGit *)
module ProdPlumbing : Plumbing = struct
(** Types and methods to access and construct the type *)
type result = {
stdout : string list;
stderr : string list;
out_and_err : string list; (*exit_code : int;*)
}
(** [make result o e] is a result where [o] is the lines of stdout and
[e] is the lines of stderr *)
let make_result out err out_and_err =
{ stdout = out; stderr = err; out_and_err }
(** [get_stdout r] is the lines of stdout *)
let get_stdout result = result.stdout
(** [get_stderr r] is the lines of stderr *)
let get_stderr result = result.stderr
(** [get_out r] is the lines of of both stdout and stderr in the order
that they were sent to their respective streams.
For example, if data was written to stdout, then stderr, and then
stdout, again, then [get_out r] follows that same order. *)
let get_out result = result.out_and_err
(** Helper Methods *)
(** [read fd] is the lines of file referenced by descriptor [fd] *)
let read (fd : Unix.file_descr) : string list =
let in_ch = Unix.in_channel_of_descr fd in
let lines = ref [] in
try
while true do
lines := input_line in_ch :: !lines
done;
!lines
with End_of_file ->
close_in in_ch;
!lines
(** [fork_and_execv e a] is the result of executing program [exe] with
arguments [args]*)
let fork_and_execv (exe : string) (args : string array) : result =
let inp_stdout, out_stdout = Unix.pipe () in
(* Pipe for stdout *)
let inp_stderr, out_stderr = Unix.pipe () in
(* Pipe for stderr *)
let inp, out = Unix.pipe () in
(* Pipe for both stdout and stderr *)
let pid = Unix.fork () in
if pid = 0 then (
Unix.close inp_stdout;
(* Not used by child *)
Unix.close inp_stderr;
(* Not used by child *)
Unix.close inp;
(* Not used by child *)
Unix.dup2 out_stdout Unix.stdout;
(* Bind stdout pipe to stdout *)
Unix.dup2 out_stderr Unix.stderr;
(* Bind stderr pipe to stderr *)
Unix.dup2 out Unix.stdout;
(* Bind out pipe to stdout *)
Unix.dup2 out Unix.stderr;
(* Bind out pipe to stderr *)
Unix.execvp exe args)
else (
Unix.close out_stdout;
(* Not used by parent*)
Unix.close out_stderr;
(* Not used by parent*)
Unix.close out;
(* Not used by parent*)
let stdout = read inp_stdout in
let stdin = read inp_stderr in
let out_and_err = read inp in
(* Does not close the pipes because [read fd] does that when it
closes the input channel it creates.
Unix.close inp_stderr; Unix.close inp_stdout; Unix.close inp; *)
make_result stdout stdin out_and_err)
(** [init args] calls git init with arguments [args] *)
let init (args : string array) =
fork_and_execv "git" (Array.append [| "git"; "init" |] args)
(** [push] calls git push with arguments [args]*)
let push (args : string array) =
fork_and_execv "git" (Array.append [| "git"; "push" |] args)
(** [pull args] calls git pull with arguments [args] *)
let pull (args : string array) =
fork_and_execv "git" (Array.append [| "git"; "pull" |] args)
(** [hash_object args] calls git hash-object with arguments [args] and
is the output to standard output *)
let hash_object (args : string array) =
fork_and_execv "git" (Array.append [| "git"; "hash-object" |] args)
(** [cat_file args] calls git cat-file with arguments [args] *)
let cat_file (args : string array) =
fork_and_execv "git" (Array.append [| "git"; "cat-file" |] args)
(** [update_index args] calls git update-index with arguments [args] *)
let update_index (args : string array) =
fork_and_execv "git" (Array.append [| "git"; "update-index" |] args)
(** [write_tree args] calls git write-tree with arguments [args] *)
let write_tree (args : string array) =
fork_and_execv "git" (Array.append [| "git"; "write-tree" |] args)
(** [read_tree args] calls git read-tree with arguments [args] *)
let read_tree (args : string array) =
fork_and_execv "git" (Array.append [| "git"; "read-tree" |] args)
(** [commit_tree args] calls git commit-tree with arguments [args] *)
let commit_tree (args : string array) =
fork_and_execv "git" (Array.append [| "git"; "commit-tree" |] args)
(** [log args] calls git log with arguments [args] *)
let log (args : string array) =
fork_and_execv "git"
(Array.append
[| "git"; "--no-pager"; "log"; "--format=reference" |]
args)
(** [add args] calls git add with arguments [args] *)
let add (args : string array) =
fork_and_execv "git" (Array.append [| "git"; "add" |] args)
(** [restore args] calls git restore with arguments [args] *)
let restore (args : string array) =
fork_and_execv "git" (Array.append [| "git"; "restore" |] args)
(** [commit] calls git commit with arguments [args] *)
let commit (args : string array) =
fork_and_execv "git" (Array.append [| "git"; "commit" |] args)
(** [show args] calls git show with arguments [args] *)
let show (args : string array) =
fork_and_execv "git"
(Array.append [| "git"; "--no-pager"; "show" |] args)
(** [diff args] calls git diff with arguments [args] *)
let diff (args : string array) =
fork_and_execv "git"
(Array.append [| "git"; "--no-pager"; "diff" |] args)
(** [revparse args] calls git rev-parse with arguments [args] *)
let revparse (args : string array) =
fork_and_execv "git" (Array.append [| "git"; "rev-parse" |] args)
(** [status args] calls git status with arguments [args] *)
let status (args : string array) =
fork_and_execv "git" (Array.append [| "git"; "status" |] args)
(** [head args] calls git symbolic-ref HEAD with arguments [args]*)
let head (args : string array) =
fork_and_execv "git"
(Array.append [| "git"; "symbolic-ref"; "HEAD" |] args)
(** [checkout args] calls git checkout with arguments [args]*)
let checkout (args : string array) =
fork_and_execv "git" (Array.append [| "git"; "checkout" |] args)
(** [branch args] calls git branch with arguments [args] *)
let branch (args : string array) =
fork_and_execv "git" (Array.append [| "git"; "branch" |] args)
(** [stash args] calls git stash with arguments [args] *)
let stash (args : string array) =
fork_and_execv "git" (Array.append [| "git"; "stash" |] args)
(** [reset args] calls git reset with arguments [args] *)
let reset (args : string array) =
fork_and_execv "git" (Array.append [| "git"; "reset" |] args)
(** [git args] calls git with arguments [args] *)
let git (args : string array) =
fork_and_execv "git" (Array.append [| "git" |] args)
end
(** A simulated [Plumbing] used in an isolated testing environment *)
module MockPlumbing : PlumbingWithSet = struct
(** Types and methods to access and construct the type *)
type result = {
stdout : string list;
stderr : string list;
out_and_err : string list; (*exit_code : int;*)
}
(** [get_stdout r] is the lines of stdout *)
let get_stdout result = result.stdout
(** [get_stderr r] is the lines of stderr *)
let get_stderr result = result.stderr
(** [get_out r] is the lines of of both stdout and stderr in the order
that they were sent to their respective streams.
For example, if data was written to stdout, then stderr, and then
stdout, again, then [get_out r] follows that same order. *)
let get_out result = result.out_and_err
(** [make result o e] is a result where [o] is the lines of stdout and
[e] is the lines of stderr *)
let make_result out err out_and_err =
{ stdout = out; stderr = err; out_and_err }
(** [git args] simulates calling git with arguments [args] *)
let git (args : string array) =
make_result (Array.to_list args) [] (Array.to_list args)
(** [init args] simulates calling git init with arguments [args] *)
let init (args : string array) =
make_result
[ "Initialized empty Git repository in /home/fake/.git/" ]
[]
[ "Initialized empty Git repository in /home/fake/.git/" ]
(** [push] simulates calling git push with arguments [args]*)
let push (args : string array) =
let new_args = Array.of_list ("push" :: Array.to_list args) in
git new_args
(** [pull args] simulates calling git pull with arguments [args] *)
let pull (args : string array) =
let new_args = Array.of_list ("pull" :: Array.to_list args) in
git new_args
(** [hash_object args] simulates calling git hash-object with
arguments [args] and is the output to standard output *)
let hash_object (args : string array) =
let new_args =
Array.of_list ("hash_object" :: Array.to_list args)
in
git new_args
(** [cat_file args] simulates calling git cat-file with arguments
[args] *)
let cat_file (args : string array) =
let new_args = Array.of_list ("cat_file" :: Array.to_list args) in
git new_args
(** [branch args] simulates calling git branch with arguments [args] *)
let branch (args : string array) =
let new_args = Array.of_list ("cat_file" :: Array.to_list args) in
git new_args
(** [update_index args] simulates calling git update-index with
arguments [args] *)
let update_index (args : string array) =
let new_args =
Array.of_list ("update-index" :: Array.to_list args)
in
git new_args
(** [write_tree args] simulates calling git write-tree with arguments
[args] *)
let write_tree (args : string array) =
let new_args = Array.of_list ("write-tree" :: Array.to_list args) in
git new_args
(** [read_tree args] simulates calling git read-tree with arguments
[args] *)
let read_tree (args : string array) =
let new_args = Array.of_list ("read-tree" :: Array.to_list args) in
git new_args
(** [commit_tree args] simulates calling git commit-tree with
arguments [args] *)
let commit_tree (args : string array) =
let new_args =
Array.of_list ("commit-tree" :: Array.to_list args)
in
git new_args
(**[log_data] is the commit history log in [result] form*)
let log_data =
ref
{
stdout =
[
"59689ce (setup project files, 2021-03-22)";
"b92c19e (Initial commit, 2021-03-04)";
];
stderr = [];
out_and_err =
[
"59689ce (setup project files, 2021-03-22)";
"b92c19e (Initial commit, 2021-03-04)";
];
}
(**[set_log_data out err out_and_err changes the log to a result containing
\[out\], \[err\], and \[out_and_err\]]*)
let set_log_data out err out_and_err =
log_data := make_result out err out_and_err
(** [log args] simulates calling git log with arguments [args] *)
let log (args : string array) = !log_data
(**[status_data] is the result representing the current git status*)
let status_data = ref { stdout = []; stderr = []; out_and_err = [] }
(** [add args] simulates calling git add with arguments [args] *)
let add (args : string array) =
let new_args = Array.of_list ("add" :: Array.to_list args) in
let exec = git new_args in
set_log_data (Array.to_list args) [] (Array.to_list args);
exec
(** [stash args] simulates calling git stash with arguments [args] *)
let stash (args : string array) =
let new_args = Array.of_list ("stash" :: Array.to_list args) in
git new_args
(** [head args] simulates calling git symbolic-ref HEAD with arguments
[args]*)
let head (args : string array) =
let new_args = Array.of_list ("head" :: Array.to_list args) in
git new_args
(**[checkout args] simulates calling [git checkout] with arguments
[args]*)
let checkout (args : string array) =
let new_args = Array.of_list ("checkout" :: Array.to_list args) in
git new_args
(** [restore args] simulates calling git restore with arguments [args] *)
let restore (args : string array) =
let new_args = Array.of_list ("restore" :: Array.to_list args) in
git new_args
(** [commit] simulates calling git commit with arguments [args] *)
let commit (args : string array) =
let new_args = Array.of_list ("commit" :: Array.to_list args) in
git new_args
(** [show args] simulates calling git show with arguments [args] *)
let show (args : string array) =
let new_args = Array.of_list ("show" :: Array.to_list args) in
git new_args
(** [diff args] simulates calling git diff with arguments [args] *)
let diff (args : string array) =
let new_args = Array.of_list ("diff" :: Array.to_list args) in
git new_args
(**[head_data] is the information about the head branch*)
let head_data =
ref
{
stdout = [ "origin/master" ];
stderr = [];
out_and_err = [ "origin/master" ];
}
(**[set_head_data out err out_and_err] changes the head to a result
containing [out], [err], and [out_and_err]*)
let set_head_data out err out_and_err =
head_data := make_result out err out_and_err
let head (args : string array) = !head_data
(** [checkout args] simulates calling git checkout with arguments
[args]*)
let checkout (args : string array) =
let new_args = Array.of_list ("push" :: Array.to_list args) in
git new_args
(** [reset args] simulates calling git reset with arguments [args] *)
let reset (args : string array) =
let new_args = Array.of_list ("reset" :: Array.to_list args) in
git new_args
(** [revparse args] simulates calling git rev-parse with arguments
[args] *)
let revparse (args : string array) =
{
stdout = [ "origin/master" ];
stderr = [];
out_and_err = [ "origin/master" ];
}
(**[set_status_data out err out_and_err] changes the status to a
result containing [out], [err], and [out_and_err]*)
let set_status_data out err out_and_err =
log_data := make_result out err out_and_err;
status_data := { stdout = out; stderr = []; out_and_err }
(** [status args] simulates calling git status with arguments [args] *)
let status (args : string array) = !status_data
end