MCPcopy Create free account
hub / github.com/entireio/git-sync / newFetchCmd

Function newFetchCmd

cmd/git-sync/fetch.go:14–83  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

12)
13
14func newFetchCmd() *cobra.Command {
15 var (
16 haveRefs []string
17 haveHashesRaw []string
18 jsonOutput bool
19 sourceAuth gitsync.EndpointAuth
20 branches string
21 protocolVal = newProtocolFlag()
22 req = unstable.FetchRequest{}
23 )
24
25 cmd := &cobra.Command{
26 Use: "fetch [flags] <source-url>",
27 Short: "Negotiate a fetch against the source and report packed objects",
28 Args: cobra.MaximumNArgs(1),
29 SilenceErrors: true,
30 SilenceUsage: true,
31 RunE: func(cmd *cobra.Command, args []string) error {
32 req.Protocol = gitsync.ProtocolMode(protocolVal)
33
34 if req.Source.URL == "" && len(args) > 0 {
35 req.Source.URL = args[0]
36 }
37 if req.Source.URL == "" {
38 return errors.New("fetch requires a source repository URL")
39 }
40 if branches != "" {
41 req.Scope.Branches = splitCSV(branches)
42 }
43
44 haveHashes := make([]plumbing.Hash, 0, len(haveHashesRaw))
45 for _, raw := range haveHashesRaw {
46 hash := plumbing.NewHash(strings.TrimSpace(raw))
47 if hash.IsZero() {
48 return fmt.Errorf("invalid --have %q", raw)
49 }
50 haveHashes = append(haveHashes, hash)
51 }
52
53 req.HaveRefs = append(req.HaveRefs, haveRefs...)
54 req.HaveHashes = append(req.HaveHashes, haveHashes...)
55
56 result, err := unstable.New(unstable.Options{
57 Auth: gitsync.StaticAuthProvider{Source: sourceAuth},
58 }).Fetch(cmd.Context(), req)
59 if err != nil {
60 return fmt.Errorf("fetch: %w", err)
61 }
62 printOutput(jsonOutput, result)
63 return nil
64 },
65 }
66
67 addSourceEndpoint(cmd, &req.Source)
68 addSourceAuth(cmd, &sourceAuth)
69
70 cmd.Flags().StringVar(&branches, "branch", "", "comma-separated branch list; default is all source branches")
71 cmd.Flags().BoolVar(&req.IncludeTags, "tags", false, "include tags in the fetch request")

Callers 1

newRootCmdFunction · 0.85

Calls 9

NewFunction · 0.92
newProtocolFlagFunction · 0.85
printOutputFunction · 0.85
addSourceEndpointFunction · 0.85
addSourceAuthFunction · 0.85
excludeRefPrefixFlagFunction · 0.85
addProtocolFlagFunction · 0.85
FetchMethod · 0.80
splitCSVFunction · 0.70

Tested by

no test coverage detected