-
Notifications
You must be signed in to change notification settings - Fork 128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add stash command #101
Open
aymanbagabas
wants to merge
3
commits into
gogs:master
Choose a base branch
from
aymanbagabas:stash
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package git | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
// Stash represents a stash in the repository. | ||
type Stash struct { | ||
// Index is the index of the stash. | ||
Index int | ||
// Message is the message of the stash. | ||
Message string | ||
// Files is the list of files in the stash. | ||
Files []string | ||
} | ||
|
||
// StashListOptions describes the options for the StashList function. | ||
type StashListOptions struct { | ||
// CommandOptions describes the options for the command. | ||
CommandOptions | ||
} | ||
|
||
var stashLineRegexp = regexp.MustCompile(`^stash@\{(\d+)\}: (.*)$`) | ||
|
||
// StashList returns a list of stashes in the repository. | ||
// This must be run in a work tree. | ||
func (r *Repository) StashList(opts ...StashListOptions) ([]*Stash, error) { | ||
var opt StashListOptions | ||
if len(opts) > 0 { | ||
opt = opts[0] | ||
} | ||
|
||
stashes := make([]*Stash, 0) | ||
cmd := NewCommand("stash", "list", "--name-only").AddOptions(opt.CommandOptions) | ||
stdout, stderr := new(bytes.Buffer), new(bytes.Buffer) | ||
if err := cmd.RunInDirPipeline(stdout, stderr, r.path); err != nil { | ||
return nil, concatenateError(err, stderr.String()) | ||
} | ||
|
||
var stash *Stash | ||
lines := strings.Split(stdout.String(), "\n") | ||
for i := 0; i < len(lines); i++ { | ||
line := strings.TrimSpace(lines[i]) | ||
// Init entry | ||
if match := stashLineRegexp.FindStringSubmatch(line); len(match) == 3 { | ||
// Append the previous stash | ||
if stash != nil { | ||
stashes = append(stashes, stash) | ||
} | ||
|
||
idx, err := strconv.Atoi(match[1]) | ||
if err != nil { | ||
idx = -1 | ||
} | ||
stash = &Stash{ | ||
Index: idx, | ||
Message: match[2], | ||
Files: make([]string, 0), | ||
} | ||
} else if stash != nil && line != "" { | ||
stash.Files = append(stash.Files, line) | ||
} | ||
} | ||
|
||
// Append the last stash | ||
if stash != nil { | ||
stashes = append(stashes, stash) | ||
} | ||
return stashes, nil | ||
} | ||
|
||
// StashDiff returns a parsed diff object for the given stash index. | ||
// This must be run in a work tree. | ||
func (r *Repository) StashDiff(index int, maxFiles, maxFileLines, maxLineChars int, opts ...DiffOptions) (*Diff, error) { | ||
var opt DiffOptions | ||
if len(opts) > 0 { | ||
opt = opts[0] | ||
} | ||
|
||
cmd := NewCommand("stash", "show", "-p", "--full-index", "-M", strconv.Itoa(index)).AddOptions(opt.CommandOptions) | ||
stdout, w := io.Pipe() | ||
done := make(chan SteamParseDiffResult) | ||
go StreamParseDiff(stdout, done, maxFiles, maxFileLines, maxLineChars) | ||
|
||
stderr := new(bytes.Buffer) | ||
err := cmd.RunInDirPipeline(w, stderr, r.path) | ||
_ = w.Close() // Close writer to exit parsing goroutine | ||
if err != nil { | ||
return nil, concatenateError(err, stderr.String()) | ||
} | ||
|
||
result := <-done | ||
return result.Diff, result.Err | ||
} | ||
|
||
// StashPushOptions describes the options for the StashPush function. | ||
type StashPushOptions struct { | ||
// CommandOptions describes the options for the command. | ||
CommandOptions | ||
} | ||
|
||
// StashPush pushes the current worktree to the stash. | ||
// This must be run in a work tree. | ||
func (r *Repository) StashPush(msg string, opts ...StashPushOptions) error { | ||
var opt StashPushOptions | ||
if len(opts) > 0 { | ||
opt = opts[0] | ||
} | ||
|
||
cmd := NewCommand("stash", "push") | ||
if msg != "" { | ||
cmd.AddArgs("-m", msg) | ||
} | ||
cmd.AddOptions(opt.CommandOptions) | ||
|
||
_, err := cmd.RunInDir(r.path) | ||
return err | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you convert all |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
package git | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestStashWorktreeError(t *testing.T) { | ||
_, err := testrepo.StashList() | ||
if err == nil { | ||
t.Errorf("StashList() error = %v, wantErr %v", err, true) | ||
return | ||
} | ||
} | ||
|
||
func TestStash(t *testing.T) { | ||
tmp := t.TempDir() | ||
path, err := filepath.Abs(repoPath) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if err := Clone("file://"+path, tmp); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
repo, err := Open(tmp) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if err := os.WriteFile(tmp+"/resources/newfile", []byte("hello, world!"), 0o644); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
f, err := os.OpenFile(tmp+"/README.txt", os.O_APPEND|os.O_WRONLY, 0o644) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if _, err := f.WriteString("\n\ngit-module"); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
f.Close() | ||
if err := repo.Add(AddOptions{ | ||
All: true, | ||
}); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if err := repo.StashPush(""); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
f, err = os.OpenFile(tmp+"/README.txt", os.O_APPEND|os.O_WRONLY, 0o644) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if _, err := f.WriteString("\n\nstash 1"); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
f.Close() | ||
if err := repo.Add(AddOptions{ | ||
All: true, | ||
}); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if err := repo.StashPush("custom message"); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
want := []*Stash{ | ||
{ | ||
Index: 0, | ||
Message: "On master: custom message", | ||
Files: []string{"README.txt"}, | ||
}, | ||
{ | ||
Index: 1, | ||
Message: "WIP on master: cfc3b29 Add files with same SHA", | ||
Files: []string{"README.txt", "resources/newfile"}, | ||
}, | ||
} | ||
|
||
stash, err := repo.StashList(StashListOptions{ | ||
CommandOptions: CommandOptions{ | ||
Envs: []string{"GIT_CONFIG_GLOBAL=/dev/null"}, | ||
}, | ||
}) | ||
require.NoError(t, err) | ||
require.Equalf(t, want, stash, "StashList() got = %v, want %v", stash, want) | ||
|
||
wantDiff := &Diff{ | ||
totalAdditions: 4, | ||
totalDeletions: 0, | ||
isIncomplete: false, | ||
Files: []*DiffFile{ | ||
{ | ||
Name: "README.txt", | ||
Type: DiffFileChange, | ||
Index: "72e29aca01368bc0aca5d599c31fa8705b11787d", | ||
OldIndex: "adfd6da3c0a3fb038393144becbf37f14f780087", | ||
Sections: []*DiffSection{ | ||
{ | ||
Lines: []*DiffLine{ | ||
{ | ||
Type: DiffLineSection, | ||
Content: `@@ -13,3 +13,6 @@ As a quick reminder, this came from one of three locations in either SSH, Git, o`, | ||
}, | ||
{ | ||
Type: DiffLinePlain, | ||
Content: " We can, as an example effort, even modify this README and change it as if it were source code for the purposes of the class.", | ||
LeftLine: 13, | ||
RightLine: 13, | ||
}, | ||
{ | ||
Type: DiffLinePlain, | ||
Content: " ", | ||
LeftLine: 14, | ||
RightLine: 14, | ||
}, | ||
{ | ||
Type: DiffLinePlain, | ||
Content: " This demo also includes an image with changes on a branch for examination of image diff on GitHub.", | ||
LeftLine: 15, | ||
RightLine: 15, | ||
}, | ||
{ | ||
Type: DiffLineAdd, | ||
Content: "+", | ||
LeftLine: 0, | ||
RightLine: 16, | ||
}, | ||
{ | ||
Type: DiffLineAdd, | ||
Content: "+", | ||
LeftLine: 0, | ||
RightLine: 17, | ||
}, | ||
{ | ||
Type: DiffLineAdd, | ||
Content: "+git-module", | ||
LeftLine: 0, | ||
RightLine: 18, | ||
}, | ||
}, | ||
numAdditions: 3, | ||
numDeletions: 0, | ||
}, | ||
}, | ||
numAdditions: 3, | ||
numDeletions: 0, | ||
oldName: "README.txt", | ||
mode: 0o100644, | ||
oldMode: 0o100644, | ||
isBinary: false, | ||
isSubmodule: false, | ||
isIncomplete: false, | ||
}, | ||
{ | ||
Name: "resources/newfile", | ||
Type: DiffFileAdd, | ||
Index: "30f51a3fba5274d53522d0f19748456974647b4f", | ||
OldIndex: "0000000000000000000000000000000000000000", | ||
Sections: []*DiffSection{ | ||
{ | ||
Lines: []*DiffLine{ | ||
{ | ||
Type: DiffLineSection, | ||
Content: "@@ -0,0 +1 @@", | ||
}, | ||
{ | ||
Type: DiffLineAdd, | ||
Content: "+hello, world!", | ||
LeftLine: 0, | ||
RightLine: 1, | ||
}, | ||
}, | ||
numAdditions: 1, | ||
numDeletions: 0, | ||
}, | ||
}, | ||
numAdditions: 1, | ||
numDeletions: 0, | ||
oldName: "resources/newfile", | ||
mode: 0o100644, | ||
oldMode: 0o100644, | ||
isBinary: false, | ||
isSubmodule: false, | ||
isIncomplete: false, | ||
}, | ||
}, | ||
} | ||
|
||
diff, err := repo.StashDiff(want[1].Index, 0, 0, 0, DiffOptions{ | ||
CommandOptions: CommandOptions{ | ||
Envs: []string{"GIT_CONFIG_GLOBAL=/dev/null"}, | ||
}, | ||
}) | ||
require.NoError(t, err) | ||
require.Equalf(t, wantDiff, diff, "StashDiff() got = %v, want %v", diff, wantDiff) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does this mean to have
Index == -1
? Should we ignore the line instead?