Skip to content

Commit

Permalink
minor fixup logic
Browse files Browse the repository at this point in the history
  • Loading branch information
unknwon committed Sep 30, 2024
1 parent 8a91a13 commit aa89c50
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
- name: Checkout code
uses: actions/checkout@v4
- name: Install Go
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version: 1.23.x
- name: Check Go module tidiness
Expand Down
8 changes: 4 additions & 4 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ func (c *Command) AddEnvs(envs ...string) *Command {
}

// WithContext returns a new Command with the given context.
func (c Command) WithContext(ctx context.Context) *Command {
func (c *Command) WithContext(ctx context.Context) *Command {
c.ctx = ctx
return &c
return c
}

// WithTimeout returns a new Command with given timeout.
func (c Command) WithTimeout(timeout time.Duration) *Command {
func (c *Command) WithTimeout(timeout time.Duration) *Command {
c.timeout = timeout
return &c
return c
}

// SetTimeout sets the timeout for the command.
Expand Down
29 changes: 13 additions & 16 deletions repo_stash.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ import (
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
}
Expand All @@ -36,44 +34,43 @@ func (r *Repository) StashList(opts ...StashListOptions) ([]*Stash, error) {
opt = opts[0]
}

stash := make([]*Stash, 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 entry *Stash
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 {
if entry != nil {
stash = append(stash, entry)
// Append the previous stash
if stash != nil {
stashes = append(stashes, stash)
}

idx, err := strconv.Atoi(match[1])
if err != nil {
idx = -1
}
entry = &Stash{
stash = &Stash{
Index: idx,
Message: match[2],
Files: make([]string, 0),
}
} else if entry != nil && line != "" {
entry.Files = append(entry.Files, line)
} else {
continue
} else if stash != nil && line != "" {
stash.Files = append(stash.Files, line)
}
}

if entry != nil {
stash = append(stash, entry)
// Append the last stash
if stash != nil {
stashes = append(stashes, stash)
}

return stash, nil
return stashes, nil
}

// StashDiff returns a parsed diff object for the given stash index.
Expand All @@ -90,7 +87,7 @@ func (r *Repository) StashDiff(index int, maxFiles, maxFileLines, maxLineChars i
go StreamParseDiff(stdout, done, maxFiles, maxFileLines, maxLineChars)

stderr := new(bytes.Buffer)
err := cmd.RunInDirPipelineWithTimeout(opt.Timeout, w, stderr, r.path)
err := cmd.RunInDirPipeline(w, stderr, r.path)
_ = w.Close() // Close writer to exit parsing goroutine
if err != nil {
return nil, concatenateError(err, stderr.String())
Expand Down

0 comments on commit aa89c50

Please sign in to comment.