As you move from Subversion to Git, you have to remember to add an empty directory to your repository. Git doesn't version control empty directories, so you'll need to put some files there. Some files are `.gitignore``` and ```empty```, but I personally try to put
`.gitkeep``` in a Rails style.
Basically, you can run find. -Type d -name .git -prune -o -type d -empty -exec touch {} /. Gitkeep \;` ``. The tool I made this time is optional, such as the function to check which directory is empty before placing `` .gitkeep
and the ability to specify the `` `.gitkeep``` part of this command. It is an added function.
List empty directories:
cd git-repo
git-empty-dir list
Place .gitkeep
in an empty directory:
cd git-repo
git-empty-dir keep
There are other options such as `--dir``` and
`--keeper```.
$ git-empty-dir keep --help
usage: git-empty-dir keep [-h] [--dir DIR] [--keeper KEEPER]
optional arguments:
-h, --help show this help message and exit
--dir DIR path to git directory
--keeper KEEPER dummy file name. Default is ".gitkeep"
git-empry-dir.py
git-empry-dir.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import argparse
import commands
import sys
def list(args):
command = "find %(dir)s -type d -name .git -prune -o -type d -empty" % vars(args)
result = commands.getstatusoutput(command)
print result[1]
if result[0] > 0:
sys.exit(1)
def keep(args):
command = "find %(dir)s -type d -name .git -prune -o -type d -empty -exec touch {}/%(keeper)s \;" % vars(args)
result = commands.getstatusoutput(command)
print result[1]
if result[0] > 0:
sys.exit(1)
def check_git_dir(dir):
git_dir = dir + '/.git'
if os.path.isdir(git_dir) == False or os.path.islink(git_dir) == True:
print 'Not found .git in ' + dir
sys.exit(1)
def main():
parser = argparse.ArgumentParser(description='Add .gitkeep to empty directories.')
subparsers = parser.add_subparsers(title='commands', metavar='command')
parser_list = subparsers.add_parser('list', help='list candidates')
parser_list.set_defaults(func=list)
parser_list.add_argument('--dir', type=str, help='path to git directory', default=os.getcwd())
parser_keep = subparsers.add_parser('keep', help='')
parser_keep.set_defaults(func=keep)
parser_keep.add_argument('--dir', type=str, help='path to git directory', default=os.getcwd())
parser_keep.add_argument('--keeper', type=str, help='dummy file name. Default is ".gitkeep"', default='.gitkeep')
args = parser.parse_args()
args.dir = args.dir.rstrip('/')
check_git_dir(args.dir)
args.func(args)
if __name__ == "__main__":
main()
Python's argparse
is useful!
[2012-04-05 11:55] I accidentally kept it in the absence of .git, which was disastrous, so I checked for .git.
Recommended Posts