2 from yap
import YapPlugin
, YapError
3 from yap
.util
import get_output
, takes_options
, run_command
, run_safely
, short_help
6 class SvnPlugin(YapPlugin
):
7 def __init__(self
, yap
):
10 def _clone_svn(self
, url
, directory
=None, **flags
):
13 directory
= url
.rsplit('/')[-1]
14 directory
= directory
.replace('.git', '')
19 raise YapError("Directory exists: %s" % directory
)
22 os
.system("git svn init %s" % url
)
23 os
.system("git svn fetch -r %s:HEAD" % flags
.get('-r', '1'))
24 self
.yap
.cmd_repo("svn", url
)
25 os
.system("git config yap.svn.enabled 1")
28 enabled
= get_output("git config yap.svn.enabled")
31 def _get_svn_ref(self
):
32 fetch
= get_output("git config svn-remote.svn.fetch")
34 raise YapError("No svn remote configured")
35 ref
= fetch
[0].split(':')[1]
38 # Ensure users don't accidentally kill our "svn" repo
39 def pre_repo(self
, *args
, **flags
):
40 if not self
._enabled
():
42 if '-d' in flags
and args
and args
[0] == "svn":
43 raise YapError("Refusing to delete special svn repository")
46 def cmd_clone(self
, *args
, **flags
):
47 if args
and not run_command("svn info %s" % args
[0]):
48 self
._clone
_svn
(*args
, **flags
)
50 self
.yap
._call
_base
("cmd_commit", *args
, **flags
)
52 def cmd_fetch(self
, *args
, **flags
):
54 if args
and args
[0] == 'svn':
55 os
.system("git svn fetch")
57 self
.yap
._call
_base
("cmd_fetch", *args
, **flags
)
59 def cmd_push(self
, *args
, **flags
):
61 if args
and args
[0] == 'svn':
62 print "Verifying branch is up-to-date"
63 self
.yap
.cmd_fetch("svn")
64 ref
= self
._get
_svn
_ref
()
65 rev
= get_output("git rev-parse %s" % ref
)
67 base
= get_output("git merge-base HEAD %s" % rev
[0])
69 raise YapError("Branch not up-to-date. Update first.")
70 current
= get_output("git symbolic-ref HEAD")
72 raise YapError("Not on a branch!")
73 current
= current
[0].replace('refs/heads/', '')
74 self
.yap
._confirm
_push
(current
, "trunk", "svn")
75 if run_command("git update-index --refresh"):
76 raise YapError("Can't push with uncommitted changes")
77 os
.system("git svn dcommit")
78 run_safely("git svn rebase")
80 self
.yap
._call
_base
("cmd_push", *args
, **flags
)
82 @short_help("change options for the svn plugin")
83 def cmd_svn(self
, subcmd
):
86 if subcmd
not in ["enable"]:
89 if "svn" not in [x
[0] for x
in self
.yap
._list
_remotes
()]:
90 raise YapError("The svn plugin requires a remote named 'svn'")
92 os
.system("git config yap.svn.enabled 1")