git-codereview: remove usage of io/ioutil

io/ioutil is now deprecated. Use non-deprecated equivalents.

Change-Id: Iad1cc9c14dbdad3a6f9ada25072bc194bace4081
Reviewed-on: https://go-review.googlesource.com/c/review/+/543615
Reviewed-by: qiulaidongfeng <2645477756@qq.com>
Auto-Submit: Dmitri Shuralyov <dmitshur@golang.org>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
diff --git a/git-codereview/api.go b/git-codereview/api.go
index 9f7fe23..1b9d0e0 100644
--- a/git-codereview/api.go
+++ b/git-codereview/api.go
@@ -9,7 +9,6 @@
 	"encoding/json"
 	"fmt"
 	"io"
-	"io/ioutil"
 	"net/http"
 	"net/url"
 	"os"
@@ -171,7 +170,7 @@
 	// First look in Git's http.cookiefile, which is where Gerrit
 	// now tells users to store this information.
 	if cookieFile, _ := trimErr(cmdOutputErr("git", "config", "--path", "--get-urlmatch", "http.cookiefile", auth.url)); cookieFile != "" {
-		data, _ := ioutil.ReadFile(cookieFile)
+		data, _ := os.ReadFile(cookieFile)
 		maxMatch := -1
 		for _, line := range lines(string(data)) {
 			f := strings.Split(line, "\t")
@@ -200,7 +199,7 @@
 		}
 		homeDir = usr.HomeDir
 	}
-	data, _ := ioutil.ReadFile(filepath.Join(homeDir, netrc))
+	data, _ := os.ReadFile(filepath.Join(homeDir, netrc))
 	for _, line := range lines(string(data)) {
 		if i := strings.Index(line, "#"); i >= 0 {
 			line = line[:i]
@@ -292,7 +291,7 @@
 	if err != nil {
 		return err
 	}
-	body, err := ioutil.ReadAll(resp.Body)
+	body, err := io.ReadAll(resp.Body)
 	resp.Body.Close()
 
 	respBodyBytes = body
diff --git a/git-codereview/branch.go b/git-codereview/branch.go
index 7d60b68..b22ac39 100644
--- a/git-codereview/branch.go
+++ b/git-codereview/branch.go
@@ -7,7 +7,6 @@
 import (
 	"bytes"
 	"fmt"
-	"io/ioutil"
 	"net/url"
 	"os"
 	"os/exec"
@@ -69,7 +68,7 @@
 	}
 	var cfgText string
 	if b.Current {
-		data, _ := ioutil.ReadFile(filepath.Join(repoRoot(), "codereview.cfg"))
+		data, _ := os.ReadFile(filepath.Join(repoRoot(), "codereview.cfg"))
 		cfgText = string(data)
 	} else {
 		out, err := cmdOutputDirErr(repoRoot(), "git", "show", b.Name+":codereview.cfg")
diff --git a/git-codereview/config.go b/git-codereview/config.go
index 4e0a565..debeb5e 100644
--- a/git-codereview/config.go
+++ b/git-codereview/config.go
@@ -6,7 +6,7 @@
 
 import (
 	"fmt"
-	"io/ioutil"
+	"os"
 	"path/filepath"
 	"strings"
 )
@@ -26,7 +26,7 @@
 		return cachedConfig
 	}
 	configPath = filepath.Join(repoRoot(), "codereview.cfg")
-	b, err := ioutil.ReadFile(configPath)
+	b, err := os.ReadFile(configPath)
 	raw := string(b)
 	if err != nil {
 		verbosef("%sfailed to load config from %q: %v", raw, configPath, err)
diff --git a/git-codereview/editor.go b/git-codereview/editor.go
index c3de65f..ad112d1 100644
--- a/git-codereview/editor.go
+++ b/git-codereview/editor.go
@@ -6,7 +6,6 @@
 
 import (
 	"io"
-	"io/ioutil"
 	"os"
 	"os/exec"
 )
@@ -20,7 +19,7 @@
 	gitEditor := trim(cmdOutput("git", "var", "GIT_EDITOR"))
 
 	// Create temporary file.
-	temp, err := ioutil.TempFile("", "git-codereview")
+	temp, err := os.CreateTemp("", "git-codereview")
 	if err != nil {
 		dief("creating temp file: %v", err)
 	}
@@ -42,7 +41,7 @@
 	}
 
 	// Read the edited file.
-	b, err := ioutil.ReadFile(tempName)
+	b, err := os.ReadFile(tempName)
 	if err != nil {
 		dief("%v", err)
 	}
diff --git a/git-codereview/gofmt_test.go b/git-codereview/gofmt_test.go
index 81f82a4..de64760 100644
--- a/git-codereview/gofmt_test.go
+++ b/git-codereview/gofmt_test.go
@@ -6,7 +6,6 @@
 
 import (
 	"fmt"
-	"io/ioutil"
 	"os"
 	"strings"
 	"testing"
@@ -222,7 +221,7 @@
 
 	// Read files to make sure unstaged did not bleed into staged.
 	for i, file := range allFiles {
-		if data, err := ioutil.ReadFile(gt.client + "/" + file); err != nil {
+		if data, err := os.ReadFile(gt.client + "/" + file); err != nil {
 			t.Errorf("%v", err)
 		} else if want := fixed[i%N]; string(data) != want {
 			t.Errorf("%s: working tree = %q, want %q", file, string(data), want)
diff --git a/git-codereview/hook.go b/git-codereview/hook.go
index 0b4995f..a2c54e5 100644
--- a/git-codereview/hook.go
+++ b/git-codereview/hook.go
@@ -9,7 +9,6 @@
 	"crypto/rand"
 	"fmt"
 	"io"
-	"io/ioutil"
 	"os"
 	"path/filepath"
 	"regexp"
@@ -34,7 +33,7 @@
 		filename := filepath.Join(hooksDir, hookFile)
 		hookContent := fmt.Sprintf(hookScript, hookFile)
 
-		if data, err := ioutil.ReadFile(filename); err == nil {
+		if data, err := os.ReadFile(filename); err == nil {
 			// Special case: remove old hooks that use 'git-review'
 			oldHookContent := fmt.Sprintf(oldHookScript, hookFile)
 			if string(data) == oldHookContent {
@@ -54,7 +53,7 @@
 		// If hook file exists but has different content, let the user know.
 		_, err := os.Stat(filename)
 		if err == nil {
-			data, err := ioutil.ReadFile(filename)
+			data, err := os.ReadFile(filename)
 			if err != nil {
 				verbosef("reading hook: %v", err)
 			} else if string(data) != hookContent {
@@ -77,7 +76,7 @@
 				dief("creating hooks directory: %v", err)
 			}
 		}
-		if err := ioutil.WriteFile(filename, []byte(hookContent), 0700); err != nil {
+		if err := os.WriteFile(filename, []byte(hookContent), 0700); err != nil {
 			dief("writing hook: %v", err)
 		}
 	}
@@ -185,7 +184,7 @@
 	*/
 
 	file := args[0]
-	oldData, err := ioutil.ReadFile(file)
+	oldData, err := os.ReadFile(file)
 	if err != nil {
 		dief("%v", err)
 	}
@@ -194,7 +193,7 @@
 
 	// Write back.
 	if !bytes.Equal(data, oldData) {
-		if err := ioutil.WriteFile(file, data, 0666); err != nil {
+		if err := os.WriteFile(file, data, 0666); err != nil {
 			dief("%v", err)
 		}
 	}
diff --git a/git-codereview/hook_test.go b/git-codereview/hook_test.go
index 8ffe234..218b425 100644
--- a/git-codereview/hook_test.go
+++ b/git-codereview/hook_test.go
@@ -7,7 +7,6 @@
 import (
 	"bytes"
 	"fmt"
-	"io/ioutil"
 	"os"
 	"path/filepath"
 	"regexp"
@@ -87,11 +86,11 @@
 		testMain(t, "hook-invoke", "commit-msg", gt.client+"/in.txt")
 		write(t, gt.client+"/want.txt", tt.want, 0644)
 		testMain(t, "hook-invoke", "commit-msg", gt.client+"/want.txt")
-		got, err := ioutil.ReadFile(gt.client + "/in.txt")
+		got, err := os.ReadFile(gt.client + "/in.txt")
 		if err != nil {
 			t.Fatal(err)
 		}
-		want, err := ioutil.ReadFile(gt.client + "/want.txt")
+		want, err := os.ReadFile(gt.client + "/want.txt")
 		if err != nil {
 			t.Fatal(err)
 		}
@@ -166,7 +165,7 @@
 	checkPrefix := func(prefix string) {
 		write(t, gt.client+"/msg.txt", "Test message.\n", 0644)
 		testMain(t, "hook-invoke", "commit-msg", gt.client+"/msg.txt")
-		data, err := ioutil.ReadFile(gt.client + "/msg.txt")
+		data, err := os.ReadFile(gt.client + "/msg.txt")
 		if err != nil {
 			t.Fatal(err)
 		}
@@ -179,7 +178,7 @@
 			for _, magic := range []string{"fixup!", "squash!"} {
 				write(t, gt.client+"/msg.txt", magic+" Test message.\n", 0644)
 				testMain(t, "hook-invoke", "commit-msg", gt.client+"/msg.txt")
-				data, err := ioutil.ReadFile(gt.client + "/msg.txt")
+				data, err := os.ReadFile(gt.client + "/msg.txt")
 				if err != nil {
 					t.Fatal(err)
 				}
@@ -411,7 +410,7 @@
 	gt.removeStubHooks()
 	testMain(t, "hooks") // install hooks
 
-	data, err := ioutil.ReadFile(gt.client + "/.git/hooks/commit-msg")
+	data, err := os.ReadFile(gt.client + "/.git/hooks/commit-msg")
 	if err != nil {
 		t.Fatalf("hooks did not write commit-msg hook: %v", err)
 	}
@@ -441,7 +440,7 @@
 	gt.removeStubHooks()
 	testMain(t, "hooks") // install hooks
 
-	data, err := ioutil.ReadFile(gt.client + "/.git/hooks/commit-msg")
+	data, err := os.ReadFile(gt.client + "/.git/hooks/commit-msg")
 	if err != nil {
 		t.Fatalf("hooks did not write commit-msg hook: %v", err)
 	}
@@ -462,7 +461,7 @@
 
 	testMain(t, "hooks") // install hooks
 
-	data, err := ioutil.ReadFile(gt.client + "/.git/hooks/commit-msg")
+	data, err := os.ReadFile(gt.client + "/.git/hooks/commit-msg")
 	if err != nil {
 		t.Fatalf("hooks did not write commit-msg hook: %v", err)
 	}
@@ -479,7 +478,7 @@
 	mkdir(t, gt.client+"/.git/hooks")
 	write(t, gt.client+"/.git/hooks/commit-msg", oldCommitMsgHook, 0755)
 	testMain(t, "hooks") // install hooks
-	data, err := ioutil.ReadFile(gt.client + "/.git/hooks/commit-msg")
+	data, err := os.ReadFile(gt.client + "/.git/hooks/commit-msg")
 	if err != nil {
 		t.Fatalf("hooks did not write commit-msg hook: %v", err)
 	}
diff --git a/git-codereview/pending_test.go b/git-codereview/pending_test.go
index 1823439..370f44f 100644
--- a/git-codereview/pending_test.go
+++ b/git-codereview/pending_test.go
@@ -6,7 +6,6 @@
 
 import (
 	"fmt"
-	"io/ioutil"
 	"os"
 	"os/exec"
 	"regexp"
@@ -505,14 +504,14 @@
 }
 
 func diff(b1, b2 []byte) (data []byte, err error) {
-	f1, err := ioutil.TempFile("", "gofmt")
+	f1, err := os.CreateTemp("", "gofmt")
 	if err != nil {
 		return
 	}
 	defer os.Remove(f1.Name())
 	defer f1.Close()
 
-	f2, err := ioutil.TempFile("", "gofmt")
+	f2, err := os.CreateTemp("", "gofmt")
 	if err != nil {
 		return
 	}
diff --git a/git-codereview/reword.go b/git-codereview/reword.go
index 8f12405..efeb26b 100644
--- a/git-codereview/reword.go
+++ b/git-codereview/reword.go
@@ -7,7 +7,6 @@
 import (
 	"bytes"
 	"fmt"
-	"io/ioutil"
 	"os"
 	"path/filepath"
 	"strings"
@@ -90,7 +89,7 @@
 	var buf bytes.Buffer
 	saveFile := filepath.Join(gitPathDir(), "REWORD_MSGS")
 	saveBuf := func() {
-		if err := ioutil.WriteFile(saveFile, buf.Bytes(), 0666); err != nil {
+		if err := os.WriteFile(saveFile, buf.Bytes(), 0666); err != nil {
 			dief("cannot save messages: %v", err)
 		}
 	}
diff --git a/git-codereview/sync.go b/git-codereview/sync.go
index 0555d67..db6650e 100644
--- a/git-codereview/sync.go
+++ b/git-codereview/sync.go
@@ -8,7 +8,6 @@
 	"encoding/json"
 	"flag"
 	"fmt"
-	"io/ioutil"
 	"os"
 	"strings"
 )
@@ -107,7 +106,7 @@
 }
 
 func readSyncBranchStatus() *syncBranchStatus {
-	data, err := ioutil.ReadFile(syncBranchStatusFile())
+	data, err := os.ReadFile(syncBranchStatusFile())
 	if err != nil {
 		dief("cannot sync-branch: reading status: %v", err)
 	}
@@ -124,7 +123,7 @@
 	if err != nil {
 		dief("cannot sync-branch: writing status: %v", err)
 	}
-	if err := ioutil.WriteFile(syncBranchStatusFile(), js, 0666); err != nil {
+	if err := os.WriteFile(syncBranchStatusFile(), js, 0666); err != nil {
 		dief("cannot sync-branch: writing status: %v", err)
 	}
 }
diff --git a/git-codereview/sync_test.go b/git-codereview/sync_test.go
index 1dbd7bf..c073cbf 100644
--- a/git-codereview/sync_test.go
+++ b/git-codereview/sync_test.go
@@ -6,7 +6,6 @@
 
 import (
 	"bytes"
-	"io/ioutil"
 	"os"
 	"path/filepath"
 	"strings"
@@ -269,7 +268,7 @@
 	// server does not default to having a codereview.cfg on main,
 	// but sync-branch requires one.
 	var mainCfg = []byte("branch: main\n")
-	ioutil.WriteFile(filepath.Join(gt.server, "codereview.cfg"), mainCfg, 0666)
+	os.WriteFile(filepath.Join(gt.server, "codereview.cfg"), mainCfg, 0666)
 	trun(t, gt.server, "git", "add", "codereview.cfg")
 	trun(t, gt.server, "git", "commit", "-m", "config for main", "codereview.cfg")
 
@@ -319,7 +318,7 @@
 	)
 	testPrintedStderr(t, "Merge commit created.")
 
-	data, err := ioutil.ReadFile(filepath.Join(gt.client, "codereview.cfg"))
+	data, err := os.ReadFile(filepath.Join(gt.client, "codereview.cfg"))
 	if err != nil {
 		t.Fatal(err)
 	}
diff --git a/git-codereview/util_test.go b/git-codereview/util_test.go
index 2d3bebe..931d017 100644
--- a/git-codereview/util_test.go
+++ b/git-codereview/util_test.go
@@ -8,7 +8,6 @@
 	"bytes"
 	"encoding/json"
 	"fmt"
-	"io/ioutil"
 	"net"
 	"net/http"
 	"os"
@@ -132,7 +131,7 @@
 		t.Skipf("cannot find git in path: %v", err)
 	}
 
-	tmpdir, err := ioutil.TempDir("", "git-codereview-test")
+	tmpdir, err := os.MkdirTemp("", "git-codereview-test")
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -246,14 +245,14 @@
 }
 
 func write(t *testing.T, file, data string, perm os.FileMode) {
-	if err := ioutil.WriteFile(file, []byte(data), perm); err != nil {
+	if err := os.WriteFile(file, []byte(data), perm); err != nil {
 		t.Helper()
 		t.Fatal(err)
 	}
 }
 
 func read(t *testing.T, file string) []byte {
-	b, err := ioutil.ReadFile(file)
+	b, err := os.ReadFile(file)
 	if err != nil {
 		t.Helper()
 		t.Fatal(err)