Skip to content

Commit e2fcb52

Browse files
author
embs
committed
Allow tracking empty directories
Handle creating and removing placeholders files for versioning empty directories as suggested in [1]. [1]: https://git.wiki.kernel.org/index.php/GitFaq#Can_I_add_empty_directories.3F
1 parent c736e29 commit e2fcb52

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

gitless/cli/file_cmd.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,16 @@ def f(args, repo):
3535

3636
for fp in args.files:
3737
try:
38+
empty_dir = fp.endswith('.glkeep')
3839
getattr(curr_b, subcmd + '_file')(fp)
40+
if empty_dir:
41+
fp = fp.replace('.glkeep', '')
3942
pprint.ok(
40-
'File {0} is now a{1} {2}{3}d file'.format(
43+
'{0} {1} is now a{2} {3}{4}d {5}'.format(
44+
'Empty directory' if empty_dir else 'File',
4145
fp, 'n' if subcmd.startswith(VOWELS) else '', subcmd,
42-
'' if subcmd.endswith('e') else 'e'))
46+
'' if subcmd.endswith('e') else 'e',
47+
'directory' if empty_dir else 'file'))
4348
except KeyError:
4449
pprint.err('Can\'t {0} non-existent file {1}'.format(subcmd, fp))
4550
success = False

gitless/cli/helpers.py

+3
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ def process_paths():
129129
self.skip_dir_cb(curr_dir_rel)
130130
dirs[:] = []
131131
continue
132+
if not fps:
133+
open(curr_dir + "/.glkeep.", "a").close()
134+
fps.append(".glkeep")
132135
for fp in fps:
133136
yield os.path.join(curr_dir_rel, fp)
134137
else:

gitless/tests/test_e2e.py

+12
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,18 @@ def assert_not_in_repo(*cmds):
6363
gl.status, gl.diff, gl.commit, gl.branch, gl.merge, gl.fuse, gl.remote,
6464
gl.publish, gl.history)
6565

66+
class TestTrackEmptyDir(TestEndToEnd):
67+
68+
def test_tracked_empty_dir(self):
69+
dir_to_track = 'wanted_empty_dir'
70+
dir_to_track_path = os.path.join(self.path, dir_to_track)
71+
os.mkdir(dir_to_track_path)
72+
expected_out = 'Empty directory {0}/ is now a tracked directory'.format(
73+
dir_to_track)
74+
75+
out = utils.stdout(gl.track(dir_to_track_path))
76+
77+
self.assertIn(expected_out, out, 'Empty dir wasn\'t tracked')
6678

6779
class TestBasic(TestEndToEnd):
6880

0 commit comments

Comments
 (0)