| author | desharna |
| Mon, 23 May 2022 10:23:33 +0200 | |
| changeset 75460 | 7c2fe41f5ee8 |
| parent 75394 | 42267c650205 |
| child 75470 | e3f753ef0b5c |
| 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 |
|
|
65832
2fb85623c386
implicitly check for unknown files (not part of a Mercurial repository);
wenzelm
parents:
65825
diff
changeset
|
11 |
import scala.annotation.tailrec |
|
2fb85623c386
implicitly check for unknown files (not part of a Mercurial repository);
wenzelm
parents:
65825
diff
changeset
|
12 |
import scala.collection.mutable |
|
2fb85623c386
implicitly check for unknown files (not part of a Mercurial repository);
wenzelm
parents:
65825
diff
changeset
|
13 |
|
|
63997
e11ccb5aa82f
more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff
changeset
|
14 |
|
| 75393 | 15 |
object Mercurial {
|
| 66895 | 16 |
type Graph = isabelle.Graph[String, Unit] |
17 |
||
18 |
||
| 73611 | 19 |
/* HTTP server */ |
20 |
||
| 75393 | 21 |
object Server {
|
| 73611 | 22 |
def apply(root: String): Server = new Server(root) |
23 |
||
| 75393 | 24 |
def start(root: Path): Server = {
|
| 73611 | 25 |
val hg = repository(root) |
| 73609 | 26 |
|
| 73611 | 27 |
val server_process = Future.promise[Bash.Process] |
28 |
val server_root = Future.promise[String] |
|
29 |
Isabelle_Thread.fork("hg") {
|
|
30 |
val process = |
|
31 |
Exn.capture { Bash.process(hg.command_line("serve", options = "--port 0 --print-url")) }
|
|
32 |
server_process.fulfill_result(process) |
|
33 |
Exn.release(process).result(progress_stdout = |
|
34 |
line => if (!server_root.is_finished) {
|
|
35 |
server_root.fulfill(Library.try_unsuffix("/", line).getOrElse(line))
|
|
36 |
}) |
|
37 |
} |
|
38 |
server_process.join |
|
39 |
||
40 |
new Server(server_root.join) {
|
|
41 |
override def close(): Unit = server_process.join.terminate() |
|
42 |
} |
|
43 |
} |
|
| 73609 | 44 |
} |
45 |
||
| 75393 | 46 |
class Server private(val root: String) extends AutoCloseable {
|
| 73609 | 47 |
override def toString: String = root |
48 |
||
| 73611 | 49 |
def close(): Unit = () |
50 |
||
| 73610 | 51 |
def changeset(rev: String = "tip", raw: Boolean = false): String = |
52 |
root + (if (raw) "/raw-rev/" else "/rev/") + rev |
|
53 |
||
54 |
def file(path: Path, rev: String = "tip", raw: Boolean = false): String = |
|
55 |
root + (if (raw) "/raw-file/" else "/file/") + rev + "/" + path.expand.implode |
|
56 |
||
57 |
def archive(rev: String = "tip"): String = |
|
58 |
root + "/archive/" + rev + ".tar.gz" |
|
59 |
||
60 |
def read_changeset(rev: String = "tip"): String = |
|
61 |
Url.read(changeset(rev = rev, raw = true)) |
|
62 |
||
63 |
def read_file(path: Path, rev: String = "tip"): String = |
|
64 |
Url.read(file(path, rev = rev, raw = true)) |
|
65 |
||
66 |
def download_archive(rev: String = "tip", progress: Progress = new Progress): HTTP.Content = |
|
67 |
Isabelle_System.download(archive(rev = rev), progress = progress) |
|
68 |
||
| 75393 | 69 |
def download_dir(dir: Path, rev: String = "tip", progress: Progress = new Progress): Unit = {
|
| 73610 | 70 |
Isabelle_System.new_directory(dir) |
| 75394 | 71 |
Isabelle_System.with_tmp_file("rev", ext = ".tar.gz") { archive_path =>
|
| 73610 | 72 |
val content = download_archive(rev = rev, progress = progress) |
73 |
Bytes.write(archive_path, content.bytes) |
|
74 |
progress.echo("Unpacking " + rev + ".tar.gz")
|
|
| 73624 | 75 |
Isabelle_System.gnutar("-xzf " + File.bash_path(archive_path),
|
76 |
dir = dir, original_owner = true, strip = 1).check |
|
| 75394 | 77 |
} |
| 73610 | 78 |
} |
| 73609 | 79 |
} |
80 |
||
81 |
||
|
63997
e11ccb5aa82f
more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff
changeset
|
82 |
/* command-line syntax */ |
|
e11ccb5aa82f
more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff
changeset
|
83 |
|
| 63999 | 84 |
def optional(s: String, prefix: String = ""): String = |
| 64304 | 85 |
if (s == "") "" else " " + prefix + " " + Bash.string(s) |
| 63999 | 86 |
|
87 |
def opt_flag(flag: String, b: Boolean): String = if (b) " " + flag else "" |
|
88 |
def opt_rev(s: String): String = optional(s, "--rev") |
|
89 |
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
|
90 |
|
|
e11ccb5aa82f
more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff
changeset
|
91 |
|
|
73520
4cba4e250c28
clarified ISABELLE_ID: distribution vs. hg archive vs. hg repos;
wenzelm
parents:
73483
diff
changeset
|
92 |
/* repository archives */ |
|
4cba4e250c28
clarified ISABELLE_ID: distribution vs. hg archive vs. hg repos;
wenzelm
parents:
73483
diff
changeset
|
93 |
|
|
4cba4e250c28
clarified ISABELLE_ID: distribution vs. hg archive vs. hg repos;
wenzelm
parents:
73483
diff
changeset
|
94 |
private val Archive_Node = """^node: (\S{12}).*$""".r
|
| 73521 | 95 |
private val Archive_Tag = """^tag: (\S+).*$""".r |
|
73520
4cba4e250c28
clarified ISABELLE_ID: distribution vs. hg archive vs. hg repos;
wenzelm
parents:
73483
diff
changeset
|
96 |
|
| 75393 | 97 |
sealed case class Archive_Info(lines: List[String]) {
|
|
73520
4cba4e250c28
clarified ISABELLE_ID: distribution vs. hg archive vs. hg repos;
wenzelm
parents:
73483
diff
changeset
|
98 |
def id: Option[String] = lines.collectFirst({ case Archive_Node(a) => a })
|
| 73521 | 99 |
def tags: List[String] = for (Archive_Tag(tag) <- lines if tag != "tip") yield tag |
|
73520
4cba4e250c28
clarified ISABELLE_ID: distribution vs. hg archive vs. hg repos;
wenzelm
parents:
73483
diff
changeset
|
100 |
} |
|
4cba4e250c28
clarified ISABELLE_ID: distribution vs. hg archive vs. hg repos;
wenzelm
parents:
73483
diff
changeset
|
101 |
|
| 75393 | 102 |
def archive_info(root: Path): Option[Archive_Info] = {
|
|
73520
4cba4e250c28
clarified ISABELLE_ID: distribution vs. hg archive vs. hg repos;
wenzelm
parents:
73483
diff
changeset
|
103 |
val path = root + Path.explode(".hg_archival.txt")
|
|
4cba4e250c28
clarified ISABELLE_ID: distribution vs. hg archive vs. hg repos;
wenzelm
parents:
73483
diff
changeset
|
104 |
if (path.is_file) Some(Archive_Info(Library.trim_split_lines(File.read(path)))) else None |
|
4cba4e250c28
clarified ISABELLE_ID: distribution vs. hg archive vs. hg repos;
wenzelm
parents:
73483
diff
changeset
|
105 |
} |
|
4cba4e250c28
clarified ISABELLE_ID: distribution vs. hg archive vs. hg repos;
wenzelm
parents:
73483
diff
changeset
|
106 |
|
| 73521 | 107 |
def archive_id(root: Path): Option[String] = |
108 |
archive_info(root).flatMap(_.id) |
|
109 |
||
110 |
def archive_tags(root: Path): Option[String] = |
|
111 |
archive_info(root).map(info => info.tags.mkString(" "))
|
|
|
73520
4cba4e250c28
clarified ISABELLE_ID: distribution vs. hg archive vs. hg repos;
wenzelm
parents:
73483
diff
changeset
|
112 |
|
|
4cba4e250c28
clarified ISABELLE_ID: distribution vs. hg archive vs. hg repos;
wenzelm
parents:
73483
diff
changeset
|
113 |
|
|
63997
e11ccb5aa82f
more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff
changeset
|
114 |
/* repository access */ |
|
e11ccb5aa82f
more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff
changeset
|
115 |
|
| 66570 | 116 |
def is_repository(root: Path, ssh: SSH.System = SSH.Local): Boolean = |
117 |
ssh.is_dir(root + Path.explode(".hg")) &&
|
|
118 |
new Repository(root, ssh).command("root").ok
|
|
| 64330 | 119 |
|
| 75393 | 120 |
def repository(root: Path, ssh: SSH.System = SSH.Local): Repository = {
|
| 64168 | 121 |
val hg = new Repository(root, ssh) |
122 |
hg.command("root").check
|
|
123 |
hg |
|
124 |
} |
|
125 |
||
| 75393 | 126 |
def find_repository(start: Path, ssh: SSH.System = SSH.Local): Option[Repository] = {
|
| 71601 | 127 |
@tailrec def find(root: Path): Option[Repository] = |
| 65559 | 128 |
if (is_repository(root, ssh)) Some(repository(root, ssh = ssh)) |
129 |
else if (root.is_root) None |
|
130 |
else find(root + Path.parent) |
|
131 |
||
| 66570 | 132 |
find(ssh.expand_path(start)) |
| 65559 | 133 |
} |
134 |
||
| 75393 | 135 |
private def make_repository( |
136 |
root: Path, |
|
137 |
cmd: String, |
|
138 |
args: String, |
|
139 |
ssh: SSH.System = SSH.Local |
|
140 |
) : Repository = {
|
|
| 64230 | 141 |
val hg = new Repository(root, ssh) |
| 72375 | 142 |
ssh.make_directory(hg.root.dir) |
| 67066 | 143 |
hg.command(cmd, args, repository = false).check |
| 64168 | 144 |
hg |
145 |
} |
|
|
63997
e11ccb5aa82f
more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff
changeset
|
146 |
|
| 67065 | 147 |
def init_repository(root: Path, ssh: SSH.System = SSH.Local): Repository = |
| 67066 | 148 |
make_repository(root, "init", ssh.bash_path(root), ssh = ssh) |
| 67065 | 149 |
|
150 |
def clone_repository(source: String, root: Path, |
|
151 |
rev: String = "", options: String = "", ssh: SSH.System = SSH.Local): Repository = |
|
152 |
make_repository(root, "clone", |
|
| 67066 | 153 |
options + " " + Bash.string(source) + " " + ssh.bash_path(root) + opt_rev(rev), ssh = ssh) |
| 67065 | 154 |
|
| 75393 | 155 |
def setup_repository(source: String, root: Path, ssh: SSH.System = SSH.Local): Repository = {
|
| 66570 | 156 |
if (ssh.is_dir(root)) { val hg = repository(root, ssh = ssh); hg.pull(remote = source); hg }
|
|
66105
8889aad1ff92
reverted 94cad7590015: does not help much on Windows;
wenzelm
parents:
66104
diff
changeset
|
157 |
else clone_repository(source, root, options = "--noupdate", ssh = ssh) |
|
64252
e84cba30d7ff
clarified setup_repository: more uniform pull vs. clone, without update;
wenzelm
parents:
64233
diff
changeset
|
158 |
} |
| 64230 | 159 |
|
| 75393 | 160 |
class Repository private[Mercurial](root_path: Path, ssh: SSH.System = SSH.Local) {
|
|
64167
097d122222f6
support remote repositories via ssh command execution;
wenzelm
parents:
64162
diff
changeset
|
161 |
hg => |
|
097d122222f6
support remote repositories via ssh command execution;
wenzelm
parents:
64162
diff
changeset
|
162 |
|
|
71312
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
163 |
val root: Path = ssh.expand_path(root_path) |
| 66570 | 164 |
def root_url: String = ssh.hg_url + root.implode |
| 64230 | 165 |
|
| 71562 | 166 |
override def toString: String = ssh.hg_url + root.implode |
|
63997
e11ccb5aa82f
more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff
changeset
|
167 |
|
| 75393 | 168 |
def command_line( |
169 |
name: String, |
|
170 |
args: String = "", |
|
171 |
options: String = "", |
|
172 |
repository: Boolean = true |
|
173 |
): String = {
|
|
| 73611 | 174 |
"export LANG=C HGPLAIN=\n\"${HG:-hg}\" --config " + Bash.string("defaults." + name + "=") +
|
175 |
(if (repository) " --repository " + ssh.bash_path(root) else "") + |
|
176 |
" --noninteractive " + name + " " + options + " " + args |
|
177 |
} |
|
178 |
||
179 |
def command( |
|
| 75393 | 180 |
name: String, |
181 |
args: String = "", |
|
182 |
options: String = "", |
|
183 |
repository: Boolean = true |
|
184 |
): Process_Result = {
|
|
| 73611 | 185 |
ssh.execute(command_line(name, args = args, options = options, repository = repository)) |
|
64167
097d122222f6
support remote repositories via ssh command execution;
wenzelm
parents:
64162
diff
changeset
|
186 |
} |
| 63999 | 187 |
|
| 67068 | 188 |
def add(files: List[Path]): Unit = |
| 71601 | 189 |
hg.command("add", files.map(ssh.bash_path).mkString(" "))
|
| 67068 | 190 |
|
| 64505 | 191 |
def archive(target: String, rev: String = "", options: String = ""): Unit = |
192 |
hg.command("archive", opt_rev(rev) + " " + Bash.string(target), options).check
|
|
193 |
||
| 63999 | 194 |
def heads(template: String = "{node|short}\n", options: String = ""): List[String] =
|
| 64168 | 195 |
hg.command("heads", opt_template(template), options).check.out_lines
|
| 63999 | 196 |
|
|
64232
367d83d6030e
clarified hg.id operation, with explicit tip as default;
wenzelm
parents:
64230
diff
changeset
|
197 |
def identify(rev: String = "tip", options: String = ""): String = |
| 64168 | 198 |
hg.command("id", opt_rev(rev), options).check.out_lines.headOption getOrElse ""
|
| 63998 | 199 |
|
|
64232
367d83d6030e
clarified hg.id operation, with explicit tip as default;
wenzelm
parents:
64230
diff
changeset
|
200 |
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
|
201 |
|
| 75393 | 202 |
def tags(rev: String = "tip"): String = {
|
| 73524 | 203 |
val result = identify(rev, options = "-t") |
204 |
Library.space_explode(' ', result).filterNot(_ == "tip").mkString(" ")
|
|
205 |
} |
|
| 73521 | 206 |
|
| 71313 | 207 |
def paths(args: String = "", options: String = ""): List[String] = |
208 |
hg.command("paths", args = args, options = options).check.out_lines
|
|
209 |
||
| 63998 | 210 |
def manifest(rev: String = "", options: String = ""): List[String] = |
| 64168 | 211 |
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
|
212 |
|
| 64033 | 213 |
def log(rev: String = "", template: String = "", options: String = ""): String = |
| 64168 | 214 |
hg.command("log", opt_rev(rev) + opt_template(template), options).check.out
|
| 64027 | 215 |
|
| 67755 | 216 |
def parent(): String = log(rev = "p1()", template = "{node|short}")
|
217 |
||
| 73340 | 218 |
def push( |
| 75393 | 219 |
remote: String = "", |
220 |
rev: String = "", |
|
221 |
force: Boolean = false, |
|
222 |
options: String = "" |
|
223 |
): Unit = {
|
|
|
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
|
224 |
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
|
225 |
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
|
226 |
} |
|
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
|
227 |
|
| 63999 | 228 |
def pull(remote: String = "", rev: String = "", options: String = ""): Unit = |
| 64168 | 229 |
hg.command("pull", opt_rev(rev) + optional(remote), options).check
|
| 63999 | 230 |
|
| 64168 | 231 |
def update( |
| 75393 | 232 |
rev: String = "", |
233 |
clean: Boolean = false, |
|
234 |
check: Boolean = false, |
|
235 |
options: String = "" |
|
236 |
): Unit = {
|
|
| 64168 | 237 |
hg.command("update",
|
238 |
opt_rev(rev) + opt_flag("--clean", clean) + opt_flag("--check", check), options).check
|
|
| 63999 | 239 |
} |
|
65819
ff3dc9325eaa
explore repository structure, with minimal assumptions about "hg log" output;
wenzelm
parents:
65818
diff
changeset
|
240 |
|
|
65822
17b8528c2f53
clarified notion of known files (before actual commit);
wenzelm
parents:
65819
diff
changeset
|
241 |
def known_files(): List[String] = |
|
65825
11f87ab51ddb
more robust command invocation, without defaults from hgrc;
wenzelm
parents:
65822
diff
changeset
|
242 |
hg.command("status", options = "--modified --added --clean --no-status").check.out_lines
|
|
65819
ff3dc9325eaa
explore repository structure, with minimal assumptions about "hg log" output;
wenzelm
parents:
65818
diff
changeset
|
243 |
|
| 75393 | 244 |
def graph(): Graph = {
|
|
65819
ff3dc9325eaa
explore repository structure, with minimal assumptions about "hg log" output;
wenzelm
parents:
65818
diff
changeset
|
245 |
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
|
246 |
val log_result = |
|
65825
11f87ab51ddb
more robust command invocation, without defaults from hgrc;
wenzelm
parents:
65822
diff
changeset
|
247 |
log(template = """node: {node|short} {p1node|short} {p2node|short}\n""")
|
| 73359 | 248 |
split_lines(log_result).foldLeft(Graph.string[Unit]) {
|
|
65819
ff3dc9325eaa
explore repository structure, with minimal assumptions about "hg log" output;
wenzelm
parents:
65818
diff
changeset
|
249 |
case (graph, Node(x, y, z)) => |
|
ff3dc9325eaa
explore repository structure, with minimal assumptions about "hg log" output;
wenzelm
parents:
65818
diff
changeset
|
250 |
val deps = List(y, z).filterNot(s => s.forall(_ == '0')) |
| 73359 | 251 |
val graph1 = (x :: deps).foldLeft(graph)(_.default_node(_, ())) |
252 |
deps.foldLeft(graph1)({ case (g, dep) => g.add_edge(dep, x) })
|
|
|
65819
ff3dc9325eaa
explore repository structure, with minimal assumptions about "hg log" output;
wenzelm
parents:
65818
diff
changeset
|
253 |
case (graph, _) => graph |
|
ff3dc9325eaa
explore repository structure, with minimal assumptions about "hg log" output;
wenzelm
parents:
65818
diff
changeset
|
254 |
} |
|
ff3dc9325eaa
explore repository structure, with minimal assumptions about "hg log" output;
wenzelm
parents:
65818
diff
changeset
|
255 |
} |
|
63997
e11ccb5aa82f
more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff
changeset
|
256 |
} |
|
65832
2fb85623c386
implicitly check for unknown files (not part of a Mercurial repository);
wenzelm
parents:
65825
diff
changeset
|
257 |
|
|
2fb85623c386
implicitly check for unknown files (not part of a Mercurial repository);
wenzelm
parents:
65825
diff
changeset
|
258 |
|
|
67782
7e223a05e6d8
clarified notion of unknown files: ignore files outside of a Mercurial repository;
wenzelm
parents:
67755
diff
changeset
|
259 |
/* check files */ |
|
65832
2fb85623c386
implicitly check for unknown files (not part of a Mercurial repository);
wenzelm
parents:
65825
diff
changeset
|
260 |
|
| 75393 | 261 |
def check_files(files: List[Path], ssh: SSH.System = SSH.Local): (List[Path], List[Path]) = {
|
|
67782
7e223a05e6d8
clarified notion of unknown files: ignore files outside of a Mercurial repository;
wenzelm
parents:
67755
diff
changeset
|
262 |
val outside = new mutable.ListBuffer[Path] |
|
65832
2fb85623c386
implicitly check for unknown files (not part of a Mercurial repository);
wenzelm
parents:
65825
diff
changeset
|
263 |
val unknown = new mutable.ListBuffer[Path] |
|
2fb85623c386
implicitly check for unknown files (not part of a Mercurial repository);
wenzelm
parents:
65825
diff
changeset
|
264 |
|
| 75393 | 265 |
@tailrec def check(paths: List[Path]): Unit = {
|
|
65832
2fb85623c386
implicitly check for unknown files (not part of a Mercurial repository);
wenzelm
parents:
65825
diff
changeset
|
266 |
paths match {
|
|
2fb85623c386
implicitly check for unknown files (not part of a Mercurial repository);
wenzelm
parents:
65825
diff
changeset
|
267 |
case path :: rest => |
|
2fb85623c386
implicitly check for unknown files (not part of a Mercurial repository);
wenzelm
parents:
65825
diff
changeset
|
268 |
find_repository(path, ssh) match {
|
|
67782
7e223a05e6d8
clarified notion of unknown files: ignore files outside of a Mercurial repository;
wenzelm
parents:
67755
diff
changeset
|
269 |
case None => outside += path; check(rest) |
|
65832
2fb85623c386
implicitly check for unknown files (not part of a Mercurial repository);
wenzelm
parents:
65825
diff
changeset
|
270 |
case Some(hg) => |
|
2fb85623c386
implicitly check for unknown files (not part of a Mercurial repository);
wenzelm
parents:
65825
diff
changeset
|
271 |
val known = |
|
2fb85623c386
implicitly check for unknown files (not part of a Mercurial repository);
wenzelm
parents:
65825
diff
changeset
|
272 |
hg.known_files().iterator.map(name => |
| 65833 | 273 |
(hg.root + Path.explode(name)).canonical_file).toSet |
274 |
if (!known(path.canonical_file)) unknown += path |
|
275 |
check(rest.filterNot(p => known(p.canonical_file))) |
|
|
65832
2fb85623c386
implicitly check for unknown files (not part of a Mercurial repository);
wenzelm
parents:
65825
diff
changeset
|
276 |
} |
|
2fb85623c386
implicitly check for unknown files (not part of a Mercurial repository);
wenzelm
parents:
65825
diff
changeset
|
277 |
case Nil => |
|
2fb85623c386
implicitly check for unknown files (not part of a Mercurial repository);
wenzelm
parents:
65825
diff
changeset
|
278 |
} |
|
2fb85623c386
implicitly check for unknown files (not part of a Mercurial repository);
wenzelm
parents:
65825
diff
changeset
|
279 |
} |
|
2fb85623c386
implicitly check for unknown files (not part of a Mercurial repository);
wenzelm
parents:
65825
diff
changeset
|
280 |
|
|
2fb85623c386
implicitly check for unknown files (not part of a Mercurial repository);
wenzelm
parents:
65825
diff
changeset
|
281 |
check(files) |
|
67782
7e223a05e6d8
clarified notion of unknown files: ignore files outside of a Mercurial repository;
wenzelm
parents:
67755
diff
changeset
|
282 |
(outside.toList, unknown.toList) |
|
65832
2fb85623c386
implicitly check for unknown files (not part of a Mercurial repository);
wenzelm
parents:
65825
diff
changeset
|
283 |
} |
|
71312
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
284 |
|
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
285 |
|
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
286 |
/* setup remote vs. local repository */ |
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
287 |
|
| 75393 | 288 |
private def edit_hgrc(local_hg: Repository, path_name: String, source: String): Unit = {
|
| 71313 | 289 |
val hgrc = local_hg.root + Path.explode(".hg/hgrc")
|
290 |
def header(line: String): Boolean = line.startsWith("[paths]")
|
|
291 |
val Entry = """^(\S+)\s*=\s*(.*)$""".r |
|
292 |
val new_entry = path_name + " = " + source |
|
293 |
||
| 75393 | 294 |
def commit(lines: List[String]): Boolean = {
|
| 71320 | 295 |
File.write(hgrc, cat_lines(lines)) |
| 71313 | 296 |
true |
297 |
} |
|
298 |
val edited = |
|
299 |
hgrc.is_file && {
|
|
300 |
val lines = split_lines(File.read(hgrc)) |
|
| 71383 | 301 |
lines.count(header) == 1 && {
|
| 71313 | 302 |
if (local_hg.paths(options = "-q").contains(path_name)) {
|
303 |
val old_source = local_hg.paths(args = path_name).head |
|
| 71320 | 304 |
val old_entry = path_name + ".old = " + old_source |
| 71313 | 305 |
val lines1 = |
306 |
lines.map {
|
|
| 71320 | 307 |
case Entry(a, b) if a == path_name && b == old_source => |
308 |
new_entry + "\n" + old_entry |
|
| 71313 | 309 |
case line => line |
310 |
} |
|
311 |
lines != lines1 && commit(lines1) |
|
312 |
} |
|
313 |
else {
|
|
314 |
val prefix = lines.takeWhile(line => !header(line)) |
|
315 |
val n = prefix.length |
|
316 |
commit(prefix ::: List(lines(n), new_entry) ::: lines.drop(n + 1)) |
|
317 |
} |
|
318 |
} |
|
319 |
} |
|
320 |
if (!edited) File.append(hgrc, "\n[paths]\n" + new_entry + "\n") |
|
321 |
} |
|
322 |
||
|
71312
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
323 |
val default_path_name = "default" |
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
324 |
|
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
325 |
def hg_setup( |
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
326 |
remote: String, |
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
327 |
local_path: Path, |
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
328 |
remote_name: String = "", |
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
329 |
path_name: String = default_path_name, |
|
71321
edf3210a61a2
added option -r: support more robust consolidation of local clones with varying names;
wenzelm
parents:
71320
diff
changeset
|
330 |
remote_exists: Boolean = false, |
| 75393 | 331 |
progress: Progress = new Progress |
332 |
): Unit = {
|
|
|
71312
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
333 |
/* local repository */ |
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
334 |
|
| 72375 | 335 |
Isabelle_System.make_directory(local_path) |
|
71312
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
336 |
|
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
337 |
val repos_name = |
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
338 |
proper_string(remote_name) getOrElse local_path.absolute.base.implode |
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
339 |
|
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
340 |
val local_hg = |
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
341 |
if (is_repository(local_path)) repository(local_path) |
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
342 |
else init_repository(local_path) |
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
343 |
|
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
344 |
progress.echo("Local repository " + local_hg.root.absolute)
|
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
345 |
|
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
346 |
|
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
347 |
/* remote repository */ |
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
348 |
|
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
349 |
val remote_url = |
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
350 |
remote match {
|
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
351 |
case _ if remote.startsWith("ssh://") =>
|
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
352 |
val ssh_url = remote + "/" + repos_name |
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
353 |
|
|
71321
edf3210a61a2
added option -r: support more robust consolidation of local clones with varying names;
wenzelm
parents:
71320
diff
changeset
|
354 |
if (!remote_exists) {
|
|
edf3210a61a2
added option -r: support more robust consolidation of local clones with varying names;
wenzelm
parents:
71320
diff
changeset
|
355 |
try { local_hg.command("init", ssh_url, repository = false).check }
|
|
edf3210a61a2
added option -r: support more robust consolidation of local clones with varying names;
wenzelm
parents:
71320
diff
changeset
|
356 |
catch { case ERROR(msg) => progress.echo_warning(msg) }
|
|
edf3210a61a2
added option -r: support more robust consolidation of local clones with varying names;
wenzelm
parents:
71320
diff
changeset
|
357 |
} |
|
71312
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
358 |
|
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
359 |
ssh_url |
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
360 |
|
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
361 |
case SSH.Target(user, host) => |
| 71314 | 362 |
val phabricator = Phabricator.API(user, host) |
|
71312
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
363 |
|
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
364 |
var repos = |
|
71321
edf3210a61a2
added option -r: support more robust consolidation of local clones with varying names;
wenzelm
parents:
71320
diff
changeset
|
365 |
phabricator.get_repositories().find(r => r.short_name == repos_name) getOrElse {
|
|
edf3210a61a2
added option -r: support more robust consolidation of local clones with varying names;
wenzelm
parents:
71320
diff
changeset
|
366 |
if (remote_exists) {
|
|
edf3210a61a2
added option -r: support more robust consolidation of local clones with varying names;
wenzelm
parents:
71320
diff
changeset
|
367 |
error("Remote repository " +
|
|
edf3210a61a2
added option -r: support more robust consolidation of local clones with varying names;
wenzelm
parents:
71320
diff
changeset
|
368 |
quote(phabricator.hg_url + "/source/" + repos_name) + " expected to exist") |
|
edf3210a61a2
added option -r: support more robust consolidation of local clones with varying names;
wenzelm
parents:
71320
diff
changeset
|
369 |
} |
|
edf3210a61a2
added option -r: support more robust consolidation of local clones with varying names;
wenzelm
parents:
71320
diff
changeset
|
370 |
else phabricator.create_repository(repos_name, short_name = repos_name) |
|
edf3210a61a2
added option -r: support more robust consolidation of local clones with varying names;
wenzelm
parents:
71320
diff
changeset
|
371 |
} |
|
71312
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
372 |
|
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
373 |
while (repos.importing) {
|
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
374 |
progress.echo("Awaiting remote repository ...")
|
|
73702
7202e12cb324
tuned signature --- following hints by IntelliJ IDEA;
wenzelm
parents:
73624
diff
changeset
|
375 |
Time.seconds(0.5).sleep() |
|
71312
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
376 |
repos = phabricator.the_repository(repos.phid) |
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
377 |
} |
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
378 |
|
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
379 |
repos.ssh_url |
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
380 |
|
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
381 |
case _ => error("Malformed remote specification " + quote(remote))
|
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
382 |
} |
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
383 |
|
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
384 |
progress.echo("Remote repository " + quote(remote_url))
|
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
385 |
|
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
386 |
|
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
387 |
/* synchronize local and remote state */ |
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
388 |
|
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
389 |
progress.echo("Synchronizing ...")
|
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
390 |
|
| 71313 | 391 |
edit_hgrc(local_hg, path_name, remote_url) |
|
71312
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
392 |
|
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
393 |
local_hg.pull(options = "-u") |
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
394 |
|
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
395 |
local_hg.push(remote = remote_url) |
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
396 |
} |
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
397 |
|
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
398 |
val isabelle_tool = |
| 72763 | 399 |
Isabelle_Tool("hg_setup", "setup remote vs. local Mercurial repository",
|
| 75394 | 400 |
Scala_Project.here, |
401 |
{ args =>
|
|
402 |
var remote_name = "" |
|
403 |
var path_name = default_path_name |
|
404 |
var remote_exists = false |
|
|
71312
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
405 |
|
| 75394 | 406 |
val getopts = Getopts("""
|
| 71322 | 407 |
Usage: isabelle hg_setup [OPTIONS] REMOTE LOCAL_DIR |
|
71312
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
408 |
|
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
409 |
Options are: |
| 71322 | 410 |
-n NAME remote repository name (default: base name of LOCAL_DIR) |
|
71312
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
411 |
-p PATH Mercurial path name (default: """ + quote(default_path_name) + """) |
|
71321
edf3210a61a2
added option -r: support more robust consolidation of local clones with varying names;
wenzelm
parents:
71320
diff
changeset
|
412 |
-r assume that remote repository already exists |
|
71312
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
413 |
|
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
414 |
Setup a remote vs. local Mercurial repository: REMOTE either refers to a |
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
415 |
Phabricator server "user@host" or SSH file server "ssh://user@host/path". |
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
416 |
""", |
| 75394 | 417 |
"n:" -> (arg => remote_name = arg), |
418 |
"p:" -> (arg => path_name = arg), |
|
419 |
"r" -> (_ => remote_exists = true)) |
|
|
71312
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
420 |
|
| 75394 | 421 |
val more_args = getopts(args) |
|
71312
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
422 |
|
| 75394 | 423 |
val (remote, local_path) = |
424 |
more_args match {
|
|
425 |
case List(arg1, arg2) => (arg1, Path.explode(arg2)) |
|
426 |
case _ => getopts.usage() |
|
427 |
} |
|
|
71312
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
428 |
|
| 75394 | 429 |
val progress = new Console_Progress |
|
71312
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
430 |
|
| 75394 | 431 |
hg_setup(remote, local_path, remote_name = remote_name, path_name = path_name, |
432 |
remote_exists = remote_exists, progress = progress) |
|
433 |
}) |
|
|
63997
e11ccb5aa82f
more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff
changeset
|
434 |
} |