forked from forgejo/forgejo
refactor: move from io/ioutil to io and os package (#17109)
The io/ioutil package has been deprecated as of Go 1.16, see https://golang.org/doc/go1.16#ioutil. This commit replaces the existing io/ioutil functions with their new definitions in io and os packages. Signed-off-by: Eng Zer Jun <engzerjun@gmail.com> Co-authored-by: techknowlogick <techknowlogick@gitea.io>
This commit is contained in:
parent
aa631d8cd1
commit
f2e7d5477f
103 changed files with 261 additions and 314 deletions
|
@ -6,7 +6,7 @@ package git
|
|||
|
||||
import (
|
||||
"context"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
@ -84,7 +84,7 @@ e2aa991e10ffd924a828ec149951f2f20eecead2 7 7
|
|||
`
|
||||
|
||||
func TestReadingBlameOutput(t *testing.T) {
|
||||
tempFile, err := ioutil.TempFile("", ".txt")
|
||||
tempFile, err := os.CreateTemp("", ".txt")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
|
|
@ -9,7 +9,6 @@ import (
|
|||
"bytes"
|
||||
"encoding/base64"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
|
||||
"code.gitea.io/gitea/modules/typesniffer"
|
||||
)
|
||||
|
@ -83,7 +82,7 @@ func (b *Blob) GetBlobContentBase64() (string, error) {
|
|||
}
|
||||
}()
|
||||
|
||||
out, err := ioutil.ReadAll(pr)
|
||||
out, err := io.ReadAll(pr)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
|
|
@ -11,7 +11,6 @@ import (
|
|||
"bufio"
|
||||
"bytes"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"math"
|
||||
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
|
@ -46,13 +45,13 @@ func (b *Blob) DataAsync() (io.ReadCloser, error) {
|
|||
b.size = size
|
||||
|
||||
if size < 4096 {
|
||||
bs, err := ioutil.ReadAll(io.LimitReader(rd, size))
|
||||
bs, err := io.ReadAll(io.LimitReader(rd, size))
|
||||
defer cancel()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, err = rd.Discard(1)
|
||||
return ioutil.NopCloser(bytes.NewReader(bs)), err
|
||||
return io.NopCloser(bytes.NewReader(bs)), err
|
||||
}
|
||||
|
||||
return &blobReader{
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
package git
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
|
@ -30,7 +30,7 @@ func TestBlob_Data(t *testing.T) {
|
|||
assert.NoError(t, err)
|
||||
require.NotNil(t, r)
|
||||
|
||||
data, err := ioutil.ReadAll(r)
|
||||
data, err := io.ReadAll(r)
|
||||
assert.NoError(t, r.Close())
|
||||
|
||||
assert.NoError(t, err)
|
||||
|
@ -55,7 +55,7 @@ func Benchmark_Blob_Data(b *testing.B) {
|
|||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
ioutil.ReadAll(r)
|
||||
io.ReadAll(r)
|
||||
_ = r.Close()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,7 +7,6 @@ package git
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
|
@ -59,14 +58,14 @@ func GetHook(repoPath, name string) (*Hook, error) {
|
|||
}
|
||||
samplePath := filepath.Join(repoPath, "hooks", name+".sample")
|
||||
if isFile(h.path) {
|
||||
data, err := ioutil.ReadFile(h.path)
|
||||
data, err := os.ReadFile(h.path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
h.IsActive = true
|
||||
h.Content = string(data)
|
||||
} else if isFile(samplePath) {
|
||||
data, err := ioutil.ReadFile(samplePath)
|
||||
data, err := os.ReadFile(samplePath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -97,7 +96,7 @@ func (h *Hook) Update() error {
|
|||
return err
|
||||
}
|
||||
|
||||
err := ioutil.WriteFile(h.path, []byte(strings.ReplaceAll(h.Content, "\r", "")), os.ModePerm)
|
||||
err := os.WriteFile(h.path, []byte(strings.ReplaceAll(h.Content, "\r", "")), os.ModePerm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -143,5 +142,5 @@ func SetUpdateHook(repoPath, content string) (err error) {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return ioutil.WriteFile(hookPath, []byte(content), 0777)
|
||||
return os.WriteFile(hookPath, []byte(content), 0777)
|
||||
}
|
||||
|
|
|
@ -9,7 +9,7 @@ package git
|
|||
|
||||
import (
|
||||
"context"
|
||||
"io/ioutil"
|
||||
"io"
|
||||
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
|
||||
|
@ -58,7 +58,7 @@ func GetNote(ctx context.Context, repo *Repository, commitID string, note *Note)
|
|||
}
|
||||
|
||||
defer dataRc.Close()
|
||||
d, err := ioutil.ReadAll(dataRc)
|
||||
d, err := io.ReadAll(dataRc)
|
||||
if err != nil {
|
||||
log.Error("Unable to read blob with ID %q. Error: %v", blob.ID, err)
|
||||
return err
|
||||
|
|
|
@ -9,7 +9,7 @@ package git
|
|||
|
||||
import (
|
||||
"context"
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
|
@ -60,7 +60,7 @@ func GetNote(ctx context.Context, repo *Repository, commitID string, note *Note)
|
|||
_ = dataRc.Close()
|
||||
}
|
||||
}()
|
||||
d, err := ioutil.ReadAll(dataRc)
|
||||
d, err := io.ReadAll(dataRc)
|
||||
if err != nil {
|
||||
log.Error("Unable to read blob with ID %q. Error: %v", blob.ID, err)
|
||||
return err
|
||||
|
|
|
@ -6,7 +6,7 @@ package git
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
|
@ -34,7 +34,7 @@ func TestRepository_GetBlob_Found(t *testing.T) {
|
|||
dataReader, err := blob.DataAsync()
|
||||
assert.NoError(t, err)
|
||||
|
||||
data, err := ioutil.ReadAll(dataReader)
|
||||
data, err := io.ReadAll(dataReader)
|
||||
assert.NoError(t, dataReader.Close())
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, testCase.Data, data)
|
||||
|
|
|
@ -8,7 +8,6 @@ package git
|
|||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
|
@ -222,7 +221,7 @@ func (repo *Repository) CommitsByFileAndRange(revision, file string, page int) (
|
|||
}()
|
||||
|
||||
if skip > 0 {
|
||||
_, err := io.CopyN(ioutil.Discard, stdoutReader, int64(skip*41))
|
||||
_, err := io.CopyN(io.Discard, stdoutReader, int64(skip*41))
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
return []*Commit{}, nil
|
||||
|
@ -232,7 +231,7 @@ func (repo *Repository) CommitsByFileAndRange(revision, file string, page int) (
|
|||
}
|
||||
}
|
||||
|
||||
stdout, err := ioutil.ReadAll(stdoutReader)
|
||||
stdout, err := io.ReadAll(stdoutReader)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -11,7 +11,6 @@ import (
|
|||
"bufio"
|
||||
"errors"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
|
@ -77,7 +76,7 @@ func (repo *Repository) getCommitFromBatchReader(rd *bufio.Reader, id SHA1) (*Co
|
|||
case "tag":
|
||||
// then we need to parse the tag
|
||||
// and load the commit
|
||||
data, err := ioutil.ReadAll(io.LimitReader(rd, size))
|
||||
data, err := io.ReadAll(io.LimitReader(rd, size))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@ package git
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
|
@ -25,7 +25,7 @@ func TestGetFormatPatch(t *testing.T) {
|
|||
rd := &bytes.Buffer{}
|
||||
err = repo.GetPatch("8d92fc95^", "8d92fc95", rd)
|
||||
assert.NoError(t, err)
|
||||
patchb, err := ioutil.ReadAll(rd)
|
||||
patchb, err := io.ReadAll(rd)
|
||||
assert.NoError(t, err)
|
||||
patch := string(patchb)
|
||||
assert.Regexp(t, "^From 8d92fc95", patch)
|
||||
|
|
|
@ -7,7 +7,6 @@ package git
|
|||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
|
@ -47,7 +46,7 @@ func (repo *Repository) readTreeToIndex(id SHA1, indexFilename ...string) error
|
|||
|
||||
// ReadTreeToTemporaryIndex reads a treeish to a temporary index file
|
||||
func (repo *Repository) ReadTreeToTemporaryIndex(treeish string) (filename string, cancel context.CancelFunc, err error) {
|
||||
tmpIndex, err := ioutil.TempFile("", "index")
|
||||
tmpIndex, err := os.CreateTemp("", "index")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@ import (
|
|||
"bytes"
|
||||
"context"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
||||
"code.gitea.io/gitea/modules/analyze"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
|
@ -51,7 +51,7 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
|
|||
indexFilename, deleteTemporaryFile, err := repo.ReadTreeToTemporaryIndex(commitID)
|
||||
if err == nil {
|
||||
defer deleteTemporaryFile()
|
||||
tmpWorkTree, err := ioutil.TempDir("", "empty-work-dir")
|
||||
tmpWorkTree, err := os.MkdirTemp("", "empty-work-dir")
|
||||
if err == nil {
|
||||
defer func() {
|
||||
_ = util.RemoveAll(tmpWorkTree)
|
||||
|
@ -174,7 +174,7 @@ func readFile(f *object.File, limit int64) ([]byte, error) {
|
|||
defer r.Close()
|
||||
|
||||
if limit <= 0 {
|
||||
return ioutil.ReadAll(r)
|
||||
return io.ReadAll(r)
|
||||
}
|
||||
|
||||
size := f.Size
|
||||
|
|
|
@ -12,8 +12,8 @@ import (
|
|||
"bytes"
|
||||
"context"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"math"
|
||||
"os"
|
||||
|
||||
"code.gitea.io/gitea/modules/analyze"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
|
@ -71,7 +71,7 @@ func (repo *Repository) GetLanguageStats(commitID string) (map[string]int64, err
|
|||
indexFilename, deleteTemporaryFile, err := repo.ReadTreeToTemporaryIndex(commitID)
|
||||
if err == nil {
|
||||
defer deleteTemporaryFile()
|
||||
tmpWorkTree, err := ioutil.TempDir("", "empty-work-dir")
|
||||
tmpWorkTree, err := os.MkdirTemp("", "empty-work-dir")
|
||||
if err == nil {
|
||||
defer func() {
|
||||
_ = util.RemoveAll(tmpWorkTree)
|
||||
|
|
|
@ -9,7 +9,6 @@ package git
|
|||
|
||||
import (
|
||||
"io"
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
func (repo *Repository) getTree(id SHA1) (*Tree, error) {
|
||||
|
@ -27,7 +26,7 @@ func (repo *Repository) getTree(id SHA1) (*Tree, error) {
|
|||
switch typ {
|
||||
case "tag":
|
||||
resolvedID := id
|
||||
data, err := ioutil.ReadAll(io.LimitReader(rd, size))
|
||||
data, err := io.ReadAll(io.LimitReader(rd, size))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue