diff options
author | Eng Zer Jun <engzerjun@gmail.com> | 2021-09-22 13:38:34 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-22 13:38:34 +0800 |
commit | f2e7d5477f076789da5d0e95fe61a56ddb939f5a (patch) | |
tree | 922ca8769761c30e93f3b4deaf27858026b27ebf /modules/git | |
parent | aa631d8cd18251aa9b18ce72f75c8d8c7090e5e7 (diff) | |
download | gitea-f2e7d5477f076789da5d0e95fe61a56ddb939f5a.tar.gz gitea-f2e7d5477f076789da5d0e95fe61a56ddb939f5a.zip |
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>
Diffstat (limited to 'modules/git')
-rw-r--r-- | modules/git/blame_test.go | 4 | ||||
-rw-r--r-- | modules/git/blob.go | 3 | ||||
-rw-r--r-- | modules/git/blob_nogogit.go | 5 | ||||
-rw-r--r-- | modules/git/blob_test.go | 6 | ||||
-rw-r--r-- | modules/git/hook.go | 9 | ||||
-rw-r--r-- | modules/git/notes_gogit.go | 4 | ||||
-rw-r--r-- | modules/git/notes_nogogit.go | 4 | ||||
-rw-r--r-- | modules/git/repo_blob_test.go | 4 | ||||
-rw-r--r-- | modules/git/repo_commit.go | 5 | ||||
-rw-r--r-- | modules/git/repo_commit_nogogit.go | 3 | ||||
-rw-r--r-- | modules/git/repo_compare_test.go | 4 | ||||
-rw-r--r-- | modules/git/repo_index.go | 3 | ||||
-rw-r--r-- | modules/git/repo_language_stats_gogit.go | 6 | ||||
-rw-r--r-- | modules/git/repo_language_stats_nogogit.go | 4 | ||||
-rw-r--r-- | modules/git/repo_tree_nogogit.go | 3 |
15 files changed, 30 insertions, 37 deletions
diff --git a/modules/git/blame_test.go b/modules/git/blame_test.go index 734d63ee14..4bee8cd27a 100644 --- a/modules/git/blame_test.go +++ b/modules/git/blame_test.go @@ -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) } diff --git a/modules/git/blob.go b/modules/git/blob.go index 5831bc3735..c7919f3c41 100644 --- a/modules/git/blob.go +++ b/modules/git/blob.go @@ -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 } diff --git a/modules/git/blob_nogogit.go b/modules/git/blob_nogogit.go index 3391bc3931..65a73ebc52 100644 --- a/modules/git/blob_nogogit.go +++ b/modules/git/blob_nogogit.go @@ -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{ diff --git a/modules/git/blob_test.go b/modules/git/blob_test.go index 2ceda6c4ef..34d8054d1e 100644 --- a/modules/git/blob_test.go +++ b/modules/git/blob_test.go @@ -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() } } diff --git a/modules/git/hook.go b/modules/git/hook.go index 7007d23be2..ecd2db3cc3 100644 --- a/modules/git/hook.go +++ b/modules/git/hook.go @@ -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) } diff --git a/modules/git/notes_gogit.go b/modules/git/notes_gogit.go index 84f66d86bf..28dbbc0ee5 100644 --- a/modules/git/notes_gogit.go +++ b/modules/git/notes_gogit.go @@ -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 diff --git a/modules/git/notes_nogogit.go b/modules/git/notes_nogogit.go index 9a54b48106..5afe8d4614 100644 --- a/modules/git/notes_nogogit.go +++ b/modules/git/notes_nogogit.go @@ -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 diff --git a/modules/git/repo_blob_test.go b/modules/git/repo_blob_test.go index ccf418b305..132a3fa50c 100644 --- a/modules/git/repo_blob_test.go +++ b/modules/git/repo_blob_test.go @@ -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) diff --git a/modules/git/repo_commit.go b/modules/git/repo_commit.go index e456f04e87..25060f56da 100644 --- a/modules/git/repo_commit.go +++ b/modules/git/repo_commit.go @@ -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 } diff --git a/modules/git/repo_commit_nogogit.go b/modules/git/repo_commit_nogogit.go index b7e49a6501..8bfc953759 100644 --- a/modules/git/repo_commit_nogogit.go +++ b/modules/git/repo_commit_nogogit.go @@ -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 } diff --git a/modules/git/repo_compare_test.go b/modules/git/repo_compare_test.go index f0e20838f4..3a6cda955c 100644 --- a/modules/git/repo_compare_test.go +++ b/modules/git/repo_compare_test.go @@ -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) diff --git a/modules/git/repo_index.go b/modules/git/repo_index.go index b301ff2437..27cb7fbebe 100644 --- a/modules/git/repo_index.go +++ b/modules/git/repo_index.go @@ -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 } diff --git a/modules/git/repo_language_stats_gogit.go b/modules/git/repo_language_stats_gogit.go index 1f2738a5a8..d37827c3de 100644 --- a/modules/git/repo_language_stats_gogit.go +++ b/modules/git/repo_language_stats_gogit.go @@ -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 diff --git a/modules/git/repo_language_stats_nogogit.go b/modules/git/repo_language_stats_nogogit.go index 8e9a3d6bcd..06269a466c 100644 --- a/modules/git/repo_language_stats_nogogit.go +++ b/modules/git/repo_language_stats_nogogit.go @@ -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) diff --git a/modules/git/repo_tree_nogogit.go b/modules/git/repo_tree_nogogit.go index 9d4268b13a..b27abb6e02 100644 --- a/modules/git/repo_tree_nogogit.go +++ b/modules/git/repo_tree_nogogit.go @@ -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 } |