How to add an empty folder in a Mercurial project?

19,380

Solution 1

Mercurial only keeps track of files, not directories.

One solution is to add a .empty file to your repository:

$ touch uploads/.empty
$ hg add uploads/.empty

Solution 2

I have created a python script that automates the process of creating/deleting those files.

Here's the script's source: http://pastebin.com/inbYmMut

#!/usr/bin/python

# Copyright (c) 2011 Ernesto Mendez (der-design.com)
# Dual licensed under the MIT and GPL licenses:
# http://www.opensource.org/licenses/mit-license.php
# http://www.gnu.org/licenses/gpl.html

# Version 1.0.0
# - Initial Release

from __future__ import generators
import sys
from optparse import OptionParser
import os

def main():
    # Process arguments

    if len(args) > 1:
        parser.error('Too many arguments')
        sys.exit()

    elif len(args) == 0:
        parser.error('Missing filename')
        sys.exit()

    if not os.path.exists(options.directory):
        parser.error("%s: No such directory" % options.directory)
        sys.exit()

    filename = args[0]

    # Create generator

    filetree = dirwalk(os.path.abspath(options.directory))

    # Walk directory tree, create files

    if options.remove == True:

        removed = ['Removing the following files: \n']
        cmd = "rm"

        for file in filetree:
            if (os.path.basename(file) == filename):
                removed.append(file)
                cmd += " %s" % fixpath(file)

        if cmd != "rm":
            for f in removed: print f
            os.system(cmd)
        else:
            print "No files named '%s' found" % filename
            sys.exit()

    # Walk directory tree, delete files

    else:

        created = ["Creating the following files:\n"]
        cmd = "touch"

        for file in filetree:
            if (os.path.isdir(file)):
                created.append("%s%s" % (file, filename))
                cmd += " " + fixpath("%s%s" % (file, filename))

        if cmd != "touch":
            for f in created: print f
            os.system(cmd)
        else:
            print "No empty directories found"
            sys.exit()


def dirwalk(dir, giveDirs=1):
    # http://code.activestate.com/recipes/105873-walk-a-directory-tree-using-a-generator/
    for f in os.listdir(dir):
        fullpath = os.path.join(dir, f)
        if os.path.isdir(fullpath) and not os.path.islink(fullpath):
            if not len(os.listdir(fullpath)):
                yield fullpath + os.sep
            else:
                for x in dirwalk(fullpath):  # recurse into subdir
                    if os.path.isdir(x):
                        if giveDirs:
                            yield x
                    else:
                        yield x
        else:
            yield fullpath


def wrap(text, width):
    return reduce(lambda line, word, width=width: '%s%s%s' % (line, ' \n'[(len(line)-line.rfind('\n')-1 + len(word.split('\n', 1)[0] ) >= width)], word), text.split(' ') )


def fixpath(p):
    return shellquote(os.path.normpath(p))


def shellquote(s):
    return "'" + s.replace("'", "'\\''") + "'"


def init_options():
    global parser, options, args
    parser = OptionParser(usage="usage: %prog [options] filename", description="Add or Remove placeholder files for SCM (Source Control Management) tools that do not support empty directories.")
    parser.add_option("-p", "--path", dest="directory", help="search within PATH", metavar="PATH")
    parser.add_option("-r", "--remove", dest="remove", action="store_true", help="remove FILE from PATH, if it's the only file on PATH")

    (options, args) = parser.parse_args()

if __name__ == '__main__':
    print
    init_options()
    main()
    print

Solution 3

You simply do the following:

mkdir images && touch images/.hgkeep
hg add images/.hgkeep
hg commit -m"Add the images folder as an empty folder"

Note the following as a consideration when you do this:

In your case you might be uploading images in your development environment, so I would also recommend adding the following to your .hgignore file so you don't accidentally commit images you did not intend to commit:

^(images)\/(?!\.hgkeep)

The rule will ignore everything on images/** except the .hgkeep file you need to add an "empty" folder to version control. The reason why this rule is important, is that any files in that folder (ie. images/test-image.png will look like a new non-versioned file in your hg status if you don't ignore that pattern.

Share:
19,380

Related videos on Youtube

Natim
Author by

Natim

Work for Ionyse.com My CV here : http://remy.hubscher.crealio.fr/

Updated on September 17, 2022

Comments

  • Natim
    Natim almost 2 years

    In my project, I am using Mercurial and a folder in when the user can upload file. But since the user will upload files, the folder is empty.

    I don't know how I can add this folder to my project without putting any file inside.

    Do you know how I can do ?

  • Martin Geisler
    Martin Geisler over 14 years
    Yes, that is indeed the correct solution: Mercurial is only keeping track of files, not directories. Another solution is to create the empty directories when you deploy your software.
  • User
    User over 11 years
    I'm thinking naming it .hgempty might give a better clue as to what it's for
  • Natim
    Natim over 11 years
    The link is dead.
  • mendezcode
    mendezcode over 11 years
    True, updated link...
  • Phyo Arkar Lwin
    Phyo Arkar Lwin over 11 years
    host it on bitbucket (or ) github , old pastebin is old
  • Daniel Sokolowski
    Daniel Sokolowski over 11 years
    Might as well go for verbose: .hgkeepifempty :)
  • Paul Redmond
    Paul Redmond almost 9 years
    You're right. I've updated my answer to actually answer the question. I've altered my advice and left it because it's important to know and 99% of the time a desired behavior.
  • user3300803
    user3300803 over 8 years
    -1, that script exemplifies nti-patterns and bad practices.
  • aaragon
    aaragon about 6 years
    @PaulRedmond what if images is a directory deep in the path? Something like ./lectures/chapter_10/images? What is then the right syntax?
  • Paul Redmond
    Paul Redmond about 6 years
    @aaragon admittedly it has been a while since I used Mercurial, but you would need to adjust the regex to match patterns you intend. As you notice paths that you expect to be ignored, adjust the regex as needed.