Fix gitignore and add JSON output mode · Entire
Fix gitignore and add JSON output mode
2826440→main·
Sessions
9d262b2fb550View transcript
Changes
4
- M.gitignore+1/-1
- MREADME.md+3
- cmd/git-sync
- Mmain.go+30/-10
- Amain_test.go+35
35 unmodified lines
36
37
38
39
39
40
41
42
35 unmodified lines
# .vscode/
# Binary output (only in root)
git-sync
/git-sync
# worktrees
.worktrees/
M.gitignore+1/-1
25 unmodified lines
26
27
28
29
30
31
32
74 unmodified lines
107
108
109
110
111
112
113
114
25 unmodified lines
- Optional forced retargeting with `--force`
- Optional managed-ref deletion with `--prune`
- Optional transfer stats output with `--stats`
- Optional machine-readable output with `--json`
- Optional source-side Git protocol v2 for `ls-refs` and `fetch`
## Limits
74 unmodified lines
Add `--json` to `probe`, `fetch`, `plan`, or `sync` to emit machine-readable output instead of the default text format.
Probe both source and target remotes to inspect source fetch capabilities and target `receive-pack` capabilities:
```bash
MREADME.md+3
1 unmodified line
2
3
4
5
6
7
8
38 unmodified lines
47
48
49
50
51
52
53
12 unmodified lines
66
67
68
69
70
71
72
31 unmodified lines
104
105
106
107
108
105
106
107
108
109
110
111
5 unmodified lines
117
118
119
120
121
122
123
5 unmodified lines
129
130
131
132
133
134
135
17 unmodified lines
153
154
155
154
155
156
156
157
158
159
4 unmodified lines
164
165
166
167
168
169
170
3 unmodified lines
174
175
176
177
178
179
180
28 unmodified lines
209
210
211
210
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
213
230
231
232
233
234
235
236
237
238
39 unmodified lines
278
279
280
261
281
282
283
284
1 unmodified line
import (
"context"
"encoding/json"
"errors"
"flag"
"fmt"
)
cfg := syncer.Config{DryRun: dryRun}
var mappings multiStringFlag
var jsonOutput bool
fs.StringVar(&cfg.Source.URL, "source-url", "", "source repository URL")
fs.StringVar(&cfg.Target.URL, "target-url", "", "target repository URL")
fs.BoolVar(&cfg.Force, "force", false, "allow non-fast-forward branch updates and retarget tags")
fs.BoolVar(&cfg.Prune, "prune", false, "delete managed target refs that no longer exist on source")
fs.BoolVar(&cfg.ShowStats, "stats", false, "print transfer statistics")
fs.BoolVar(&jsonOutput, "json", false, "print JSON output")
fs.StringVar(&cfg.ProtocolMode, "protocol", envOr("GITSYNC_PROTOCOL", "auto"), "protocol mode: auto, v1, or v2")
fs.BoolVar(&cfg.Verbose, "v", false, "verbose logging")
if err != nil {
return err
}
printOutput(jsonOutput, result)
for _, line := range result.Lines() {
fmt.Println(line)
}
if !dryRun && result.Blocked > 0 {
return errors.New("one or more branches were skipped because the target was not fast-forwardable")
}
fs.SetOutput(os.Stderr)
cfg := syncer.Config{}
var jsonOutput bool
fs.StringVar(&cfg.Source.URL, "source-url", "", "source repository URL")
fs.StringVar(&cfg.Target.URL, "target-url", "", "optional target repository URL")
fs.StringVar(&cfg.Source.Token, "source-token", envOr("GITSYNC_SOURCE_TOKEN", ""), "source token/password")
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.BoolVar(&cfg.ShowStats, "stats", false, "print transfer statistics")
fs.BoolVar(&jsonOutput, "json", false, "print JSON output")
if err := fs.Parse(args); err != nil {
return err
}
if err != nil {
return err
}
for _, line := range result.Lines() {
fmt.Println(line)
}
printOutput(jsonOutput, result)
return nil
}
cfg := syncer.Config{}
var haveRefs multiStringFlag
var haveHashesRaw multiStringFlag
var jsonOutput bool
fs.StringVar(&cfg.Source.URL, "source-url", "", "source repository URL")
fs.StringVar(&cfg.Source.Token, "source-token", envOr("GITSYNC_SOURCE_TOKEN", ""), "source token/password")
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.BoolVar(&cfg.ShowStats, "stats", false, "print transfer statistics")
fs.BoolVar(&jsonOutput, "json", false, "print JSON output")
fs.Var(&haveRefs, "have-ref", "source ref name to advertise as have; short names map to branches")
fs.Var(&haveHashesRaw, "have", "explicit object hash to advertise as have")
if err != nil {
return err
}
for _, line := range result.Lines() {
printOutput(jsonOutput, result)
return nil
}
func printOutput(jsonOutput bool, value interface{ Lines() []string }) {
if jsonOutput {
data, err := marshalOutput(value)
if err != nil {
fmt.Fprintf(os.Stderr, "error: encode JSON output: %v\n", err)
os.Exit(1)
}
fmt.Println(string(data))
return
}
for _, line := range value.Lines() {
fmt.Println(line)
}
return nil
}
func marshalOutput(value interface{}) ([]byte, error) {
return json.MarshalIndent(value, "", " ")
}
type multiStringFlag []string
func (m *multiStringFlag) String() string {
}
func usageError(message string) error {
usage := "usage:\n git-sync sync [flags] <source-url> <target-url>\n git-sync plan [flags] <source-url> <target-url>\n git-sync probe [flags] <source-url> [target-url]\n git-sync fetch [flags] <source-url>\n\nsync/plan flags:\n --branch main,dev\n --map main:stable\n --tags\n --force\n --prune\n --stats\n --protocol auto|v1|v2\n --source-token ...\n --target-token ...\n --source-username git\n --target-username git\n --source-bearer-token ...\n --target-bearer-token ...\n -v\n\nprobe flags:\n --tags\n --stats\n --protocol auto|v1|v2\n --source-token ...\n --source-username git\n --source-bearer-token ...\n --target-token ...\n --target-username git\n --target-bearer-token ...\n\nfetch flags:\n --branch main,dev\n --tags\n --stats\n --protocol auto|v1|v2\n --have-ref main\n --have <hash>\n --source-token ...\n --source-username git\n --source-bearer-token ...\n"
usage := "usage:\n git-sync sync [flags] <source-url> <target-url>\n git-sync plan [flags] <source-url> <target-url>\n git-sync probe [flags] <source-url> [target-url]\n git-sync fetch [flags] <source-url>\n\nsync/plan flags:\n --branch main,dev\n --map main:stable\n --tags\n --force\n --prune\n --stats\n --json\n --protocol auto|v1|v2\n --source-token ...\n --target-token ...\n --source-username git\n --target-username git\n --source-bearer-token ...\n --target-bearer-token ...\n -v\n\nprobe flags:\n --tags\n --stats\n --json\n --protocol auto|v1|v2\n --source-token ...\n --source-username git\n --source-bearer-token ...\n --target-token ...\n --target-username git\n --target-bearer-token ...\n\nfetch flags:\n --branch main,dev\n --tags\n --stats\n --json\n --protocol auto|v1|v2\n --have-ref main\n --have <hash>\n --source-token ...\n --source-username git\n --source-bearer-token ...\n"
if message == "" {
return errors.New(strings.TrimSpace(usage))
}