mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-06-17 00:18:05 +02:00
Bump go-git
This commit is contained in:
97
vendor/github.com/go-git/go-billy/v5/util/util.go
generated
vendored
97
vendor/github.com/go-git/go-billy/v5/util/util.go
generated
vendored
@ -1,6 +1,7 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@ -33,14 +34,14 @@ func removeAll(fs billy.Basic, path string) error {
|
||||
|
||||
// Simple case: if Remove works, we're done.
|
||||
err := fs.Remove(path)
|
||||
if err == nil || os.IsNotExist(err) {
|
||||
if err == nil || errors.Is(err, os.ErrNotExist) {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Otherwise, is this a directory we need to recurse into?
|
||||
dir, serr := fs.Stat(path)
|
||||
if serr != nil {
|
||||
if os.IsNotExist(serr) {
|
||||
if errors.Is(serr, os.ErrNotExist) {
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -60,7 +61,7 @@ func removeAll(fs billy.Basic, path string) error {
|
||||
// Directory.
|
||||
fis, err := dirfs.ReadDir(path)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
// Race. It was deleted between the Lstat and Open.
|
||||
// Return nil per RemoveAll's docs.
|
||||
return nil
|
||||
@ -81,7 +82,7 @@ func removeAll(fs billy.Basic, path string) error {
|
||||
|
||||
// Remove directory.
|
||||
err1 := fs.Remove(path)
|
||||
if err1 == nil || os.IsNotExist(err1) {
|
||||
if err1 == nil || errors.Is(err1, os.ErrNotExist) {
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -96,22 +97,26 @@ func removeAll(fs billy.Basic, path string) error {
|
||||
// WriteFile writes data to a file named by filename in the given filesystem.
|
||||
// If the file does not exist, WriteFile creates it with permissions perm;
|
||||
// otherwise WriteFile truncates it before writing.
|
||||
func WriteFile(fs billy.Basic, filename string, data []byte, perm os.FileMode) error {
|
||||
func WriteFile(fs billy.Basic, filename string, data []byte, perm os.FileMode) (err error) {
|
||||
f, err := fs.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
if f != nil {
|
||||
err1 := f.Close()
|
||||
if err == nil {
|
||||
err = err1
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
n, err := f.Write(data)
|
||||
if err == nil && n < len(data) {
|
||||
err = io.ErrShortWrite
|
||||
}
|
||||
|
||||
if err1 := f.Close(); err == nil {
|
||||
err = err1
|
||||
}
|
||||
|
||||
return err
|
||||
return nil
|
||||
}
|
||||
|
||||
// Random number state.
|
||||
@ -146,16 +151,15 @@ func nextSuffix() string {
|
||||
// to remove the file when no longer needed.
|
||||
func TempFile(fs billy.Basic, dir, prefix string) (f billy.File, err error) {
|
||||
// This implementation is based on stdlib ioutil.TempFile.
|
||||
|
||||
if dir == "" {
|
||||
dir = os.TempDir()
|
||||
dir = getTempDir(fs)
|
||||
}
|
||||
|
||||
nconflict := 0
|
||||
for i := 0; i < 10000; i++ {
|
||||
name := filepath.Join(dir, prefix+nextSuffix())
|
||||
f, err = fs.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
|
||||
if os.IsExist(err) {
|
||||
if errors.Is(err, os.ErrExist) {
|
||||
if nconflict++; nconflict > 10 {
|
||||
randmu.Lock()
|
||||
rand = reseed()
|
||||
@ -179,14 +183,14 @@ func TempDir(fs billy.Dir, dir, prefix string) (name string, err error) {
|
||||
// This implementation is based on stdlib ioutil.TempDir
|
||||
|
||||
if dir == "" {
|
||||
dir = os.TempDir()
|
||||
dir = getTempDir(fs.(billy.Basic))
|
||||
}
|
||||
|
||||
nconflict := 0
|
||||
for i := 0; i < 10000; i++ {
|
||||
try := filepath.Join(dir, prefix+nextSuffix())
|
||||
err = fs.MkdirAll(try, 0700)
|
||||
if os.IsExist(err) {
|
||||
if errors.Is(err, os.ErrExist) {
|
||||
if nconflict++; nconflict > 10 {
|
||||
randmu.Lock()
|
||||
rand = reseed()
|
||||
@ -194,8 +198,8 @@ func TempDir(fs billy.Dir, dir, prefix string) (name string, err error) {
|
||||
}
|
||||
continue
|
||||
}
|
||||
if os.IsNotExist(err) {
|
||||
if _, err := os.Stat(dir); os.IsNotExist(err) {
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
if _, err := os.Stat(dir); errors.Is(err, os.ErrNotExist) {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
@ -207,6 +211,15 @@ func TempDir(fs billy.Dir, dir, prefix string) (name string, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func getTempDir(fs billy.Basic) string {
|
||||
ch, ok := fs.(billy.Chroot)
|
||||
if !ok || ch.Root() == "" || ch.Root() == "/" || ch.Root() == string(filepath.Separator) {
|
||||
return os.TempDir()
|
||||
}
|
||||
|
||||
return ".tmp"
|
||||
}
|
||||
|
||||
type underlying interface {
|
||||
Underlying() billy.Basic
|
||||
}
|
||||
@ -222,3 +235,53 @@ func getUnderlyingAndPath(fs billy.Basic, path string) (billy.Basic, string) {
|
||||
|
||||
return u.Underlying(), path
|
||||
}
|
||||
|
||||
// ReadFile reads the named file and returns the contents from the given filesystem.
|
||||
// A successful call returns err == nil, not err == EOF.
|
||||
// Because ReadFile reads the whole file, it does not treat an EOF from Read
|
||||
// as an error to be reported.
|
||||
func ReadFile(fs billy.Basic, name string) ([]byte, error) {
|
||||
f, err := fs.Open(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer f.Close()
|
||||
|
||||
var size int
|
||||
if info, err := fs.Stat(name); err == nil {
|
||||
size64 := info.Size()
|
||||
if int64(int(size64)) == size64 {
|
||||
size = int(size64)
|
||||
}
|
||||
}
|
||||
|
||||
size++ // one byte for final read at EOF
|
||||
// If a file claims a small size, read at least 512 bytes.
|
||||
// In particular, files in Linux's /proc claim size 0 but
|
||||
// then do not work right if read in small pieces,
|
||||
// so an initial read of 1 byte would not work correctly.
|
||||
|
||||
if size < 512 {
|
||||
size = 512
|
||||
}
|
||||
|
||||
data := make([]byte, 0, size)
|
||||
for {
|
||||
if len(data) >= cap(data) {
|
||||
d := append(data[:cap(data)], 0)
|
||||
data = d[:len(data)]
|
||||
}
|
||||
|
||||
n, err := f.Read(data[len(data):cap(data)])
|
||||
data = data[:len(data)+n]
|
||||
|
||||
if err != nil {
|
||||
if errors.Is(err, io.EOF) {
|
||||
err = nil
|
||||
}
|
||||
|
||||
return data, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user