如何修改Cmder使其提示符显示Git变更摘要(+A/~C/-D)?
It sounds like the Clink Git prompt script got a behavior shift in version 1.3.4 that's causing two annoying issues: extraneous upstream branch output and broken change status display. Since you've already narrowed down the problem to \vendor\clink.lua, let's dive into targeted fixes:
1. Strip the Unwanted "master -> origin" Text
The extra output is likely coming from the script's logic to display upstream branch info. Newer versions might be pulling in this reference automatically, but we can filter it out cleanly:
Open up \vendor\clink.lua and find the section where it retrieves the current branch name. Look for code that combines the branch with its upstream (it might use git rev-parse --abbrev-ref --symbolic-full-name @{u}). Modify it to strip the -> origin suffix:
-- Original branch retrieval (example structure) local branch = io.popen("git symbolic-ref --short HEAD 2>nul"):read("*l") -- Add this line to remove the unwanted upstream suffix branch = branch:gsub(" -> origin$", "") -- If you still want upstream info (without the messy arrow), adjust the display: local upstream = io.popen("git rev-parse --abbrev-ref --symbolic-full-name @{u} 2>nul"):read("*l") if upstream then -- Use parentheses for cleaner, less intrusive upstream display branch = branch .. " (" .. upstream:match("[^/]+$") .. ")" end
2. Fix Git Change Content Display
The broken change status is probably due to a parsing issue in how the script reads git status output. Let's replace that logic with a more robust approach that aligns with Poshgit's reliable behavior:
Add or update this function in clink.lua to correctly count staged, unstaged, and untracked changes:
local function get_git_change_counts() local counts = { staged = 0, unstaged = 0, untracked = 0 } -- Use git status --porcelain for consistent, machine-readable output local status_handle = io.popen("git status --porcelain 2>nul") if not status_handle then return counts end for line in status_handle:lines() do local status_code = line:sub(1, 2) -- Count staged changes (first character is A/M/R/D) if status_code:match("^[AMRD]") then counts.staged = counts.staged + 1 end -- Count unstaged changes (second character is A/M/R/D) if status_code:match("[AMRD]$") then counts.unstaged = counts.unstaged + 1 end -- Count untracked files (?? status) if status_code == "??" then counts.untracked = counts.untracked + 1 end end status_handle:close() return counts end
Then make sure this function is called when building the prompt—look for where the script inserts change info into the prompt string, and replace any old parsing logic with calls to this function.
3. Quick Temporary Workaround
If you need a fix right away without diving into Lua code, roll back to Clink 1.3.3. That should confirm the issue was introduced in the 1.3.4 update, giving you time to test the script changes above.
Since you know Poshgit works correctly, you can also cross-reference its Git status parsing logic to tweak your clink.lua further if needed.
内容的提问来源于stack exchange,提问作者Matt Kerr

