chore: import upstream snapshot with attribution
CI / license-header (push) Has been skipped
CI / e2e-dry-run (push) Has been skipped
CI / fast-gate (push) Failing after 0s
Test PR Label Logic / test-pr-labels (push) Failing after 1s
Skill Format Check / check-format (push) Failing after 2s
CI / security (push) Failing after 5s
CI / unit-test (push) Has been skipped
CI / lint (push) Has been skipped
CI / script-test (push) Has been skipped
CI / deterministic-gate (push) Has been skipped
CI / coverage (push) Has been skipped
CI / results (push) Has been cancelled
CI / deadcode (push) Has been cancelled
CI / e2e-live (push) Has been cancelled

This commit is contained in:
wehub-resource-sync
2026-07-13 12:22:54 +08:00
commit bf9395e022
2349 changed files with 588574 additions and 0 deletions
+133
View File
@@ -0,0 +1,133 @@
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package diff
import (
"context"
"errors"
"fmt"
"os/exec"
"sort"
"strings"
)
var ErrFileAtRevisionMissing = errors.New("file missing at revision")
type Scope struct {
Global bool
AllSkills map[string]bool
Files map[string]bool
}
func ChangedFiles(ctx context.Context, repo, from string) ([]string, error) {
if from == "" {
return nil, nil
}
return gitChangedFiles(ctx, repo, "diff", "--name-only", "-z", "--diff-filter=ACMRD", from+"...HEAD")
}
func ChangedFilesIncludingWorktree(ctx context.Context, repo, from string) ([]string, error) {
var all []string
if from != "" {
committed, err := ChangedFiles(ctx, repo, from)
if err != nil {
return nil, err
}
all = append(all, committed...)
}
for _, args := range [][]string{
{"diff", "--name-only", "-z", "--diff-filter=ACMRD"},
{"diff", "--cached", "--name-only", "-z", "--diff-filter=ACMRD"},
{"ls-files", "--others", "--exclude-standard", "-z"},
} {
files, err := gitChangedFiles(ctx, repo, args...)
if err != nil {
return nil, err
}
all = append(all, files...)
}
return uniqueSorted(all), nil
}
func gitChangedFiles(ctx context.Context, repo string, args ...string) ([]string, error) {
cmd := exec.CommandContext(ctx, "git", args...)
cmd.Dir = repo
out, err := cmd.Output()
if err != nil {
return nil, fmt.Errorf("git %s changed files: %w", strings.Join(args, " "), err)
}
// Output is NUL-delimited (-z) so paths containing whitespace stay intact.
var lines []string
for _, name := range strings.Split(string(out), "\x00") {
if name != "" {
lines = append(lines, name)
}
}
sort.Strings(lines)
return lines, nil
}
func uniqueSorted(files []string) []string {
if len(files) == 0 {
return nil
}
sort.Strings(files)
out := files[:0]
var last string
for i, file := range files {
if i > 0 && file == last {
continue
}
out = append(out, file)
last = file
}
return out
}
func FileAtRevision(ctx context.Context, repo, rev, path string) ([]byte, error) {
cmd := exec.CommandContext(ctx, "git", "show", rev+":"+path)
cmd.Dir = repo
out, err := cmd.CombinedOutput()
if err != nil {
if isFileAtRevisionMissing(string(out)) {
return nil, ErrFileAtRevisionMissing
}
return nil, fmt.Errorf("git show %s:%s: %w", rev, path, err)
}
return out, nil
}
func isFileAtRevisionMissing(stderr string) bool {
return strings.Contains(stderr, "exists on disk, but not in") ||
strings.Contains(stderr, "does not exist in")
}
func FromChangedFiles(files []string) Scope {
scope := Scope{AllSkills: map[string]bool{}, Files: map[string]bool{}}
for _, file := range files {
scope.Files[file] = true
parts := strings.Split(file, "/")
if len(parts) >= 2 && parts[0] == "skills" {
scope.AllSkills[parts[1]] = true
continue
}
if strings.HasPrefix(file, "cmd/") ||
strings.HasPrefix(file, "shortcuts/") ||
strings.HasPrefix(file, "internal/output/") ||
strings.HasPrefix(file, "internal/errclass/") ||
strings.HasPrefix(file, "errs/") {
scope.Global = true
}
}
return scope
}
func ChangedUnder(files map[string]bool, prefix string) bool {
for file := range files {
if strings.HasPrefix(file, prefix) {
return true
}
}
return false
}
+141
View File
@@ -0,0 +1,141 @@
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
// SPDX-License-Identifier: MIT
package diff
import (
"context"
"os"
"os/exec"
"path/filepath"
"reflect"
"testing"
)
func TestScopeIncludesChangedSkillAndRelatedDomain(t *testing.T) {
scope := FromChangedFiles([]string{
"skills/lark-doc/SKILL.md",
"skills/lark-im/references/lark-im-chat-list.md",
"internal/output/errors.go",
})
if !scope.AllSkills["lark-doc"] || !scope.AllSkills["lark-im"] {
t.Fatalf("skill scope missing changed skills: %#v", scope.AllSkills)
}
if !scope.Global {
t.Fatalf("internal/output/errors.go should trigger global checks")
}
}
func TestScopeTreatsDeletedShortcutAsGlobal(t *testing.T) {
scope := FromChangedFiles([]string{"shortcuts/mail/send.go"})
if !scope.Global {
t.Fatal("shortcut paths from git diff must force global checks, including deleted files")
}
}
func TestScopeDoesNotTreatDefaultMetadataAsGlobal(t *testing.T) {
scope := FromChangedFiles([]string{"internal/registry/meta_data_default.json"})
if scope.Global {
t.Fatal("default metadata changes should not force ordinary quality-gate global scope")
}
}
func TestFileAtRevisionMissingClassifier(t *testing.T) {
msg := "fatal: path 'internal/qualitygate/config/contracts/command_manifest.golden.json' exists on disk, but not in 'origin/main'"
if !isFileAtRevisionMissing(msg) {
t.Fatalf("expected missing file classifier to match")
}
if isFileAtRevisionMissing("fatal: ambiguous argument 'origin/missing'") {
t.Fatalf("bad revision should not be treated as a missing file")
}
}
func TestChangedFilesIncludingWorktree(t *testing.T) {
repo := t.TempDir()
runGit(t, repo, "init")
runGit(t, repo, "config", "user.email", "test@example.com")
runGit(t, repo, "config", "user.name", "Test User")
writeFile(t, repo, "tracked.txt", "base\n")
writeFile(t, repo, "staged.txt", "base\n")
writeFile(t, repo, "unstaged.txt", "base\n")
runGit(t, repo, "add", ".")
runGit(t, repo, "commit", "-m", "base")
base := gitOutput(t, repo, "rev-parse", "HEAD")
writeFile(t, repo, "committed.txt", "committed\n")
runGit(t, repo, "add", ".")
runGit(t, repo, "commit", "-m", "committed")
writeFile(t, repo, "staged.txt", "staged\n")
runGit(t, repo, "add", "staged.txt")
writeFile(t, repo, "unstaged.txt", "unstaged\n")
writeFile(t, repo, "untracked.txt", "untracked\n")
got, err := ChangedFilesIncludingWorktree(context.Background(), repo, base)
if err != nil {
t.Fatalf("ChangedFilesIncludingWorktree() error = %v", err)
}
want := []string{"committed.txt", "staged.txt", "unstaged.txt", "untracked.txt"}
if !reflect.DeepEqual(got, want) {
t.Fatalf("ChangedFilesIncludingWorktree() = %#v, want %#v", got, want)
}
}
func TestChangedFilesHandlesWhitespacePaths(t *testing.T) {
repo := t.TempDir()
runGit(t, repo, "init")
runGit(t, repo, "config", "user.email", "test@example.com")
runGit(t, repo, "config", "user.name", "Test User")
writeFile(t, repo, "base.txt", "base\n")
runGit(t, repo, "add", ".")
runGit(t, repo, "commit", "-m", "base")
base := gitOutput(t, repo, "rev-parse", "HEAD")
// A path containing spaces must survive intact. With whitespace splitting
// this returned four mangled tokens instead of one path.
writeFile(t, repo, "dir with space/a b.txt", "x\n")
runGit(t, repo, "add", ".")
runGit(t, repo, "commit", "-m", "spaced")
got, err := ChangedFiles(context.Background(), repo, base)
if err != nil {
t.Fatalf("ChangedFiles() error = %v", err)
}
want := []string{"dir with space/a b.txt"}
if !reflect.DeepEqual(got, want) {
t.Fatalf("ChangedFiles() = %#v, want %#v", got, want)
}
}
func writeFile(t *testing.T, repo, rel, content string) {
t.Helper()
path := filepath.Join(repo, filepath.FromSlash(rel))
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
t.Fatalf("mkdir %s: %v", rel, err)
}
if err := os.WriteFile(path, []byte(content), 0o644); err != nil {
t.Fatalf("write %s: %v", rel, err)
}
}
func runGit(t *testing.T, repo string, args ...string) {
t.Helper()
cmd := exec.Command("git", args...)
cmd.Dir = repo
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("git %v failed: %v\n%s", args, err, out)
}
}
func gitOutput(t *testing.T, repo string, args ...string) string {
t.Helper()
cmd := exec.Command("git", args...)
cmd.Dir = repo
out, err := cmd.Output()
if err != nil {
t.Fatalf("git %v failed: %v", args, err)
}
return string(out[:len(out)-1])
}