Skip to content

Commit a9418ca

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 a9418ca

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

gitless/cli/file_cmd.py

+9-2
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

+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(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

+2
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

+14
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,20 @@ def test_uncommitted_tracked_changes_that_conflict_append(self):
683683
self.assertTrue('contents 2' in contents)
684684

685685

686+
class TestEmptyDir(TestEndToEnd):
687+
688+
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)
692+
expected_out = 'Empty directory {0} is now a tracked directory'.format(
693+
os.path.join(dir_to_track, ''))
694+
695+
out = utils.stdout(gl.track(dir_to_track_path))
696+
697+
self.assertIn(expected_out, out, 'Empty dir wasn\'t tracked')
698+
699+
686700
class TestPerformance(TestEndToEnd):
687701

688702
FPS_QTY = 10000

0 commit comments

Comments
 (0)