tcommit: fix copy/paste bug
[yap.git] / plugins / tcommit.py
blobcb68a617975669367c69bb5dc6c9a8e5f7bc4755
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_branch(self, *args, **flags):
62 if '-d' in flags:
63 if args:
64 branch = args[0]
65 self._remove_branch(branch)
67 super(TCommitPlugin, self).cmd_branch(*args, **flags)
69 def cmd_switch(self, *args, **flags):
70 super(TCommitPlugin, self).cmd_switch(*args, **flags)
72 branch = get_output("git symbolic-ref HEAD")
73 if branch[0] in self._get_branches():
74 self.cmd_uncommit()
75 self._remove_branch(branch[0])