Skip to content

Commit

Permalink
Add config feature 'initial_branch_name'
Browse files Browse the repository at this point in the history
This config feature allows users to specify an initial branch name that
will be used as a `get_user_input` default when Neogit asks for a name
for a new branch.

This is useful when working in teams with a branch naming convention.
For example, teams may choose to prefix branches with a GitHub
username. In that case, configuring Neogit with:

```
initial_branch_name = "myname/"
```

...will result in the popup being prepopulated with `myname/`, after
which I can specify the branch name.
  • Loading branch information
fnune committed Jun 17, 2024
1 parent 11dea1b commit b887fb4
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ neogit.setup {
-- Flag description: https://git-scm.com/docs/git-branch#Documentation/git-branch.txt---sortltkeygt
-- Sorting keys: https://git-scm.com/docs/git-for-each-ref#_options
sort_branches = "-committerdate",
-- Default for new branch name prompts
initial_branch_name = "",
-- Change the default way of opening neogit
kind = "tab",
-- Disable line numbers and relative line numbers
Expand Down
1 change: 1 addition & 0 deletions doc/neogit.txt
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ TODO: Detail what these do
fetch_after_checkout = false,
auto_refresh = true,
sort_branches = "-committerdate",
initial_branch_name = "",
kind = "tab",
disable_line_numbers = true,
-- The time after which an output console is shown for slow running commands
Expand Down
3 changes: 3 additions & 0 deletions lua/neogit/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ end
---@field use_per_project_settings? boolean Scope persisted settings on a per-project basis
---@field remember_settings? boolean Whether neogit should persist flags from popups, e.g. git push flags
---@field sort_branches? string Value used for `--sort` for the `git branch` command
---@field initial_branch_name? string Default for new branch name prompts
---@field kind? WindowKind The default type of window neogit should open in
---@field disable_line_numbers? boolean Whether to disable line numbers
---@field disable_relative_line_numbers? boolean Whether to disable line numbers
Expand Down Expand Up @@ -314,6 +315,7 @@ function M.get_default_values()
fetch_after_checkout = false,
sort_branches = "-committerdate",
kind = "tab",
initial_branch_name = "",
disable_line_numbers = true,
disable_relative_line_numbers = true,
-- The time after which an output console is shown for slow running commands
Expand Down Expand Up @@ -1004,6 +1006,7 @@ function M.validate_config()
validate_type(config.use_per_project_settings, "use_per_project_settings", "boolean")
validate_type(config.remember_settings, "remember_settings", "boolean")
validate_type(config.sort_branches, "sort_branches", "string")
validate_type(config.initial_branch_name, "initial_branch_name", "string")
validate_type(config.notification_icon, "notification_icon", "string")
validate_type(config.console_timeout, "console_timeout", "number")
validate_kind(config.kind, "kind")
Expand Down
15 changes: 8 additions & 7 deletions lua/neogit/popups/branch/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@ local function fire_branch_event(pattern, data)
vim.api.nvim_exec_autocmds("User", { pattern = pattern, modeline = false, data = data })
end

local function get_branch_name_user_input(prompt, default)
default = default or config.values.initial_branch_name
return input.get_user_input(prompt, { strip_spaces = true, default = default })
end

local function spin_off_branch(checkout)
if git.status.is_dirty() and not checkout then
notification.info("Staying on HEAD due to uncommitted changes")
checkout = true
end

local name =
input.get_user_input(("%s branch"):format(checkout and "Spin-off" or "Spin-out"), { strip_spaces = true })
local name = get_branch_name_user_input(("%s branch"):format(checkout and "Spin-off" or "Spin-out"))
if not name then
return
end
Expand Down Expand Up @@ -64,10 +68,7 @@ local function create_branch(popup, prompt, checkout)
return
end

local name = input.get_user_input("Create branch", {
strip_spaces = true,
default = popup.state.env.suggested_branch_name,
})
local name = get_branch_name_user_input("Create branch", popup.state.env.suggested_branch_name)
if not name then
return
end
Expand Down Expand Up @@ -165,7 +166,7 @@ function M.rename_branch()
return
end

local new_name = input.get_user_input(("Rename '%s' to"):format(selected_branch), { strip_spaces = true })
local new_name = get_branch_name_user_input(("Rename '%s' to"):format(selected_branch))
if not new_name then
return
end
Expand Down
5 changes: 5 additions & 0 deletions tests/specs/neogit/config_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ describe("Neogit config", function()
assert.True(vim.tbl_count(require("neogit.config").validate_config()) ~= 0)
end)

it("should return invalid when initial_branch_name isn't a string", function()
config.values.initial_branch_name = false
assert.True(vim.tbl_count(require("neogit.config").validate_config()) ~= 0)
end)

it("should return invalid when kind isn't a string", function()
config.values.kind = true
assert.True(vim.tbl_count(require("neogit.config").validate_config()) ~= 0)
Expand Down

0 comments on commit b887fb4

Please sign in to comment.