Skip to content

Commit 98d74e6

Browse files
committed
organise git commands
1 parent 8043c66 commit 98d74e6

File tree

5 files changed

+59
-52
lines changed

5 files changed

+59
-52
lines changed

.git-tools-2.json

Whitespace-only changes.

cmd/checkout/git.go

-24
This file was deleted.

cmd/checkout/main.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/charmbracelet/bubbles/textinput"
77
tea "github.com/charmbracelet/bubbletea"
88
"github.com/charmbracelet/lipgloss"
9+
"github.com/mrados7/go-git-commands/cmd/git"
910
"os"
1011
)
1112

@@ -120,7 +121,7 @@ func (m model) UpdateBranchNameInput(msg tea.Msg) (tea.Model, tea.Cmd) {
120121
case tea.KeyCtrlC, tea.KeyEsc:
121122
return m, tea.Quit
122123
case tea.KeyEnter:
123-
err := checkoutNewBranch(getBranchName(m))
124+
err := git.CheckoutNewBranch(getBranchName(m))
124125
if err != nil {
125126
fmt.Println(err)
126127
os.Exit(1)
@@ -235,7 +236,7 @@ func getBranchName(m model) string {
235236
}
236237

237238
func main() {
238-
isGitRepo := checkIfGitRepo()
239+
isGitRepo := git.CheckIfGitRepo()
239240
if !isGitRepo {
240241
fail("Error: not a git repository")
241242
}

cmd/commit/main.go

+7-26
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"fmt"
55
"github.com/charmbracelet/lipgloss"
6+
"github.com/mrados7/go-git-commands/cmd/git"
67
"log"
78
"os"
89
"os/exec"
@@ -35,19 +36,23 @@ type model struct {
3536
}
3637

3738
func main() {
39+
isGitRepo := git.CheckIfGitRepo()
40+
if !isGitRepo {
41+
log.Fatal("Not a git repository")
42+
}
3843
p := tea.NewProgram(initialModel())
3944
if _, err := p.Run(); err != nil {
4045
log.Fatal(err)
4146
}
4247
}
4348

4449
func initialModel() model {
45-
branch, err := getCurrentGitBranch()
50+
branch, err := git.GetCurrentGitBranch()
4651
if err != nil {
4752
log.Fatal(err)
4853
}
4954

50-
stagedFiles := getStagedFiles()
55+
stagedFiles := git.GetStagedFiles()
5156

5257
if len(stagedFiles) == 0 {
5358
log.Fatal("No staged files found")
@@ -107,16 +112,6 @@ func initialModel() model {
107112
return m
108113
}
109114

110-
func getCurrentGitBranch() (string, error) {
111-
cmd := exec.Command("git", "branch", "--show-current")
112-
output, err := cmd.CombinedOutput()
113-
if err != nil {
114-
return "", fmt.Errorf("error getting branch name: %v", err)
115-
}
116-
117-
return strings.TrimSpace(string(output)), nil
118-
}
119-
120115
func (m model) Init() tea.Cmd {
121116
return textinput.Blink
122117
}
@@ -239,17 +234,3 @@ func (m model) View() string {
239234

240235
return b.String()
241236
}
242-
243-
func getStagedFiles() []string {
244-
cmd := exec.Command("git", "diff", "--name-only", "--cached")
245-
out, err := cmd.Output()
246-
if err != nil {
247-
return []string{}
248-
}
249-
250-
if len(out) == 0 {
251-
return []string{}
252-
}
253-
254-
return strings.Split(string(out), "\n")
255-
}

cmd/git/git.go

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package git
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"os/exec"
7+
"strings"
8+
)
9+
10+
func CheckoutNewBranch(branchName string) error {
11+
cmd := exec.Command("git", "checkout", "-b", branchName)
12+
cmd.Stdout = os.Stdout
13+
cmd.Stderr = os.Stderr
14+
return cmd.Run()
15+
}
16+
17+
func CheckIfGitRepo() bool {
18+
cmd := exec.Command("git", "rev-parse", "--is-inside-work-tree")
19+
cmd.Stderr = os.Stderr
20+
err := cmd.Run()
21+
if err != nil {
22+
return false
23+
}
24+
return true
25+
}
26+
27+
func GetStagedFiles() []string {
28+
cmd := exec.Command("git", "diff", "--name-only", "--cached")
29+
out, err := cmd.Output()
30+
if err != nil {
31+
return []string{}
32+
}
33+
34+
if len(out) == 0 {
35+
return []string{}
36+
}
37+
38+
return strings.Split(string(out), "\n")
39+
}
40+
41+
func GetCurrentGitBranch() (string, error) {
42+
cmd := exec.Command("git", "branch", "--show-current")
43+
output, err := cmd.CombinedOutput()
44+
if err != nil {
45+
return "", fmt.Errorf("error getting branch name: %v", err)
46+
}
47+
48+
return strings.TrimSpace(string(output)), nil
49+
}

0 commit comments

Comments
 (0)