aboutsummaryrefslogtreecommitdiffstats
path: root/_examples
diff options
context:
space:
mode:
authorPaulo Gomes <pjbgf@linux.com>2024-08-21 09:06:44 +0000
committerGitHub <noreply@github.com>2024-08-21 09:06:44 +0000
commit6d583524d3e1d79c171d4666eee3e1d174c210d0 (patch)
tree624a858e7b6b2bfcfbc58b16a0af590bdd94a397 /_examples
parent4fd9979d5c2940e72bdd6946fec21e02d959f0f6 (diff)
parent0f3639790292f843a2577d97df1f0c3665e06501 (diff)
downloadgo-git-6d583524d3e1d79c171d4666eee3e1d174c210d0.tar.gz
Merge pull request #493 from openmetagame/git-restore-for-pr-2022
Add RestoreStaged to Worktree that mimics the behaviour of git restore --staged <file>...
Diffstat (limited to '_examples')
-rw-r--r--_examples/common_test.go1
-rw-r--r--_examples/restore/main.go103
2 files changed, 104 insertions, 0 deletions
diff --git a/_examples/common_test.go b/_examples/common_test.go
index 5e3f753..7b6dbfb 100644
--- a/_examples/common_test.go
+++ b/_examples/common_test.go
@@ -30,6 +30,7 @@ var args = map[string][]string{
"progress": {defaultURL, tempFolder()},
"pull": {createRepositoryWithRemote(tempFolder(), defaultURL)},
"push": {setEmptyRemote(cloneRepository(defaultURL, tempFolder()))},
+ "restore": {cloneRepository(defaultURL, tempFolder())},
"revision": {cloneRepository(defaultURL, tempFolder()), "master~2^"},
"sha256": {tempFolder()},
"showcase": {defaultURL, tempFolder()},
diff --git a/_examples/restore/main.go b/_examples/restore/main.go
new file mode 100644
index 0000000..8016b06
--- /dev/null
+++ b/_examples/restore/main.go
@@ -0,0 +1,103 @@
+package main
+
+import (
+ "fmt"
+ "io/ioutil"
+ "os"
+ "path/filepath"
+ "time"
+
+ "github.com/go-git/go-git/v5"
+ . "github.com/go-git/go-git/v5/_examples"
+ "github.com/go-git/go-git/v5/plumbing/object"
+)
+
+func prepareRepo(w *git.Worktree, directory string) {
+ // We need a known state of files inside the worktree for testing revert a modify and delete
+ Info("echo \"hello world! Modify\" > for-modify")
+ err := ioutil.WriteFile(filepath.Join(directory, "for-modify"), []byte("hello world! Modify"), 0644)
+ CheckIfError(err)
+ Info("git add for-modify")
+ _, err = w.Add("for-modify")
+ CheckIfError(err)
+
+ Info("echo \"hello world! Delete\" > for-delete")
+ err = ioutil.WriteFile(filepath.Join(directory, "for-delete"), []byte("hello world! Delete"), 0644)
+ CheckIfError(err)
+ Info("git add for-delete")
+ _, err = w.Add("for-delete")
+ CheckIfError(err)
+
+ Info("git commit -m \"example go-git commit\"")
+ _, err = w.Commit("example go-git commit", &git.CommitOptions{
+ Author: &object.Signature{
+ Name: "John Doe",
+ Email: "john@doe.org",
+ When: time.Now(),
+ },
+ })
+ CheckIfError(err)
+}
+
+// An example of how to restore AKA unstage files
+func main() {
+ CheckArgs("<directory>")
+ directory := os.Args[1]
+
+ // Opens an already existing repository.
+ r, err := git.PlainOpen(directory)
+ CheckIfError(err)
+
+ w, err := r.Worktree()
+ CheckIfError(err)
+
+ prepareRepo(w, directory)
+
+ // Perform the operation and stage them
+ Info("echo \"hello world! Modify 2\" > for-modify")
+ err = ioutil.WriteFile(filepath.Join(directory, "for-modify"), []byte("hello world! Modify 2"), 0644)
+ CheckIfError(err)
+ Info("git add for-modify")
+ _, err = w.Add("for-modify")
+ CheckIfError(err)
+
+ Info("echo \"hello world! Add\" > for-add")
+ err = ioutil.WriteFile(filepath.Join(directory, "for-add"), []byte("hello world! Add"), 0644)
+ CheckIfError(err)
+ Info("git add for-add")
+ _, err = w.Add("for-add")
+ CheckIfError(err)
+
+ Info("rm for-delete")
+ err = os.Remove(filepath.Join(directory, "for-delete"))
+ CheckIfError(err)
+ Info("git add for-delete")
+ _, err = w.Add("for-delete")
+ CheckIfError(err)
+
+ // We can verify the current status of the worktree using the method Status.
+ Info("git status --porcelain")
+ status, err := w.Status()
+ CheckIfError(err)
+ fmt.Println(status)
+
+ // Unstage a single file and see the status
+ Info("git restore --staged for-modify")
+ err = w.Restore(&git.RestoreOptions{Staged: true, Files: []string{"for-modify"}})
+ CheckIfError(err)
+
+ Info("git status --porcelain")
+ status, err = w.Status()
+ CheckIfError(err)
+ fmt.Println(status)
+
+ // Unstage the other 2 files and see the status
+ Info("git restore --staged for-add for-delete")
+ err = w.Restore(&git.RestoreOptions{Staged: true, Files: []string{"for-add", "for-delete"}})
+ CheckIfError(err)
+
+ Info("git status --porcelain")
+ status, err = w.Status()
+ CheckIfError(err)
+ fmt.Println(status)
+}