Skip to content

Commit 845720e

Browse files
authored
Merge pull request #2847 from chrisant996/fix_2846
Fix 2846
2 parents 5567639 + a605ec5 commit 845720e

File tree

1 file changed

+69
-54
lines changed

1 file changed

+69
-54
lines changed

vendor/clink.lua

Lines changed: 69 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
-- !!! THIS FILE IS OVERWRITTEN WHEN CMDER IS UPDATED
44
-- !!! Use "%CMDER_ROOT%\config\<whatever>.lua" to add your lua startup scripts
55

6-
-- luacheck: globals clink
6+
-- luacheck: globals CMDER_SESSION
7+
-- luacheck: globals uah_color cwd_color lamb_color clean_color dirty_color conflict_color unknown_color
8+
-- luacheck: globals prompt_homeSymbol prompt_lambSymbol prompt_type prompt_useHomeSymbol prompt_useUserAtHost
9+
-- luacheck: globals prompt_singleLine prompt_includeVersionControl
10+
-- luacheck: globals prompt_overrideGitStatusOptIn prompt_overrideSvnStatusOptIn
11+
-- luacheck: globals clink io.popenyield os.isdir settings.get
712

813
-- At first, load the original clink.lua file
914
-- this is needed as we set the script path to this dir and therefore the original
@@ -151,12 +156,12 @@ local function set_prompt_filter()
151156
cwd = string.gsub(cwd, clink.get_env("HOME"), prompt_homeSymbol)
152157
end
153158

154-
uah = ''
159+
local uah = ''
155160
if prompt_useUserAtHost then
156161
uah = clink.get_env("USERNAME") .. "@" .. clink.get_env("COMPUTERNAME") .. ' '
157162
end
158163

159-
cr = "\n"
164+
local cr = "\n"
160165
if prompt_singleLine then
161166
cr = ' '
162167
end
@@ -170,7 +175,7 @@ local function set_prompt_filter()
170175

171176
local version_control = prompt_includeVersionControl and "{git}{hg}{svn}" or ""
172177

173-
prompt = "{uah}{cwd}" .. version_control .. cr .. get_lamb_color() .. "{env}{lamb}\x1b[0m "
178+
local prompt = "{uah}{cwd}" .. version_control .. cr .. get_lamb_color() .. "{env}{lamb}\x1b[0m "
174179
prompt = string.gsub(prompt, "{uah}", uah)
175180
prompt = string.gsub(prompt, "{cwd}", cwd)
176181
prompt = string.gsub(prompt, "{env}", env)
@@ -191,7 +196,7 @@ end
191196
local function get_dir_contains(path, dirname)
192197

193198
-- return parent path for specified entry (either file or directory)
194-
local function pathname(path)
199+
local function pathname(path) -- luacheck: ignore 432
195200
local prefix = ""
196201
local i = path:find("[\\/:][^\\/:]*$")
197202
if i then
@@ -201,14 +206,14 @@ local function get_dir_contains(path, dirname)
201206
end
202207

203208
-- Navigates up one level
204-
local function up_one_level(path)
209+
local function up_one_level(path) -- luacheck: ignore 432
205210
if path == nil then path = '.' end
206211
if path == '.' then path = clink.get_cwd() end
207212
return pathname(path)
208213
end
209214

210215
-- Checks if provided directory contains git directory
211-
local function has_specified_dir(path, specified_dir)
216+
local function has_specified_dir(path, specified_dir) -- luacheck: ignore 432
212217
if path == nil then path = '.' end
213218
local found_dirs = clink.find_dirs(path..'/'..specified_dir)
214219
if #found_dirs > 0 then return true end
@@ -236,7 +241,7 @@ end
236241
local function get_git_dir(path)
237242

238243
-- return parent path for specified entry (either file or directory)
239-
local function pathname(path)
244+
local function pathname(path) -- luacheck: ignore 432
240245
local prefix = ""
241246
local i = path:find("[\\/:][^\\/:]*$")
242247
if i then
@@ -255,7 +260,8 @@ local function get_git_dir(path)
255260
local gitfile = io.open(dir..'/.git')
256261
if not gitfile then return false end
257262

258-
local git_dir = gitfile:read():match('gitdir: (.*)')
263+
local line = gitfile:read() or ''
264+
local git_dir = line:match('gitdir: (.*)')
259265
gitfile:close()
260266

261267
if os.isdir then -- only available in Clink v1.0.0 and higher
@@ -303,6 +309,9 @@ local function get_git_branch(git_dir)
303309
local HEAD = head_file:read()
304310
head_file:close()
305311

312+
-- If HEAD is missing, something is wrong.
313+
if not HEAD then return end
314+
306315
-- if HEAD matches branch expression, then we're on named branch
307316
-- otherwise it is a detached commit
308317
local branch_name = HEAD:match('ref: refs/heads/(.+)')
@@ -322,6 +331,9 @@ local function get_hg_branch()
322331
-- local cmd = "hg prompt \"{branch}{status}{|{patch}}{update}\""
323332
local cmd = "hg branch 2>nul"
324333
local file = io.popen(cmd)
334+
if not file then
335+
return false
336+
end
325337

326338
for line in file:lines() do
327339
local m = line:match("(.+)$")
@@ -339,8 +351,12 @@ end
339351
-- Find out current branch
340352
-- @return {false|svn branch name}
341353
---
342-
local function get_svn_branch(svn_dir)
354+
local function get_svn_branch()
343355
local file = io_popenyield("svn info 2>nul")
356+
if not file then
357+
return false
358+
end
359+
344360
for line in file:lines() do
345361
local m = line:match("^Relative URL:")
346362
if m then
@@ -359,12 +375,16 @@ end
359375
---
360376
local function get_git_status()
361377
local file = io_popenyield("git --no-optional-locks status --porcelain 2>nul")
378+
if not file then
379+
return {}
380+
end
381+
362382
local conflict_found = false
363383
local is_status = true
364384
for line in file:lines() do
365385
local code = line:sub(1, 2)
366386
-- print (string.format("code: %s, line: %s", code, line))
367-
if code == "DD" or code == "AU" or code == "UD" or code == "UA" or code == "DU" or code == "AA" or code == "UU" then
387+
if code == "DD" or code == "AU" or code == "UD" or code == "UA" or code == "DU" or code == "AA" or code == "UU" then -- luacheck: no max line length
368388
is_status = false
369389
conflict_found = true
370390
break
@@ -374,23 +394,8 @@ local function get_git_status()
374394
end
375395
end
376396
file:close()
377-
return { status = is_status, conflict = conflict_found }
378-
end
379-
380-
381-
---
382-
-- Get the status of working dir
383-
-- @return {bool}
384-
---
385-
local function get_hg_status()
386-
local file = io.popen("hg status -0")
387-
for line in file:lines() do
388-
file:close()
389-
return false
390-
end
391-
file:close()
392397

393-
return true
398+
return { status = is_status, conflict = conflict_found }
394399
end
395400

396401
---
@@ -399,13 +404,17 @@ end
399404
---
400405
local function get_svn_status()
401406
local file = io_popenyield("svn status -q")
402-
for line in file:lines() do
407+
if not file then
408+
return { error = true }
409+
end
410+
411+
for line in file:lines() do -- luacheck: ignore 512, no unused
403412
file:close()
404-
return false
413+
return { clean = false }
405414
end
406415
file:close()
407416

408-
return true
417+
return { clean = true }
409418
end
410419

411420
---
@@ -433,24 +442,28 @@ local function get_git_status_setting()
433442
end
434443

435444
local gitStatusConfig = io_popenyield("git --no-pager config cmder.status 2>nul")
436-
for line in gitStatusConfig:lines() do
437-
if string.match(line, 'false') then
438-
gitStatusConfig:close()
439-
last_git_status_setting = false
440-
return false
445+
if gitStatusConfig then
446+
for line in gitStatusConfig:lines() do
447+
if string.match(line, 'false') then
448+
gitStatusConfig:close()
449+
last_git_status_setting = false
450+
return false
451+
end
441452
end
453+
gitStatusConfig:close()
442454
end
443-
gitStatusConfig:close()
444455

445456
local gitCmdStatusConfig = io_popenyield("git --no-pager config cmder.cmdstatus 2>nul")
446-
for line in gitCmdStatusConfig:lines() do
447-
if string.match(line, 'false') then
448-
gitCmdStatusConfig:close()
449-
last_git_status_setting = false
450-
return false
457+
if gitCmdStatusConfig then
458+
for line in gitCmdStatusConfig:lines() do
459+
if string.match(line, 'false') then
460+
gitCmdStatusConfig:close()
461+
last_git_status_setting = false
462+
return false
463+
end
451464
end
465+
gitCmdStatusConfig:close()
452466
end
453-
gitCmdStatusConfig:close()
454467

455468
last_git_status_setting = true
456469
return true
@@ -536,8 +549,6 @@ local function hg_prompt_filter()
536549
return false
537550
end
538551

539-
local result = ""
540-
541552
local hg_dir = get_hg_dir()
542553
if hg_dir then
543554
-- Colors for mercurial status
@@ -559,16 +570,20 @@ local function hg_prompt_filter()
559570
local color = colors.clean
560571

561572
local pipe = io.popen("hg status -amrd 2>&1")
562-
local output = pipe:read('*all')
563-
local rc = { pipe:close() }
573+
if pipe then
574+
output = pipe:read('*all')
575+
pipe:close()
576+
if output ~= nil and output ~= "" then color = colors.dirty end
577+
end
564578

565-
if output ~= nil and output ~= "" then color = colors.dirty end
566-
result = color .. "(" .. branch .. ")"
579+
local result = color .. "(" .. branch .. ")"
580+
clink.prompt.value = string.gsub(clink.prompt.value, "{hg}", " "..verbatim(result))
581+
return false
567582
end
568583
end
569584

570-
clink.prompt.value = string.gsub(clink.prompt.value, "{hg}", " "..verbatim(result))
571-
return false
585+
-- No hg present or not in hg repo
586+
clink.prompt.value = string.gsub(clink.prompt.value, "{hg}", "")
572587
end
573588

574589
local function svn_prompt_filter()
@@ -589,7 +604,6 @@ local function svn_prompt_filter()
589604
if svn_dir then
590605
-- if we're inside of svn repo then try to detect current branch
591606
local branch = get_svn_branch()
592-
local color
593607
if branch then
594608
-- If in a different repo or branch than last time, discard cached info
595609
if cached_info.svn_dir ~= svn_dir or cached_info.svn_branch ~= branch then
@@ -599,7 +613,7 @@ local function svn_prompt_filter()
599613
end
600614
-- Get the svn status using coroutine if available and option is enabled. Otherwise use a blocking call
601615
local svnStatus
602-
if clink.promptcoroutine and io.popenyield and settings.get("prompt.async") and prompt_overrideSvnStatusOptIn then
616+
if clink.promptcoroutine and io.popenyield and settings.get("prompt.async") and prompt_overrideSvnStatusOptIn then -- luacheck: no max line length
603617
svnStatus = clink_promptcoroutine(function ()
604618
return get_svn_status()
605619
end)
@@ -613,9 +627,10 @@ local function svn_prompt_filter()
613627
svnStatus = get_svn_status()
614628
end
615629

616-
if svnStatus == nil then
630+
local color
631+
if not svnStatus or svnStatus.error then
617632
color = colors.nostatus
618-
elseif svnStatus then
633+
elseif svnStatus.clean then
619634
color = colors.clean
620635
else
621636
color = colors.dirty

0 commit comments

Comments
 (0)