Skip to content

Commit

Permalink
Add config option for reset mode when undoing commits
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriellanata committed Jan 13, 2025
1 parent 14a91d9 commit a236882
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 7 deletions.
7 changes: 7 additions & 0 deletions docs/Config.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,13 @@ git:
# The commit message to use for a squash merge commit. Can contain "{{selectedRef}}" and "{{currentBranch}}" placeholders.
squashMergeMessage: Squash merge {{selectedRef}} into {{currentBranch}}

# Config relating to undoing
undo:
# Wether to use hard or soft git reset when undoing commits
commitReset: hard
# Wether to use hard or soft git reset when undoing rebases
rebaseReset: hard

# list of branches that are considered 'main' branches, used when displaying commits
mainBranches:
- master
Expand Down
13 changes: 13 additions & 0 deletions pkg/config/user_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ type GitConfig struct {
Commit CommitConfig `yaml:"commit"`
// Config relating to merging
Merging MergingConfig `yaml:"merging"`
// Config relating to undoing
Undo UndoConfig `yaml:"undo"`
// list of branches that are considered 'main' branches, used when displaying commits
MainBranches []string `yaml:"mainBranches" jsonschema:"uniqueItems=true"`
// Prefix to use when skipping hooks. E.g. if set to 'WIP', then pre-commit hooks will be skipped when the commit message starts with 'WIP'
Expand Down Expand Up @@ -314,6 +316,13 @@ type MergingConfig struct {
SquashMergeMessage string `yaml:"squashMergeMessage"`
}

type UndoConfig struct {
// One of: 'hard' | 'soft'
CommitReset string `yaml:"commitReset" jsonschema:"enum=hard,enum=soft"`
// One of: 'hard' | 'soft'
RebaseReset string `yaml:"rebaseReset" jsonschema:"enum=hard,enum=soft"`
}

type LogConfig struct {
// One of: 'date-order' | 'author-date-order' | 'topo-order' | 'default'
// 'topo-order' makes it easier to read the git log graph, but commits may not
Expand Down Expand Up @@ -768,6 +777,10 @@ func GetDefaultConfig() *UserConfig {
Args: "",
SquashMergeMessage: "Squash merge {{selectedRef}} into {{currentBranch}}",
},
Undo: UndoConfig{
CommitReset: "hard",
RebaseReset: "hard",
},
Log: LogConfig{
Order: "topo-order",
ShowGraph: "always",
Expand Down
50 changes: 43 additions & 7 deletions pkg/gui/controllers/undo_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,30 @@ func (self *UndoController) reflogUndo() error {

switch action.kind {
case COMMIT, REBASE:
var resetMode string
switch action.kind {
case COMMIT:
resetMode = self.c.UserConfig().Git.Undo.CommitReset
case REBASE:
resetMode = self.c.UserConfig().Git.Undo.RebaseReset
}

var resetPrompt string
switch resetMode {
case "hard":
resetPrompt = self.c.Tr.HardResetAutostashPrompt
case "soft":
resetPrompt = self.c.Tr.SoftResetAutostashPrompt
}

// 3. Use those in the confirm step
self.c.Confirm(types.ConfirmOpts{
Title: self.c.Tr.Actions.Undo,
Prompt: fmt.Sprintf(self.c.Tr.HardResetAutostashPrompt, action.from),
Prompt: fmt.Sprintf(resetPrompt, action.from),
HandleConfirm: func() error {
self.c.LogAction(self.c.Tr.Actions.Undo)
return self.hardResetWithAutoStash(action.from, hardResetOptions{
return self.resetWithAutoStash(action.from, resetOptions{
Mode: resetMode,
EnvVars: undoEnvVars,
WaitingStatus: undoingStatus,
})
Expand Down Expand Up @@ -143,12 +161,29 @@ func (self *UndoController) reflogRedo() error {

switch action.kind {
case COMMIT, REBASE:
var resetMode string
switch action.kind {
case COMMIT:
resetMode = self.c.UserConfig().Git.Undo.CommitReset
case REBASE:
resetMode = self.c.UserConfig().Git.Undo.RebaseReset
}

var resetPrompt string
switch resetMode {
case "hard":
resetPrompt = self.c.Tr.HardResetAutostashPrompt
case "soft":
resetPrompt = self.c.Tr.SoftResetAutostashPrompt
}

self.c.Confirm(types.ConfirmOpts{
Title: self.c.Tr.Actions.Redo,
Prompt: fmt.Sprintf(self.c.Tr.HardResetAutostashPrompt, action.to),
Prompt: fmt.Sprintf(resetPrompt, action.to),
HandleConfirm: func() error {
self.c.LogAction(self.c.Tr.Actions.Redo)
return self.hardResetWithAutoStash(action.to, hardResetOptions{
return self.resetWithAutoStash(action.to, resetOptions{
Mode: resetMode,
EnvVars: redoEnvVars,
WaitingStatus: redoingStatus,
})
Expand Down Expand Up @@ -233,15 +268,16 @@ func (self *UndoController) parseReflogForActions(onUserAction func(counter int,
return nil
}

type hardResetOptions struct {
type resetOptions struct {
Mode string
WaitingStatus string
EnvVars []string
}

// only to be used in the undo flow for now (does an autostash)
func (self *UndoController) hardResetWithAutoStash(commitHash string, options hardResetOptions) error {
func (self *UndoController) resetWithAutoStash(commitHash string, options resetOptions) error {
reset := func() error {
return self.c.Helpers().Refs.ResetToRef(commitHash, "hard", options.EnvVars)
return self.c.Helpers().Refs.ResetToRef(commitHash, options.Mode, options.EnvVars)
}

// if we have any modified tracked files we need to ask the user if they want us to stash for them
Expand Down
2 changes: 2 additions & 0 deletions pkg/i18n/english.go
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,7 @@ type TranslationSet struct {
RewordInEditorPrompt string
CheckoutPrompt string
HardResetAutostashPrompt string
SoftResetAutostashPrompt string
UpstreamGone string
NukeDescription string
DiscardStagedChangesDescription string
Expand Down Expand Up @@ -1735,6 +1736,7 @@ func EnglishTranslationSet() *TranslationSet {
RewordInEditorTitle: "Reword in editor",
RewordInEditorPrompt: "Are you sure you want to reword this commit in your editor?",
HardResetAutostashPrompt: "Are you sure you want to hard reset to '%s'? An auto-stash will be performed if necessary.",
SoftResetAutostashPrompt: "Are you sure you want to soft reset to '%s'? An auto-stash will be performed if necessary.",
CheckoutPrompt: "Are you sure you want to checkout '%s'?",
UpstreamGone: "(upstream gone)",
NukeDescription: "If you want to make all the changes in the worktree go away, this is the way to do it. If there are dirty submodule changes this will stash those changes in the submodule(s).",
Expand Down
1 change: 1 addition & 0 deletions pkg/i18n/translations/pl.json
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,7 @@
"RewordInEditorPrompt": "Czy na pewno chcesz przeformułować ten commit w swoim edytorze?",
"CheckoutPrompt": "Czy na pewno chcesz przełączyć się na '%s'?",
"HardResetAutostashPrompt": "Czy na pewno chcesz zrobić twardy reset do '%s'? Auto-stash zostanie wykonany jeśli będzie potrzebny.",
"SoftResetAutostashPrompt": "Czy na pewno chcesz zrobić miękki reset do '%s'? Auto-stash zostanie wykonany jeśli będzie potrzebny.",
"UpstreamGone": "(upstream zniknął)",
"NukeDescription": "Jeśli chcesz, aby wszystkie zmiany w drzewie pracy zniknęły, to jest sposób na to. Jeśli są brudne zmiany w submodule, to zostaną one zapisane w submodule(s).",
"DiscardStagedChangesDescription": "To stworzy nowy wpis stash zawierający tylko pliki w stanie staged, a następnie go usunie, tak że drzewo pracy zostanie tylko ze zmianami niezatwierdzonymi",
Expand Down
1 change: 1 addition & 0 deletions pkg/i18n/translations/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@
"RewordInEditorPrompt": "Вы уверены, что хотите перефразировать этот коммит вашем редакторе?",
"CheckoutPrompt": "Вы уверены, что хотите переключить '%s'?",
"HardResetAutostashPrompt": "Вы уверены, что хотите сделать жёсткий сброс на '%s'? При необходимости будет выполнен автосохранение в хранилище.",
"SoftResetAutostashPrompt": "Вы уверены, что хотите сделать мягкий сброс на '%s'? При необходимости будет выполнен автосохранение в хранилище.",
"NukeDescription": "Если вы хотите, чтобы все изменения в рабочем дереве исчезли, это способ сделать это. Если есть какие-либо изменения подмодуля, эти изменения будут припрятаны в подмодуле(-ях).",
"DiscardStagedChangesDescription": "Это создаст новую запись в хранилище, содержащую только проиндексированные файлы, а затем удалит её, так что в рабочем дереве останутся только непроиндексированные изменения.",
"EmptyOutput": "<Пустой вывод>",
Expand Down
1 change: 1 addition & 0 deletions pkg/i18n/translations/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,7 @@
"RewordInEditorPrompt": "您确定要在编辑器中重写此提交吗?",
"CheckoutPrompt": "您确定要检出 '%s' 吗?",
"HardResetAutostashPrompt": "您确定要硬重置到 '%s' 吗?如有必要,将执行自动贮藏。",
"SoftResetAutostashPrompt": "您确定要软重置到 '%s' 吗?如有必要,将执行自动贮藏。",
"UpstreamGone": "(上游不存在)",
"NukeDescription": "如果您想让工作树中的所有更改消失,可以这样做。如果存在受污染的子模块更改,这会将这些更改贮藏在子模块中。",
"DiscardStagedChangesDescription": "这将创建一个仅包含已暂存更改的新贮藏条目,然后这些更改删除,以便工作树仅保留未暂存的更改。",
Expand Down
1 change: 1 addition & 0 deletions pkg/i18n/translations/zh-TW.json
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,7 @@
"RewordInEditorPrompt": "是否在編輯器中改寫此提交?",
"CheckoutPrompt": "是否檢出 '%s' ?",
"HardResetAutostashPrompt": "是否強制重設為 '%s' ?如果需要會進行自動存儲。",
"SoftResetAutostashPrompt": "是否軟重設為 '%s' ?如果需要會進行自動存儲。",
"UpstreamGone": "(遠端已經不存在)",
"NukeDescription": "如果你想讓所有工作樹上的變更消失,這就是正確的選項。如果有未提交的子模組變更,它們將被收藏在子模組中。",
"DiscardStagedChangesDescription": "這將創建一個新的存儲條目,其中只包含預存檔案,然後如果存儲條目不需要,將其刪除,因此工作樹僅保留未預存的變更。",
Expand Down

0 comments on commit a236882

Please sign in to comment.