Port plugins to the new plugin API
[yap.git] / plugins / tcommit.py
blobe6fd4b28177e645d50471a61d812f2e19adbd950
2 from yap.yap import YapCore
3 from yap.util import get_output, takes_options
4 import pickle
5 import os
7 class TCommitPlugin(YapCore):
8 "Provide a 'temporory commit' mechanism"
10 def _add_branch(self, branch):
11 repo = get_output("git rev-parse --git-dir")
12 if not repo:
13 return
14 dir = os.path.join(repo[0], 'yap')
15 try:
16 os.mkdir(dir)
17 except OSError:
18 pass
19 state_file = os.path.join(dir, 'tcommit')
21 b = self._get_branches()
22 b.add(branch)
23 pickle.dump(b, file(state_file, 'w'))
25 def _get_branches(self):
26 repo = get_output("git rev-parse --git-dir")
27 state_file = os.path.join(repo[0], 'yap', 'tcommit')
29 try:
30 b = pickle.load(file(state_file))
31 except IOError:
32 b = set()
33 return b
35 def _remove_branch(self, branch):
36 repo = get_output("git rev-parse --git-dir")
37 if not repo:
38 return
39 state_file = os.path.join(repo[0], 'yap', 'tcommit')
41 b = self._get_branches()
42 b.remove(branch)
43 pickle.dump(b, file(state_file, 'w'))
45 @takes_options("t")
46 def cmd_commit(self, *args, **flags):
47 if '-t' in flags:
48 override = True
49 args = []
50 flags = {'-a': 1, '-m': 'yap wip'}
51 else:
52 override = False
54 super(TCommitPlugin, self).cmd_commit(*args, **flags)
56 if override is True:
57 branch = get_output("git symbolic-ref HEAD")
58 if branch:
59 self._add_branch(branch[0])
61 def cmd_switch(self, *args, **flags):
62 super(TCommitPlugin, self).cmd_switch(*args, **flags)
64 branch = get_output("git symbolic-ref HEAD")
65 if branch[0] in self._get_branches():
66 self.cmd_uncommit()
67 self._remove_branch(branch[0])