mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-07-28 16:02:01 +03:00
Restrucure project in a way where it is more modular
This commit is contained in:
71
pkg/commands/git.go
Normal file
71
pkg/commands/git.go
Normal file
@ -0,0 +1,71 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/Sirupsen/logrus"
|
||||
git "gopkg.in/src-d/go-git.v4"
|
||||
)
|
||||
|
||||
// GitCommand is our main git interface
|
||||
type GitCommand struct {
|
||||
Log *logrus.Logger
|
||||
OSCommand *OSCommand
|
||||
Worktree *git.Worktree
|
||||
Repo *git.Repository
|
||||
}
|
||||
|
||||
// NewGitCommand it runs git commands
|
||||
func NewGitCommand(log *logrus.Logger, osCommand *OSCommand) (*GitCommand, error) {
|
||||
gitCommand := &GitCommand{
|
||||
Log: log,
|
||||
OSCommand: osCommand,
|
||||
}
|
||||
return gitCommand, nil
|
||||
}
|
||||
|
||||
// SetupGit sets git repo up
|
||||
func (c *GitCommand) SetupGit() {
|
||||
c.verifyInGitRepo()
|
||||
c.navigateToRepoRootDirectory()
|
||||
c.setupWorktree()
|
||||
}
|
||||
|
||||
func (c *GitCommand) GitIgnore(filename string) {
|
||||
if _, err := c.OSCommand.RunDirectCommand("echo '" + filename + "' >> .gitignore"); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *GitCommand) verifyInGitRepo() {
|
||||
if output, err := c.OSCommand.RunCommand("git status"); err != nil {
|
||||
fmt.Println(output)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *GitCommand) navigateToRepoRootDirectory() {
|
||||
_, err := os.Stat(".git")
|
||||
for os.IsNotExist(err) {
|
||||
c.Log.Debug("going up a directory to find the root")
|
||||
os.Chdir("..")
|
||||
_, err = os.Stat(".git")
|
||||
}
|
||||
}
|
||||
|
||||
func (c *GitCommand) setupWorktree() {
|
||||
var err error
|
||||
r, err := git.PlainOpen(".")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
c.Repo = r
|
||||
|
||||
w, err := r.Worktree()
|
||||
c.Worktree = w
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
c.Worktree = w
|
||||
}
|
Reference in New Issue
Block a user