this repo has no description
1package main
2
3import (
4 "context"
5 "fmt"
6 "os"
7 "os/exec"
8 "path"
9 "strings"
10)
11
12func main() {
13 ctx := context.Background()
14 rootDirs, err := run(ctx, "jj", "workspace", "root")
15 if err != nil || len(rootDirs) != 1 {
16 fmt.Fprintln(os.Stderr, "find workspace root", rootDirs, err)
17 os.Exit(1)
18 }
19 os.Chdir(rootDirs[0])
20 diffFiles, err := run(ctx, "jj", "diff", "--name-only")
21 if err != nil {
22 fmt.Fprintln(os.Stderr, "find changed files", err)
23 os.Exit(1)
24 } else if len(diffFiles) == 0 {
25 fmt.Fprintln(os.Stderr, "no changed files")
26 os.Exit(1)
27 }
28
29 common := diffFiles[0]
30findCommon:
31 for {
32 common = path.Dir(common)
33 if common == "." {
34 common = "all"
35 break findCommon
36 }
37 allMatch := true
38 for _, file := range diffFiles {
39 if !strings.HasPrefix(file, common) {
40 allMatch = false
41 }
42 }
43 if allMatch {
44 break findCommon
45 }
46 }
47 fmt.Print(common)
48}
49
50func run(ctx context.Context, cmd string, args ...string) ([]string, error) {
51 b, err := exec.CommandContext(ctx, cmd, args...).Output()
52 if err != nil {
53 return nil, fmt.Errorf("exec %v %v: %w", cmd, args, err)
54 }
55 lines := strings.FieldsFunc(string(b), func(r rune) bool {
56 return r == '\n'
57 })
58 return lines, nil
59}