@@ -4,6 +4,29 @@ local cli = require 'neogit.lib.git.cli'
44local popup = require (' neogit.lib.popup' )
55local branch = require (' neogit.lib.git.branch' )
66local operation = require (' neogit.operations' )
7+ local BranchSelectViewBuffer = require ' neogit.buffers.branch_select_view'
8+ local input = require (' neogit.lib.input' )
9+
10+ local function format_branches (list )
11+ local branches = {}
12+ for _ ,name in ipairs (list ) do
13+ local name_formatted = name :match (" ^remotes/(.*)" ) or name
14+ if not name_formatted :match (' ^(.*)/HEAD' ) then
15+ table.insert (branches , name_formatted )
16+ end
17+ end
18+ return branches
19+ end
20+
21+ local function parse_remote_branch_name (remote_name )
22+ local offset = remote_name :find (' /' )
23+ if not offset then return nil , nil end
24+
25+ local remote = remote_name :sub (1 , offset - 1 )
26+ local branch_name = remote_name :sub (offset + 1 , remote_name :len ())
27+
28+ return remote , branch_name
29+ end
730
831function M .create ()
932 local p = popup .builder ()
@@ -13,27 +36,68 @@ function M.create()
1336 status .refresh (true )
1437 end ))
1538 :action (" b" , " checkout branch/revision" , operation (' checkout_branch' , function ()
16- branch .checkout ()
17- status .refresh (true )
39+ local branches = format_branches (branch .get_all_branches ())
40+ BranchSelectViewBuffer .new (branches , function (selected_branch )
41+ if selected_branch == ' ' then return end
42+
43+ cli .checkout .branch (selected_branch ).call_sync ()
44+ status .dispatch_refresh (true )
45+ end ):open ()
1846 end ))
1947 :action (" d" , " delete local branch" , operation (' delete_branch' , function ()
20- branch .delete ()
21- status .refresh (true )
48+ local branches = branch .get_local_branches ()
49+ BranchSelectViewBuffer .new (branches , function (selected_branch )
50+ cli .branch .delete .name (selected_branch ).call_sync ()
51+ status .dispatch_refresh (true )
52+ end ):open ()
2253 end ))
2354 :action (" D" , " delete local branch and remote" , operation (' delete_branch' , function ()
24- local branch = branch .delete ()
25- if branch and branch ~= ' ' then
26- cli .interactive_git_cmd (tostring (cli .push .remote (" origin" ).delete .to (branch )))
27- end
28- status .refresh (true )
55+ local branches = format_branches (branch .get_remote_branches ())
56+ BranchSelectViewBuffer .new (branches , function (selected_branch )
57+ if selected_branch == ' ' then return end
58+
59+ local remote , branch_name = parse_remote_branch_name (selected_branch )
60+ if not remote or not branch_name then return end
61+
62+ cli .branch .delete .name (branch_name ).call_sync ()
63+ cli .push .remote (remote ).delete .to (branch_name ).call_sync ()
64+ status .dispatch_refresh (true )
65+ end ):open ()
2966 end ))
3067 :action (" l" , " checkout local branch" , operation (' checkout_local-branch' , function ()
31- branch .checkout_local ()
32- status .refresh (true )
68+ local branches = branch .get_local_branches ()
69+ BranchSelectViewBuffer .new (branches , function (selected_branch )
70+ if selected_branch == ' ' then return end
71+ cli .checkout .branch (selected_branch ).call_sync ()
72+ status .dispatch_refresh (true )
73+ end ):open ()
3374 end ))
3475 :action (" c" , " checkout new branch" , operation (' checkout_create-branch' , function ()
35- branch .checkout_new ()
36- status .refresh (true )
76+ local branches = format_branches (branch .get_all_branches ())
77+ BranchSelectViewBuffer .new (branches , function (selected_branch )
78+ if selected_branch == ' ' then return end
79+
80+ local name = input .get_user_input (' branch > ' )
81+ if not name or name == ' ' then return end
82+
83+ cli .checkout .new_branch_with_start_point (name , selected_branch ).call_sync ()
84+ status .dispatch_refresh (true )
85+ end ):open ()
86+ end ))
87+ :action (" m" , " rename branch" , operation (' rename_branch' , function ()
88+ local current_branch = branch .current () or ' '
89+ local branches = branch .get_local_branches ()
90+ table.insert (branches , current_branch )
91+
92+ BranchSelectViewBuffer .new (branches , function (selected_branch )
93+ if selected_branch == ' ' then return end
94+
95+ local new_name = input .get_user_input (' new branch name > ' )
96+ if not new_name or new_name == ' ' then return end
97+
98+ cli .branch .move .args (selected_branch , new_name ).call_sync ()
99+ status .dispatch_refresh (true )
100+ end ):open ()
37101 end ))
38102 :build ()
39103
0 commit comments