Skip to content

Commit 2d184d3

Browse files
authored
Feat extend git functions (#17)
* add stashing and rename branch Signed-off-by: Ayush <mail@ayuch.dev> * implement: - new branch feature - amend commit feature fix: - errors with stashing changes - esc, q and arrow keys not working in help view Signed-off-by: Ayush <mail@ayuch.dev> --------- Signed-off-by: Ayush <mail@ayuch.dev>
1 parent 02b93f0 commit 2d184d3

File tree

7 files changed

+346
-87
lines changed

7 files changed

+346
-87
lines changed

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ4pzQ=
2+
github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE=
13
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
24
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
35
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=

internal/git/stage.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,18 @@ func (g *GitCommands) Revert(commitHash string) (string, error) {
128128

129129
return string(output), nil
130130
}
131+
132+
// ResetToCommit resets the current HEAD to the specified commit.
133+
func (g *GitCommands) ResetToCommit(commitHash string) (string, error) {
134+
if commitHash == "" {
135+
return "", fmt.Errorf("commit hash is required")
136+
}
137+
138+
cmd := exec.Command("git", "reset", "--hard", commitHash)
139+
output, err := cmd.CombinedOutput()
140+
if err != nil {
141+
return string(output), fmt.Errorf("failed to reset to commit: %v", err)
142+
}
143+
144+
return string(output), nil
145+
}

internal/git/stash.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,13 @@ func (g *GitCommands) Stash(options StashOptions) (string, error) {
107107

108108
return string(output), nil
109109
}
110+
111+
// StashAll stashes all changes, including untracked files.
112+
func (g *GitCommands) StashAll() (string, error) {
113+
cmd := exec.Command("git", "stash", "push", "-u", "-m", "gitx auto stash")
114+
output, err := cmd.CombinedOutput()
115+
if err != nil {
116+
return string(output), fmt.Errorf("failed to stash all changes: %v", err)
117+
}
118+
return string(output), nil
119+
}

internal/tui/keys.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ type KeyMap struct {
2929
StageItem key.Binding
3030
StageAll key.Binding
3131
Discard key.Binding
32-
Reset key.Binding
3332
Stash key.Binding
3433
StashAll key.Binding
3534
Commit key.Binding
@@ -73,7 +72,7 @@ func (k KeyMap) FullHelp() []HelpSection {
7372
Title: "Files",
7473
Bindings: []key.Binding{
7574
k.Commit, k.Stash, k.StashAll, k.StageItem,
76-
k.StageAll, k.Discard, k.Reset,
75+
k.StageAll, k.Discard,
7776
},
7877
},
7978
{
@@ -211,17 +210,13 @@ func DefaultKeyMap() KeyMap {
211210
key.WithKeys("d"),
212211
key.WithHelp("d", "Discard"),
213212
),
214-
Reset: key.NewBinding(
215-
key.WithKeys("D"),
216-
key.WithHelp("D", "Reset"),
217-
),
218213
Stash: key.NewBinding(
219214
key.WithKeys("s"),
220215
key.WithHelp("s", "Stash"),
221216
),
222217
StashAll: key.NewBinding(
223218
key.WithKeys("S"),
224-
key.WithHelp("S", "Stage all"),
219+
key.WithHelp("S", "Stash all"),
225220
),
226221
Commit: key.NewBinding(
227222
key.WithKeys("c"),

internal/tui/model.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package tui
33
import (
44
"github.com/charmbracelet/bubbles/help"
55
"github.com/charmbracelet/bubbles/key"
6+
"github.com/charmbracelet/bubbles/textarea"
67
"github.com/charmbracelet/bubbles/textinput"
78
"github.com/charmbracelet/bubbles/viewport"
89
tea "github.com/charmbracelet/bubbletea"
@@ -16,6 +17,7 @@ const (
1617
modeNormal appMode = iota
1718
modeInput
1819
modeConfirm
20+
modeCommit
1921
)
2022

2123
// Model represents the state of the TUI.
@@ -37,12 +39,14 @@ type Model struct {
3739
repoName string
3840
branchName string
3941
// New fields for pop-ups
40-
mode appMode
41-
promptTitle string
42-
confirmMessage string
43-
textInput textinput.Model
44-
inputCallback func(string) tea.Cmd
45-
confirmCallback func(bool) tea.Cmd
42+
mode appMode
43+
promptTitle string
44+
confirmMessage string
45+
textInput textinput.Model
46+
descriptionInput textarea.Model
47+
inputCallback func(string) tea.Cmd
48+
commitCallback func(title, description string) tea.Cmd
49+
confirmCallback func(bool) tea.Cmd
4650
}
4751

4852
// initialModel creates the initial state of the application.
@@ -65,7 +69,12 @@ func initialModel() Model {
6569
ti := textinput.New()
6670
ti.Focus()
6771
ti.CharLimit = 256
68-
ti.Width = 50
72+
ti.Width = 80
73+
74+
ta := textarea.New()
75+
ta.Placeholder = "Enter commit description"
76+
ta.SetWidth(80)
77+
ta.SetHeight(5)
6978

7079
return Model{
7180
theme: Themes[themeNames[0]],
@@ -82,6 +91,7 @@ func initialModel() Model {
8291
panels: panels,
8392
mode: modeNormal,
8493
textInput: ti,
94+
descriptionInput: ta,
8595
}
8696
}
8797

0 commit comments

Comments
 (0)