local-ci 0.20.0

Run the GitHub Actions jobs a branch would trigger, locally, in an isolated worktree, and print a short report
Documentation
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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
//! What a `run:` script does, read from its simple commands.
//!
//! A step that would push, publish or deploy, or that installs system
//! packages, is not run here (it is left out and the run is `incomplete`).
//! That is decided from the commands the script runs, not from words it
//! happens to contain: the script is cut into simple commands (at `;`, `&&`,
//! `||`, `|`, `&` and newlines; comments and heredoc bodies are dropped), the
//! command word is found past `sudo`, `env`, `command` and the like, and its
//! subcommand decides. `echo gh pr list`, `grep -q x vercel.json` and
//! `git push --dry-run` run nothing outside this machine.

type Cmd = Vec<String>;

/// Cut `script` into simple commands, each a list of words with the quotes
/// removed. Comments, heredoc bodies and redirections are left out.
pub(crate) fn commands(script: &str) -> Vec<Cmd> {
    let mut out = Vec::new();
    collect(script, 0, &mut out);
    out
}

fn collect(script: &str, depth: usize, out: &mut Vec<Cmd>) {
    for cmd in split(script) {
        // `bash -c '...'` runs a script of its own
        if depth < 4 {
            if let Some(inner) = shell_c(&cmd) {
                collect(&inner, depth + 1, out);
            }
        }
        out.push(cmd);
    }
}

fn shell_c(cmd: &Cmd) -> Option<String> {
    let (_, rest) = strip(cmd)?;
    let (head, args) = rest.split_first()?;
    if !matches!(base(head), "bash" | "sh" | "zsh" | "dash") {
        return None;
    }
    let i = args
        .iter()
        .position(|a| a.starts_with('-') && !a.starts_with("--") && a.ends_with('c'))?;
    args.get(i + 1).cloned()
}

struct Words {
    words: Vec<(String, bool)>,
    cur: String,
    in_word: bool,
    quoted: bool,
    out: Vec<Cmd>,
}

impl Words {
    fn end_word(&mut self) {
        if self.in_word {
            self.words
                .push((std::mem::take(&mut self.cur), self.quoted));
        }
        self.in_word = false;
        self.quoted = false;
    }

    fn end_command(&mut self) {
        self.end_word();
        let words = std::mem::take(&mut self.words);
        let mut cmd = Vec::new();
        let mut it = words.into_iter();
        while let Some((w, quoted)) = it.next() {
            if !quoted && is_redirect(&w) {
                // `2>&1`, `>file` carry their target; a bare `>` takes the next word
                if w.trim_start_matches(|c: char| c.is_ascii_digit()).len() <= 2
                    && !w.contains('&')
                    && w.trim_start_matches(|c: char| c.is_ascii_digit())
                        .chars()
                        .all(|c| c == '<' || c == '>')
                {
                    it.next();
                }
                continue;
            }
            cmd.push(w);
        }
        if !cmd.is_empty() {
            self.out.push(cmd);
        }
    }
}

fn is_redirect(w: &str) -> bool {
    let t = w.trim_start_matches(|c: char| c.is_ascii_digit());
    t.starts_with('<') || t.starts_with('>')
}

fn split(script: &str) -> Vec<Cmd> {
    let chars: Vec<char> = script.chars().collect();
    let mut w = Words {
        words: vec![],
        cur: String::new(),
        in_word: false,
        quoted: false,
        out: vec![],
    };
    // delimiters of heredocs whose bodies start after the current line
    let mut heredocs: Vec<(String, bool)> = Vec::new();
    let mut i = 0;
    while i < chars.len() {
        let c = chars[i];
        match c {
            '\\' => {
                match chars.get(i + 1) {
                    // a line continuation
                    Some('\n') => {}
                    Some(&n) => {
                        w.cur.push(n);
                        w.in_word = true;
                    }
                    None => {}
                }
                i += 2;
                continue;
            }
            '\'' => {
                w.in_word = true;
                w.quoted = true;
                i += 1;
                while i < chars.len() && chars[i] != '\'' {
                    w.cur.push(chars[i]);
                    i += 1;
                }
                i += 1;
                continue;
            }
            '"' => {
                w.in_word = true;
                w.quoted = true;
                i += 1;
                while i < chars.len() && chars[i] != '"' {
                    if chars[i] == '\\' && i + 1 < chars.len() {
                        i += 1;
                    }
                    w.cur.push(chars[i]);
                    i += 1;
                }
                i += 1;
                continue;
            }
            '#' if !w.in_word => {
                while i < chars.len() && chars[i] != '\n' {
                    i += 1;
                }
                continue;
            }
            '\n' => {
                w.end_command();
                i += 1;
                // heredoc bodies start on the next line
                for (delim, strip_tabs) in std::mem::take(&mut heredocs) {
                    while i < chars.len() {
                        let end = chars[i..]
                            .iter()
                            .position(|&c| c == '\n')
                            .map_or(chars.len(), |p| i + p);
                        let line: String = chars[i..end].iter().collect();
                        i = (end + 1).min(chars.len());
                        let line = if strip_tabs {
                            line.trim_start_matches('\t')
                        } else {
                            line.as_str()
                        };
                        if line.trim_end() == delim {
                            break;
                        }
                    }
                }
                continue;
            }
            '<' if chars.get(i + 1) == Some(&'<') && chars.get(i + 2) != Some(&'<') => {
                // `<<EOF`, `<<-EOF`, `<<'EOF'`
                let mut j = i + 2;
                let strip_tabs = chars.get(j) == Some(&'-');
                if strip_tabs {
                    j += 1;
                }
                while chars.get(j).is_some_and(|c| *c == ' ' || *c == '\t') {
                    j += 1;
                }
                let mut delim = String::new();
                while let Some(&d) = chars.get(j) {
                    if d.is_whitespace() || matches!(d, ';' | '&' | '|' | '(' | ')') {
                        break;
                    }
                    if !matches!(d, '\'' | '"' | '\\') {
                        delim.push(d);
                    }
                    j += 1;
                }
                if !delim.is_empty() {
                    heredocs.push((delim, strip_tabs));
                }
                w.end_word();
                i = j;
                continue;
            }
            ';' | '(' | ')' | '`' => {
                w.end_command();
            }
            '|' => {
                w.end_command();
                if matches!(chars.get(i + 1), Some('|') | Some('&')) {
                    i += 1;
                }
            }
            '&' => {
                // `2>&1`, `>&2`, `&>file` are redirections, not a separator
                let redirect =
                    (w.in_word && w.cur.ends_with(['>', '<'])) || chars.get(i + 1) == Some(&'>');
                if redirect {
                    w.cur.push('&');
                    w.in_word = true;
                } else {
                    w.end_command();
                    if chars.get(i + 1) == Some(&'&') {
                        i += 1;
                    }
                }
            }
            c if c.is_whitespace() => w.end_word(),
            c => {
                w.cur.push(c);
                w.in_word = true;
            }
        }
        i += 1;
    }
    w.end_command();
    w.out
}

fn base(word: &str) -> &str {
    word.rsplit('/').next().unwrap_or(word)
}

fn is_assignment(w: &str) -> bool {
    match w.split_once('=') {
        Some((k, _)) => {
            !k.is_empty()
                && !k.starts_with(|c: char| c.is_ascii_digit())
                && k.chars().all(|c| c.is_ascii_alphanumeric() || c == '_')
        }
        None => false,
    }
}

const KEYWORDS: &[&str] = &[
    "if", "then", "else", "elif", "do", "while", "until", "!", "{", "}", "time",
];

/// Past keywords, assignments and wrappers (`env`, `command`, `sudo`, ...):
/// whether `sudo` was among them and the command that is run. `None` for a
/// command that only looks something up (`command -v x`) or has nothing to run.
fn strip(cmd: &[String]) -> Option<(bool, &[String])> {
    let mut sudo = false;
    let mut i = 0;
    loop {
        let w = cmd.get(i)?.as_str();
        if KEYWORDS.contains(&w) || is_assignment(w) {
            i += 1;
            continue;
        }
        match base(w) {
            "sudo" => {
                sudo = true;
                i += 1;
                while let Some(a) = cmd.get(i) {
                    if matches!(
                        a.as_str(),
                        "-u" | "-g" | "-C" | "-p" | "-h" | "-D" | "-r" | "-t" | "-U"
                    ) {
                        i += 2;
                    } else if a.starts_with('-') || is_assignment(a) {
                        i += 1;
                    } else {
                        break;
                    }
                }
            }
            "env" => {
                i += 1;
                while let Some(a) = cmd.get(i) {
                    if matches!(a.as_str(), "-u" | "-C" | "-S") {
                        i += 2;
                    } else if a.starts_with('-') || is_assignment(a) {
                        i += 1;
                    } else {
                        break;
                    }
                }
            }
            "command" => {
                i += 1;
                while let Some(a) = cmd.get(i) {
                    match a.as_str() {
                        "-v" | "-V" => return None,
                        "-p" | "--" => i += 1,
                        _ => break,
                    }
                }
            }
            "exec" | "nohup" | "builtin" | "setsid" | "xargs" | "stdbuf" => {
                i += 1;
                while cmd.get(i).is_some_and(|a| a.starts_with('-')) {
                    i += 1;
                }
            }
            "nice" => {
                i += 1;
                while let Some(a) = cmd.get(i) {
                    if a == "-n" {
                        i += 2;
                    } else if a.starts_with('-') {
                        i += 1;
                    } else {
                        break;
                    }
                }
            }
            "timeout" => {
                i += 1;
                while cmd.get(i).is_some_and(|a| a.starts_with('-')) {
                    i += 1;
                }
                i += 1; // the duration
            }
            "npx" | "bunx" => i = skip_runner_options(cmd, i + 1),
            tool @ ("npm" | "pnpm" | "yarn" | "bun") if runner_sub(cmd, i, tool).is_some() => {
                i = skip_runner_options(cmd, runner_sub(cmd, i, tool).unwrap_or(i + 1));
            }
            _ => return Some((sudo, &cmd[i..])),
        }
    }
}

/// For `pnpm dlx x`, `pnpm exec x`, `yarn dlx x`, `yarn exec x`, `npm exec x`
/// and `bun x x`: the index after the runner subcommand, past options before it
/// (`pnpm --filter pkg exec x`).
fn runner_sub(cmd: &[String], i: usize, tool: &str) -> Option<usize> {
    let subs: &[&str] = match tool {
        "npm" => &["exec", "x"],
        "pnpm" | "yarn" => &["dlx", "exec"],
        "bun" => &["x"],
        _ => return None,
    };
    let mut j = i + 1;
    loop {
        let a = cmd.get(j)?.as_str();
        if a.starts_with('-') {
            j += if matches!(
                a,
                "--filter" | "-F" | "--prefix" | "-C" | "--cwd" | "--dir" | "--workspace" | "-w"
            ) {
                2
            } else {
                1
            };
        } else {
            return subs.contains(&a).then_some(j + 1);
        }
    }
}

/// Past the options of a package runner (`-y`, `-p pkg`, `--`) to the command.
fn skip_runner_options(cmd: &[String], mut j: usize) -> usize {
    while let Some(a) = cmd.get(j).map(String::as_str) {
        if !a.starts_with('-') {
            break;
        }
        j += if matches!(
            a,
            "-p" | "--package" | "--registry" | "--cache" | "--node-options" | "-c"
        ) {
            2
        } else {
            1
        };
        if a == "--" {
            break;
        }
    }
    j
}

/// The words of `args` that are not options, skipping the values of the
/// options named in `valued`.
fn positionals<'a>(args: &'a [String], valued: &[&str]) -> Vec<&'a str> {
    let mut out = Vec::new();
    let mut i = 0;
    while i < args.len() {
        let a = args[i].as_str();
        if valued.contains(&a) {
            i += 2;
            continue;
        }
        if !a.starts_with('-') || a == "-" {
            out.push(a);
        }
        i += 1;
    }
    out
}

fn has_flag(args: &[String], names: &[&str]) -> bool {
    args.iter().any(|a| {
        names
            .iter()
            .any(|n| a == n || a.starts_with(&format!("{n}=")))
    })
}

/// A `-n` among short options (`-n`, `-nu`).
fn short_n(args: &[String]) -> bool {
    args.iter()
        .any(|a| a.starts_with('-') && !a.starts_with("--") && a.contains('n'))
}

const AWS_VALUED: &[&str] = &[
    "--profile",
    "--region",
    "--endpoint-url",
    "--output",
    "--query",
    "--cli-read-timeout",
    "--cli-connect-timeout",
    "--ca-bundle",
    "--color",
];

fn aws_remote(args: &[String]) -> bool {
    let pos = positionals(args, AWS_VALUED);
    let (Some(service), op) = (pos.first().copied(), pos.get(1).copied().unwrap_or("")) else {
        return false;
    };
    if has_flag(args, &["--dryrun", "--dry-run"]) {
        return false;
    }
    match service {
        "s3" => match op {
            "cp" | "sync" => {
                // writing to a bucket; a download is a read
                pos.get(3).is_some_and(|d| d.starts_with("s3://"))
            }
            "mv" | "rm" | "mb" | "rb" => true,
            _ => false,
        },
        "deploy" => op != "help",
        "configure" | "help" | "sts" => false,
        _ => {
            const READ: &[&str] = &[
                "describe-",
                "get-",
                "list-",
                "head-",
                "batch-get-",
                "lookup-",
                "search-",
                "scan",
                "query",
                "filter-",
                "validate-",
                "estimate-",
                "check-",
                "preview-",
                "select-",
            ];
            if op.is_empty()
                || op == "help"
                || READ.iter().any(|p| op.starts_with(p))
                || args.iter().any(|a| a == "--version")
            {
                return false;
            }
            const WRITE: &[&str] = &[
                "create",
                "delete",
                "update",
                "put",
                "start",
                "stop",
                "run",
                "invoke",
                "terminate",
                "modify",
                "attach",
                "detach",
                "publish",
                "send",
                "tag",
                "untag",
                "register",
                "deregister",
                "upload",
                "deploy",
                "remove",
                "add",
                "set",
                "reboot",
                "restore",
                "release",
                "associate",
                "disassociate",
                "enable",
                "disable",
                "execute",
                "import",
                "copy",
                "purge",
                "cancel",
                "reset",
                "rotate",
                "revoke",
                "authorize",
                "grant",
                "apply",
                "replace",
                "change",
                "write",
                "post",
                "commit",
                "promote",
                "rollback",
            ];
            WRITE.iter().any(|p| op.starts_with(p))
        }
    }
}

fn gh_remote(args: &[String]) -> bool {
    let pos = positionals(args, &["-R", "--repo", "--hostname"]);
    let (Some(group), sub) = (pos.first().copied(), pos.get(1).copied().unwrap_or("")) else {
        return false;
    };
    match group {
        "api" => {
            let method = args
                .iter()
                .enumerate()
                .find_map(|(i, a)| {
                    if a == "-X" || a == "--method" {
                        args.get(i + 1).cloned()
                    } else if let Some(m) = a.strip_prefix("--method=") {
                        Some(m.to_string())
                    } else {
                        a.strip_prefix("-X")
                            .filter(|m| !m.is_empty())
                            .map(String::from)
                    }
                })
                .map(|m| m.to_ascii_uppercase());
            match method {
                Some(m) => matches!(m.as_str(), "POST" | "PATCH" | "PUT" | "DELETE"),
                // fields and an input make it a POST
                None => has_flag(args, &["-f", "-F", "--field", "--raw-field", "--input"]),
            }
        }
        "pr" => matches!(
            sub,
            "create" | "merge" | "comment" | "edit" | "close" | "reopen" | "review" | "ready"
        ),
        "issue" => matches!(
            sub,
            "create" | "comment" | "edit" | "close" | "reopen" | "delete" | "transfer" | "lock"
        ),
        "release" => matches!(
            sub,
            "create" | "upload" | "edit" | "delete" | "delete-asset"
        ),
        "workflow" => matches!(sub, "run" | "enable" | "disable"),
        "run" => matches!(sub, "rerun" | "cancel" | "delete"),
        "repo" => matches!(
            sub,
            "create" | "delete" | "edit" | "fork" | "rename" | "archive" | "unarchive"
        ),
        "secret" | "variable" => matches!(sub, "set" | "delete" | "remove"),
        "label" => matches!(sub, "create" | "edit" | "delete" | "clone"),
        "gist" => matches!(sub, "create" | "edit" | "delete" | "rename"),
        _ => false,
    }
}

fn kubectl_remote(args: &[String]) -> bool {
    if has_flag(args, &["--dry-run"]) {
        return false;
    }
    let pos = positionals(
        args,
        &[
            "-n",
            "--namespace",
            "--context",
            "--kubeconfig",
            "-s",
            "--server",
        ],
    );
    match pos.first().copied() {
        Some("rollout") => !matches!(pos.get(1).copied(), Some("status" | "history")),
        Some(
            "apply" | "delete" | "create" | "replace" | "patch" | "scale" | "set" | "annotate"
            | "label" | "edit" | "drain" | "cordon" | "uncordon" | "taint" | "run" | "expose"
            | "autoscale" | "cp" | "exec",
        ) => true,
        _ => false,
    }
}

fn git_remote(args: &[String]) -> bool {
    let pos = positionals(args, &["-C", "-c", "--git-dir", "--work-tree"]);
    if pos.first().copied() != Some("push") {
        return false;
    }
    let after: &[String] = match args.iter().position(|a| a == "push") {
        Some(i) => &args[i + 1..],
        None => &[],
    };
    !(has_flag(after, &["--dry-run"]) || short_n(after))
}

const DOCKER_VALUED: &[&str] = &[
    "--context",
    "-c",
    "-H",
    "--host",
    "--config",
    "-l",
    "--log-level",
    "-f",
    "--file",
    "-p",
    "--project-name",
    "--profile",
    "--env-file",
    "--project-directory",
];

fn docker_remote(head: &str, args: &[String]) -> bool {
    let pos = positionals(args, DOCKER_VALUED);
    let (first, second) = (
        pos.first().copied().unwrap_or(""),
        pos.get(1).copied().unwrap_or(""),
    );
    let pushes = has_flag(args, &["--push"])
        || args
            .iter()
            .any(|a| a.contains("type=registry") || a.contains("push=true"));
    // `docker-compose push` has no `compose` word
    let (first, second) = if base(head) == "docker-compose" {
        ("compose", first)
    } else {
        (first, second)
    };
    match first {
        "push" | "login" => true,
        "image" | "manifest" => second == "push",
        "compose" => second == "push",
        "buildx" => {
            matches!(second, "build" | "bake" | "b") && pushes
                || (second == "imagetools" && pos.get(2).copied() == Some("create"))
        }
        "build" | "bake" => pushes,
        _ => false,
    }
}

/// Short options of `curl` that take a value, so the rest of the cluster (or
/// the next word) is theirs.
const CURL_SHORT_VALUED: &str = "oHuAebcDEKwxyYzUrCmQ";

fn write_method(m: &str) -> bool {
    matches!(
        m.to_ascii_uppercase().as_str(),
        "POST" | "PUT" | "PATCH" | "DELETE"
    )
}

fn curl_remote(args: &[String]) -> bool {
    let get = has_flag(args, &["-G", "--get"]);
    let mut body = false;
    let mut i = 0;
    while i < args.len() {
        let a = args[i].as_str();
        if let Some(long) = a.strip_prefix("--") {
            let (name, inline) = match long.split_once('=') {
                Some((n, v)) => (n, Some(v)),
                None => (long, None),
            };
            match name {
                "request" => {
                    let v = inline.or_else(|| args.get(i + 1).map(String::as_str));
                    if v.is_some_and(write_method) {
                        return true;
                    }
                }
                "data" | "data-raw" | "data-binary" | "data-urlencode" | "data-ascii" | "form"
                | "form-string" | "upload-file" | "json" => body = true,
                _ => {}
            }
        } else if a.starts_with('-') && a.len() > 1 {
            let cluster: Vec<char> = a[1..].chars().collect();
            let mut k = 0;
            while k < cluster.len() {
                match cluster[k] {
                    'X' => {
                        let rest: String = cluster[k + 1..].iter().collect();
                        let v = if rest.is_empty() {
                            args.get(i + 1).cloned().unwrap_or_default()
                        } else {
                            rest
                        };
                        if write_method(&v) {
                            return true;
                        }
                        break;
                    }
                    'd' | 'F' | 'T' => {
                        body = true;
                        break;
                    }
                    c if CURL_SHORT_VALUED.contains(c) => break,
                    _ => {}
                }
                k += 1;
            }
        }
        i += 1;
    }
    // `-G` turns `-d` into a query string
    body && !get
}

fn wget_remote(args: &[String]) -> bool {
    has_flag(
        args,
        &["--post-data", "--post-file", "--body-data", "--body-file"],
    ) || args.iter().enumerate().any(|(i, a)| {
        let v = if a == "--method" {
            args.get(i + 1).map(String::as_str)
        } else {
            a.strip_prefix("--method=")
        };
        v.is_some_and(write_method)
    })
}

fn is_remote(sudo_less: &[String]) -> bool {
    let Some((head, args)) = sudo_less.split_first() else {
        return false;
    };
    let pos = positionals(args, &[]);
    let first = pos.first().copied().unwrap_or("");
    let second = pos.get(1).copied().unwrap_or("");
    let dry = has_flag(args, &["--dry-run", "--dryrun", "-n"]);
    match base(head) {
        "git" => git_remote(args),
        "gh" => gh_remote(args),
        "aws" => aws_remote(args),
        "kubectl" | "oc" => kubectl_remote(args),
        // options take values (`--filter pkg`, `--prefix a`, `yarn npm`,
        // `yarn workspace x`): look for the word anywhere, not at one place
        "npm" | "pnpm" | "yarn" | "bun" => {
            args.iter()
                .any(|a| matches!(a.as_str(), "publish" | "unpublish" | "deprecate"))
                && !dry
        }
        "cargo" => first == "publish" && !dry,
        "dart" | "flutter" => first == "pub" && second == "publish" && !dry,
        "twine" => first == "upload",
        "docker" | "docker-compose" | "podman" | "buildah" => docker_remote(head, args),
        "curl" => curl_remote(args),
        "wget" => wget_remote(args),
        "helm" => matches!(first, "install" | "upgrade" | "uninstall" | "rollback") && !dry,
        "terraform" | "tofu" => matches!(first, "apply" | "destroy"),
        "serverless" | "sls" => matches!(first, "deploy" | "remove"),
        "firebase" => first == "deploy",
        "wrangler" => {
            (matches!(
                first,
                "deploy" | "publish" | "secret" | "delete" | "rollback"
            ) || (first == "pages" && matches!(second, "deploy" | "publish")))
                && !has_flag(args, &["--dry-run"])
        }
        "vercel" => {
            if has_flag(args, &["--version", "-v", "--help", "-h"]) {
                return false;
            }
            first.is_empty()
                || first.starts_with(['.', '/', '~'])
                || matches!(
                    first,
                    "deploy" | "promote" | "rollback" | "remove" | "rm" | "alias"
                )
        }
        "netlify" => first == "deploy",
        "gcloud" => pos.iter().any(|w| {
            matches!(
                *w,
                "deploy"
                    | "create"
                    | "delete"
                    | "update"
                    | "submit"
                    | "set"
                    | "add"
                    | "remove"
                    | "cp"
                    | "rsync"
                    | "mv"
                    | "rm"
                    | "import"
                    | "export"
            )
        }),
        _ => false,
    }
}

/// Whether the script runs something that changes the world outside this
/// machine: a push, a publish, a deploy, a write through `gh`, `aws` or
/// `kubectl`. Reading is not: `git push --dry-run`, `aws sts get-caller-identity`,
/// `gh pr list` and `kubectl get` run here.
pub(crate) fn remote_effect(script: &str) -> bool {
    commands(script)
        .iter()
        .any(|c| strip(c).is_some_and(|(_, rest)| is_remote(rest)))
}

/// What a script wants of the system's packages.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Install {
    /// Nothing.
    No,
    /// `brew install` of formulae already on the path: running it changes
    /// nothing, so the step runs.
    Present,
    /// Installs or changes system packages; not done here.
    Needed,
}

/// Commands run as root that change the system.
const SUDO_MUTATES: &[&str] = &[
    "apt",
    "apt-get",
    "aptitude",
    "yum",
    "dnf",
    "apk",
    "snap",
    "brew",
    "choco",
    "pacman",
    "zypper",
    "dpkg",
    "rpm",
    "pip",
    "pip3",
    "npm",
    "gem",
    "cargo",
    "make",
    "cp",
    "mv",
    "install",
    "ln",
    "tee",
    "rm",
    "chmod",
    "chown",
    "mkdir",
    "systemctl",
    "service",
    "sysctl",
    "ufw",
    "tar",
    "curl",
    "wget",
    "sh",
    "bash",
    "useradd",
    "usermod",
    "mount",
    "umount",
    "update-alternatives",
];

fn pkg_installs(head: &str, args: &[String]) -> bool {
    let first = positionals(args, &[]).first().copied().unwrap_or("");
    match head {
        "apt" | "apt-get" | "aptitude" | "yum" | "dnf" | "zypper" => matches!(
            first,
            "install"
                | "update"
                | "upgrade"
                | "dist-upgrade"
                | "full-upgrade"
                | "remove"
                | "purge"
                | "autoremove"
                | "reinstall"
                | "localinstall"
                | "groupinstall"
                | "downgrade"
        ),
        "apk" => matches!(first, "add" | "del" | "upgrade" | "update"),
        "snap" | "choco" => matches!(first, "install" | "upgrade" | "remove" | "refresh"),
        "pacman" => args
            .iter()
            .any(|a| a.starts_with("-S") || a.starts_with("-R")),
        "dpkg" | "rpm" => args
            .iter()
            .any(|a| matches!(a.as_str(), "-i" | "-U" | "-e" | "-ivh")),
        _ => false,
    }
}

/// Whether the script installs system packages. `have` says whether a command
/// is on the path, for `brew install` of something that is there already.
pub(crate) fn global_install(script: &str, have: &dyn Fn(&str) -> bool) -> Install {
    let mut found = Install::No;
    for c in commands(script) {
        let Some((sudo, rest)) = strip(&c) else {
            continue;
        };
        let Some((head, args)) = rest.split_first() else {
            continue;
        };
        let head = base(head);
        if head == "brew" {
            let first = positionals(args, &[]).first().copied().unwrap_or("");
            match first {
                "install" | "reinstall" => {
                    let names: Vec<&str> = positionals(args, &[]).into_iter().skip(1).collect();
                    let cask = has_flag(args, &["--cask", "--casks"]);
                    let present = !cask
                        && !names.is_empty()
                        && names
                            .iter()
                            .all(|n| have(n.rsplit('/').next().unwrap_or(n)))
                        && first == "install";
                    if present {
                        found = Install::Present;
                    } else {
                        return Install::Needed;
                    }
                }
                "upgrade" | "tap" | "untap" | "uninstall" | "update" => return Install::Needed,
                _ => {}
            }
        } else if pkg_installs(head, args) || (sudo && SUDO_MUTATES.contains(&head)) {
            return Install::Needed;
        }
    }
    found
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn false_positives_are_not_remote_effects() {
        for s in [
            "git push --dry-run origin HEAD",
            "git push -n origin HEAD",
            "echo using aws sdk",
            "grep -q x vercel.json",
            "kubectl kustomize .",
            "echo gh pr list",
            "sudo -n true",
            "# git push origin main\necho hi",
            "cat <<EOF\ngit push origin main\nEOF\necho done",
            "git diff --exit-code",
            "npm run lint",
            "echo pushing",
            "npm publish --dry-run",
            "gh pr list --state open",
            "gh pr view 1",
            "gh api repos/x/y",
            "gh api -X GET repos/x/y",
            "aws --version",
            "aws sts get-caller-identity",
            "aws ec2 describe-instances --region us-east-1",
            "aws s3 ls s3://b",
            "aws s3 cp s3://b/f .",
            "aws s3 sync s3://b/dir out",
            "kubectl get pods",
            "kubectl rollout status deploy/x",
            "kubectl apply -f x.yaml --dry-run=client",
            "vercel --version",
            "vercel build",
            "command -v kubectl",
            "FOO=1 echo vercel",
            "git status && git log -1",
            "git push -n",
            "aws s3 ls",
            "curl https://example.test",
            "curl -I https://example.test",
            "curl -sSL -o out.tgz https://example.test/x.tgz",
            "curl -G -d q=1 https://example.test",
            "curl -X GET https://example.test",
            "wget https://example.test/x",
            "wget -q -O - https://example.test",
            "npx --yes prettier --check .",
            "pnpm exec eslint .",
            "pnpm dlx create-thing",
            "yarn dlx tsc",
            "npm exec -- tsc -p .",
            "npm --prefix a run build",
            "pnpm --filter pkg test",
            "yarn workspace x build",
            "docker build -t x .",
            "docker image ls",
            "docker compose up -d",
            "docker buildx build -t x .",
            "wrangler deploy --dry-run",
            "vercel pull --yes",
            "vercel ls",
            "npx publish-check",
        ] {
            assert!(!remote_effect(s), "{s:?}");
        }
    }

    #[test]
    fn remote_effects_are_found() {
        for s in [
            "git push origin main",
            "git -C sub push",
            "npm publish",
            "pnpm publish --access public",
            "gh pr merge 3 --squash",
            "gh pr create --fill",
            "gh pr comment 3 -b hi",
            "gh release create v1",
            "gh api -X POST repos/x/y/issues",
            "gh api --method=DELETE repos/x/y",
            "gh api repos/x/y -f a=b",
            "aws s3 sync dist s3://bucket",
            "aws s3 cp a.txt s3://bucket/a.txt",
            "aws s3 rm s3://bucket/a --recursive",
            "aws deploy create-deployment --application-name a",
            "aws cloudformation deploy --template-file t.yml --stack-name s",
            "aws lambda update-function-code --function-name f",
            "kubectl apply -f x.yaml",
            "kubectl delete pod x",
            "kubectl rollout restart deploy/x",
            "vercel --prod",
            "vercel deploy",
            "netlify deploy --prod",
            "echo hi\ncd out && git push origin HEAD:main",
            "if true; then git push; fi",
            "sudo git push",
            "env GIT_TERMINAL_PROMPT=0 git push origin x",
            "bash -c 'git push origin main'",
            "terraform apply -auto-approve",
            "cargo publish",
            "docker push img:1",
            "true | gh pr merge 1",
            "npx wrangler deploy",
            "npx --yes vercel --prod",
            "bunx netlify deploy --prod",
            "pnpm dlx wrangler pages deploy dist",
            "pnpm exec npm publish",
            "yarn dlx vercel deploy",
            "yarn exec -- npm publish",
            "npm exec -y -- wrangler deploy",
            "npm exec --package=x -- git push",
            "yarn npm publish",
            "yarn workspace x publish",
            "pnpm --filter pkg publish",
            "pnpm -r publish --no-git-checks",
            "npm --prefix a publish",
            "bun publish",
            "docker image push img:1",
            "docker buildx build --push -t x .",
            "docker buildx bake --push",
            "docker compose push",
            "docker compose -f ci.yml push",
            "docker-compose push",
            "docker manifest push img:1",
            "docker buildx imagetools create -t a b",
            "wrangler deploy",
            "wrangler pages deploy dist",
            "vercel",
            "vercel ./app",
            "netlify deploy",
            "curl -X POST https://example.test",
            "curl -XPUT https://example.test",
            "curl --request DELETE https://example.test",
            "curl --request=patch https://example.test",
            "curl -d a=b https://example.test",
            "curl --data-binary @f https://example.test",
            "curl -sSd a=b https://example.test",
            "curl -F f=@x https://example.test",
            "curl --form f=@x https://example.test",
            "curl -T f https://example.test/f",
            "curl --upload-file f https://example.test/f",
            "curl --json '{}' https://example.test",
            "wget --post-data=a=b https://example.test",
            "wget --post-file f https://example.test",
            "wget --method=PUT https://example.test",
            "wget --method DELETE https://example.test",
        ] {
            assert!(remote_effect(s), "{s:?}");
        }
    }

    fn none(_: &str) -> bool {
        false
    }

    #[test]
    fn global_installs() {
        for s in [
            "sudo apt-get install -y jq",
            "sudo apt-get update && echo x",
            "apt-get install jq",
            "apt install jq",
            "brew install jq",
            "brew upgrade",
            "yum install -y x",
            "sudo make install",
            "sudo cp a /usr/local/bin/a",
            "sudo pip install x",
            "apk add curl",
        ] {
            assert_eq!(global_install(s, &none), Install::Needed, "{s:?}");
        }
        for s in [
            "npm ci",
            "echo sudo",
            "echo brew install x",
            "sudo -n true",
            "sudo -v",
            "apt list --installed",
            "apt-cache policy jq",
            "command -v brew",
            "# sudo apt-get install x",
        ] {
            assert_eq!(global_install(s, &none), Install::No, "{s:?}");
        }
    }

    #[test]
    fn brew_install_of_what_is_there_runs() {
        let have = |c: &str| c == "jq" || c == "yq";
        assert_eq!(global_install("brew install jq", &have), Install::Present);
        assert_eq!(
            global_install("brew install jq yq", &have),
            Install::Present
        );
        assert_eq!(
            global_install("brew install --quiet mikefarah/yq/yq", &have),
            Install::Present
        );
        assert_eq!(
            global_install("brew install jq ripgrep", &have),
            Install::Needed
        );
        assert_eq!(
            global_install("brew install --cask jq", &have),
            Install::Needed
        );
        assert_eq!(global_install("brew install", &have), Install::Needed);
        assert_eq!(
            global_install("brew install jq\nsudo apt-get install x", &have),
            Install::Needed
        );
    }

    #[test]
    fn heredoc_bodies_and_continuations() {
        let s = "cat > f <<-'EOF'\n\tgit push\n\tEOF\ngit \\\n  push origin x";
        assert!(remote_effect(s));
        let s = "cat > f <<EOF\ngit push\naws s3 rm x\nEOF\n";
        assert!(!remote_effect(s));
    }
}