author | wenzelm |
Wed, 26 Oct 2016 16:04:05 +0200 | |
changeset 64408 | 50bcf976f276 |
parent 64348 | 4c253e84ae62 |
child 64451 | cdbfa9f64110 |
permissions | -rw-r--r-- |
63997
e11ccb5aa82f
more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff
changeset
|
1 |
/* Title: Pure/General/mercurial.scala |
e11ccb5aa82f
more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff
changeset
|
2 |
Author: Makarius |
e11ccb5aa82f
more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff
changeset
|
3 |
|
64280 | 4 |
Support for Mercurial repositories, with local or remote repository clone |
5 |
and working directory (via ssh connection). |
|
63997
e11ccb5aa82f
more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff
changeset
|
6 |
*/ |
e11ccb5aa82f
more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff
changeset
|
7 |
|
e11ccb5aa82f
more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff
changeset
|
8 |
package isabelle |
e11ccb5aa82f
more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff
changeset
|
9 |
|
e11ccb5aa82f
more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff
changeset
|
10 |
|
e11ccb5aa82f
more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff
changeset
|
11 |
import java.io.{File => JFile} |
e11ccb5aa82f
more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff
changeset
|
12 |
|
e11ccb5aa82f
more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff
changeset
|
13 |
|
e11ccb5aa82f
more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff
changeset
|
14 |
object Mercurial |
e11ccb5aa82f
more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff
changeset
|
15 |
{ |
e11ccb5aa82f
more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff
changeset
|
16 |
/* command-line syntax */ |
e11ccb5aa82f
more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff
changeset
|
17 |
|
63999 | 18 |
def optional(s: String, prefix: String = ""): String = |
64304 | 19 |
if (s == "") "" else " " + prefix + " " + Bash.string(s) |
63999 | 20 |
|
21 |
def opt_flag(flag: String, b: Boolean): String = if (b) " " + flag else "" |
|
22 |
def opt_rev(s: String): String = optional(s, "--rev") |
|
23 |
def opt_template(s: String): String = optional(s, "--template") |
|
63997
e11ccb5aa82f
more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff
changeset
|
24 |
|
e11ccb5aa82f
more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff
changeset
|
25 |
|
e11ccb5aa82f
more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff
changeset
|
26 |
/* repository access */ |
e11ccb5aa82f
more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff
changeset
|
27 |
|
64330 | 28 |
def is_repository(root: Path, ssh: Option[SSH.Session] = None): Boolean = |
29 |
new Repository(root, ssh).command("root").ok |
|
30 |
||
64167
097d122222f6
support remote repositories via ssh command execution;
wenzelm
parents:
64162
diff
changeset
|
31 |
def repository(root: Path, ssh: Option[SSH.Session] = None): Repository = |
64168 | 32 |
{ |
33 |
val hg = new Repository(root, ssh) |
|
34 |
hg.command("root").check |
|
35 |
hg |
|
36 |
} |
|
37 |
||
38 |
def clone_repository( |
|
64230 | 39 |
source: String, root: Path, options: String = "", ssh: Option[SSH.Session] = None): Repository = |
64168 | 40 |
{ |
64230 | 41 |
val hg = new Repository(root, ssh) |
64255 | 42 |
ssh match { |
43 |
case None => Isabelle_System.mkdirs(hg.root.dir) |
|
64256
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64255
diff
changeset
|
44 |
case Some(ssh) => ssh.mkdirs(hg.root.dir) |
64255 | 45 |
} |
64304 | 46 |
hg.command("clone", Bash.string(source) + " " + File.bash_path(hg.root), options).check |
64168 | 47 |
hg |
48 |
} |
|
63997
e11ccb5aa82f
more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff
changeset
|
49 |
|
64230 | 50 |
def setup_repository(source: String, root: Path, ssh: Option[SSH.Session] = None): Repository = |
64252
e84cba30d7ff
clarified setup_repository: more uniform pull vs. clone, without update;
wenzelm
parents:
64233
diff
changeset
|
51 |
{ |
e84cba30d7ff
clarified setup_repository: more uniform pull vs. clone, without update;
wenzelm
parents:
64233
diff
changeset
|
52 |
val present = |
e84cba30d7ff
clarified setup_repository: more uniform pull vs. clone, without update;
wenzelm
parents:
64233
diff
changeset
|
53 |
ssh match { |
e84cba30d7ff
clarified setup_repository: more uniform pull vs. clone, without update;
wenzelm
parents:
64233
diff
changeset
|
54 |
case None => root.is_dir |
64256
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64255
diff
changeset
|
55 |
case Some(ssh) => ssh.is_dir(root) |
64252
e84cba30d7ff
clarified setup_repository: more uniform pull vs. clone, without update;
wenzelm
parents:
64233
diff
changeset
|
56 |
} |
e84cba30d7ff
clarified setup_repository: more uniform pull vs. clone, without update;
wenzelm
parents:
64233
diff
changeset
|
57 |
if (present) { val hg = repository(root, ssh = ssh); hg.pull(); hg } |
e84cba30d7ff
clarified setup_repository: more uniform pull vs. clone, without update;
wenzelm
parents:
64233
diff
changeset
|
58 |
else clone_repository(source, root, options = "--noupdate", ssh = ssh) |
e84cba30d7ff
clarified setup_repository: more uniform pull vs. clone, without update;
wenzelm
parents:
64233
diff
changeset
|
59 |
} |
64230 | 60 |
|
61 |
class Repository private[Mercurial](root_path: Path, ssh: Option[SSH.Session]) |
|
63997
e11ccb5aa82f
more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff
changeset
|
62 |
{ |
64167
097d122222f6
support remote repositories via ssh command execution;
wenzelm
parents:
64162
diff
changeset
|
63 |
hg => |
097d122222f6
support remote repositories via ssh command execution;
wenzelm
parents:
64162
diff
changeset
|
64 |
|
64230 | 65 |
val root = |
66 |
ssh match { |
|
67 |
case None => root_path.expand |
|
64256
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64255
diff
changeset
|
68 |
case Some(ssh) => root_path.expand_env(ssh.settings) |
64230 | 69 |
} |
70 |
||
64347 | 71 |
def root_url: String = |
72 |
ssh match { |
|
73 |
case None => root.implode |
|
74 |
case Some(ssh) => ssh.hg_url + root.implode |
|
75 |
} |
|
76 |
||
64167
097d122222f6
support remote repositories via ssh command execution;
wenzelm
parents:
64162
diff
changeset
|
77 |
override def toString: String = |
097d122222f6
support remote repositories via ssh command execution;
wenzelm
parents:
64162
diff
changeset
|
78 |
ssh match { |
097d122222f6
support remote repositories via ssh command execution;
wenzelm
parents:
64162
diff
changeset
|
79 |
case None => root.implode |
64256
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64255
diff
changeset
|
80 |
case Some(ssh) => ssh.toString + ":" + root.implode |
64167
097d122222f6
support remote repositories via ssh command execution;
wenzelm
parents:
64162
diff
changeset
|
81 |
} |
63997
e11ccb5aa82f
more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff
changeset
|
82 |
|
64168 | 83 |
def command(name: String, args: String = "", options: String = ""): Process_Result = |
64167
097d122222f6
support remote repositories via ssh command execution;
wenzelm
parents:
64162
diff
changeset
|
84 |
{ |
097d122222f6
support remote repositories via ssh command execution;
wenzelm
parents:
64162
diff
changeset
|
85 |
val cmdline = |
64168 | 86 |
"\"${HG:-hg}\"" + |
64230 | 87 |
(if (name == "clone") "" else " --repository " + File.bash_path(root)) + |
64168 | 88 |
" --noninteractive " + name + " " + options + " " + args |
64167
097d122222f6
support remote repositories via ssh command execution;
wenzelm
parents:
64162
diff
changeset
|
89 |
ssh match { |
097d122222f6
support remote repositories via ssh command execution;
wenzelm
parents:
64162
diff
changeset
|
90 |
case None => Isabelle_System.bash(cmdline) |
64256
c3197aeae90b
simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents:
64255
diff
changeset
|
91 |
case Some(ssh) => ssh.execute(cmdline) |
64167
097d122222f6
support remote repositories via ssh command execution;
wenzelm
parents:
64162
diff
changeset
|
92 |
} |
097d122222f6
support remote repositories via ssh command execution;
wenzelm
parents:
64162
diff
changeset
|
93 |
} |
63999 | 94 |
|
95 |
def heads(template: String = "{node|short}\n", options: String = ""): List[String] = |
|
64168 | 96 |
hg.command("heads", opt_template(template), options).check.out_lines |
63999 | 97 |
|
64232
367d83d6030e
clarified hg.id operation, with explicit tip as default;
wenzelm
parents:
64230
diff
changeset
|
98 |
def identify(rev: String = "tip", options: String = ""): String = |
64168 | 99 |
hg.command("id", opt_rev(rev), options).check.out_lines.headOption getOrElse "" |
63998 | 100 |
|
64232
367d83d6030e
clarified hg.id operation, with explicit tip as default;
wenzelm
parents:
64230
diff
changeset
|
101 |
def id(rev: String = "tip"): String = identify(rev, options = "-i") |
367d83d6030e
clarified hg.id operation, with explicit tip as default;
wenzelm
parents:
64230
diff
changeset
|
102 |
|
63998 | 103 |
def manifest(rev: String = "", options: String = ""): List[String] = |
64168 | 104 |
hg.command("manifest", opt_rev(rev), options).check.out_lines |
63997
e11ccb5aa82f
more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff
changeset
|
105 |
|
64033 | 106 |
def log(rev: String = "", template: String = "", options: String = ""): String = |
64168 | 107 |
hg.command("log", opt_rev(rev) + opt_template(template), options).check.out |
64027 | 108 |
|
64348
4c253e84ae62
clarified push/pull chain: current ISABELLE_HOME may server as source for changes that are not published on isabelle_repos_source yet (e.g. isabelle-release branch);
wenzelm
parents:
64347
diff
changeset
|
109 |
def push(remote: String = "", rev: String = "", force: Boolean = false, options: String = "") |
4c253e84ae62
clarified push/pull chain: current ISABELLE_HOME may server as source for changes that are not published on isabelle_repos_source yet (e.g. isabelle-release branch);
wenzelm
parents:
64347
diff
changeset
|
110 |
{ |
4c253e84ae62
clarified push/pull chain: current ISABELLE_HOME may server as source for changes that are not published on isabelle_repos_source yet (e.g. isabelle-release branch);
wenzelm
parents:
64347
diff
changeset
|
111 |
hg.command("push", opt_rev(rev) + opt_flag("--force", force) + optional(remote), options). |
64408
50bcf976f276
clarified hg push return code: 1 means "nothing to push";
wenzelm
parents:
64348
diff
changeset
|
112 |
check_rc(rc => rc == 0 | rc == 1) |
64348
4c253e84ae62
clarified push/pull chain: current ISABELLE_HOME may server as source for changes that are not published on isabelle_repos_source yet (e.g. isabelle-release branch);
wenzelm
parents:
64347
diff
changeset
|
113 |
} |
4c253e84ae62
clarified push/pull chain: current ISABELLE_HOME may server as source for changes that are not published on isabelle_repos_source yet (e.g. isabelle-release branch);
wenzelm
parents:
64347
diff
changeset
|
114 |
|
63999 | 115 |
def pull(remote: String = "", rev: String = "", options: String = ""): Unit = |
64168 | 116 |
hg.command("pull", opt_rev(rev) + optional(remote), options).check |
63999 | 117 |
|
64168 | 118 |
def update( |
119 |
rev: String = "", clean: Boolean = false, check: Boolean = false, options: String = "") |
|
63999 | 120 |
{ |
64168 | 121 |
hg.command("update", |
122 |
opt_rev(rev) + opt_flag("--clean", clean) + opt_flag("--check", check), options).check |
|
63999 | 123 |
} |
63997
e11ccb5aa82f
more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff
changeset
|
124 |
} |
e11ccb5aa82f
more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff
changeset
|
125 |
} |