Skip to content

Instantly share code, notes, and snippets.

@vvavrychuk
Created August 17, 2016 11:34
Show Gist options
  • Save vvavrychuk/7d8b767cf66e10b0929fa5172b27deb1 to your computer and use it in GitHub Desktop.
Save vvavrychuk/7d8b767cf66e10b0929fa5172b27deb1 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
"""
Extends git assume unchanged feature to work with directories. For convenience add alias in your gitconfig
[alias]
assume-unchanged = !git-assume-unchanged.py
Usage:
- git assume-unchanged path - assume unchanged all files in the path
- git assume-unchanged -n|--no path - unassume unchanged all files in the path
- git assume-unchanged - print assumed unchanged files or directories
"""
import getopt
import os
import subprocess
import sys
def list_files(directory):
files = subprocess.check_output(["git", "ls-files", directory]).split("\n")
files = filter(lambda item: len(item) > 0, files)
return files
def list_assume_unchanged():
files = subprocess.Popen(['git', 'ls-files', '-v'], stdout=subprocess.PIPE)
files = subprocess.Popen(['grep', '^[[:lower:]]'], stdin=files.stdout, stdout=subprocess.PIPE)
files = subprocess.check_output(['awk', '{print $2}'], stdin=files.stdout).split('\n')
files = filter(lambda item: len(item) > 0, files)
return files
assume_unchanged_files = frozenset(list_assume_unchanged())
def compact_assume_unchanged_it(compact, compact_finished):
compact_new = []
for path in compact:
if (len(compact_new) > 0) and (path.startswith(compact_new[-1])):
continue
parent = os.path.dirname(path)
files = frozenset(list_files(parent))
if files <= assume_unchanged_files:
compact_new.append(parent)
else:
compact_finished.append(path)
return compact_new
def compact_assume_unchanged():
compact_finished = []
compact = list(assume_unchanged_files)
while compact:
compact = compact_assume_unchanged_it(compact, compact_finished)
return compact_finished
def assume_unchanged(path, no):
action = '--no-assume-unchanged' if no else '--assume-unchanged'
subprocess.call('git ls-files %(path)s | xargs git update-index %(action)s' % {'path': path, 'action': action}, shell=True)
optlist, args = getopt.getopt(sys.argv[1:], 'n', ['no', 'help'])
no = optlist[0][0] == '-n' or optlist[0][0] == '--no' if optlist else False
if args:
for arg in args:
assume_unchanged(arg, no)
else:
print("\n".join(compact_assume_unchanged()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment