diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2019-07-26 18:59:37 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-07-26 18:59:37 +0200 |
commit | 1edb992dbc419a0767b1cf3a524b0d35529799f5 (patch) | |
tree | fe994302589318133ec34e74bfe8c8f302a5147d /remote.go | |
parent | 4a6d836e2fa35fd05d2bfeeaecbb546c2e10ece1 (diff) | |
parent | 17ea92a0703c21b744560e6586ee61431dba9ffb (diff) | |
download | go-git-1edb992dbc419a0767b1cf3a524b0d35529799f5.tar.gz |
Merge pull request #1165 from seletskiy/push-prune
Remote: add Prune option to PushOptions
Diffstat (limited to 'remote.go')
-rw-r--r-- | remote.go | 67 |
1 files changed, 52 insertions, 15 deletions
@@ -171,7 +171,17 @@ func (r *Remote) PushContext(ctx context.Context, o *PushOptions) (err error) { } } - rs, err := pushHashes(ctx, s, r.s, req, hashesToPush, r.useRefDeltas(ar)) + if len(hashesToPush) == 0 { + allDelete = true + for _, command := range req.Commands { + if command.Action() != packp.Delete { + allDelete = false + break + } + } + } + + rs, err := pushHashes(ctx, s, r.s, req, hashesToPush, r.useRefDeltas(ar), allDelete) if err != nil { return err } @@ -204,7 +214,7 @@ func (r *Remote) newReferenceUpdateRequest( } } - if err := r.addReferencesToUpdate(o.RefSpecs, localRefs, remoteRefs, req); err != nil { + if err := r.addReferencesToUpdate(o.RefSpecs, localRefs, remoteRefs, req, o.Prune); err != nil { return nil, err } @@ -392,6 +402,7 @@ func (r *Remote) addReferencesToUpdate( localRefs []*plumbing.Reference, remoteRefs storer.ReferenceStorer, req *packp.ReferenceUpdateRequest, + prune bool, ) error { // This references dictionary will be used to search references by name. refsDict := make(map[string]*plumbing.Reference) @@ -401,7 +412,7 @@ func (r *Remote) addReferencesToUpdate( for _, rs := range refspecs { if rs.IsDelete() { - if err := r.deleteReferences(rs, remoteRefs, req); err != nil { + if err := r.deleteReferences(rs, remoteRefs, refsDict, req, false); err != nil { return err } } else { @@ -409,6 +420,12 @@ func (r *Remote) addReferencesToUpdate( if err != nil { return err } + + if prune { + if err := r.deleteReferences(rs, remoteRefs, refsDict, req, true); err != nil { + return err + } + } } } @@ -444,7 +461,10 @@ func (r *Remote) addOrUpdateReferences( } func (r *Remote) deleteReferences(rs config.RefSpec, - remoteRefs storer.ReferenceStorer, req *packp.ReferenceUpdateRequest) error { + remoteRefs storer.ReferenceStorer, + refsDict map[string]*plumbing.Reference, + req *packp.ReferenceUpdateRequest, + prune bool) error { iter, err := remoteRefs.IterReferences() if err != nil { return err @@ -455,8 +475,19 @@ func (r *Remote) deleteReferences(rs config.RefSpec, return nil } - if rs.Dst("") != ref.Name() { - return nil + if prune { + rs := rs.Reverse() + if !rs.Match(ref.Name()) { + return nil + } + + if _, ok := refsDict[rs.Dst(ref.Name()).String()]; ok { + return nil + } + } else { + if rs.Dst("") != ref.Name() { + return nil + } } cmd := &packp.Command{ @@ -1015,10 +1046,11 @@ func pushHashes( req *packp.ReferenceUpdateRequest, hs []plumbing.Hash, useRefDeltas bool, + allDelete bool, ) (*packp.ReportStatus, error) { rd, wr := io.Pipe() - req.Packfile = rd + config, err := s.Config() if err != nil { return nil, err @@ -1029,15 +1061,20 @@ func pushHashes( // to the channel. done := make(chan error, 1) - go func() { - e := packfile.NewEncoder(wr, s, useRefDeltas) - if _, err := e.Encode(hs, config.Pack.Window); err != nil { - done <- wr.CloseWithError(err) - return - } + if !allDelete { + req.Packfile = rd + go func() { + e := packfile.NewEncoder(wr, s, useRefDeltas) + if _, err := e.Encode(hs, config.Pack.Window); err != nil { + done <- wr.CloseWithError(err) + return + } - done <- wr.Close() - }() + done <- wr.Close() + }() + } else { + close(done) + } rs, err := sess.ReceivePack(ctx, req) if err != nil { |