File: sync.go

package info (click to toggle)
gittuf 0.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,692 kB
  • sloc: python: 85; makefile: 58; sh: 1
file content (135 lines) | stat: -rw-r--r-- 3,139 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright The gittuf Authors
// SPDX-License-Identifier: Apache-2.0

package gitinterface

import (
	"fmt"
	"path"
	"strings"

	"github.com/jonboulle/clockwork"
)

const DefaultRemoteName = "origin"

type FetchOptions struct {
	Depth int
}

type FetchOption func(*FetchOptions)

func WithFetchDepth(depth int) FetchOption {
	return func(o *FetchOptions) {
		o.Depth = depth
	}
}

func (r *Repository) PushRefSpec(remoteName string, refSpecs []string) error {
	args := []string{"push", remoteName}
	args = append(args, refSpecs...)

	_, err := r.executor(args...).executeString()
	if err != nil {
		return fmt.Errorf("unable to push: %w", err)
	}

	return nil
}

func (r *Repository) Push(remoteName string, refs []string) error {
	refSpecs := make([]string, 0, len(refs))
	for _, ref := range refs {
		refSpec, err := r.RefSpec(ref, "", true)
		if err != nil {
			return err
		}
		refSpecs = append(refSpecs, refSpec)
	}

	return r.PushRefSpec(remoteName, refSpecs)
}

func (r *Repository) FetchRefSpec(remoteName string, refSpecs []string, opts ...FetchOption) error {
	options := &FetchOptions{}
	for _, fn := range opts {
		fn(options)
	}

	args := []string{"fetch"}

	if options.Depth != 0 {
		args = append(args, "--depth", fmt.Sprintf("%d", options.Depth))
	}

	args = append(args, remoteName)
	args = append(args, refSpecs...)

	_, err := r.executor(args...).executeString()
	if err != nil {
		return fmt.Errorf("unable to fetch: %w", err)
	}

	return nil
}

func (r *Repository) Fetch(remoteName string, refs []string, fastForwardOnly bool, opts ...FetchOption) error {
	refSpecs := make([]string, 0, len(refs))
	for _, ref := range refs {
		refSpec, err := r.RefSpec(ref, "", fastForwardOnly)
		if err != nil {
			return err
		}
		refSpecs = append(refSpecs, refSpec)
	}

	return r.FetchRefSpec(remoteName, refSpecs, opts...)
}

func (r *Repository) FetchObject(remoteName string, objectID Hash) error {
	args := []string{"fetch", remoteName, objectID.String()}
	_, err := r.executor(args...).executeString()
	if err != nil {
		return fmt.Errorf("unable to fetch object: %w", err)
	}

	return nil
}

func CloneAndFetchRepository(remoteURL, dir, initialBranch string, refs []string, bare bool) (*Repository, error) {
	if dir == "" {
		return nil, fmt.Errorf("target directory must be specified")
	}

	repo := &Repository{clock: clockwork.NewRealClock()}

	args := []string{"clone", remoteURL}
	if initialBranch != "" {
		initialBranch = strings.TrimPrefix(initialBranch, BranchRefPrefix)
		args = append(args, "--branch", initialBranch)
	}
	args = append(args, dir)

	if bare {
		args = append(args, "--bare")
		repo.gitDirPath = dir
	} else {
		repo.gitDirPath = path.Join(dir, ".git")
	}

	_, stdErr, err := repo.executor(args...).execute()
	if err != nil {
		return nil, fmt.Errorf("unable to clone repository: %s", stdErr)
	}

	return repo, repo.Fetch(DefaultRemoteName, refs, true)
}

func (r *Repository) CreateRemote(remoteName, remoteURL string) error {
	_, err := r.executor("remote", "add", remoteName, remoteURL).executeString()
	if err != nil {
		return fmt.Errorf("unable to add remote: %w", err)
	}

	return nil
}