| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"io/ioutil" |
| 6 |
"os" |
| 7 |
"path/filepath" |
| 8 |
"time" |
| 9 |
|
| 10 |
"github.com/go-git/go-git/v5" |
| 11 |
. "github.com/go-git/go-git/v5/_examples" |
| 12 |
"github.com/go-git/go-git/v5/plumbing/object" |
| 13 |
) |
| 14 |
|
| 15 |
func prepareRepo(w *git.Worktree, directory string) { |
| 16 |
// We need a known state of files inside the worktree for testing revert a modify and delete |
| 17 |
Info("echo \"hello world! Modify\" > for-modify") |
| 18 |
err := ioutil.WriteFile(filepath.Join(directory, "for-modify"), []byte("hello world! Modify"), 0644) |
| 19 |
CheckIfError(err) |
| 20 |
Info("git add for-modify") |
| 21 |
_, err = w.Add("for-modify") |
| 22 |
CheckIfError(err) |
| 23 |
|
| 24 |
Info("echo \"hello world! Delete\" > for-delete") |
| 25 |
err = ioutil.WriteFile(filepath.Join(directory, "for-delete"), []byte("hello world! Delete"), 0644) |
| 26 |
CheckIfError(err) |
| 27 |
Info("git add for-delete") |
| 28 |
_, err = w.Add("for-delete") |
| 29 |
CheckIfError(err) |
| 30 |
|
| 31 |
Info("git commit -m \"example go-git commit\"") |
| 32 |
_, err = w.Commit("example go-git commit", &git.CommitOptions{ |
| 33 |
Author: &object.Signature{ |
| 34 |
Name: "John Doe", |
| 35 |
Email: "[email protected]", |
| 36 |
When: time.Now(), |
| 37 |
}, |
| 38 |
}) |
| 39 |
CheckIfError(err) |
| 40 |
} |
| 41 |
|
| 42 |
// An example of how to restore AKA unstage files |
| 43 |
func main() { |
| 44 |
CheckArgs("<directory>") |
| 45 |
directory := os.Args[1] |
| 46 |
|
| 47 |
// Opens an already existing repository. |
| 48 |
r, err := git.PlainOpen(directory) |
| 49 |
CheckIfError(err) |
| 50 |
|
| 51 |
w, err := r.Worktree() |
| 52 |
CheckIfError(err) |
| 53 |
|
| 54 |
prepareRepo(w, directory) |
| 55 |
|
| 56 |
// Perform the operation and stage them |
| 57 |
Info("echo \"hello world! Modify 2\" > for-modify") |
| 58 |
err = ioutil.WriteFile(filepath.Join(directory, "for-modify"), []byte("hello world! Modify 2"), 0644) |
| 59 |
CheckIfError(err) |
| 60 |
Info("git add for-modify") |
| 61 |
_, err = w.Add("for-modify") |
| 62 |
CheckIfError(err) |
| 63 |
|
| 64 |
Info("echo \"hello world! Add\" > for-add") |
| 65 |
err = ioutil.WriteFile(filepath.Join(directory, "for-add"), []byte("hello world! Add"), 0644) |
| 66 |
CheckIfError(err) |
| 67 |
Info("git add for-add") |
| 68 |
_, err = w.Add("for-add") |
| 69 |
CheckIfError(err) |
| 70 |
|
| 71 |
Info("rm for-delete") |
| 72 |
err = os.Remove(filepath.Join(directory, "for-delete")) |
| 73 |
CheckIfError(err) |
| 74 |
Info("git add for-delete") |
| 75 |
_, err = w.Add("for-delete") |
| 76 |
CheckIfError(err) |
| 77 |
|
| 78 |
// We can verify the current status of the worktree using the method Status. |
| 79 |
Info("git status --porcelain") |
| 80 |
status, err := w.Status() |
| 81 |
CheckIfError(err) |
| 82 |
fmt.Println(status) |
| 83 |
|
| 84 |
// Unstage a single file and see the status |
| 85 |
Info("git restore --staged for-modify") |
| 86 |
err = w.Restore(&git.RestoreOptions{Staged: true, Files: []string{"for-modify"}}) |
| 87 |
CheckIfError(err) |
| 88 |
|
| 89 |
Info("git status --porcelain") |
| 90 |
status, err = w.Status() |
| 91 |
CheckIfError(err) |
| 92 |
fmt.Println(status) |
| 93 |
|
| 94 |
// Unstage the other 2 files and see the status |
| 95 |
Info("git restore --staged for-add for-delete") |
| 96 |
err = w.Restore(&git.RestoreOptions{Staged: true, Files: []string{"for-add", "for-delete"}}) |
| 97 |
CheckIfError(err) |
| 98 |
|
| 99 |
Info("git status --porcelain") |
| 100 |
status, err = w.Status() |
| 101 |
CheckIfError(err) |
| 102 |
fmt.Println(status) |
| 103 |
} |