Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit a48bc6e

Browse files
authored
Repository.Progress moved as a field in *Options (#237)
1 parent 77b6391 commit a48bc6e

File tree

8 files changed

+87
-62
lines changed

8 files changed

+87
-62
lines changed

examples/progress/main.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@ func main() {
1515
r, err := git.NewFilesystemRepository(directory)
1616
CheckIfError(err)
1717

18-
// as git does, when you make a clone, pull or some other operations, the
19-
// server sends information via the sideband, this information can being
20-
// collected provinding a io.Writer to the repository
21-
r.Progress = os.Stdout
22-
2318
// Clone the given repository to the given directory
2419
Info("git clone %s %s", url, directory)
2520

2621
err = r.Clone(&git.CloneOptions{
2722
URL: url,
2823
Depth: 1,
24+
25+
// as git does, when you make a clone, pull or some other operations the
26+
// server sends information via the sideband, this information can being
27+
// collected provinding a io.Writer to the CloneOptions options
28+
Progress: os.Stdout,
2929
})
3030

3131
CheckIfError(err)

options.go

+13
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"gopkg.in/src-d/go-git.v4/config"
77
"gopkg.in/src-d/go-git.v4/plumbing"
8+
"gopkg.in/src-d/go-git.v4/plumbing/protocol/packp/sideband"
89
"gopkg.in/src-d/go-git.v4/plumbing/transport"
910
)
1011

@@ -32,6 +33,10 @@ type CloneOptions struct {
3233
SingleBranch bool
3334
// Limit fetching to the specified number of commits
3435
Depth int
36+
// Progress is where the human readable information sent by the server is
37+
// stored, if nil nothing is stored and the capability (if supported)
38+
// no-progress, is sent to the server to avoid send this information
39+
Progress sideband.Progress
3540
}
3641

3742
// Validate validates the fields and sets the default values
@@ -63,6 +68,10 @@ type PullOptions struct {
6368
Depth int
6469
// Auth credentials, if required, to use with the remote repository
6570
Auth transport.AuthMethod
71+
// Progress is where the human readable information sent by the server is
72+
// stored, if nil nothing is stored and the capability (if supported)
73+
// no-progress, is sent to the server to avoid send this information
74+
Progress sideband.Progress
6675
}
6776

6877
// Validate validates the fields and sets the default values.
@@ -88,6 +97,10 @@ type FetchOptions struct {
8897
Depth int
8998
// Auth credentials, if required, to use with the remote repository
9099
Auth transport.AuthMethod
100+
// Progress is where the human readable information sent by the server is
101+
// stored, if nil nothing is stored and the capability (if supported)
102+
// no-progress, is sent to the server to avoid send this information
103+
Progress sideband.Progress
91104
}
92105

93106
// Validate validates the fields and sets the default values

plumbing/protocol/packp/sideband/demux.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ import (
1111
// ErrMaxPackedExceeded returned by Read, if the maximum packed size is exceeded
1212
var ErrMaxPackedExceeded = errors.New("max. packed size exceeded")
1313

14-
// Progress allows to read the progress information
14+
// Progress where the progress information is stored
1515
type Progress interface {
16-
io.Reader
1716
io.Writer
1817
}
1918

plumbing/protocol/packp/sideband/demux_test.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -84,23 +84,24 @@ func (s *SidebandSuite) TestDecodeFromFailingReader(c *C) {
8484
func (s *SidebandSuite) TestDecodeWithProgress(c *C) {
8585
expected := []byte("abcdefghijklmnopqrstuvwxyz")
8686

87-
buf := bytes.NewBuffer(nil)
88-
e := pktline.NewEncoder(buf)
87+
input := bytes.NewBuffer(nil)
88+
e := pktline.NewEncoder(input)
8989
e.Encode(PackData.WithPayload(expected[0:8]))
9090
e.Encode(ProgressMessage.WithPayload([]byte{'F', 'O', 'O', '\n'}))
9191
e.Encode(PackData.WithPayload(expected[8:16]))
9292
e.Encode(PackData.WithPayload(expected[16:26]))
9393

94+
output := bytes.NewBuffer(nil)
9495
content := make([]byte, 26)
95-
d := NewDemuxer(Sideband64k, buf)
96-
d.Progress = bytes.NewBuffer(nil)
96+
d := NewDemuxer(Sideband64k, input)
97+
d.Progress = output
9798

9899
n, err := io.ReadFull(d, content)
99100
c.Assert(err, IsNil)
100101
c.Assert(n, Equals, 26)
101102
c.Assert(content, DeepEquals, expected)
102103

103-
progress, err := ioutil.ReadAll(d.Progress)
104+
progress, err := ioutil.ReadAll(output)
104105
c.Assert(err, IsNil)
105106
c.Assert(progress, DeepEquals, []byte{'F', 'O', 'O', '\n'})
106107
}

remote.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,10 @@ var NoErrAlreadyUpToDate = errors.New("already up-to-date")
2626
type Remote struct {
2727
c *config.RemoteConfig
2828
s Storer
29-
p sideband.Progress
3029
}
3130

32-
func newRemote(s Storer, p sideband.Progress, c *config.RemoteConfig) *Remote {
33-
return &Remote{s: s, p: p, c: c}
31+
func newRemote(s Storer, c *config.RemoteConfig) *Remote {
32+
return &Remote{s: s, c: c}
3433
}
3534

3635
// Config return the config
@@ -59,6 +58,7 @@ func (r *Remote) Fetch(o *FetchOptions) error {
5958
// TODO: Support deletes.
6059
// TODO: Support pushing tags.
6160
// TODO: Check if force update is given, otherwise reject non-fast forward.
61+
// TODO: Sideband support
6262
func (r *Remote) Push(o *PushOptions) (err error) {
6363
if o.RemoteName == "" {
6464
o.RemoteName = r.c.Name
@@ -222,7 +222,7 @@ func (r *Remote) fetchPack(o *FetchOptions, s transport.UploadPackSession,
222222
}
223223

224224
if err = packfile.UpdateObjectStorage(r.s,
225-
buildSidebandIfSupported(req.Capabilities, reader, r.p),
225+
buildSidebandIfSupported(req.Capabilities, reader, o.Progress),
226226
); err != nil {
227227
return err
228228
}
@@ -400,7 +400,7 @@ func (r *Remote) newUploadPackRequest(o *FetchOptions,
400400
}
401401
}
402402

403-
if r.p == nil && ar.Capabilities.Supports(capability.NoProgress) {
403+
if o.Progress == nil && ar.Capabilities.Supports(capability.NoProgress) {
404404
if err := req.Capabilities.Set(capability.NoProgress); err != nil {
405405
return nil, err
406406
}

remote_test.go

+19-18
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,25 @@ type RemoteSuite struct {
2626
var _ = Suite(&RemoteSuite{})
2727

2828
func (s *RemoteSuite) TestFetchInvalidEndpoint(c *C) {
29-
r := newRemote(nil, nil, &config.RemoteConfig{Name: "foo", URL: "qux"})
29+
r := newRemote(nil, &config.RemoteConfig{Name: "foo", URL: "qux"})
3030
err := r.Fetch(&FetchOptions{})
3131
c.Assert(err, ErrorMatches, ".*invalid endpoint.*")
3232
}
3333

3434
func (s *RemoteSuite) TestFetchNonExistentEndpoint(c *C) {
35-
r := newRemote(nil, nil, &config.RemoteConfig{Name: "foo", URL: "ssh://non-existent/foo.git"})
35+
r := newRemote(nil, &config.RemoteConfig{Name: "foo", URL: "ssh://non-existent/foo.git"})
3636
err := r.Fetch(&FetchOptions{})
3737
c.Assert(err, NotNil)
3838
}
3939

4040
func (s *RemoteSuite) TestFetchInvalidSchemaEndpoint(c *C) {
41-
r := newRemote(nil, nil, &config.RemoteConfig{Name: "foo", URL: "qux://foo"})
41+
r := newRemote(nil, &config.RemoteConfig{Name: "foo", URL: "qux://foo"})
4242
err := r.Fetch(&FetchOptions{})
4343
c.Assert(err, ErrorMatches, ".*unsupported scheme.*")
4444
}
4545

4646
func (s *RemoteSuite) TestFetchInvalidFetchOptions(c *C) {
47-
r := newRemote(nil, nil, &config.RemoteConfig{Name: "foo", URL: "qux://foo"})
47+
r := newRemote(nil, &config.RemoteConfig{Name: "foo", URL: "qux://foo"})
4848
invalid := config.RefSpec("^*$ñ")
4949
err := r.Fetch(&FetchOptions{RefSpecs: []config.RefSpec{invalid}})
5050
c.Assert(err, Equals, ErrInvalidRefSpec)
@@ -53,7 +53,7 @@ func (s *RemoteSuite) TestFetchInvalidFetchOptions(c *C) {
5353
func (s *RemoteSuite) TestFetch(c *C) {
5454
url := s.GetBasicLocalRepositoryURL()
5555
sto := memory.NewStorage()
56-
r := newRemote(sto, nil, &config.RemoteConfig{Name: "foo", URL: url})
56+
r := newRemote(sto, &config.RemoteConfig{Name: "foo", URL: url})
5757

5858
refspec := config.RefSpec("+refs/heads/*:refs/remotes/origin/*")
5959
err := r.Fetch(&FetchOptions{
@@ -77,7 +77,7 @@ func (s *RemoteSuite) TestFetch(c *C) {
7777
func (s *RemoteSuite) TestFetchDepth(c *C) {
7878
url := s.GetBasicLocalRepositoryURL()
7979
sto := memory.NewStorage()
80-
r := newRemote(sto, nil, &config.RemoteConfig{Name: "foo", URL: url})
80+
r := newRemote(sto, &config.RemoteConfig{Name: "foo", URL: url})
8181

8282
refspec := config.RefSpec("+refs/heads/*:refs/remotes/origin/*")
8383
err := r.Fetch(&FetchOptions{
@@ -112,11 +112,12 @@ func (s *RemoteSuite) TestFetchWithProgress(c *C) {
112112
sto := memory.NewStorage()
113113
buf := bytes.NewBuffer(nil)
114114

115-
r := newRemote(sto, buf, &config.RemoteConfig{Name: "foo", URL: url})
115+
r := newRemote(sto, &config.RemoteConfig{Name: "foo", URL: url})
116116

117117
refspec := config.RefSpec("+refs/heads/*:refs/remotes/origin/*")
118118
err := r.Fetch(&FetchOptions{
119119
RefSpecs: []config.RefSpec{refspec},
120+
Progress: buf,
120121
})
121122

122123
c.Assert(err, IsNil)
@@ -147,7 +148,7 @@ func (s *RemoteSuite) TestFetchWithPackfileWriter(c *C) {
147148
mock := &mockPackfileWriter{Storer: fss}
148149

149150
url := s.GetBasicLocalRepositoryURL()
150-
r := newRemote(mock, nil, &config.RemoteConfig{Name: "foo", URL: url})
151+
r := newRemote(mock, &config.RemoteConfig{Name: "foo", URL: url})
151152

152153
refspec := config.RefSpec("+refs/heads/*:refs/remotes/origin/*")
153154
err = r.Fetch(&FetchOptions{
@@ -183,7 +184,7 @@ func (s *RemoteSuite) TestFetchNoErrAlreadyUpToDateWithNonCommitObjects(c *C) {
183184
func (s *RemoteSuite) doTestFetchNoErrAlreadyUpToDate(c *C, url string) {
184185

185186
sto := memory.NewStorage()
186-
r := newRemote(sto, nil, &config.RemoteConfig{Name: "foo", URL: url})
187+
r := newRemote(sto, &config.RemoteConfig{Name: "foo", URL: url})
187188

188189
refspec := config.RefSpec("+refs/heads/*:refs/remotes/origin/*")
189190
o := &FetchOptions{
@@ -197,7 +198,7 @@ func (s *RemoteSuite) doTestFetchNoErrAlreadyUpToDate(c *C, url string) {
197198
}
198199

199200
func (s *RemoteSuite) TestString(c *C) {
200-
r := newRemote(nil, nil, &config.RemoteConfig{
201+
r := newRemote(nil, &config.RemoteConfig{
201202
Name: "foo",
202203
URL: "https://github.com/git-fixtures/basic.git",
203204
})
@@ -216,7 +217,7 @@ func (s *RemoteSuite) TestPushToEmptyRepository(c *C) {
216217
dstFs := fixtures.ByTag("empty").One().DotGit()
217218
url := fmt.Sprintf("file://%s", dstFs.Base())
218219

219-
r := newRemote(sto, nil, &config.RemoteConfig{
220+
r := newRemote(sto, &config.RemoteConfig{
220221
Name: DefaultRemoteName,
221222
URL: url,
222223
})
@@ -253,7 +254,7 @@ func (s *RemoteSuite) TestPushNoErrAlreadyUpToDate(c *C) {
253254
sto, err := filesystem.NewStorage(f.DotGit())
254255
c.Assert(err, IsNil)
255256
url := fmt.Sprintf("file://%s", f.DotGit().Base())
256-
r := newRemote(sto, nil, &config.RemoteConfig{
257+
r := newRemote(sto, &config.RemoteConfig{
257258
Name: DefaultRemoteName,
258259
URL: url,
259260
})
@@ -266,32 +267,32 @@ func (s *RemoteSuite) TestPushNoErrAlreadyUpToDate(c *C) {
266267
}
267268

268269
func (s *RemoteSuite) TestPushInvalidEndpoint(c *C) {
269-
r := newRemote(nil, nil, &config.RemoteConfig{Name: "foo", URL: "qux"})
270+
r := newRemote(nil, &config.RemoteConfig{Name: "foo", URL: "qux"})
270271
err := r.Push(&PushOptions{})
271272
c.Assert(err, ErrorMatches, ".*invalid endpoint.*")
272273
}
273274

274275
func (s *RemoteSuite) TestPushNonExistentEndpoint(c *C) {
275-
r := newRemote(nil, nil, &config.RemoteConfig{Name: "foo", URL: "ssh://non-existent/foo.git"})
276+
r := newRemote(nil, &config.RemoteConfig{Name: "foo", URL: "ssh://non-existent/foo.git"})
276277
err := r.Push(&PushOptions{})
277278
c.Assert(err, NotNil)
278279
}
279280

280281
func (s *RemoteSuite) TestPushInvalidSchemaEndpoint(c *C) {
281-
r := newRemote(nil, nil, &config.RemoteConfig{Name: "foo", URL: "qux://foo"})
282+
r := newRemote(nil, &config.RemoteConfig{Name: "foo", URL: "qux://foo"})
282283
err := r.Push(&PushOptions{})
283284
c.Assert(err, ErrorMatches, ".*unsupported scheme.*")
284285
}
285286

286287
func (s *RemoteSuite) TestPushInvalidFetchOptions(c *C) {
287-
r := newRemote(nil, nil, &config.RemoteConfig{Name: "foo", URL: "qux://foo"})
288+
r := newRemote(nil, &config.RemoteConfig{Name: "foo", URL: "qux://foo"})
288289
invalid := config.RefSpec("^*$ñ")
289290
err := r.Push(&PushOptions{RefSpecs: []config.RefSpec{invalid}})
290291
c.Assert(err, Equals, ErrInvalidRefSpec)
291292
}
292293

293294
func (s *RemoteSuite) TestPushInvalidRefSpec(c *C) {
294-
r := newRemote(nil, nil, &config.RemoteConfig{
295+
r := newRemote(nil, &config.RemoteConfig{
295296
Name: DefaultRemoteName,
296297
URL: "file:///some-url",
297298
})
@@ -304,7 +305,7 @@ func (s *RemoteSuite) TestPushInvalidRefSpec(c *C) {
304305
}
305306

306307
func (s *RemoteSuite) TestPushWrongRemoteName(c *C) {
307-
r := newRemote(nil, nil, &config.RemoteConfig{
308+
r := newRemote(nil, &config.RemoteConfig{
308309
Name: DefaultRemoteName,
309310
URL: "file:///some-url",
310311
})

repository.go

+8-12
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"gopkg.in/src-d/go-git.v4/config"
88
"gopkg.in/src-d/go-git.v4/plumbing"
99
"gopkg.in/src-d/go-git.v4/plumbing/object"
10-
"gopkg.in/src-d/go-git.v4/plumbing/protocol/packp/sideband"
1110
"gopkg.in/src-d/go-git.v4/plumbing/storer"
1211
"gopkg.in/src-d/go-git.v4/storage/filesystem"
1312
"gopkg.in/src-d/go-git.v4/storage/memory"
@@ -27,11 +26,6 @@ var (
2726
type Repository struct {
2827
r map[string]*Remote
2928
s Storer
30-
31-
// Progress is where the human readable information sent by the server is
32-
// stored, if nil nothing is stored and the capability (if supported)
33-
// no-progress, is sent to the server to avoid send this information
34-
Progress sideband.Progress
3529
}
3630

3731
// NewMemoryRepository creates a new repository, backed by a memory.Storage
@@ -77,7 +71,7 @@ func (r *Repository) Remote(name string) (*Remote, error) {
7771
return nil, ErrRemoteNotFound
7872
}
7973

80-
return newRemote(r.s, r.Progress, c), nil
74+
return newRemote(r.s, c), nil
8175
}
8276

8377
// Remotes return all the remotes
@@ -91,7 +85,7 @@ func (r *Repository) Remotes() ([]*Remote, error) {
9185

9286
var i int
9387
for _, c := range cfg.Remotes {
94-
remotes[i] = newRemote(r.s, r.Progress, c)
88+
remotes[i] = newRemote(r.s, c)
9589
i++
9690
}
9791

@@ -104,7 +98,7 @@ func (r *Repository) CreateRemote(c *config.RemoteConfig) (*Remote, error) {
10498
return nil, err
10599
}
106100

107-
remote := newRemote(r.s, r.Progress, c)
101+
remote := newRemote(r.s, c)
108102

109103
cfg, err := r.s.Config()
110104
if err != nil {
@@ -169,6 +163,7 @@ func (r *Repository) Clone(o *CloneOptions) error {
169163
RefSpecs: r.cloneRefSpec(o, c),
170164
Depth: o.Depth,
171165
Auth: o.Auth,
166+
Progress: o.Progress,
172167
})
173168
if err != nil {
174169
return err
@@ -343,8 +338,9 @@ func (r *Repository) Pull(o *PullOptions) error {
343338
}
344339

345340
remoteRefs, err := remote.fetch(&FetchOptions{
346-
Depth: o.Depth,
347-
Auth: o.Auth,
341+
Depth: o.Depth,
342+
Auth: o.Auth,
343+
Progress: o.Progress,
348344
})
349345

350346
updated := true
@@ -405,7 +401,7 @@ func (r *Repository) Push(o *PushOptions) error {
405401
return remote.Push(o)
406402
}
407403

408-
// object.Commit return the commit with the given hash
404+
// Commit return the commit with the given hash
409405
func (r *Repository) Commit(h plumbing.Hash) (*object.Commit, error) {
410406
return object.GetCommit(r.s, h)
411407
}

0 commit comments

Comments
 (0)