Files
upstream-sqlc/internal/cmd/diff.go
upstream-mirror 0f4df23290
Some checks failed
kotlin / test (push) Has been skipped
python / test (push) Has been skipped
typescript / test (push) Has been skipped
go / build darwin/amd64 (push) Has been cancelled
go / build linux/amd64 (push) Has been cancelled
go / build windows/amd64 (push) Has been cancelled
go / build darwin/arm64 (push) Has been cancelled
go / build linux/arm64 (push) Has been cancelled
go / build windows/arm64 (push) Has been cancelled
go / test (push) Has been cancelled
go / vuln_check (push) Has been cancelled
snapshot of https://github.com/sqlc-dev/sqlc.git (main, 2026-09-16; blobs > 131072B omitted)
2026-09-16 02:17:34 +05:00

60 lines
1.2 KiB
Go

package cmd
import (
"context"
"errors"
"fmt"
"os"
"runtime/trace"
"sort"
"strings"
"github.com/cubicdaiya/gonp"
)
func Diff(ctx context.Context, dir, name string, opts *Options) error {
stderr := opts.Stderr
output, err := Generate(ctx, dir, name, opts)
if err != nil {
return err
}
defer trace.StartRegion(ctx, "checkfiles").End()
var errored bool
keys := make([]string, 0, len(output))
for k, _ := range output {
kk := k
keys = append(keys, kk)
}
sort.Strings(keys)
for _, filename := range keys {
source := output[filename]
if _, err := os.Stat(filename); errors.Is(err, os.ErrNotExist) {
errored = true
// stdout message
continue
}
existing, err := os.ReadFile(filename)
if err != nil {
errored = true
fmt.Fprintf(stderr, "%s: %s\n", filename, err)
continue
}
diff := gonp.New(getLines(existing), getLines([]byte(source)))
diff.Compose()
uniHunks := filterHunks(diff.UnifiedHunks())
if len(uniHunks) > 0 {
errored = true
fmt.Fprintf(stderr, "--- a%s\n", strings.TrimPrefix(filename, dir))
fmt.Fprintf(stderr, "+++ b%s\n", strings.TrimPrefix(filename, dir))
diff.FprintUniHunks(stderr, uniHunks)
}
}
if errored {
return errors.New("diff found")
}
return nil
}