Extract validation package · Entire

Extract validation package

ddd4c7c→main·

Sessions

b5aa1d18a757View transcript

Changes

4

10 unmodified lines

11
12
13
14
15
16
17
55 unmodified lines

73
74
75
75
76
77
78
79
15 unmodified lines

95
96
97
97
98
99
100
101
45 unmodified lines

147
148
149
149
150
151
152
153
15 unmodified lines

169
170
171
171
172
173
174
175
29 unmodified lines

205
206
207
207
208
209
210
211
40 unmodified lines

252
253
254
254
255
256
257
258
66 unmodified lines

325
326
327
327
328
329
330
331
332
333
334
335
336
337
338
339
328
329
330

10 unmodified lines

"github.com/go-git/go-git/v6/plumbing"
    "github.com/soph/git-sync/internal/syncer"
    "github.com/soph/git-sync/internal/validation"

func main() {
55 unmodified lines

fs.BoolVar(&cfg.ShowStats, "stats", false, "print transfer statistics")
    fs.BoolVar(&cfg.MeasureMemory, "measure-memory", false, "sample elapsed time and Go heap usage")
    fs.BoolVar(&jsonOutput, "json", false, "print JSON output")
    fs.StringVar(&cfg.ProtocolMode, "protocol", envOr("GITSYNC_PROTOCOL", "auto"), "protocol mode: auto, v1, or v2")
    fs.StringVar(&cfg.ProtocolMode, "protocol", envOr("GITSYNC_PROTOCOL", validation.ProtocolAuto), "protocol mode: auto, v1, or v2")
    fs.BoolVar(&cfg.Verbose, "v", false, "verbose logging")

if err := fs.Parse(args); err != nil {
15 unmodified lines

cfg.Branches = splitCSV(*branches)
    }
    for _, raw := range mappings {
        mapping, err := parseMapping(raw)
        mapping, err := validation.ParseMapping(raw)
        if err != nil {
            return err
        }
45 unmodified lines

fs.BoolVar(&jsonOutput, "json", false, "print JSON output")
    fs.Int64Var(&cfg.MaxPackBytes, "max-pack-bytes", 0, "abort bootstrap if the streamed source pack exceeds this many bytes")
    fs.Int64Var(&cfg.BatchMaxPackBytes, "batch-max-pack-bytes", 0, "split branch bootstrap into relay batches capped at this many bytes per batch")
    fs.StringVar(&cfg.ProtocolMode, "protocol", envOr("GITSYNC_PROTOCOL", "auto"), "protocol mode: auto, v1, or v2")
    fs.StringVar(&cfg.ProtocolMode, "protocol", envOr("GITSYNC_PROTOCOL", validation.ProtocolAuto), "protocol mode: auto, v1, or v2")
    fs.BoolVar(&cfg.Verbose, "v", false, "verbose logging")

if err := fs.Parse(args); err != nil {
15 unmodified lines

cfg.Branches = splitCSV(*branches)
    }
    for _, raw := range mappings {
        mapping, err := parseMapping(raw)
        mapping, err := validation.ParseMapping(raw)
        if err != nil {
            return err
        }
29 unmodified lines

fs.BoolVar(&cfg.Source.SkipTLSVerify, "source-insecure-skip-tls-verify", envBool("GITSYNC_SOURCE_INSECURE_SKIP_TLS_VERIFY"), "skip TLS certificate verification for the source")
    fs.BoolVar(&cfg.Target.SkipTLSVerify, "target-insecure-skip-tls-verify", envBool("GITSYNC_TARGET_INSECURE_SKIP_TLS_VERIFY"), "skip TLS certificate verification for the target")
    fs.BoolVar(&cfg.IncludeTags, "tags", false, "include tag ref prefixes in probe")
    fs.StringVar(&cfg.ProtocolMode, "protocol", envOr("GITSYNC_PROTOCOL", "auto"), "protocol mode: auto, v1, or v2")
    fs.StringVar(&cfg.ProtocolMode, "protocol", envOr("GITSYNC_PROTOCOL", validation.ProtocolAuto), "protocol mode: auto, v1, or v2")
    fs.BoolVar(&cfg.ShowStats, "stats", false, "print transfer statistics")
    fs.BoolVar(&cfg.MeasureMemory, "measure-memory", false, "sample elapsed time and Go heap usage")
    fs.BoolVar(&jsonOutput, "json", false, "print JSON output")
40 unmodified lines

fs.BoolVar(&cfg.Source.SkipTLSVerify, "source-insecure-skip-tls-verify", envBool("GITSYNC_SOURCE_INSECURE_SKIP_TLS_VERIFY"), "skip TLS certificate verification for the source")
    branches := fs.String("branch", "", "comma-separated branch list; default is all source branches")
    fs.BoolVar(&cfg.IncludeTags, "tags", false, "include tags in the fetch request")
    fs.StringVar(&cfg.ProtocolMode, "protocol", envOr("GITSYNC_PROTOCOL", "auto"), "protocol mode: auto, v1, or v2")
    fs.StringVar(&cfg.ProtocolMode, "protocol", envOr("GITSYNC_PROTOCOL", validation.ProtocolAuto), "protocol mode: auto, v1, or v2")
    fs.BoolVar(&cfg.ShowStats, "stats", false, "print transfer statistics")
    fs.BoolVar(&cfg.MeasureMemory, "measure-memory", false, "sample elapsed time and Go heap usage")
    fs.BoolVar(&jsonOutput, "json", false, "print JSON output")
66 unmodified lines

return nil
}

func parseMapping(raw string) (syncer.RefMapping, error) {
    parts := strings.SplitN(raw, ":", 2)
    if len(parts) != 2 {
        return syncer.RefMapping{}, fmt.Errorf("invalid --map %q, expected src:dst", raw)
    }
    source := strings.TrimSpace(parts[0])
    target := strings.TrimSpace(parts[1])
    if source == "" || target == "" {
        return syncer.RefMapping{}, fmt.Errorf("invalid --map %q, expected src:dst", raw)
    }
    return syncer.RefMapping{Source: source, Target: target}, nil
}

func splitCSV(value string) []string {
    parts := strings.Split(value, ",")
    out := make([]string, 0, len(parts))

Mcmd/git-sync/main.go+7/-19

26 unmodified lines

27
28
29
30
31
32
33
33
34
35
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

package validation

import (
    "fmt"
    "strings"

"github.com/go-git/go-git/v6/plumbing"

"github.com/soph/git-sync/internal/planner"
)

const (
    ProtocolAuto = "auto"
    ProtocolV1   = "v1"
    ProtocolV2   = "v2"
)

// NormalizeProtocolMode validates the configured protocol mode and applies the
// default auto mode when the user did not specify one.
func NormalizeProtocolMode(mode string) (string, error) {
    if mode == "" {
        return ProtocolAuto, nil
    }
    switch mode {
    case ProtocolAuto, ProtocolV1, ProtocolV2:
        return mode, nil
    default:
        return "", fmt.Errorf("unsupported protocol mode %q", mode)
    }
}

// ParseMapping parses a CLI --map value into a planner mapping.
func ParseMapping(raw string) (planner.RefMapping, error) {
    parts := strings.SplitN(raw, ":", 2)
    if len(parts) != 2 {
        return planner.RefMapping{}, fmt.Errorf("invalid --map %q, expected src:dst", raw)
    }
    source := strings.TrimSpace(parts[0])
    target := strings.TrimSpace(parts[1])
    if source == "" || target == "" {
        return planner.RefMapping{}, fmt.Errorf("invalid --map %q, expected src:dst", raw)
    }
    return planner.RefMapping{Source: source, Target: target}, nil
}

// ParseHaveRef normalizes a have-ref CLI value. Short names are treated as
// branch names for compatibility with other CLI ref selectors.
func ParseHaveRef(raw string) plumbing.ReferenceName {
    raw = strings.TrimSpace(raw)
    if strings.HasPrefix(raw, "refs/") {
        return plumbing.ReferenceName(raw)
    }
    return plumbing.NewBranchReferenceName(raw)
}