Skip to content

Commit 712fdad

Browse files
committed
Fix Git::Status#untracked when run from worktree subdir
1 parent 6a59bc8 commit 712fdad

File tree

3 files changed

+54
-7
lines changed

3 files changed

+54
-7
lines changed

lib/git/lib.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,9 @@ def ignored_files
600600
command_lines('ls-files', '--others', '-i', '--exclude-standard')
601601
end
602602

603+
def untracked_files
604+
command_lines('ls-files', '--others', '--exclude-standard', chdir: @git_work_dir)
605+
end
603606

604607
def config_remote(name)
605608
hsh = {}

lib/git/status.rb

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,7 @@ def construct_status
170170
end
171171

172172
def fetch_untracked
173-
ignore = @base.lib.ignored_files
174-
175-
root_dir = @base.dir.path
176-
Dir.glob('**/*', File::FNM_DOTMATCH, base: root_dir) do |file|
177-
next if @files[file] || File.directory?(File.join(root_dir, file)) ||
178-
ignore.include?(file) || file =~ %r{^.git\/.+}
179-
173+
@base.lib.untracked_files.each do |file|
180174
@files[file] = { path: file, untracked: true }
181175
end
182176
end

tests/units/test_status.rb

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,56 @@ def test_deleted_boolean
8787
end
8888
end
8989

90+
def test_untracked
91+
in_temp_dir do |path|
92+
`git init`
93+
File.write('file1', 'contents1')
94+
File.write('file2', 'contents2')
95+
Dir.mkdir('subdir')
96+
File.write('subdir/file3', 'contents3')
97+
File.write('subdir/file4', 'contents4')
98+
`git add file1 subdir/file3`
99+
`git commit -m "my message"`
100+
101+
git = Git.open('.')
102+
assert_equal(2, git.status.untracked.size)
103+
assert_equal(['file2', 'subdir/file4'], git.status.untracked.keys)
104+
end
105+
end
106+
107+
def test_untracked_no_untracked_files
108+
in_temp_dir do |path|
109+
`git init`
110+
File.write('file1', 'contents1')
111+
Dir.mkdir('subdir')
112+
File.write('subdir/file3', 'contents3')
113+
`git add file1 subdir/file3`
114+
`git commit -m "my message"`
115+
116+
git = Git.open('.')
117+
assert_equal(0, git.status.untracked.size)
118+
end
119+
end
120+
121+
def test_untracked_from_subdir
122+
in_temp_dir do |path|
123+
`git init`
124+
File.write('file1', 'contents1')
125+
File.write('file2', 'contents2')
126+
Dir.mkdir('subdir')
127+
File.write('subdir/file3', 'contents3')
128+
File.write('subdir/file4', 'contents4')
129+
`git add file1 subdir/file3`
130+
`git commit -m "my message"`
131+
132+
Dir.chdir('subdir') do
133+
git = Git.open('..')
134+
assert_equal(2, git.status.untracked.size)
135+
assert_equal(['file2', 'subdir/file4'], git.status.untracked.keys)
136+
end
137+
end
138+
end
139+
90140
def test_untracked_boolean
91141
in_temp_dir do |path|
92142
git = Git.clone(@wdir, 'test_dot_files_status')

0 commit comments

Comments
 (0)