This repository was archived by the owner on Sep 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 534
remote: support for non-force, fast-forward-only fetches #665
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
702718f
Support non-force fetches
3834e57
Use optionally locking when updating refs
taruti c4766ff
Document Lock+Close usage
taruti 77482f9
Fetch - honor per refspec force flag
taruti d3f5eaf
remote: add test for non-force, fast-forward fetching
strib 83e7046
Merge remote-tracking branch 'src-d/master' into gh-fast-forward-fetch
strib cbab840
dotgit: add CheckAndSetReference tests
strib File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import ( | |
"bufio" | ||
"errors" | ||
"fmt" | ||
"io" | ||
stdioutil "io/ioutil" | ||
"os" | ||
"strings" | ||
|
@@ -239,7 +240,39 @@ func (d *DotGit) Object(h plumbing.Hash) (billy.File, error) { | |
return d.fs.Open(file) | ||
} | ||
|
||
func (d *DotGit) SetRef(r *plumbing.Reference) error { | ||
func (d *DotGit) readReferenceFrom(rd io.Reader, name string) (ref *plumbing.Reference, err error) { | ||
b, err := stdioutil.ReadAll(rd) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
line := strings.TrimSpace(string(b)) | ||
return plumbing.NewReferenceFromStrings(name, line), nil | ||
} | ||
|
||
func (d *DotGit) checkReferenceAndTruncate(f billy.File, old *plumbing.Reference) error { | ||
if old == nil { | ||
return nil | ||
} | ||
ref, err := d.readReferenceFrom(f, old.Name().String()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function is not covered by the test. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if err != nil { | ||
return err | ||
} | ||
if ref.Hash() != old.Hash() { | ||
return fmt.Errorf("reference has changed concurrently") | ||
} | ||
_, err = f.Seek(0, io.SeekStart) | ||
if err != nil { | ||
return err | ||
} | ||
err = f.Truncate(0) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (d *DotGit) SetRef(r, old *plumbing.Reference) error { | ||
var content string | ||
switch r.Type() { | ||
case plumbing.SymbolicReference: | ||
|
@@ -248,13 +281,34 @@ func (d *DotGit) SetRef(r *plumbing.Reference) error { | |
content = fmt.Sprintln(r.Hash().String()) | ||
} | ||
|
||
f, err := d.fs.Create(r.Name().String()) | ||
// If we are not checking an old ref, just truncate the file. | ||
mode := os.O_RDWR | os.O_CREATE | ||
if old == nil { | ||
mode |= os.O_TRUNC | ||
} | ||
|
||
f, err := d.fs.OpenFile(r.Name().String(), mode, 0666) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer ioutil.CheckClose(f, &err) | ||
|
||
// Lock is unlocked by the deferred Close above. This is because Unlock | ||
// does not imply a fsync and thus there would be a race between | ||
// Unlock+Close and other concurrent writers. Adding Sync to go-billy | ||
// could work, but this is better (and avoids superfluous syncs). | ||
err = f.Lock() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// this is a no-op to call even when old is nil. | ||
err = d.checkReferenceAndTruncate(f, old) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = f.Write([]byte(content)) | ||
return err | ||
} | ||
|
@@ -523,13 +577,7 @@ func (d *DotGit) readReferenceFile(path, name string) (ref *plumbing.Reference, | |
} | ||
defer ioutil.CheckClose(f, &err) | ||
|
||
b, err := stdioutil.ReadAll(f) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
line := strings.TrimSpace(string(b)) | ||
return plumbing.NewReferenceFromStrings(name, line), nil | ||
return d.readReferenceFrom(f, name) | ||
} | ||
|
||
// Module return a billy.Filesystem poiting to the module folder | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it is a good time to add doc comments to the other methods in this interface?