author | wenzelm |
Sun, 14 May 2017 15:07:13 +0200 | |
changeset 65819 | ff3dc9325eaa |
parent 65818 | 94cad7590015 |
child 65822 | 17b8528c2f53 |
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 |
||
65559 | 38 |
def find_repository(start: Path, ssh: Option[SSH.Session] = None): Option[Repository] = |
39 |
{ |
|
40 |
def find(root: Path): Option[Repository] = |
|
41 |
if (is_repository(root, ssh)) Some(repository(root, ssh = ssh)) |
|
42 |
else if (root.is_root) None |
|
43 |
else find(root + Path.parent) |
|
44 |
||
45 |
ssh match { |
|
46 |
case None => find(start.expand) |
|
47 |
case Some(ssh) => find(ssh.expand_path(start)) |
|
48 |
} |
|
49 |
} |
|
50 |
||
64168 | 51 |
def clone_repository( |
64230 | 52 |
source: String, root: Path, options: String = "", ssh: Option[SSH.Session] = None): Repository = |
64168 | 53 |
{ |
64230 | 54 |
val hg = new Repository(root, ssh) |
64255 | 55 |
ssh match { |
56 |
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
|
57 |
case Some(ssh) => ssh.mkdirs(hg.root.dir) |
64255 | 58 |
} |
64304 | 59 |
hg.command("clone", Bash.string(source) + " " + File.bash_path(hg.root), options).check |
64168 | 60 |
hg |
61 |
} |
|
63997
e11ccb5aa82f
more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff
changeset
|
62 |
|
64230 | 63 |
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
|
64 |
{ |
e84cba30d7ff
clarified setup_repository: more uniform pull vs. clone, without update;
wenzelm
parents:
64233
diff
changeset
|
65 |
val present = |
e84cba30d7ff
clarified setup_repository: more uniform pull vs. clone, without update;
wenzelm
parents:
64233
diff
changeset
|
66 |
ssh match { |
e84cba30d7ff
clarified setup_repository: more uniform pull vs. clone, without update;
wenzelm
parents:
64233
diff
changeset
|
67 |
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
|
68 |
case Some(ssh) => ssh.is_dir(root) |
64252
e84cba30d7ff
clarified setup_repository: more uniform pull vs. clone, without update;
wenzelm
parents:
64233
diff
changeset
|
69 |
} |
64451
cdbfa9f64110
clarified setup_repository: even more uniform pull vs. clone (see also e84cba30d7ff);
wenzelm
parents:
64408
diff
changeset
|
70 |
if (present) { val hg = repository(root, ssh = ssh); hg.pull(remote = source); hg } |
65818
94cad7590015
avoid hardlinks, for more robustness on Windows file-systems;
wenzelm
parents:
65559
diff
changeset
|
71 |
else clone_repository(source, root, options = "--pull --noupdate", ssh = ssh) |
64252
e84cba30d7ff
clarified setup_repository: more uniform pull vs. clone, without update;
wenzelm
parents:
64233
diff
changeset
|
72 |
} |
64230 | 73 |
|
74 |
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
|
75 |
{ |
64167
097d122222f6
support remote repositories via ssh command execution;
wenzelm
parents:
64162
diff
changeset
|
76 |
hg => |
097d122222f6
support remote repositories via ssh command execution;
wenzelm
parents:
64162
diff
changeset
|
77 |
|
64230 | 78 |
val root = |
79 |
ssh match { |
|
80 |
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
|
81 |
case Some(ssh) => root_path.expand_env(ssh.settings) |
64230 | 82 |
} |
83 |
||
64347 | 84 |
def root_url: String = |
85 |
ssh match { |
|
86 |
case None => root.implode |
|
87 |
case Some(ssh) => ssh.hg_url + root.implode |
|
88 |
} |
|
89 |
||
64167
097d122222f6
support remote repositories via ssh command execution;
wenzelm
parents:
64162
diff
changeset
|
90 |
override def toString: String = |
097d122222f6
support remote repositories via ssh command execution;
wenzelm
parents:
64162
diff
changeset
|
91 |
ssh match { |
097d122222f6
support remote repositories via ssh command execution;
wenzelm
parents:
64162
diff
changeset
|
92 |
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
|
93 |
case Some(ssh) => ssh.toString + ":" + root.implode |
64167
097d122222f6
support remote repositories via ssh command execution;
wenzelm
parents:
64162
diff
changeset
|
94 |
} |
63997
e11ccb5aa82f
more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff
changeset
|
95 |
|
64168 | 96 |
def command(name: String, args: String = "", options: String = ""): Process_Result = |
64167
097d122222f6
support remote repositories via ssh command execution;
wenzelm
parents:
64162
diff
changeset
|
97 |
{ |
097d122222f6
support remote repositories via ssh command execution;
wenzelm
parents:
64162
diff
changeset
|
98 |
val cmdline = |
64168 | 99 |
"\"${HG:-hg}\"" + |
64230 | 100 |
(if (name == "clone") "" else " --repository " + File.bash_path(root)) + |
64168 | 101 |
" --noninteractive " + name + " " + options + " " + args |
64167
097d122222f6
support remote repositories via ssh command execution;
wenzelm
parents:
64162
diff
changeset
|
102 |
ssh match { |
097d122222f6
support remote repositories via ssh command execution;
wenzelm
parents:
64162
diff
changeset
|
103 |
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
|
104 |
case Some(ssh) => ssh.execute(cmdline) |
64167
097d122222f6
support remote repositories via ssh command execution;
wenzelm
parents:
64162
diff
changeset
|
105 |
} |
097d122222f6
support remote repositories via ssh command execution;
wenzelm
parents:
64162
diff
changeset
|
106 |
} |
63999 | 107 |
|
64505 | 108 |
def archive(target: String, rev: String = "", options: String = ""): Unit = |
109 |
hg.command("archive", opt_rev(rev) + " " + Bash.string(target), options).check |
|
110 |
||
63999 | 111 |
def heads(template: String = "{node|short}\n", options: String = ""): List[String] = |
64168 | 112 |
hg.command("heads", opt_template(template), options).check.out_lines |
63999 | 113 |
|
64232
367d83d6030e
clarified hg.id operation, with explicit tip as default;
wenzelm
parents:
64230
diff
changeset
|
114 |
def identify(rev: String = "tip", options: String = ""): String = |
64168 | 115 |
hg.command("id", opt_rev(rev), options).check.out_lines.headOption getOrElse "" |
63998 | 116 |
|
64232
367d83d6030e
clarified hg.id operation, with explicit tip as default;
wenzelm
parents:
64230
diff
changeset
|
117 |
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
|
118 |
|
63998 | 119 |
def manifest(rev: String = "", options: String = ""): List[String] = |
64168 | 120 |
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
|
121 |
|
64033 | 122 |
def log(rev: String = "", template: String = "", options: String = ""): String = |
64168 | 123 |
hg.command("log", opt_rev(rev) + opt_template(template), options).check.out |
64027 | 124 |
|
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
|
125 |
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
|
126 |
{ |
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
|
127 |
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
|
128 |
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
|
129 |
} |
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
|
130 |
|
63999 | 131 |
def pull(remote: String = "", rev: String = "", options: String = ""): Unit = |
64168 | 132 |
hg.command("pull", opt_rev(rev) + optional(remote), options).check |
63999 | 133 |
|
64168 | 134 |
def update( |
135 |
rev: String = "", clean: Boolean = false, check: Boolean = false, options: String = "") |
|
63999 | 136 |
{ |
64168 | 137 |
hg.command("update", |
138 |
opt_rev(rev) + opt_flag("--clean", clean) + opt_flag("--check", check), options).check |
|
63999 | 139 |
} |
65819
ff3dc9325eaa
explore repository structure, with minimal assumptions about "hg log" output;
wenzelm
parents:
65818
diff
changeset
|
140 |
|
ff3dc9325eaa
explore repository structure, with minimal assumptions about "hg log" output;
wenzelm
parents:
65818
diff
changeset
|
141 |
def length(): Int = |
ff3dc9325eaa
explore repository structure, with minimal assumptions about "hg log" output;
wenzelm
parents:
65818
diff
changeset
|
142 |
identify(options = "-n") match { |
ff3dc9325eaa
explore repository structure, with minimal assumptions about "hg log" output;
wenzelm
parents:
65818
diff
changeset
|
143 |
case Value.Int(n) => n + 1 |
ff3dc9325eaa
explore repository structure, with minimal assumptions about "hg log" output;
wenzelm
parents:
65818
diff
changeset
|
144 |
case s => error("Cannot determine repository length from " + quote(s)) |
ff3dc9325eaa
explore repository structure, with minimal assumptions about "hg log" output;
wenzelm
parents:
65818
diff
changeset
|
145 |
} |
ff3dc9325eaa
explore repository structure, with minimal assumptions about "hg log" output;
wenzelm
parents:
65818
diff
changeset
|
146 |
|
ff3dc9325eaa
explore repository structure, with minimal assumptions about "hg log" output;
wenzelm
parents:
65818
diff
changeset
|
147 |
def graph(): Graph[String, Unit] = |
ff3dc9325eaa
explore repository structure, with minimal assumptions about "hg log" output;
wenzelm
parents:
65818
diff
changeset
|
148 |
{ |
ff3dc9325eaa
explore repository structure, with minimal assumptions about "hg log" output;
wenzelm
parents:
65818
diff
changeset
|
149 |
val Node = """^node: (\w{12}) (\w{12}) (\w{12})""".r |
ff3dc9325eaa
explore repository structure, with minimal assumptions about "hg log" output;
wenzelm
parents:
65818
diff
changeset
|
150 |
val log_result = |
ff3dc9325eaa
explore repository structure, with minimal assumptions about "hg log" output;
wenzelm
parents:
65818
diff
changeset
|
151 |
log(template = """node: {node|short} {p1node|short} {p2node|short}\n""", |
ff3dc9325eaa
explore repository structure, with minimal assumptions about "hg log" output;
wenzelm
parents:
65818
diff
changeset
|
152 |
options = "-l " + length()) |
ff3dc9325eaa
explore repository structure, with minimal assumptions about "hg log" output;
wenzelm
parents:
65818
diff
changeset
|
153 |
(Graph.string[Unit] /: split_lines(log_result)) { |
ff3dc9325eaa
explore repository structure, with minimal assumptions about "hg log" output;
wenzelm
parents:
65818
diff
changeset
|
154 |
case (graph, Node(x, y, z)) => |
ff3dc9325eaa
explore repository structure, with minimal assumptions about "hg log" output;
wenzelm
parents:
65818
diff
changeset
|
155 |
val deps = List(y, z).filterNot(s => s.forall(_ == '0')) |
ff3dc9325eaa
explore repository structure, with minimal assumptions about "hg log" output;
wenzelm
parents:
65818
diff
changeset
|
156 |
val graph1 = (graph /: (x :: deps))(_.default_node(_, ())) |
ff3dc9325eaa
explore repository structure, with minimal assumptions about "hg log" output;
wenzelm
parents:
65818
diff
changeset
|
157 |
(graph1 /: deps)({ case (g, dep) => g.add_edge(dep, x) }) |
ff3dc9325eaa
explore repository structure, with minimal assumptions about "hg log" output;
wenzelm
parents:
65818
diff
changeset
|
158 |
case (graph, _) => graph |
ff3dc9325eaa
explore repository structure, with minimal assumptions about "hg log" output;
wenzelm
parents:
65818
diff
changeset
|
159 |
} |
ff3dc9325eaa
explore repository structure, with minimal assumptions about "hg log" output;
wenzelm
parents:
65818
diff
changeset
|
160 |
} |
63997
e11ccb5aa82f
more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff
changeset
|
161 |
} |
e11ccb5aa82f
more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff
changeset
|
162 |
} |