b4a901ea65186c14830cc374fba84ccda6a9c0d9
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')
17 lockfile
= os
.path
.join(dir, branch
.replace('/', '\/'))
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')
32 fd
, tmplock
= tempfile
.mkstemp("yap")
33 os
.write(fd
, locked_by
)
36 lockfile
= os
.path
.join(dir, branch
.replace('/', '\/'))
38 os
.link(tmplock
, lockfile
)
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")
49 def cmd_workdir(self
, branch
, workdir
=None):
52 branches
= get_output("git for-each-ref --format='%(refname)' 'refs/heads'")
53 if 'refs/heads/%s' % branch
not in branches
:
54 raise YapError("Not a branch: %s" % branch
)
56 current
= get_output("git symbolic-ref HEAD")[0]
58 repo
= get_output('git rev-parse --git-dir')[0]
59 repo
= os
.path
.join(os
.getcwd(), repo
)
60 repodir
= os
.path
.dirname(repo
)
62 repoparent
, reponame
= os
.path
.split(repodir
)
63 workdir
= os
.path
.join(repoparent
, "%s-%s" % (reponame
, branch
))
65 # Make sure the current branch is locked
67 self
._lock
_branch
(current
.replace('refs/heads/', ''), repodir
)
71 self
._lock
_branch
(branch
, workdir
)
76 raise YapError("Can't create new workdir: %s (%s)" % (workdir
, e
))
82 for x
in ["config", "refs", "logs/refs", "objects", "info",
83 "hooks", "packed-refs", "remotes", "svn"]:
84 if os
.path
.dirname(x
):
85 os
.makedirs(os
.path
.dirname(x
))
86 os
.symlink(os
.path
.join(repo
, x
), x
)
88 run_safely("cp %s HEAD" % os
.path
.join(repo
, 'HEAD'))
90 run_safely("git symbolic-ref HEAD refs/heads/%s" % branch
)
91 self
.cmd_revert(**{'-a': 1})
93 def cmd_switch(self
, branch
, *args
, **flags
):
96 current
= get_output("git symbolic-ref HEAD")[0]
98 repo
= get_output('git rev-parse --git-dir')[0]
99 self
._lock
_branch
(branch
, repo
)
102 super(WorkdirPlugin
, self
).cmd_switch(branch
, *args
, **flags
)
104 self
._unlock
_branch
(branch
)
107 self
._unlock
_branch
(current
.replace('refs/heads/', ''))