Command line tool to put .gitkeep in an empty directory

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.

How to use

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

Command line tool to put .gitkeep in an empty directory
Procedure memo to put AWS command line interface in CentOS6
A note I looked up to make a command line tool in Python
How to receive command line arguments in Python
How to create an article from the command line
How to make an interactive CLI tool in Golang
I wrote an empty directory automatic creation script in Python
How to specify command line arguments when debugging in PyCharm
How to get a string from a command line argument in python
An easy way to re-execute a previously executed command in ipython
Create a command line tool to convert dollars to yen using Python
Command line tool for Chainer ChainerCMD
In the python command python points to python3.8
Try to put data in MongoDB
An alternative to `pause` in Python
Create an empty array in Numpy to add rows for each loop
I made a CLI tool to convert images in each directory to PDF
I want to leave an arbitrary command in the command history of Shell
Pin current directory to script directory in Python
[Itertools.permutations] How to put permutations in Python
PUT gzip directly to S3 in Python
To win in Forex Part 4 ~ LINE Notification
Decompose command arguments in one line in Python
[V11 ~] A memorandum to put in Misskey
Shell command to visualize line feed code
Directory move tool to Python package (pycd)
I made an appdo command to execute a command in the context of the app
Build a command line app in Python to understand setup.py, argparse, GitHub Actions
How to manage arguments when implementing a Python script as a command line tool