diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2019-02-18 21:29:43 +1100 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2019-02-18 21:29:43 +1100 |
commit | 76a27f417fc7fd24b4fdf12f0aeeb94ecca958d3 (patch) | |
tree | 2c75a15e6facb18b1f5ff1831cf67d5664b796dd | |
parent | adc252901944e84433cb8a50de9b114ba120282d (diff) |
rename any commit
-rw-r--r-- | main.go | 2 | ||||
-rw-r--r-- | pkg/app/app.go | 41 | ||||
-rw-r--r-- | pkg/commands/git.go | 66 | ||||
-rw-r--r-- | pkg/commands/git_test.go | 1 | ||||
-rw-r--r-- | pkg/config/app_config.go | 9 | ||||
-rw-r--r-- | pkg/gui/commits_panel.go | 15 | ||||
-rw-r--r-- | pkg/gui/recent_repos_panel.go | 2 |
7 files changed, 114 insertions, 22 deletions
@@ -40,7 +40,7 @@ func main() { fmt.Printf("%s\n", config.GetDefaultConfig()) os.Exit(0) } - appConfig, err := config.NewAppConfig("lazygit", version, commit, date, buildSource, debuggingFlag) + appConfig, err := config.NewAppConfig("lazygit", version, commit, date, buildSource, *debuggingFlag) if err != nil { log.Fatal(err.Error()) } diff --git a/pkg/app/app.go b/pkg/app/app.go index 54706682c..e1f6654af 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -20,13 +20,14 @@ import ( type App struct { closers []io.Closer - Config config.AppConfigurer - Log *logrus.Entry - OSCommand *commands.OSCommand - GitCommand *commands.GitCommand - Gui *gui.Gui - Tr *i18n.Localizer - Updater *updates.Updater // may only need this on the Gui + Config config.AppConfigurer + Log *logrus.Entry + OSCommand *commands.OSCommand + GitCommand *commands.GitCommand + Gui *gui.Gui + Tr *i18n.Localizer + Updater *updates.Updater // may only need this on the Gui + DemonContext string } func newProductionLogger(config config.AppConfigurer) *logrus.Logger { @@ -54,7 +55,7 @@ func newDevelopmentLogger(config config.AppConfigurer) *logrus.Logger { func newLogger(config config.AppConfigurer) *logrus.Entry { var log *logrus.Logger environment := "production" - if config.GetDebug() { + if config.GetDebug() || os.Getenv("DEBUG") == "TRUE" { environment = "development" log = newDevelopmentLogger(config) } else { @@ -86,15 +87,21 @@ func NewApp(config config.AppConfigurer) (*App, error) { } var err error app.Log = newLogger(config) - app.OSCommand = commands.NewOSCommand(app.Log, config) - app.Tr = i18n.NewLocalizer(app.Log) + // if we are being called in 'demon' mode, we can just return here + app.DemonContext = os.Getenv("LAZYGIT_CONTEXT") + if app.DemonContext != "" { + return app, nil + } + + app.OSCommand = commands.NewOSCommand(app.Log, config) + app.Updater, err = updates.NewUpdater(app.Log, config, app.OSCommand, app.Tr) if err != nil { return app, err } - app.GitCommand, err = commands.NewGitCommand(app.Log, app.OSCommand, app.Tr) + app.GitCommand, err = commands.NewGitCommand(app.Log, app.OSCommand, app.Tr, app.Config) if err != nil { return app, err } @@ -106,9 +113,21 @@ func NewApp(config config.AppConfigurer) (*App, error) { } func (app *App) Run() error { + if app.DemonContext == "INTERACTIVE_REBASE" { + return app.Rebase() + } + return app.Gui.RunWithSubprocesses() } +func (app *App) Rebase() error { + app.Log.Error("TEST") + + ioutil.WriteFile(".git/rebase-merge/git-rebase-todo", []byte(os.Getenv("LAZYGIT_REBASE_TODO")), 0644) + + return nil +} + // Close closes any resources func (app *App) Close() error { for _, closer := range app.closers { diff --git a/pkg/commands/git.go b/pkg/commands/git.go index 66ddf5e68..51e943ba6 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -9,9 +9,12 @@ import ( "regexp" "strings" + "github.com/mgutz/str" + "github.com/fatih/color" "github.com/go-errors/errors" + "github.com/jesseduffield/lazygit/pkg/config" "github.com/jesseduffield/lazygit/pkg/i18n" "github.com/jesseduffield/lazygit/pkg/utils" "github.com/sirupsen/logrus" @@ -68,13 +71,14 @@ type GitCommand struct { Worktree *gogit.Worktree Repo *gogit.Repository Tr *i18n.Localizer + Config config.AppConfigurer getGlobalGitConfig func(string) (string, error) getLocalGitConfig func(string) (string, error) removeFile func(string) error } // NewGitCommand it runs git commands -func NewGitCommand(log *logrus.Entry, osCommand *OSCommand, tr *i18n.Localizer) (*GitCommand, error) { +func NewGitCommand(log *logrus.Entry, osCommand *OSCommand, tr *i18n.Localizer, config config.AppConfigurer) (*GitCommand, error) { var worktree *gogit.Worktree var repo *gogit.Repository @@ -104,6 +108,7 @@ func NewGitCommand(log *logrus.Entry, osCommand *OSCommand, tr *i18n.Localizer) Tr: tr, Worktree: worktree, Repo: repo, + Config: config, getGlobalGitConfig: gitconfig.Global, getLocalGitConfig: gitconfig.Local, removeFile: os.RemoveAll, @@ -774,3 +779,62 @@ func (c *GitCommand) GenericMerge(commandType string, command string) error { gitCommand := fmt.Sprintf("git %s %s --%s", c.OSCommand.Platform.skipEditorArg, commandType, command) return c.OSCommand.RunCommand(gitCommand) } + +func (c *GitCommand) InteractiveRebase(commits []*Commit, index int, action string) (*exec.Cmd, error) { + ex, err := os.Executable() // get the executable path for git to use + if err != nil { + ex = os.Args[0] // fallback to the first call argument if needed + } + + // assume for now they won't pick the bottom commit + c.Log.Warn(len(commits)) + c.Log.Warn(index) + if len(commits) <= index+1 { + // TODO: support more than say 30 commits and ensure this logic is correct, and i18n + return nil, errors.New("You cannot interactive rebase onto the first commit") + } + + todo := "" + for i, commit := range commits[0 : index+1] { + a := "pick" + if i == index { + a = action + } + todo += a + " " + commit.Sha + "\n" + } + + debug := "FALSE" + if c.OSCommand.Config.GetDebug() == true { + debug = "TRUE" + } + + splitCmd := str.ToArgv(fmt.Sprintf("git rebase --interactive %s", commits[index+1].Sha)) + + cmd := exec.Command(splitCmd[0], splitCmd[1:]...) + + cmd.Env = os.Environ() + cmd.Env = append( + cmd.Env, + "LAZYGIT_CONTEXT=INTERACTIVE_REBASE", + "LAZYGIT_REBASE_TODO="+todo, + "DEBUG="+debug, + "LANG=en_US.UTF-8", // Force using EN as language + "LC_ALL=en_US.UTF-8", // Force using EN as language + "GIT_SEQUENCE_EDITOR="+ex, + ) + + if true { + return cmd, nil + } + + out, err := cmd.CombinedOutput() + outString := string(out) + c.Log.Info(outString) + if err != nil { + if len(outString) == 0 { + return nil, err + } + return nil, errors.New(outString) + } + return nil, nil +} diff --git a/pkg/commands/git_test.go b/pkg/commands/git_test.go index 188844486..4159683ca 100644 --- a/pkg/commands/git_test.go +++ b/pkg/commands/git_test.go @@ -66,6 +66,7 @@ func newDummyGitCommand() *GitCommand { Log: newDummyLog(), OSCommand: newDummyOSCommand(), Tr: i18n.NewLocalizer(newDummyLog()), + Config: newDummyAppConfig(), getGlobalGitConfig: func(string) (string, error) { return "", nil }, getLocalGitConfig: func(string) (string, error) { return "", nil }, removeFile: func(string) error { return nil }, diff --git a/pkg/config/app_config.go b/pkg/config/app_config.go index b5052fc12..9ac2fb904 100644 --- a/pkg/config/app_config.go +++ b/pkg/config/app_config.go @@ -3,6 +3,7 @@ package config import ( "bytes" "io/ioutil" + "os" "path/filepath" "github.com/shibukawa/configdir" @@ -42,18 +43,22 @@ type AppConfigurer interface { } // NewAppConfig makes a new app config -func NewAppConfig(name, version, commit, date string, buildSource string, debuggingFlag *bool) (*AppConfig, error) { +func NewAppConfig(name, version, commit, date string, buildSource string, debuggingFlag bool) (*AppConfig, error) { userConfig, err := LoadConfig("config", true) if err != nil { return nil, err } + if os.Getenv("DEBUG") == "TRUE" { + debuggingFlag = true + } + appConfig := &AppConfig{ Name: "lazygit", Version: version, Commit: commit, BuildDate: date, - Debug: *debuggingFlag, + Debug: debuggingFlag, BuildSource: buildSource, UserConfig: userConfig, AppState: &AppState{}, diff --git a/pkg/gui/commits_panel.go b/pkg/gui/commits_panel.go index e791cf87b..4bf0d4362 100644 --- a/pkg/gui/commits_panel.go +++ b/pkg/gui/commits_panel.go @@ -181,14 +181,17 @@ func (gui *Gui) handleRenameCommit(g *gocui.Gui, v *gocui.View) error { } func (gui *Gui) handleRenameCommitEditor(g *gocui.Gui, v *gocui.View) error { - if gui.State.Panels.Commits.SelectedLine != 0 { - return gui.createErrorPanel(g, gui.Tr.SLocalize("OnlyRenameTopCommit")) + subProcess, err := gui.GitCommand.InteractiveRebase(gui.State.Commits, gui.State.Panels.Commits.SelectedLine, "reword") + if err != nil { + return err } - - gui.SubProcess = gui.GitCommand.PrepareCommitAmendSubProcess() - g.Update(func(g *gocui.Gui) error { + if subProcess != nil { + gui.SubProcess = subProcess + // g.Update(func(g *gocui.Gui) error { + // return gui.Errors.ErrSubProcess + // }) return gui.Errors.ErrSubProcess - }) + } return nil } diff --git a/pkg/gui/recent_repos_panel.go b/pkg/gui/recent_repos_panel.go index 6108b737c..3f92431ea 100644 --- a/pkg/gui/recent_repos_panel.go +++ b/pkg/gui/recent_repos_panel.go @@ -36,7 +36,7 @@ func (gui *Gui) handleCreateRecentReposMenu(g *gocui.Gui, v *gocui.View) error { if err := os.Chdir(repo.path); err != nil { return err } - newGitCommand, err := commands.NewGitCommand(gui.Log, gui.OSCommand, gui.Tr) + newGitCommand, err := commands.NewGitCommand(gui.Log, gui.OSCommand, gui.Tr, gui.Config) if err != nil { return err } |