-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
1061 lines (969 loc) · 27.9 KB
/
Copy pathmain.go
File metadata and controls
1061 lines (969 loc) · 27.9 KB
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
// diffhance preprocesses each side of a diff before handing the result
// to a diff tool. It is designed to compose with git, diff, delta, etc.
//
// diffhance --pre 'jq .' a.json b.json
// diffhance --rule '*.json:jq .' --rule '*.xml:xmllint --format -' a b
// diffhance --config .diffhance-rules a b
// diffhance --print-rules
// diffhance --print-config-dirs
// GIT_EXTERNAL_DIFF='diffhance --git --rule "*.json:jq ."' git diff HEAD~1
package main
import (
"bytes"
"errors"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
)
const usage = `diffhance - preprocess each side of a diff before diffing
USAGE
diffhance [flags] LEFT RIGHT
diffhance [flags] git [GIT-DIFF-ARGS...]
diffhance --git [flags] PATH OLD-FILE OLD-HEX OLD-MODE NEW-FILE NEW-HEX NEW-MODE [RENAME-TO RENAME-MODE]
GIT SHORTHAND
"diffhance [flags] git ..." runs "git diff ..." with GIT_EXTERNAL_DIFF set to
this binary in --git mode, forwarding any flags given before "git".
FLAGS
-p, --pre CMD Shell pipeline applied to BOTH sides (file streamed on stdin)
--pre-left CMD Shell pipeline applied to LEFT side only
--pre-right CMD Shell pipeline applied to RIGHT side only
-r, --rule GLOB:CMD Apply CMD when the file's basename matches GLOB (repeatable).
Matching rules are chained in order. Ignored when --pre/--pre-* is set.
--pre-errors TARGET Report ignored rule preprocessor failures to "stderr" or PATH.
Default: silent.
-c, --config PATH Read rules from PATH, one GLOB:CMD per line. Blank lines and
lines starting with # are ignored. May be repeated.
Existing default configs are also loaded from the platform's
config directories as diffhance/rules.
-d, --diff CMD Diff backend (default: "diff -u"). Receives preprocessed
LEFT and RIGHT as arguments. Bare "hunk" invokes
"hunk difftool -- LEFT RIGHT PATH" with the display path.
--git Treat positional args as git's external-diff invocation:
PATH OLD-FILE OLD-HEX OLD-MODE NEW-FILE NEW-HEX NEW-MODE ...
--print Skip diffing. Print "LEFT-PATH\tRIGHT-PATH" of the
preprocessed files (kept on disk) for downstream tools.
--print-rules Print resolved rules, one per line, then exit.
--print-config-dirs Print default config file locations checked, one per line, then exit.
--no-color Do not auto-add color flags to the default diff backend.
--version Print the release name and exit.
-h, --help Show this help and exit.
EXAMPLES
# Diff two JSON files after running jq on both
diffhance --pre 'jq .' old.json new.json
# Use delta as the renderer
diffhance --pre 'jq .' -d delta old.json new.json
# Review the preprocessed pair in Hunk
diffhance --pre 'jq .' -d hunk old.json new.json
# Per-extension rules
diffhance -r '*.json:jq .' -r '*.xml:xmllint --format -' a/ b/ # (per file)
# Rules from a config file
diffhance --config .diffhance-rules a.json b.json
# Inspect resolved rules in application order
diffhance --print-rules
# Inspect default config file locations
diffhance --print-config-dirs
# Pipe two preprocessed files into your own pipeline
read L R < <(diffhance --print --pre 'jq .' a.json b.json); diff -u "$L" "$R"
# Use as a git external diff
GIT_EXTERNAL_DIFF='diffhance --git --rule "*.json:jq ." -d "diff -u"' \
git diff HEAD~1 HEAD
# Same, via the git shorthand (flags before "git" are forwarded)
diffhance -r '*.json:jq .' git HEAD~1 HEAD
EXIT STATUS
Mirrors the diff backend (0 = identical, 1 = differ, >1 = error).
`
type stringSlice []string
func (s *stringSlice) String() string { return strings.Join(*s, ",") }
func (s *stringSlice) Set(v string) error { *s = append(*s, v); return nil }
var releaseName = "dev"
const (
leftPrefix = "l"
rightPrefix = "r"
)
type options struct {
pre, preLeft, preRight string
diff string
rules []string
git bool
printPaths bool
printRules bool
printConfigDirs bool
version bool
noColor bool
preErrors string
args []string
}
func main() {
if forward, gitArgs, ok := splitGitShorthand(os.Args[1:]); ok {
code, err := runGitDiff(forward, gitArgs)
if err != nil {
fmt.Fprintf(os.Stderr, "diffhance: %v\n", err)
if code == 0 {
code = 2
}
}
os.Exit(code)
}
opts, err := parseArgs(os.Args[1:])
if err != nil {
if errors.Is(err, errHelp) {
fmt.Fprint(os.Stdout, usage)
return
}
fmt.Fprintf(os.Stderr, "diffhance: %v\n\n%s", err, usage)
os.Exit(2)
}
code, err := run(opts)
if err != nil {
fmt.Fprintf(os.Stderr, "diffhance: %v\n", err)
if code == 0 {
code = 2
}
}
os.Exit(code)
}
var errHelp = errors.New("help requested")
// valueFlags lists flags that consume the following argument as their value.
var valueFlags = map[string]bool{
"-p": true, "--pre": true,
"--pre-left": true, "--pre-right": true,
"-d": true, "--diff": true,
"-r": true, "--rule": true,
"-c": true, "--config": true,
"--pre-errors": true,
}
// splitGitShorthand detects the "diffhance [flags] git ..." shorthand, where
// the first positional argument is the literal word "git". It returns the
// flags preceding "git" (forwarded to the GIT_EXTERNAL_DIFF command) and the
// arguments following it (passed through to "git diff").
func splitGitShorthand(argv []string) (forward, gitArgs []string, ok bool) {
for i := 0; i < len(argv); i++ {
a := argv[i]
switch {
case a == "git":
return argv[:i], argv[i+1:], true
case a == "--":
// Explicit end of flags: positionals are LEFT/RIGHT files.
return nil, nil, false
case valueFlags[a]:
i++ // skip the flag's value; parseArgs reports missing values
case strings.HasPrefix(a, "-") && a != "-":
// Flag with attached value (or boolean); nothing extra to skip.
default:
// First positional is not "git": normal LEFT/RIGHT mode.
return nil, nil, false
}
}
return nil, nil, false
}
// executablePath is swapped in tests.
var executablePath = func() string {
if exe, err := os.Executable(); err == nil && exe != "" {
return exe
}
return os.Args[0]
}
// runGitDiff implements "diffhance [flags] git [ARGS...]": it runs
// "git diff ARGS..." with GIT_EXTERNAL_DIFF pointing back at this binary in
// --git mode, forwarding any diffhance flags that preceded "git".
func runGitDiff(forward, gitArgs []string) (int, error) {
gitCommand := []string{"diff"}
if forwardedDiffUsesHunk(forward) {
// Git normally puts diff output behind its pager when stdout is a
// terminal. A direct Hunk backend needs that terminal itself.
gitCommand = []string{"--no-pager", "diff"}
}
c := exec.Command("git", append(gitCommand, gitArgs...)...)
c.Env = append(os.Environ(), "GIT_EXTERNAL_DIFF="+gitExternalDiffCommand(executablePath(), forward))
c.Stdin = os.Stdin
c.Stdout = os.Stdout
c.Stderr = os.Stderr
if err := c.Run(); err != nil {
var ee *exec.ExitError
if errors.As(err, &ee) {
return ee.ExitCode(), nil
}
return 2, fmt.Errorf("running git diff: %w", err)
}
return 0, nil
}
func forwardedDiffUsesHunk(argv []string) bool {
backend := ""
for i := 0; i < len(argv); i++ {
a := argv[i]
switch {
case a == "-d" || a == "--diff":
if i+1 < len(argv) {
i++
backend = argv[i]
}
case strings.HasPrefix(a, "--diff="):
backend = strings.TrimPrefix(a, "--diff=")
case strings.HasPrefix(a, "-d") && len(a) > 2:
backend = a[2:]
case valueFlags[a]:
if i+1 < len(argv) {
i++
}
}
}
return isHunkDifftoolBackend(backend)
}
// gitExternalDiffCommand builds the GIT_EXTERNAL_DIFF command string. Git
// always runs external diff commands through a POSIX shell (on Windows via
// Git's bundled sh), so single-quote quoting applies on every platform.
func gitExternalDiffCommand(exe string, forward []string) string {
parts := []string{posixQuote(exe), "--git"}
for _, f := range forward {
parts = append(parts, posixQuote(f))
}
return strings.Join(parts, " ")
}
// parseArgs implements a small POSIX-ish flag parser so we can support both
// "--pre CMD" and "--pre=CMD" plus short forms like "-p CMD" / "-pCMD",
// without taking on a third-party dependency.
func parseArgs(argv []string) (*options, error) {
o := &options{diff: ""}
take := func(i *int, name string) (string, error) {
if *i+1 >= len(argv) {
return "", fmt.Errorf("flag %s requires a value", name)
}
*i++
return argv[*i], nil
}
for i := 0; i < len(argv); i++ {
a := argv[i]
switch {
case a == "--":
o.args = append(o.args, argv[i+1:]...)
i = len(argv)
case a == "-h" || a == "--help":
return nil, errHelp
case a == "--git":
o.git = true
case a == "--print":
o.printPaths = true
case a == "--print-rules":
o.printRules = true
case a == "--print-config-dirs":
o.printConfigDirs = true
case a == "--version":
o.version = true
case a == "--no-color":
o.noColor = true
case a == "--pre-errors":
v, err := take(&i, a)
if err != nil {
return nil, err
}
o.preErrors = v
case strings.HasPrefix(a, "--pre-errors="):
o.preErrors = strings.TrimPrefix(a, "--pre-errors=")
case a == "-p", a == "--pre":
v, err := take(&i, a)
if err != nil {
return nil, err
}
o.pre = v
case strings.HasPrefix(a, "--pre="):
o.pre = strings.TrimPrefix(a, "--pre=")
case strings.HasPrefix(a, "-p") && len(a) > 2:
o.pre = a[2:]
case a == "--pre-left":
v, err := take(&i, a)
if err != nil {
return nil, err
}
o.preLeft = v
case strings.HasPrefix(a, "--pre-left="):
o.preLeft = strings.TrimPrefix(a, "--pre-left=")
case a == "--pre-right":
v, err := take(&i, a)
if err != nil {
return nil, err
}
o.preRight = v
case strings.HasPrefix(a, "--pre-right="):
o.preRight = strings.TrimPrefix(a, "--pre-right=")
case a == "-d", a == "--diff":
v, err := take(&i, a)
if err != nil {
return nil, err
}
o.diff = v
case strings.HasPrefix(a, "--diff="):
o.diff = strings.TrimPrefix(a, "--diff=")
case strings.HasPrefix(a, "-d") && len(a) > 2:
o.diff = a[2:]
case a == "-r", a == "--rule":
v, err := take(&i, a)
if err != nil {
return nil, err
}
o.rules = append(o.rules, v)
case strings.HasPrefix(a, "--rule="):
o.rules = append(o.rules, strings.TrimPrefix(a, "--rule="))
case strings.HasPrefix(a, "-r") && len(a) > 2:
o.rules = append(o.rules, a[2:])
case a == "-c", a == "--config":
v, err := take(&i, a)
if err != nil {
return nil, err
}
rules, err := readRulesFile(v)
if err != nil {
return nil, err
}
o.rules = append(o.rules, rules...)
case strings.HasPrefix(a, "--config="):
rules, err := readRulesFile(strings.TrimPrefix(a, "--config="))
if err != nil {
return nil, err
}
o.rules = append(o.rules, rules...)
case strings.HasPrefix(a, "-c") && len(a) > 2:
rules, err := readRulesFile(a[2:])
if err != nil {
return nil, err
}
o.rules = append(o.rules, rules...)
case strings.HasPrefix(a, "-") && a != "-":
return nil, fmt.Errorf("unknown flag %q", a)
default:
o.args = append(o.args, a)
}
}
if o.diff == "" {
o.diff = "diff -u"
}
if o.printConfigDirs || o.version {
return o, nil
}
rules, err := readExistingRulesFiles(defaultRulesConfigPaths())
if err != nil {
return nil, err
}
o.rules = append(o.rules, rules...)
return o, nil
}
var defaultRulesConfigPaths = platformRulesConfigPaths
func platformRulesConfigPaths() []string {
var paths []string
if dir, err := os.UserConfigDir(); err == nil && dir != "" {
paths = append(paths, filepath.Join(dir, "diffhance", "rules"))
}
switch runtime.GOOS {
case "windows":
if dir := os.Getenv("ProgramData"); dir != "" {
paths = append(paths, filepath.Join(dir, "diffhance", "rules"))
}
case "darwin":
if dir, err := os.UserHomeDir(); err == nil && dir != "" {
paths = append(paths, filepath.Join(dir, ".config", "diffhance", "rules"))
}
paths = append(paths, filepath.Join(string(filepath.Separator), "Library", "Application Support", "diffhance", "rules"))
default:
dirs := os.Getenv("XDG_CONFIG_DIRS")
if dirs == "" {
dirs = filepath.Join(string(filepath.Separator), "etc", "xdg")
}
for _, dir := range filepath.SplitList(dirs) {
if dir != "" {
paths = append(paths, filepath.Join(dir, "diffhance", "rules"))
}
}
}
return paths
}
func readExistingRulesFiles(paths []string) ([]string, error) {
var rules []string
for _, path := range paths {
fileRules, err := readRulesFile(path)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
continue
}
return nil, err
}
rules = append(rules, fileRules...)
}
return rules, nil
}
func readRulesFile(path string) ([]string, error) {
b, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("read config %q: %w", path, err)
}
var rules []string
for line := range strings.SplitSeq(string(b), "\n") {
line = strings.TrimSpace(line)
if line == "" || strings.HasPrefix(line, "#") {
continue
}
rules = append(rules, line)
}
return rules, nil
}
func run(o *options) (int, error) {
if o.version {
fmt.Println(releaseName)
return 0, nil
}
if o.printConfigDirs {
printConfigDirs(defaultRulesConfigPaths())
return 0, nil
}
if o.printRules {
printRules(o.rules)
return 0, nil
}
leftPath, rightPath, leftDisplayPath, rightDisplayPath, err := resolveInputs(o)
if err != nil {
return 2, err
}
leftCmds, leftIgnorePreprocessErrors := pickPre(o, leftDisplayPath, side("left"))
rightCmds, rightIgnorePreprocessErrors := pickPre(o, rightDisplayPath, side("right"))
preErrOut, closePreErrorWriter, err := preprocessErrorWriter(o.preErrors)
if err != nil {
return 2, err
}
if closePreErrorWriter != nil {
defer closePreErrorWriter()
}
tmpDir, err := os.MkdirTemp("", "diffhance-")
if err != nil {
return 2, err
}
if !o.printPaths {
defer os.RemoveAll(tmpDir)
}
leftOut, err := stage(tmpDir, leftPrefix, leftDisplayPath, leftPath, leftCmds, leftIgnorePreprocessErrors, preErrOut)
if err != nil {
return 2, fmt.Errorf("preprocess left: %w", err)
}
rightOut, err := stage(tmpDir, rightPrefix, rightDisplayPath, rightPath, rightCmds, rightIgnorePreprocessErrors, preErrOut)
if err != nil {
return 2, fmt.Errorf("preprocess right: %w", err)
}
if o.printPaths {
fmt.Printf("%s\t%s\n", leftOut, rightOut)
return 0, nil
}
leftLabel := filepath.Join(leftPrefix, stageDisplayPath(leftDisplayPath, leftPrefix))
rightLabel := filepath.Join(rightPrefix, stageDisplayPath(rightDisplayPath, rightPrefix))
code, err := runDiff(o, leftOut, rightOut, leftLabel, rightLabel, rightDisplayPath)
// Git treats any non-zero exit from GIT_EXTERNAL_DIFF as fatal. The "files
// differ" signal (exit 1) is meaningless to git here — it already knew
// they differ, that's why it invoked us — so swallow it in --git mode.
if o.git && err == nil && code == 1 {
code = 0
}
return code, err
}
func printRules(rules []string) {
for _, r := range rules {
fmt.Println(r)
}
}
func printConfigDirs(paths []string) {
for _, path := range paths {
fmt.Println(path)
}
}
type side string
// resolveInputs returns left, right, and per-side display paths used for rule
// matching and staged output paths. In --git mode the display paths are the
// repo-relative paths that git provides.
func resolveInputs(o *options) (left, right, leftDisplay, rightDisplay string, err error) {
if o.git {
if len(o.args) < 7 {
return "", "", "", "", fmt.Errorf("--git expects at least 7 positional args, got %d", len(o.args))
}
// path old-file old-hex old-mode new-file new-hex new-mode [rename-to rename-mode]
rightDisplay = o.args[0]
if len(o.args) >= 9 {
rightDisplay = o.args[7]
}
return o.args[1], o.args[4], o.args[0], rightDisplay, nil
}
if len(o.args) != 2 {
return "", "", "", "", fmt.Errorf("expected exactly 2 positional args (LEFT RIGHT), got %d", len(o.args))
}
return o.args[0], o.args[1], o.args[0], o.args[0], nil
}
func pickPre(o *options, displayPath string, s side) ([]string, bool) {
switch s {
case "left":
if o.preLeft != "" {
return []string{o.preLeft}, false
}
case "right":
if o.preRight != "" {
return []string{o.preRight}, false
}
}
if o.pre != "" {
return []string{o.pre}, false
}
base := filepath.Base(displayPath)
var chain []string
for _, r := range o.rules {
glob, cmd, ok := strings.Cut(r, ":")
if !ok {
continue
}
match, err := filepath.Match(glob, base)
if err == nil && match {
chain = append(chain, cmd)
}
}
return chain, true
}
// stage either copies src into tmpDir or streams it through cmd, returning
// the path of the staged file. The staged path preserves the original display
// path so diff output stays meaningful.
func stage(tmpDir, side, displayPath, src string, cmds []string, ignorePreprocessErrors bool, preErrorWriter io.Writer) (string, error) {
sub := filepath.Join(tmpDir, side)
if err := os.MkdirAll(sub, 0o755); err != nil {
return "", err
}
rel := stageDisplayPath(displayPath, side)
out := filepath.Join(sub, rel)
if err := os.MkdirAll(filepath.Dir(out), 0o755); err != nil {
return "", err
}
if len(cmds) == 0 {
in, err := openInput(src)
if err != nil {
return "", err
}
defer in.Close()
f, err := os.Create(out)
if err != nil {
return "", err
}
defer f.Close()
if _, err := io.Copy(f, in); err != nil {
return "", err
}
return out, nil
}
if ignorePreprocessErrors {
return stageIgnoringPreprocessErrors(tmpDir, out, src, cmds, preErrorWriter)
}
in, err := openInput(src)
if err != nil {
return "", err
}
defer in.Close()
var tempPaths []string
defer func() {
for _, path := range tempPaths {
_ = os.Remove(path)
}
}()
for i, cmd := range cmds {
isLast := i == len(cmds)-1
outPath := out
var f *os.File
var err error
if isLast {
f, err = os.Create(outPath)
} else {
f, err = os.CreateTemp(tmpDir, "pre-")
if err == nil {
outPath = f.Name()
tempPaths = append(tempPaths, outPath)
}
}
if err != nil {
return "", err
}
err = runPreprocess(cmd, in, f)
closeErr := f.Close()
_ = in.Close()
if err != nil {
return "", err
}
if closeErr != nil {
return "", closeErr
}
if !isLast {
in, err = os.Open(outPath)
if err != nil {
return "", err
}
}
}
return out, nil
}
func stageIgnoringPreprocessErrors(tmpDir, out, src string, cmds []string, preErrorWriter io.Writer) (string, error) {
currentPath := src
var tempPaths []string
defer func() {
for _, path := range tempPaths {
_ = os.Remove(path)
}
}()
for _, cmd := range cmds {
in, err := openInput(currentPath)
if err != nil {
return "", err
}
f, err := os.CreateTemp(tmpDir, "pre-")
if err != nil {
_ = in.Close()
return "", err
}
tempPath := f.Name()
runErr, stderr := runPreprocessCapturingStderr(cmd, in, f)
closeErr := f.Close()
_ = in.Close()
if runErr != nil {
reportIgnoredPreprocessError(preErrorWriter, cmd, runErr, stderr)
_ = os.Remove(tempPath)
continue
}
if closeErr != nil {
_ = os.Remove(tempPath)
return "", closeErr
}
currentPath = tempPath
tempPaths = append(tempPaths, tempPath)
}
in, err := openInput(currentPath)
if err != nil {
return "", err
}
defer in.Close()
f, err := os.Create(out)
if err != nil {
return "", err
}
defer f.Close()
if _, err := io.Copy(f, in); err != nil {
return "", err
}
return out, nil
}
func runPreprocess(cmd string, in io.Reader, out io.Writer) error {
return runPreprocessWithStderr(cmd, in, out, os.Stderr)
}
func runPreprocessCapturingStderr(cmd string, in io.Reader, out io.Writer) (error, string) {
var stderr bytes.Buffer
err := runPreprocessWithStderr(cmd, in, out, &stderr)
return err, stderr.String()
}
func preprocessErrorWriter(target string) (io.Writer, func() error, error) {
switch target {
case "":
return nil, nil, nil
case "stderr":
return os.Stderr, nil, nil
default:
f, err := os.Create(target)
if err != nil {
return nil, nil, fmt.Errorf("open preprocessor error log %q: %w", target, err)
}
return f, f.Close, nil
}
}
func reportIgnoredPreprocessError(w io.Writer, cmd string, err error, stderr string) {
if w == nil {
return
}
exitCode := "unknown"
var ee *exec.ExitError
if errors.As(err, &ee) {
exitCode = fmt.Sprint(ee.ExitCode())
}
fmt.Fprintf(w, "diffhance: ignored rule preprocessor failure\ncommand: %q\nexit code: %s\nstderr:\n%s", cmd, exitCode, stderr)
if stderr == "" || !strings.HasSuffix(stderr, "\n") {
fmt.Fprintln(w)
}
}
func runPreprocessWithStderr(cmd string, in io.Reader, out io.Writer, stderr io.Writer) error {
shell, flag := shell()
c := exec.Command(shell, flag, cmd)
c.Stdin = in
c.Stdout = out
c.Stderr = stderr
if err := c.Run(); err != nil {
return fmt.Errorf("running %q: %w", cmd, err)
}
return nil
}
func stageDisplayPath(displayPath, fallback string) string {
path := filepath.Clean(displayPath)
if path == "." || path == string(filepath.Separator) {
return fallback
}
if filepath.IsAbs(path) {
if rel, err := filepath.Rel(mustGetwd(), path); err == nil && isSafeRelativePath(rel) {
return rel
}
path = filepath.Base(path)
}
if !isSafeRelativePath(path) {
path = filepath.Base(path)
}
if path == "." || path == string(filepath.Separator) {
return fallback
}
return path
}
func isSafeRelativePath(path string) bool {
if path == "" || filepath.IsAbs(path) {
return false
}
for _, part := range strings.Split(filepath.Clean(path), string(filepath.Separator)) {
if part == ".." {
return false
}
}
return true
}
func mustGetwd() string {
wd, err := os.Getwd()
if err != nil {
return "."
}
return wd
}
// openInput opens src for reading. /dev/null on Windows is mapped to NUL so
// the git deletion/creation case still works cross-platform.
func openInput(src string) (io.ReadCloser, error) {
if runtime.GOOS == "windows" && src == "/dev/null" {
src = "NUL"
}
return os.Open(src)
}
func shell() (string, string) {
if runtime.GOOS == "windows" {
if s := os.Getenv("ComSpec"); s != "" {
return s, "/C"
}
return "cmd", "/C"
}
return "/bin/sh", "-c"
}
func runDiff(o *options, left, right, leftLabel, rightLabel, displayPath string) (int, error) {
cmdline := o.diff
if !o.noColor && shouldColor() {
cmdline = injectColor(cmdline)
}
if shouldPipeLabeledDiff(cmdline) {
return runLabeledDiffThroughBackend(cmdline, left, right, leftLabel, rightLabel)
}
hunkBackend := isHunkDifftoolBackend(cmdline)
diffCode := 0
if hunkBackend {
equal, err := filesEqual(left, right)
if err != nil {
return 2, fmt.Errorf("compare staged files: %w", err)
}
if !equal {
diffCode = 1
}
}
full := diffCommand(cmdline, left, right, leftLabel, rightLabel, displayPath)
shellBin, flag := shell()
c := exec.Command(shellBin, flag, full)
c.Stdout = os.Stdout
c.Stderr = os.Stderr
c.Stdin = os.Stdin
err := c.Run()
if err != nil {
var ee *exec.ExitError
if errors.As(err, &ee) {
if hunkBackend && ee.ExitCode() == 1 {
// Hunk uses 1 for an application error, not "files differ".
return 2, nil
}
// Treat exit code 1 (differences) as success for diffhance itself.
return ee.ExitCode(), nil
}
return 2, err
}
return diffCode, nil
}
func filesEqual(left, right string) (bool, error) {
l, err := os.Open(left)
if err != nil {
return false, err
}
defer l.Close()
r, err := os.Open(right)
if err != nil {
return false, err
}
defer r.Close()
leftInfo, err := l.Stat()
if err != nil {
return false, err
}
rightInfo, err := r.Stat()
if err != nil {
return false, err
}
if leftInfo.Size() != rightInfo.Size() {
return false, nil
}
leftBuf := make([]byte, 32*1024)
rightBuf := make([]byte, len(leftBuf))
for {
n, readErr := io.ReadFull(l, leftBuf)
if readErr != nil && readErr != io.EOF && readErr != io.ErrUnexpectedEOF {
return false, readErr
}
if n == 0 {
return true, nil
}
if _, err := io.ReadFull(r, rightBuf[:n]); err != nil {
return false, err
}
if !bytes.Equal(leftBuf[:n], rightBuf[:n]) {
return false, nil
}
if readErr == io.ErrUnexpectedEOF {
return true, nil
}
}
}
func runLabeledDiffThroughBackend(cmdline, left, right, leftLabel, rightLabel string) (int, error) {
diffText, diffCode, err := runLabeledDiff(left, right, leftLabel, rightLabel)
if err != nil {
return diffCode, err
}
shellBin, flag := shell()
c := exec.Command(shellBin, flag, cmdline)
c.Stdin = strings.NewReader(diffText)
c.Stdout = os.Stdout
c.Stderr = os.Stderr
if err := c.Run(); err != nil {
var ee *exec.ExitError
if errors.As(err, &ee) {
return ee.ExitCode(), nil
}
return 2, err
}
return diffCode, nil
}
func runLabeledDiff(left, right, leftLabel, rightLabel string) (string, int, error) {
shellBin, flag := shell()
c := exec.Command(shellBin, flag, labeledDiffCommand(left, right, leftLabel, rightLabel))
c.Stderr = os.Stderr
out, err := c.Output()
if err != nil {
var ee *exec.ExitError
if errors.As(err, &ee) {
return string(out), ee.ExitCode(), nil
}
return string(out), 2, err
}
return string(out), 0, nil
}
func diffCommand(cmdline, left, right, leftLabel, rightLabel, displayPath string) string {
if isHunkDifftoolBackend(cmdline) {
fields := strings.Fields(cmdline)
if len(fields) == 1 {
cmdline += " difftool"
}
hasEndOfOptions := false
for _, field := range fields {
if field == "--" {
hasEndOfOptions = true
break
}
}
if !hasEndOfOptions {
cmdline += " --"
}
return cmdline + " " + shellQuote(left) + " " + shellQuote(right) + " " + shellQuote(displayPath)
}
if shouldLabelBareDiff(cmdline) {
cmdline += " --label " + shellQuote(leftLabel) + " --label " + shellQuote(rightLabel)
}
return cmdline + " " + shellQuote(left) + " " + shellQuote(right)
}
func isHunkDifftoolBackend(cmdline string) bool {
fields := strings.Fields(cmdline)
if len(fields) == 0 || strings.TrimSuffix(strings.ToLower(filepath.Base(fields[0])), ".exe") != "hunk" {
return false
}
return len(fields) == 1 || fields[1] == "difftool"
}
func labeledDiffCommand(left, right, leftLabel, rightLabel string) string {
return "diff -u --label " + shellQuote(leftLabel) + " --label " + shellQuote(rightLabel) + " " + shellQuote(left) + " " + shellQuote(right)
}
func shouldLabelBareDiff(cmdline string) bool {
fields := strings.Fields(cmdline)
if len(fields) == 0 || filepath.Base(fields[0]) != "diff" {
return false
}
for _, f := range fields[1:] {
if f == "--" || f == "--label" || strings.HasPrefix(f, "--label=") {
return false
}
}
return true
}