workdir: add usage and a completion message
[yap.git] / plugins / workdir.py
blobe08b48b1aea1308437e8d7cde9348967eec8969a
2 from yap.yap import YapCore, YapError
3 from yap.util import get_output, takes_options, run_safely
5 import os
6 import tempfile
8 class WorkdirPlugin(YapCore):
9 "Create extra work directories of a repository"
11 def __init__(self, *args, **flags):
12 super(WorkdirPlugin, self).__init__(*args, **flags)
14 def _unlock_branch(self, branch):
15 repo = get_output('git rev-parse --git-dir')[0]
16 dir = os.path.join(repo, 'yap', 'lock')
17 lockfile = os.path.join(dir, branch.replace('/', '\/'))
19 try:
20 os.unlink(lockfile)
21 except OSError:
22 pass
24 def _lock_branch(self, branch, locked_by):
25 repo = get_output('git rev-parse --git-dir')[0]
26 dir = os.path.join(repo, 'yap', 'lock')
27 try:
28 os.mkdir(dir)
29 except OSError:
30 pass
32 fd, tmplock = tempfile.mkstemp("yap")
33 os.write(fd, locked_by)
34 os.close(fd)
35 while True:
36 lockfile = os.path.join(dir, branch.replace('/', '\/'))
37 try:
38 os.link(tmplock, lockfile)
39 break
40 except OSError:
41 fd = file(lockfile)
42 user = fd.readline()
43 # If the workdir has been deleted, break his lock
44 if os.access(user, os.R_OK):
45 raise YapError("That branch is being used by an existing workdir")
46 os.unlink(lockfile)
47 continue
49 def cmd_workdir(self, branch, workdir=None):
50 "<branch> [workdir]"
52 self._check_git()
54 branches = get_output("git for-each-ref --format='%(refname)' 'refs/heads'")
55 if 'refs/heads/%s' % branch not in branches:
56 raise YapError("Not a branch: %s" % branch)
58 current = get_output("git symbolic-ref HEAD")[0]
60 repo = get_output('git rev-parse --git-dir')[0]
61 repo = os.path.join(os.getcwd(), repo)
62 repodir = os.path.dirname(repo)
63 if workdir is None:
64 repoparent, reponame = os.path.split(repodir)
65 workdir = os.path.join(repoparent, "%s-%s" % (reponame, branch))
67 # Make sure the current branch is locked
68 try:
69 self._lock_branch(current.replace('refs/heads/', ''), repodir)
70 except:
71 pass
73 self._lock_branch(branch, workdir)
75 try:
76 os.mkdir(workdir)
77 except OSError, e:
78 raise YapError("Can't create new workdir: %s (%s)" % (workdir, e))
80 os.chdir(workdir)
81 os.mkdir(".git")
82 os.chdir(".git")
84 for x in ["config", "refs", "logs/refs", "objects", "info",
85 "hooks", "packed-refs", "remotes", "svn"]:
86 if os.path.dirname(x):
87 os.makedirs(os.path.dirname(x))
88 os.symlink(os.path.join(repo, x), x)
90 run_safely("cp %s HEAD" % os.path.join(repo, 'HEAD'))
91 os.chdir("..")
92 run_safely("git symbolic-ref HEAD refs/heads/%s" % branch)
93 self.cmd_revert(**{'-a': 1})
95 print "Workdir created at %s for branch %s" % (workdir, branch)
97 def cmd_switch(self, branch, *args, **flags):
98 self._check_git()
100 current = get_output("git symbolic-ref HEAD")[0]
102 repo = get_output('git rev-parse --git-dir')[0]
103 self._lock_branch(branch, repo)
105 try:
106 super(WorkdirPlugin, self).cmd_switch(branch, *args, **flags)
107 except:
108 self._unlock_branch(branch)
109 raise
111 self._unlock_branch(current.replace('refs/heads/', ''))