Skip to content

Commit 76a27f4

Browse files
committed
rename any commit
1 parent adc2529 commit 76a27f4

File tree

7 files changed

+114
-22
lines changed

7 files changed

+114
-22
lines changed

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func main() {
4040
fmt.Printf("%s\n", config.GetDefaultConfig())
4141
os.Exit(0)
4242
}
43-
appConfig, err := config.NewAppConfig("lazygit", version, commit, date, buildSource, debuggingFlag)
43+
appConfig, err := config.NewAppConfig("lazygit", version, commit, date, buildSource, *debuggingFlag)
4444
if err != nil {
4545
log.Fatal(err.Error())
4646
}

pkg/app/app.go

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@ import (
2020
type App struct {
2121
closers []io.Closer
2222

23-
Config config.AppConfigurer
24-
Log *logrus.Entry
25-
OSCommand *commands.OSCommand
26-
GitCommand *commands.GitCommand
27-
Gui *gui.Gui
28-
Tr *i18n.Localizer
29-
Updater *updates.Updater // may only need this on the Gui
23+
Config config.AppConfigurer
24+
Log *logrus.Entry
25+
OSCommand *commands.OSCommand
26+
GitCommand *commands.GitCommand
27+
Gui *gui.Gui
28+
Tr *i18n.Localizer
29+
Updater *updates.Updater // may only need this on the Gui
30+
DemonContext string
3031
}
3132

3233
func newProductionLogger(config config.AppConfigurer) *logrus.Logger {
@@ -54,7 +55,7 @@ func newDevelopmentLogger(config config.AppConfigurer) *logrus.Logger {
5455
func newLogger(config config.AppConfigurer) *logrus.Entry {
5556
var log *logrus.Logger
5657
environment := "production"
57-
if config.GetDebug() {
58+
if config.GetDebug() || os.Getenv("DEBUG") == "TRUE" {
5859
environment = "development"
5960
log = newDevelopmentLogger(config)
6061
} else {
@@ -86,15 +87,21 @@ func NewApp(config config.AppConfigurer) (*App, error) {
8687
}
8788
var err error
8889
app.Log = newLogger(config)
89-
app.OSCommand = commands.NewOSCommand(app.Log, config)
90-
9190
app.Tr = i18n.NewLocalizer(app.Log)
9291

92+
// if we are being called in 'demon' mode, we can just return here
93+
app.DemonContext = os.Getenv("LAZYGIT_CONTEXT")
94+
if app.DemonContext != "" {
95+
return app, nil
96+
}
97+
98+
app.OSCommand = commands.NewOSCommand(app.Log, config)
99+
93100
app.Updater, err = updates.NewUpdater(app.Log, config, app.OSCommand, app.Tr)
94101
if err != nil {
95102
return app, err
96103
}
97-
app.GitCommand, err = commands.NewGitCommand(app.Log, app.OSCommand, app.Tr)
104+
app.GitCommand, err = commands.NewGitCommand(app.Log, app.OSCommand, app.Tr, app.Config)
98105
if err != nil {
99106
return app, err
100107
}
@@ -106,9 +113,21 @@ func NewApp(config config.AppConfigurer) (*App, error) {
106113
}
107114

108115
func (app *App) Run() error {
116+
if app.DemonContext == "INTERACTIVE_REBASE" {
117+
return app.Rebase()
118+
}
119+
109120
return app.Gui.RunWithSubprocesses()
110121
}
111122

123+
func (app *App) Rebase() error {
124+
app.Log.Error("TEST")
125+
126+
ioutil.WriteFile(".git/rebase-merge/git-rebase-todo", []byte(os.Getenv("LAZYGIT_REBASE_TODO")), 0644)
127+
128+
return nil
129+
}
130+
112131
// Close closes any resources
113132
func (app *App) Close() error {
114133
for _, closer := range app.closers {

pkg/commands/git.go

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@ import (
99
"regexp"
1010
"strings"
1111

12+
"github.com/mgutz/str"
13+
1214
"github.com/fatih/color"
1315
"github.com/go-errors/errors"
1416

17+
"github.com/jesseduffield/lazygit/pkg/config"
1518
"github.com/jesseduffield/lazygit/pkg/i18n"
1619
"github.com/jesseduffield/lazygit/pkg/utils"
1720
"github.com/sirupsen/logrus"
@@ -68,13 +71,14 @@ type GitCommand struct {
6871
Worktree *gogit.Worktree
6972
Repo *gogit.Repository
7073
Tr *i18n.Localizer
74+
Config config.AppConfigurer
7175
getGlobalGitConfig func(string) (string, error)
7276
getLocalGitConfig func(string) (string, error)
7377
removeFile func(string) error
7478
}
7579

7680
// NewGitCommand it runs git commands
77-
func NewGitCommand(log *logrus.Entry, osCommand *OSCommand, tr *i18n.Localizer) (*GitCommand, error) {
81+
func NewGitCommand(log *logrus.Entry, osCommand *OSCommand, tr *i18n.Localizer, config config.AppConfigurer) (*GitCommand, error) {
7882
var worktree *gogit.Worktree
7983
var repo *gogit.Repository
8084

@@ -104,6 +108,7 @@ func NewGitCommand(log *logrus.Entry, osCommand *OSCommand, tr *i18n.Localizer)
104108
Tr: tr,
105109
Worktree: worktree,
106110
Repo: repo,
111+
Config: config,
107112
getGlobalGitConfig: gitconfig.Global,
108113
getLocalGitConfig: gitconfig.Local,
109114
removeFile: os.RemoveAll,
@@ -774,3 +779,62 @@ func (c *GitCommand) GenericMerge(commandType string, command string) error {
774779
gitCommand := fmt.Sprintf("git %s %s --%s", c.OSCommand.Platform.skipEditorArg, commandType, command)
775780
return c.OSCommand.RunCommand(gitCommand)
776781
}
782+
783+
func (c *GitCommand) InteractiveRebase(commits []*Commit, index int, action string) (*exec.Cmd, error) {
784+
ex, err := os.Executable() // get the executable path for git to use
785+
if err != nil {
786+
ex = os.Args[0] // fallback to the first call argument if needed
787+
}
788+
789+
// assume for now they won't pick the bottom commit
790+
c.Log.Warn(len(commits))
791+
c.Log.Warn(index)
792+
if len(commits) <= index+1 {
793+
// TODO: support more than say 30 commits and ensure this logic is correct, and i18n
794+
return nil, errors.New("You cannot interactive rebase onto the first commit")
795+
}
796+
797+
todo := ""
798+
for i, commit := range commits[0 : index+1] {
799+
a := "pick"
800+
if i == index {
801+
a = action
802+
}
803+
todo += a + " " + commit.Sha + "\n"
804+
}
805+
806+
debug := "FALSE"
807+
if c.OSCommand.Config.GetDebug() == true {
808+
debug = "TRUE"
809+
}
810+
811+
splitCmd := str.ToArgv(fmt.Sprintf("git rebase --interactive %s", commits[index+1].Sha))
812+
813+
cmd := exec.Command(splitCmd[0], splitCmd[1:]...)
814+
815+
cmd.Env = os.Environ()
816+
cmd.Env = append(
817+
cmd.Env,
818+
"LAZYGIT_CONTEXT=INTERACTIVE_REBASE",
819+
"LAZYGIT_REBASE_TODO="+todo,
820+
"DEBUG="+debug,
821+
"LANG=en_US.UTF-8", // Force using EN as language
822+
"LC_ALL=en_US.UTF-8", // Force using EN as language
823+
"GIT_SEQUENCE_EDITOR="+ex,
824+
)
825+
826+
if true {
827+
return cmd, nil
828+
}
829+
830+
out, err := cmd.CombinedOutput()
831+
outString := string(out)
832+
c.Log.Info(outString)
833+
if err != nil {
834+
if len(outString) == 0 {
835+
return nil, err
836+
}
837+
return nil, errors.New(outString)
838+
}
839+
return nil, nil
840+
}

pkg/commands/git_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ func newDummyGitCommand() *GitCommand {
6666
Log: newDummyLog(),
6767
OSCommand: newDummyOSCommand(),
6868
Tr: i18n.NewLocalizer(newDummyLog()),
69+
Config: newDummyAppConfig(),
6970
getGlobalGitConfig: func(string) (string, error) { return "", nil },
7071
getLocalGitConfig: func(string) (string, error) { return "", nil },
7172
removeFile: func(string) error { return nil },

pkg/config/app_config.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package config
33
import (
44
"bytes"
55
"io/ioutil"
6+
"os"
67
"path/filepath"
78

89
"github.com/shibukawa/configdir"
@@ -42,18 +43,22 @@ type AppConfigurer interface {
4243
}
4344

4445
// NewAppConfig makes a new app config
45-
func NewAppConfig(name, version, commit, date string, buildSource string, debuggingFlag *bool) (*AppConfig, error) {
46+
func NewAppConfig(name, version, commit, date string, buildSource string, debuggingFlag bool) (*AppConfig, error) {
4647
userConfig, err := LoadConfig("config", true)
4748
if err != nil {
4849
return nil, err
4950
}
5051

52+
if os.Getenv("DEBUG") == "TRUE" {
53+
debuggingFlag = true
54+
}
55+
5156
appConfig := &AppConfig{
5257
Name: "lazygit",
5358
Version: version,
5459
Commit: commit,
5560
BuildDate: date,
56-
Debug: *debuggingFlag,
61+
Debug: debuggingFlag,
5762
BuildSource: buildSource,
5863
UserConfig: userConfig,
5964
AppState: &AppState{},

pkg/gui/commits_panel.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,17 @@ func (gui *Gui) handleRenameCommit(g *gocui.Gui, v *gocui.View) error {
181181
}
182182

183183
func (gui *Gui) handleRenameCommitEditor(g *gocui.Gui, v *gocui.View) error {
184-
if gui.State.Panels.Commits.SelectedLine != 0 {
185-
return gui.createErrorPanel(g, gui.Tr.SLocalize("OnlyRenameTopCommit"))
184+
subProcess, err := gui.GitCommand.InteractiveRebase(gui.State.Commits, gui.State.Panels.Commits.SelectedLine, "reword")
185+
if err != nil {
186+
return err
186187
}
187-
188-
gui.SubProcess = gui.GitCommand.PrepareCommitAmendSubProcess()
189-
g.Update(func(g *gocui.Gui) error {
188+
if subProcess != nil {
189+
gui.SubProcess = subProcess
190+
// g.Update(func(g *gocui.Gui) error {
191+
// return gui.Errors.ErrSubProcess
192+
// })
190193
return gui.Errors.ErrSubProcess
191-
})
194+
}
192195

193196
return nil
194197
}

pkg/gui/recent_repos_panel.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func (gui *Gui) handleCreateRecentReposMenu(g *gocui.Gui, v *gocui.View) error {
3636
if err := os.Chdir(repo.path); err != nil {
3737
return err
3838
}
39-
newGitCommand, err := commands.NewGitCommand(gui.Log, gui.OSCommand, gui.Tr)
39+
newGitCommand, err := commands.NewGitCommand(gui.Log, gui.OSCommand, gui.Tr, gui.Config)
4040
if err != nil {
4141
return err
4242
}

0 commit comments

Comments
 (0)