Add descriptions to the plugins
[yap.git] / plugins / tcommit.py
blob2c34d16d03ec324fbe021172fe278bcd2d3c4f9c
2 from yap import YapPlugin
3 from yap.util import get_output, takes_options
4 import pickle
5 import os
7 class TCommitPlugin(YapPlugin):
8 "Provide a 'temporory commit' mechanism"
10 def __init__(self, yap):
11 self.yap = yap
13 def _add_branch(self, branch):
14 repo = get_output("git rev-parse --git-dir")
15 if not repo:
16 return
17 dir = os.path.join(repo[0], 'yap')
18 try:
19 os.mkdir(dir)
20 except OSError:
21 pass
22 state_file = os.path.join(dir, 'tcommit')
24 b = self._get_branches()
25 b.add(branch)
26 pickle.dump(b, file(state_file, 'w'))
28 def _get_branches(self):
29 repo = get_output("git rev-parse --git-dir")
30 state_file = os.path.join(repo[0], 'yap', 'tcommit')
32 try:
33 b = pickle.load(file(state_file))
34 except IOError:
35 b = set()
36 return b
38 def _remove_branch(self, branch):
39 repo = get_output("git rev-parse --git-dir")
40 if not repo:
41 return
42 state_file = os.path.join(repo[0], 'yap', 'tcommit')
44 b = self._get_branches()
45 b.remove(branch)
46 pickle.dump(b, file(state_file, 'w'))
48 @takes_options("t")
49 def cmd_commit(self, *args, **flags):
50 if '-t' in flags:
51 self.yap.cmd_commit(*[], **{'-a': 1, '-m': 'yap wip'})
52 branch = get_output("git symbolic-ref HEAD")
53 if branch:
54 self._add_branch(branch[0])
55 else:
56 self.yap._call_base("cmd_commit", *args, **flags)
58 def post_switch(self):
59 branch = get_output("git symbolic-ref HEAD")
60 if branch[0] in self._get_branches():
61 self.yap.cmd_uncommit()
62 self._remove_branch(branch[0])