f9b43535dda27f1ca575a01d127116e3ee1b4925
[yap.git] / plugins / workdir.py
blobf9b43535dda27f1ca575a01d127116e3ee1b4925
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", dir=dir)
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, e:
46 try:
47 fd = file(lockfile)
48 except:
49 raise e
50 user = fd.readline()
51 # If the workdir has been deleted, break his lock
52 if os.access(user, os.R_OK):
53 raise YapError("That branch is being used by an existing workdir")
54 os.unlink(lockfile)
55 continue
57 def cmd_workdir(self, branch, workdir=None):
58 "<branch> [workdir]"
60 self._check_git()
62 branches = get_output("git for-each-ref --format='%(refname)' 'refs/heads'")
63 if 'refs/heads/%s' % branch not in branches:
64 raise YapError("Not a branch: %s" % branch)
66 current = get_output("git symbolic-ref HEAD")[0]
68 repo = get_output('git rev-parse --git-dir')[0]
69 repo = os.path.join(os.getcwd(), repo)
70 repodir = os.path.dirname(repo)
71 if workdir is None:
72 repoparent, reponame = os.path.split(repodir)
73 workdir = os.path.join(repoparent, "%s-%s" % (reponame, branch))
75 # Make sure the current branch is locked
76 try:
77 self._lock_branch(current.replace('refs/heads/', ''), repodir)
78 except:
79 pass
81 self._lock_branch(branch, workdir)
83 try:
84 os.mkdir(workdir)
85 except OSError, e:
86 raise YapError("Can't create new workdir: %s (%s)" % (workdir, e))
88 os.chdir(workdir)
89 os.mkdir(".git")
90 os.chdir(".git")
92 for x in ["config", "refs", "logs/refs", "objects", "info",
93 "hooks", "packed-refs", "remotes", "svn"]:
94 if os.path.dirname(x):
95 os.makedirs(os.path.dirname(x))
96 os.symlink(os.path.join(repo, x), x)
98 run_safely("cp %s HEAD" % os.path.join(repo, 'HEAD'))
99 os.chdir("..")
100 run_safely("git symbolic-ref HEAD refs/heads/%s" % branch)
101 self.cmd_revert(**{'-a': 1})
103 print "Workdir created at %s for branch %s" % (workdir, branch)
105 def cmd_switch(self, branch, *args, **flags):
106 self._check_git()
108 current = get_output("git symbolic-ref HEAD")[0]
110 repo = get_output('git rev-parse --git-dir')[0]
111 self._lock_branch(branch, repo)
113 try:
114 super(WorkdirPlugin, self).cmd_switch(branch, *args, **flags)
115 except:
116 self._unlock_branch(branch)
117 raise
119 self._unlock_branch(current.replace('refs/heads/', ''))