-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgit-flagged
executable file
·147 lines (128 loc) · 4.47 KB
/
git-flagged
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#! /usr/bin/env python
#
# git-flagged: detect files that are flagged --assume-unchanged
# and/or --skip-worktree
from __future__ import print_function
import argparse
import subprocess
import sys
def reset_flag(bc_filename, pr_filename, flag):
"reset update-index or skip-worktree flag"
ret = subprocess.call(['git', 'update-index', '--no-' + flag, bc_filename])
if ret != 0:
print('failed to clear {} on {} (update-index '
'status {})'.format(flag, pr_filename, ret),
file=sys.stderr)
return ret
def gitescape(path):
"""
Emulate git's trick of escaping unprintable characters in a path.
The path argument is a byte-string so we can just check each
char.
TODO: core.quotePath
"""
result = []
usequotes = False
for c in path:
# py2k: c is str; py3k, c is number
num = ord(c) if isinstance(c, str) else c
if num >= 32 and num < 127 and c not in b'"\\"':
result.append(chr(num))
continue
usequotes = True
if num == 8:
result.append('\\b')
continue
if num == 9:
result.append('\\t')
continue
if num == 10:
result.append('\\n')
continue
if num == 13:
result.append('\\r')
continue
result.append('\\{:03o}'.format(num))
result = ''.join(result)
if usequotes:
return '"{}"'.format(result)
return result
def show_file(args, filename, auf, swf):
"""
Print the file name and its flags.
If args.list is set, just print the file name.
If args.reest is set, announce that we're resetting.
(Note: list + reset = reset with list but without announcements.)
"""
if args.list:
print(filename)
return
flags = []
if auf:
flags.append('--assume-unchanged')
if swf:
flags.append('--skip-worktree')
reset = 'reset ' if args.reset else ''
print('{}: {}{}'.format(filename, reset, ' '.join(flags)))
def main():
parser = argparse.ArgumentParser(description="\
detect git files with assume-unchanged and/or skip-worktree bits")
parser.add_argument('-r', '--reset', action='store_true',
help='clear the flags')
group = parser.add_mutually_exclusive_group()
group.add_argument('-l', '--list', action='store_true',
help='list only the file names, not the flags')
group.add_argument('-q', '--quiet', action='store_false', dest='verbose',
help='inhibit output')
args = parser.parse_args()
proc = subprocess.Popen(['git', 'ls-files', '-vz'],
stdout=subprocess.PIPE)
data = proc.stdout.read()
status = proc.wait()
if status != 0:
# we'll assume Git printed an error
return 1
# if py2k, b'\0' is '\0' and we're good, if py3k, we're good
lines = data.split(b'\0')
errors = 0
for line in lines:
# Git likes \0 terminators rather than separators, so
# we get one empty line at the end.
if len(line) == 0:
continue
# Assume-unchanged => h, skip-worktree => S, both => s.
# Figure out which flag(s) are set, or skip the file.
#
# Note: the weird b'h'[0] etc is a trick to make this
# work regardless of whether we're using py2k (where
# line holds a string and b'h' is a string) or a list
# of bytes (where line[0] is a number, and so is b'h'[0]).
if line[0] == b'h'[0]:
auf, swf = True, False
elif line[0] == b'S'[0]:
auf, swf = False, True
elif line[0] == b's'[0]:
auf, swf = True, True
else:
continue
# Get OS (byte-coded) and printable (pr) form of
# file name, for verbose output.
bc_filename = line[2:]
pr_filename = gitescape(bc_filename)
if args.verbose:
show_file(args, pr_filename, auf, swf)
if args.reset:
# Annoying: update-index must be used
# once per flag.
if auf:
errors |= reset_flag(bc_filename, pr_filename,
'assume-unchanged')
if swf:
errors |= reset_flag(bc_filename, pr_filename,
'skip-worktree')
return errors
if __name__ == '__main__':
try:
sys.exit(main())
except KeyboardInterrupt:
sys.exit('\nInterrupted')