Skip to content

Commit 6b7a7ad

Browse files
author
embs
committed
Handle empty dirs in gl status
So users can operate (track, untrack, etc.) on them if they wish.
1 parent a9418ca commit 6b7a7ad

File tree

3 files changed

+49
-6
lines changed

3 files changed

+49
-6
lines changed

gitless/cli/gl_status.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,10 @@ def _print_tracked_mod_files(tracked_mod_list, relative_paths, repo):
8484
for f in tracked_mod_list:
8585
exp = ''
8686
color = colored.yellow
87+
is_keep_file = f.fp.endswith(core.GL_KEEP_FILENAME)
8788
if not f.exists_at_head:
88-
exp = ' (new file)'
89+
kind = 'directory' if is_keep_file else 'file'
90+
exp = ' (new {0})'.format(kind)
8991
color = colored.green
9092
elif not f.exists_in_wd:
9193
exp = ' (deleted)'
@@ -97,6 +99,8 @@ def _print_tracked_mod_files(tracked_mod_list, relative_paths, repo):
9799
fp = os.path.relpath(os.path.join(root, f.fp)) if relative_paths else f.fp
98100
if fp == '.':
99101
continue
102+
if is_keep_file:
103+
fp = fp.replace(core.GL_KEEP_FILENAME, '')
100104

101105
pprint.item(color(fp), opt_text=exp)
102106

gitless/core.py

+8
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,14 @@ def status(self):
771771
yield self.FileStatus(
772772
fp, GL_STATUS_UNTRACKED, True, exists_in_wd, True, False)
773773

774+
# find untracked empty dirs
775+
for dirpath, dirs, files in os.walk('.', topdown=True):
776+
dirs[:] = [d for d in dirs if d not in ['.git'] and not
777+
self.path_is_ignored(d)]
778+
if not dirs and not files:
779+
yield self.FileStatus(dirpath, GL_STATUS_UNTRACKED, False, True, False,
780+
False)
781+
774782
def status_file(self, path):
775783
"""Return the status (see FileStatus) of the given path."""
776784
return self._status_file(path)[0]

gitless/tests/test_e2e.py

+36-5
Original file line numberDiff line numberDiff line change
@@ -685,17 +685,48 @@ def test_uncommitted_tracked_changes_that_conflict_append(self):
685685

686686
class TestEmptyDir(TestEndToEnd):
687687

688+
def test_empty_dir_status(self):
689+
untracked_empty_dir = self._mk_empty_dir('untracked_empty_dir')
690+
691+
out = utils.stdout(gl.status())
692+
693+
self.assertIn(untracked_empty_dir, out, 'Empty dir didn\'t appear in status')
694+
695+
def test_ignored_empty_dir_status(self):
696+
ignored_empty_dir = self._mk_empty_dir('ignored_empty_dir')
697+
utils.write_file(os.path.join(self.path, '.gitignore'), ignored_empty_dir)
698+
699+
out = utils.stdout(gl.status())
700+
701+
self.assertFalse(ignored_empty_dir in out,
702+
'Ignored empty dir was listed in status')
703+
688704
def test_track_empty_dir(self):
689-
dir_to_track = 'wanted_empty_dir'
690-
dir_to_track_path = os.path.join(self.path, dir_to_track)
691-
os.mkdir(dir_to_track_path)
705+
dir_to_track = self._mk_empty_dir('wanted_empty_dir')
692706
expected_out = 'Empty directory {0} is now a tracked directory'.format(
693-
os.path.join(dir_to_track, ''))
707+
self._dir_path(dir_to_track))
694708

695-
out = utils.stdout(gl.track(dir_to_track_path))
709+
out = utils.stdout(gl.track(dir_to_track))
696710

697711
self.assertIn(expected_out, out, 'Empty dir wasn\'t tracked')
698712

713+
def test_tracked_empty_dir_status(self):
714+
tracked_empty_dir = self._mk_empty_dir('tracked_empty_dir')
715+
gl.track(tracked_empty_dir)
716+
expected_out = '{0} (new directory)'.format(
717+
self._dir_path(tracked_empty_dir))
718+
719+
out = utils.stdout(gl.status())
720+
721+
self.assertIn(expected_out, out, 'Didn\'t report newly tracked dir')
722+
723+
def _mk_empty_dir(self, name):
724+
os.mkdir(os.path.join(self.path, name))
725+
return name
726+
727+
def _dir_path(self, path):
728+
return os.path.join(path, '')
729+
699730

700731
class TestPerformance(TestEndToEnd):
701732

0 commit comments

Comments
 (0)