Skip to content

Commit 7b3d870

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 190611e commit 7b3d870

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

gitless/cli/file_cmd.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
from __future__ import unicode_literals
99

10+
from gitless import core
11+
1012
from . import helpers, pprint
1113

1214

@@ -35,11 +37,16 @@ def f(args, repo):
3537

3638
for fp in args.files:
3739
try:
40+
empty_dir = fp.endswith(core.GL_KEEP_FILENAME)
3841
getattr(curr_b, subcmd + '_file')(fp)
42+
if empty_dir:
43+
fp = fp.replace(core.GL_KEEP_FILENAME, '')
3944
pprint.ok(
40-
'File {0} is now a{1} {2}{3}d file'.format(
45+
'{0} {1} is now a{2} {3}{4}d {5}'.format(
46+
'Empty directory' if empty_dir else 'File',
4147
fp, 'n' if subcmd.startswith(VOWELS) else '', subcmd,
42-
'' if subcmd.endswith('e') else 'e'))
48+
'' if subcmd.endswith('e') else 'e',
49+
'directory' if empty_dir else 'file'))
4350
except KeyError:
4451
pprint.err('Can\'t {0} non-existent file {1}'.format(subcmd, fp))
4552
success = False

gitless/cli/helpers.py

Lines changed: 3 additions & 0 deletions
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(os.path.join(curr_dir, core.GL_KEEP_FILENAME), 'a').close()
134+
fps.append(core.GL_KEEP_FILENAME)
132135
for fp in fps:
133136
yield os.path.join(curr_dir_rel, fp)
134137
else:

gitless/core.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ class ApplyFailedError(GlError): pass
5454
GL_STATUS_TRACKED = 2
5555
GL_STATUS_IGNORED = 3
5656

57+
GL_KEEP_FILENAME = '.glkeep'
58+
5759

5860
def init_repository(url=None):
5961
"""Creates a new Gitless's repository in the cwd.

gitless/tests/test_e2e.py

Lines changed: 12 additions & 0 deletions
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+
os.path.join(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)