Skip to content

Commit

Permalink
feat(stash): add command
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Jun 28, 2024
1 parent 099b3aa commit e600790
Show file tree
Hide file tree
Showing 6 changed files with 484 additions and 0 deletions.
10 changes: 10 additions & 0 deletions git/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,16 @@ func LsFilesWithContext(ctx context.Context, subCommand ...types.Option) (string
return command(ctx, "ls-files", subCommand...)
}

// Stash https://git-scm.com/docs/git-stash
func Stash(options ...types.Option) (string, error) {
return command(context.Background(), "stash", options...)
}

// StashWithContext https://git-scm.com/docs/git-stash
func StashWithContext(ctx context.Context, options ...types.Option) (string, error) {
return command(ctx, "stash", options...)
}

// Raw use to execute arbitrary git commands.
func Raw(cmd string, options ...types.Option) (string, error) {
return command(context.Background(), cmd, options...)
Expand Down
155 changes: 155 additions & 0 deletions git/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/ldez/go-git-cmd-wrapper/v2/remote"
"github.com/ldez/go-git-cmd-wrapper/v2/reset"
"github.com/ldez/go-git-cmd-wrapper/v2/revparse"
"github.com/ldez/go-git-cmd-wrapper/v2/stash"
"github.com/ldez/go-git-cmd-wrapper/v2/status"
"github.com/ldez/go-git-cmd-wrapper/v2/tag"
"github.com/ldez/go-git-cmd-wrapper/v2/types"
Expand Down Expand Up @@ -453,6 +454,160 @@ func ExampleNotesWithContext_getRef() {
// Output: git notes get-ref
}

func ExampleStash_push() {
out, _ := git.Stash(stash.Push("foo", stash.All), git.CmdExecutor(cmdExecutorMock))

fmt.Println(out)
// Output: git stash push --all foo
}

func ExampleStash_save() {
out, _ := git.Stash(stash.Save("foo", stash.Patch), git.CmdExecutor(cmdExecutorMock))

fmt.Println(out)
// Output: git stash save --patch foo
}

func ExampleStash_list() {
out, _ := git.Stash(stash.List(), git.CmdExecutor(cmdExecutorMock))

fmt.Println(out)
// Output: git stash list
}

func ExampleStash_show() {
out, _ := git.Stash(stash.Show("stash@{1}", stash.IncludeUntracked), git.CmdExecutor(cmdExecutorMock))

fmt.Println(out)
// Output: git stash show --include-untracked stash@{1}
}

func ExampleStash_pop() {
out, _ := git.Stash(stash.Pop("stash@{1}", stash.Quiet), git.CmdExecutor(cmdExecutorMock))

fmt.Println(out)
// Output: git stash pop --quiet stash@{1}
}

func ExampleStash_apply() {
out, _ := git.Stash(stash.Apply("stash@{1}", stash.Quiet), git.CmdExecutor(cmdExecutorMock))

fmt.Println(out)
// Output: git stash apply --quiet stash@{1}
}

func ExampleStash_branch() {
out, _ := git.Stash(stash.Branch("foo", "stash@{1}"), git.CmdExecutor(cmdExecutorMock))

fmt.Println(out)
// Output: git stash branch foo stash@{1}
}

func ExampleStash_clear() {
out, _ := git.Stash(stash.Clear(), git.CmdExecutor(cmdExecutorMock))

fmt.Println(out)
// Output: git stash clear
}

func ExampleStash_drop() {
out, _ := git.Stash(stash.Drop("stash@{1}", stash.Quiet), git.CmdExecutor(cmdExecutorMock))

fmt.Println(out)
// Output: git stash drop --quiet stash@{1}
}

func ExampleStash_create() {
out, _ := git.Stash(stash.Create(), git.CmdExecutor(cmdExecutorMock))

fmt.Println(out)
// Output: git stash create
}

func ExampleStash_store() {
out, _ := git.Stash(stash.Store(), git.CmdExecutor(cmdExecutorMock))

fmt.Println(out)
// Output: git stash store
}

func ExampleStashWithContext_push() {
out, _ := git.StashWithContext(context.Background(), stash.Push("foo", stash.All), git.CmdExecutor(cmdExecutorMock))

fmt.Println(out)
// Output: git stash push --all foo
}

func ExampleStashWithContext_save() {
out, _ := git.StashWithContext(context.Background(), stash.Save("foo", stash.Patch), git.CmdExecutor(cmdExecutorMock))

fmt.Println(out)
// Output: git stash save --patch foo
}

func ExampleStashWithContext_list() {
out, _ := git.StashWithContext(context.Background(), stash.List(), git.CmdExecutor(cmdExecutorMock))

fmt.Println(out)
// Output: git stash list
}

func ExampleStashWithContext_show() {
out, _ := git.StashWithContext(context.Background(), stash.Show("stash@{1}", stash.IncludeUntracked), git.CmdExecutor(cmdExecutorMock))

fmt.Println(out)
// Output: git stash show --include-untracked stash@{1}
}

func ExampleStashWithContext_pop() {
out, _ := git.StashWithContext(context.Background(), stash.Pop("stash@{1}", stash.Quiet), git.CmdExecutor(cmdExecutorMock))

fmt.Println(out)
// Output: git stash pop --quiet stash@{1}
}

func ExampleStashWithContext_apply() {
out, _ := git.StashWithContext(context.Background(), stash.Apply("stash@{1}", stash.Quiet), git.CmdExecutor(cmdExecutorMock))

fmt.Println(out)
// Output: git stash apply --quiet stash@{1}
}

func ExampleStashWithContext_branch() {
out, _ := git.StashWithContext(context.Background(), stash.Branch("foo", "stash@{1}"), git.CmdExecutor(cmdExecutorMock))

fmt.Println(out)
// Output: git stash branch foo stash@{1}
}

func ExampleStashWithContext_clear() {
out, _ := git.StashWithContext(context.Background(), stash.Clear(), git.CmdExecutor(cmdExecutorMock))

fmt.Println(out)
// Output: git stash clear
}

func ExampleStashWithContext_drop() {
out, _ := git.StashWithContext(context.Background(), stash.Drop("stash@{1}", stash.Quiet), git.CmdExecutor(cmdExecutorMock))

fmt.Println(out)
// Output: git stash drop --quiet stash@{1}
}

func ExampleStashWithContext_create() {
out, _ := git.StashWithContext(context.Background(), stash.Create(), git.CmdExecutor(cmdExecutorMock))

fmt.Println(out)
// Output: git stash create
}

func ExampleStashWithContext_store() {
out, _ := git.StashWithContext(context.Background(), stash.Store(), git.CmdExecutor(cmdExecutorMock))

fmt.Println(out)
// Output: git stash store
}

func ExampleRaw() {
out, _ := git.Raw("stash", git.CmdExecutor(cmdExecutorMock), func(g *types.Cmd) {
g.AddOptions("list")
Expand Down
66 changes: 66 additions & 0 deletions internal/descriptions.json
Original file line number Diff line number Diff line change
Expand Up @@ -2215,6 +2215,72 @@
}
]
},
{
"command_name": "stash",
"enabled": true,
"options": [
{
"argument": "--all",
"arguments": "-a, --all",
"description": "This option is only valid for push and save commands.\nAll ignored and untracked files are also stashed and then cleaned up with git clean."
},
{
"argument": "--include-untracked",
"arguments": "-u, --include-untracked",
"description": "When used with the push and save commands, all untracked files are also stashed and then cleaned up with git clean.\nWhen used with the show command, show the untracked files in the stash entry as part of the diff."
},
{
"argument": "--no-include-untracked",
"arguments": "--no-include-untracked",
"description": "Opposite of --include-untracked."
},
{
"argument": "--only-untracked",
"arguments": "--only-untracked",
"description": "This option is only valid for the show command.\nShow only the untracked files in the stash entry as part of the diff."
},
{
"argument": "--index",
"arguments": "--index",
"description": "This option is only valid for pop and apply commands.\nTries to reinstate not only the working tree’s changes, but also the index’s ones.\nHowever, this can fail, when you have conflicts (which are stored in the index, where you therefore can no longer apply the changes as they were originally)."
},
{
"argument": "--keep-index",
"arguments": "-k, --keep-index",
"description": "This option is only valid for push and save commands.\nAll changes already added to the index are left intact."
},
{
"argument": "--no-keep-index",
"arguments": "--no-keep-index",
"description": "Opposite of --keep-index."
},
{
"argument": "--patch",
"arguments": "-p, --patch",
"description": "This option is only valid for push and save commands.\nInteractively select hunks from the diff between HEAD and the working tree to be stashed.\nThe stash entry is constructed such that its index state is the same as the index state of your repository, and its worktree contains only the changes you selected interactively.\nThe selected changes are then rolled back from your worktree.\nSee the “Interactive Mode” section of git-add(1) to learn how to operate the --patch mode.\nThe --patch option implies --keep-index.\nYou can use --no-keep-index to override this."
},
{
"argument": "--staged",
"arguments": "-S, --staged",
"description": "This option is only valid for push and save commands.\nStash only the changes that are currently staged.\nThis is similar to basic git commit except the state is committed to the stash instead of current branch.\nThe --patch option has priority over this one."
},
{
"argument": "--pathspec-from-file=<file>",
"arguments": "--pathspec-from-file=<file>",
"description": "This option is only valid for push command.\nPathspec is passed in <file> instead of commandline args.\nIf <file> is exactly - then standard input is used. Pathspec elements are separated by LF or CR/LF.\nPathspec elements can be quoted as explained for the configuration variable core.quotePath (see git-config(1)).\nSee also --pathspec-file-nul and global --literal-pathspecs."
},
{
"argument": "--pathspec-file-nul",
"arguments": "--pathspec-file-nul",
"description": "This option is only valid for push command.\nOnly meaningful with --pathspec-from-file. Pathspec elements are separated with NUL character and all other characters are taken literally (including newlines and quotes)."
},
{
"argument": "--quiet",
"arguments": "-q, --quiet",
"description": "This option is only valid for apply, drop, pop, push, save, store commands.\nQuiet, suppress feedback messages."
}
]
},
{
"command_name": "",
"enabled": false,
Expand Down
32 changes: 32 additions & 0 deletions stash/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
Package stash git-stash - Stash the changes in a dirty working directory away.
# SYNOPSIS
Reference: https://git-scm.com/docs/git-stash
git stash list [<log-options>]
git stash show [-u | --include-untracked | --only-untracked] [<diff-options>] [<stash>]
git stash drop [-q | --quiet] [<stash>]
git stash pop [--index] [-q | --quiet] [<stash>]
git stash apply [--index] [-q | --quiet] [<stash>]
git stash branch <branchname> [<stash>]
git stash [push [-p | --patch] [-S | --staged] [-k | --[no-]keep-index] [-q | --quiet]
[-u | --include-untracked] [-a | --all] [(-m | --message) <message>]
[--pathspec-from-file=<file> [--pathspec-file-nul]]
[--] [<pathspec>…​]]
git stash save [-p | --patch] [-S | --staged] [-k | --[no-]keep-index] [-q | --quiet]
[-u | --include-untracked] [-a | --all] [<message>]
git stash clear
git stash create [<message>]
git stash store [(-m | --message) <message>] [-q | --quiet] <commit>
# DESCRIPTION
Use `git stash` when you want to record the current state of the working directory and the index, but want to go back to a clean working directory. The command saves your local modifications away and reverts the working directory to match the `HEAD` commit.
The modifications stashed away by this command can be listed with `git stash list`, inspected with `git stash show`, and restored (potentially on top of a different commit) with `git stash apply`. Calling `git stash` without any arguments is equivalent to `git stash push`. A stash is by default listed as "WIP on branchname …", but you can give a more descriptive message on the command line when you create one.
The latest stash you created is stored in `refs/stash`; older stashes are found in the reflog of this reference and can be named using the usual reflog syntax (e.g. `stash@{0}` is the most recently created stash, `stash@{1}` is the one before it, `stash@{2.hours.ago}` is also possible). Stashes may also be referenced by specifying just the stash index (e.g. the integer `n` is equivalent to `stash@{n}`).
*/
package stash
Loading

0 comments on commit e600790

Please sign in to comment.