workdir: create the lock dir if needed on unlock
[yap.git] / plugins / workdir.py
blobf6c1bec6d838635e6c48694a1f3f00bd4c35f428
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 try:
18 os.mkdir(dir)
19 except OSError:
20 pass
22 lockfile = os.path.join(dir, branch.replace('/', '\/'))
24 try:
25 os.unlink(lockfile)
26 except OSError:
27 pass
29 def _lock_branch(self, branch, locked_by):
30 repo = get_output('git rev-parse --git-dir')[0]
31 dir = os.path.join(repo, 'yap', 'lock')
32 try:
33 os.mkdir(dir)
34 except OSError:
35 pass
37 fd, tmplock = tempfile.mkstemp("yap")
38 os.write(fd, locked_by)
39 os.close(fd)
40 while True:
41 lockfile = os.path.join(dir, branch.replace('/', '\/'))
42 try:
43 os.link(tmplock, lockfile)
44 break
45 except OSError:
46 fd = file(lockfile)
47 user = fd.readline()
48 # If the workdir has been deleted, break his lock
49 if os.access(user, os.R_OK):
50 raise YapError("That branch is being used by an existing workdir")
51 os.unlink(lockfile)
52 continue
54 def cmd_workdir(self, branch, workdir=None):
55 "<branch> [workdir]"
57 self._check_git()
59 branches = get_output("git for-each-ref --format='%(refname)' 'refs/heads'")
60 if 'refs/heads/%s' % branch not in branches:
61 raise YapError("Not a branch: %s" % branch)
63 current = get_output("git symbolic-ref HEAD")[0]
65 repo = get_output('git rev-parse --git-dir')[0]
66 repo = os.path.join(os.getcwd(), repo)
67 repodir = os.path.dirname(repo)
68 if workdir is None:
69 repoparent, reponame = os.path.split(repodir)
70 workdir = os.path.join(repoparent, "%s-%s" % (reponame, branch))
72 # Make sure the current branch is locked
73 try:
74 self._lock_branch(current.replace('refs/heads/', ''), repodir)
75 except:
76 pass
78 self._lock_branch(branch, workdir)
80 try:
81 os.mkdir(workdir)
82 except OSError, e:
83 raise YapError("Can't create new workdir: %s (%s)" % (workdir, e))
85 os.chdir(workdir)
86 os.mkdir(".git")
87 os.chdir(".git")
89 for x in ["config", "refs", "logs/refs", "objects", "info",
90 "hooks", "packed-refs", "remotes", "svn"]:
91 if os.path.dirname(x):
92 os.makedirs(os.path.dirname(x))
93 os.symlink(os.path.join(repo, x), x)
95 run_safely("cp %s HEAD" % os.path.join(repo, 'HEAD'))
96 os.chdir("..")
97 run_safely("git symbolic-ref HEAD refs/heads/%s" % branch)
98 self.cmd_revert(**{'-a': 1})
100 print "Workdir created at %s for branch %s" % (workdir, branch)
102 def cmd_switch(self, branch, *args, **flags):
103 self._check_git()
105 current = get_output("git symbolic-ref HEAD")[0]
107 repo = get_output('git rev-parse --git-dir')[0]
108 self._lock_branch(branch, repo)
110 try:
111 super(WorkdirPlugin, self).cmd_switch(branch, *args, **flags)
112 except:
113 self._unlock_branch(branch)
114 raise
116 self._unlock_branch(current.replace('refs/heads/', ''))