From 8b0ea26c01b9ff89c1c4642111d3419f77041e6a Mon Sep 17 00:00:00 2001 From: Claudio Bley Date: Tue, 27 Mar 2018 19:30:12 +0200 Subject: [PATCH] Skip next NUL separated git status field for renames In the `-z` git status format, a NUL follows each file name. When files are renamed, the status field is followed by a space, the "to" path, a NUL character, the "from" path and a terminating NUL. This fixes #185. --- lib/colorls/git.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/colorls/git.rb b/lib/colorls/git.rb index a0556f4..ee0c124 100644 --- a/lib/colorls/git.rb +++ b/lib/colorls/git.rb @@ -4,8 +4,13 @@ module ColorLS @git_status = {} IO.popen(['git', '-C', repo_path, 'status', '--porcelain', '-z', '-unormal', '--ignored']) do |output| - output.read.split("\x0").map { |x| x.split(' ', 2) }.each do |mode, file| + while (status_line = output.gets "\x0") + mode, file = status_line.chomp("\x0").split(' ', 2) + @git_status[file] = mode + + # skip the next \x0 separated original path for renames, issue #185 + output.gets("\x0") if mode.start_with? 'R' end end warn "git status failed in #{repo_path}" unless $CHILD_STATUS.success?