2 from yap
.yap
import YapCore
, YapError
3 from yap
.util
import get_output
, takes_options
, run_safely
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')
22 lockfile
= os
.path
.join(dir, branch
.replace('/', '\/'))
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')
37 fd
, tmplock
= tempfile
.mkstemp("yap")
38 os
.write(fd
, locked_by
)
41 lockfile
= os
.path
.join(dir, branch
.replace('/', '\/'))
43 os
.link(tmplock
, lockfile
)
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")
54 def cmd_workdir(self
, branch
, workdir
=None):
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
)
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
74 self
._lock
_branch
(current
.replace('refs/heads/', ''), repodir
)
78 self
._lock
_branch
(branch
, workdir
)
83 raise YapError("Can't create new workdir: %s (%s)" % (workdir
, e
))
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'))
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
):
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
)
111 super(WorkdirPlugin
, self
).cmd_switch(branch
, *args
, **flags
)
113 self
._unlock
_branch
(branch
)
116 self
._unlock
_branch
(current
.replace('refs/heads/', ''))