Skip to content

Commit be03f4a

Browse files
committed
Allow rebase to be smarter about picking the "parent" branch to the one
you're currently on. Based on this: https://gist.github.com/joechrysler/6073741
1 parent 8538091 commit be03f4a

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed

lua/neogit/lib/git/branch.lua

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
local a = require("plenary.async")
22
local cli = require("neogit.lib.git.cli")
33
local input = require("neogit.lib.input")
4+
local util = require("neogit.lib.util")
45
local config = require("neogit.config")
56
local M = {}
67

@@ -21,6 +22,22 @@ local function parse_branches(branches, include_current)
2122
return other_branches
2223
end
2324

25+
-- https://gist.github.com/joechrysler/6073741
26+
function M.get_nearest_parent()
27+
local head = util.pattern_escape(cli["rev-parse"].abbrev_ref().args("HEAD").call_sync():trim().stdout[1])
28+
29+
local parent
30+
local result = cli["show-branch"].all.call_sync():trim().stdout
31+
for _, branch in ipairs(result) do
32+
if branch:match("%*") and not branch:match(head) then
33+
parent = branch:match("%[(.*)[~%^]?%d*%]")
34+
break
35+
end
36+
end
37+
38+
return parent
39+
end
40+
2441
function M.get_local_branches(include_current)
2542
local branches = cli.branch.list(config.values.sort_branches).call_sync():trim().stdout
2643

lua/neogit/lib/git/cli.lua

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ local configurations = {
4141
porcelain = "--porcelain",
4242
},
4343
},
44+
4445
log = config {
4546
flags = {
4647
oneline = "--oneline",
@@ -62,6 +63,7 @@ local configurations = {
6263
end,
6364
},
6465
},
66+
6567
config = config {
6668
flags = {
6769
_get = "--get",
@@ -74,6 +76,7 @@ local configurations = {
7476
end,
7577
},
7678
},
79+
7780
diff = config {
7881
flags = {
7982
cached = "--cached",
@@ -83,13 +86,15 @@ local configurations = {
8386
no_ext_diff = "--no-ext-diff",
8487
},
8588
},
89+
8690
stash = config {
8791
flags = {
8892
apply = "apply",
8993
drop = "drop",
9094
index = "--index",
9195
},
9296
},
97+
9398
rebase = config {
9499
flags = {
95100
interactive = "-i",
@@ -98,6 +103,7 @@ local configurations = {
98103
skip = "--skip",
99104
},
100105
},
106+
101107
reset = config {
102108
flags = {
103109
hard = "--hard",
@@ -110,6 +116,7 @@ local configurations = {
110116
end,
111117
},
112118
},
119+
113120
checkout = config {
114121
short_opts = {
115122
b = "-b",
@@ -132,6 +139,7 @@ local configurations = {
132139
end,
133140
},
134141
},
142+
135143
remote = config {
136144
flags = {
137145
push = "--push",
@@ -145,6 +153,7 @@ local configurations = {
145153
end,
146154
},
147155
},
156+
148157
apply = config {
149158
flags = {
150159
cached = "--cached",
@@ -157,12 +166,14 @@ local configurations = {
157166
end,
158167
},
159168
},
169+
160170
add = config {
161171
flags = {
162172
update = "-u",
163173
all = "-A",
164174
},
165175
},
176+
166177
commit = config {
167178
flags = {
168179
amend = "--amend",
@@ -174,6 +185,7 @@ local configurations = {
174185
commit_message_file = "--file",
175186
},
176187
},
188+
177189
push = config {
178190
flags = {
179191
delete = "--delete",
@@ -191,6 +203,7 @@ local configurations = {
191203
end,
192204
},
193205
},
206+
194207
pull = config {
195208
flags = {
196209
no_commit = "--no-commit",
@@ -199,6 +212,7 @@ local configurations = {
199212
flags = {},
200213
},
201214
},
215+
202216
branch = config {
203217
flags = {
204218
all = "-a",
@@ -221,6 +235,7 @@ local configurations = {
221235
end,
222236
},
223237
},
238+
224239
["read-tree"] = config {
225240
flags = {
226241
merge = "-m",
@@ -236,7 +251,9 @@ local configurations = {
236251
end,
237252
},
238253
},
254+
239255
["write-tree"] = config {},
256+
240257
["commit-tree"] = config {
241258
flags = {
242259
no_gpg_sign = "--no-gpg-sign",
@@ -261,18 +278,27 @@ local configurations = {
261278
end,
262279
},
263280
},
281+
264282
["update-index"] = config {
265283
flags = {
266284
add = "--add",
267285
remove = "--remove",
268286
refresh = "--refresh",
269287
},
270288
},
289+
271290
["show-ref"] = config {
272291
flags = {
273292
verify = "--verify",
274293
},
275294
},
295+
296+
["show-branch"] = config {
297+
flags = {
298+
all = "--all"
299+
}
300+
},
301+
276302
["update-ref"] = config {
277303
flags = {
278304
create_reflog = "--create-reflog",
@@ -281,6 +307,7 @@ local configurations = {
281307
message = "-m",
282308
},
283309
},
310+
284311
["ls-files"] = config {
285312
flags = {
286313
others = "--others",
@@ -290,6 +317,7 @@ local configurations = {
290317
full_name = "--full-name",
291318
},
292319
},
320+
293321
["rev-parse"] = config {
294322
flags = {
295323
revs_only = "--revs-only",

lua/neogit/lib/util.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,15 @@ local function parse_command_args(args)
176176
return tbl
177177
end
178178

179+
local function pattern_escape(str)
180+
local special_chars = { "(", ")", ".", "%", "+", "-", "*", "?", "[", "^", "$" }
181+
for _, char in ipairs(special_chars) do
182+
str = str:gsub("%" .. char, "%%" .. char)
183+
end
184+
185+
return str
186+
end
187+
179188
return {
180189
time = time,
181190
time_async = time_async,
@@ -195,4 +204,5 @@ return {
195204
deepcopy = deepcopy,
196205
trim = trim,
197206
parse_command_args = parse_command_args,
207+
pattern_escape = pattern_escape,
198208
}

lua/neogit/popups/rebase.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local cli = require("neogit.lib.git.cli")
2+
local branch = require("neogit.lib.git.branch")
23
local git = require("neogit.lib.git")
34
local popup = require("neogit.lib.popup")
45
local CommitSelectViewBuffer = require("neogit.buffers.commit_select_view")
@@ -30,9 +31,9 @@ function M.create()
3031
end)
3132
:action(
3233
"p",
33-
"Rebase onto master",
34+
"Rebase onto " .. branch.get_nearest_parent(),
3435
a.void(function(popup)
35-
rebase.rebase_onto("master", popup:get_arguments())
36+
rebase.rebase_onto(branch.get_nearest_parent(), popup:get_arguments())
3637
a.util.scheduler()
3738
status.refresh(true, "rebase_master")
3839
end)

0 commit comments

Comments
 (0)