blob: 6e28b3e342b066f582fd46c48153f90a8038ebf9 [file] [log] [blame]
[email protected]97345eb2014-03-13 07:55:151#!/usr/local/bin/python
2import sys
3import re
4import optparse
5
6import subcommand
7import subprocess2
8
9from git_common import run, stream
10
11FREEZE = 'FREEZE'
12SECTIONS = {
13 'indexed': 'soft',
14 'unindexed': 'mixed'
15}
16MATCHER = re.compile(r'%s.(%s)' % (FREEZE, '|'.join(SECTIONS)))
17
18
19def freeze():
20 took_action = False
21
22 try:
23 run('commit', '-m', FREEZE + '.indexed')
24 took_action = True
25 except subprocess2.CalledProcessError:
26 pass
27
28 try:
29 run('add', '-A')
30 run('commit', '-m', FREEZE + '.unindexed')
31 took_action = True
32 except subprocess2.CalledProcessError:
33 pass
34
35 if not took_action:
36 return 'Nothing to freeze.'
37
38
39def thaw():
40 took_action = False
41 for sha in (s.strip() for s in stream('rev-list', 'HEAD').xreadlines()):
42 msg = run('show', '--format=%f%b', '-s', 'HEAD')
43 match = MATCHER.match(msg)
44 if not match:
45 if not took_action:
46 return 'Nothing to thaw.'
47 break
48
49 run('reset', '--' + SECTIONS[match.group(1)], sha)
50 took_action = True
51
52
53def CMDfreeze(parser, args): # pragma: no cover
54 """Freeze a branch's changes."""
55 parser.parse_args(args)
56 return freeze()
57
58
59def CMDthaw(parser, args): # pragma: no cover
60 """Returns a frozen branch to the state before it was frozen."""
61 parser.parse_args(args)
62 return thaw()
63
64
65def main(): # pragma: no cover
66 dispatcher = subcommand.CommandDispatcher(__name__)
67 ret = dispatcher.execute(optparse.OptionParser(), sys.argv[1:])
68 if ret:
69 print ret
70
71
72if __name__ == '__main__': # pragma: no cover
73 main()