mirror of
https://github.com/aljazceru/opencode.git
synced 2025-12-21 09:44:21 +01:00
feat: show git diff in reverted messages
This commit is contained in:
58
packages/tui/internal/components/diff/parse.go
Normal file
58
packages/tui/internal/components/diff/parse.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package diff
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type DiffStats struct {
|
||||
Added int
|
||||
Removed int
|
||||
Modified int
|
||||
}
|
||||
|
||||
func ParseStats(diff string) (map[string]DiffStats, error) {
|
||||
stats := make(map[string]DiffStats)
|
||||
var currentFile string
|
||||
scanner := bufio.NewScanner(strings.NewReader(diff))
|
||||
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
if strings.HasPrefix(line, "---") {
|
||||
continue
|
||||
} else if strings.HasPrefix(line, "+++") {
|
||||
parts := strings.SplitN(line, " ", 2)
|
||||
if len(parts) == 2 {
|
||||
currentFile = strings.TrimPrefix(parts[1], "b/")
|
||||
}
|
||||
continue
|
||||
}
|
||||
if strings.HasPrefix(line, "@@") {
|
||||
continue
|
||||
}
|
||||
if currentFile == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
fileStats := stats[currentFile]
|
||||
switch {
|
||||
case strings.HasPrefix(line, "+"):
|
||||
fileStats.Added++
|
||||
case strings.HasPrefix(line, "-"):
|
||||
fileStats.Removed++
|
||||
}
|
||||
stats[currentFile] = fileStats
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
return nil, fmt.Errorf("error reading diff string: %w", err)
|
||||
}
|
||||
|
||||
for file, fileStats := range stats {
|
||||
fileStats.Modified = fileStats.Added + fileStats.Removed
|
||||
stats[file] = fileStats
|
||||
}
|
||||
|
||||
return stats, nil
|
||||
}
|
||||
Reference in New Issue
Block a user