author | wenzelm |
Mon, 30 May 2022 11:02:13 +0200 | |
changeset 75491 | 47d790984e82 |
parent 75489 | f08fd5048df3 |
child 75493 | f775dfb55655 |
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 |
||
75472 | 19 |
|
20 |
/** HTTP server **/ |
|
73611 | 21 |
|
75393 | 22 |
object Server { |
73611 | 23 |
def apply(root: String): Server = new Server(root) |
24 |
||
75393 | 25 |
def start(root: Path): Server = { |
73611 | 26 |
val hg = repository(root) |
73609 | 27 |
|
73611 | 28 |
val server_process = Future.promise[Bash.Process] |
29 |
val server_root = Future.promise[String] |
|
30 |
Isabelle_Thread.fork("hg") { |
|
31 |
val process = |
|
32 |
Exn.capture { Bash.process(hg.command_line("serve", options = "--port 0 --print-url")) } |
|
33 |
server_process.fulfill_result(process) |
|
34 |
Exn.release(process).result(progress_stdout = |
|
35 |
line => if (!server_root.is_finished) { |
|
36 |
server_root.fulfill(Library.try_unsuffix("/", line).getOrElse(line)) |
|
37 |
}) |
|
38 |
} |
|
39 |
server_process.join |
|
40 |
||
41 |
new Server(server_root.join) { |
|
42 |
override def close(): Unit = server_process.join.terminate() |
|
43 |
} |
|
44 |
} |
|
73609 | 45 |
} |
46 |
||
75393 | 47 |
class Server private(val root: String) extends AutoCloseable { |
73609 | 48 |
override def toString: String = root |
49 |
||
73611 | 50 |
def close(): Unit = () |
51 |
||
73610 | 52 |
def changeset(rev: String = "tip", raw: Boolean = false): String = |
53 |
root + (if (raw) "/raw-rev/" else "/rev/") + rev |
|
54 |
||
55 |
def file(path: Path, rev: String = "tip", raw: Boolean = false): String = |
|
56 |
root + (if (raw) "/raw-file/" else "/file/") + rev + "/" + path.expand.implode |
|
57 |
||
58 |
def archive(rev: String = "tip"): String = |
|
59 |
root + "/archive/" + rev + ".tar.gz" |
|
60 |
||
61 |
def read_changeset(rev: String = "tip"): String = |
|
62 |
Url.read(changeset(rev = rev, raw = true)) |
|
63 |
||
64 |
def read_file(path: Path, rev: String = "tip"): String = |
|
65 |
Url.read(file(path, rev = rev, raw = true)) |
|
66 |
||
67 |
def download_archive(rev: String = "tip", progress: Progress = new Progress): HTTP.Content = |
|
68 |
Isabelle_System.download(archive(rev = rev), progress = progress) |
|
69 |
||
75393 | 70 |
def download_dir(dir: Path, rev: String = "tip", progress: Progress = new Progress): Unit = { |
73610 | 71 |
Isabelle_System.new_directory(dir) |
75394 | 72 |
Isabelle_System.with_tmp_file("rev", ext = ".tar.gz") { archive_path => |
73610 | 73 |
val content = download_archive(rev = rev, progress = progress) |
74 |
Bytes.write(archive_path, content.bytes) |
|
75 |
progress.echo("Unpacking " + rev + ".tar.gz") |
|
73624 | 76 |
Isabelle_System.gnutar("-xzf " + File.bash_path(archive_path), |
77 |
dir = dir, original_owner = true, strip = 1).check |
|
75394 | 78 |
} |
73610 | 79 |
} |
73609 | 80 |
} |
81 |
||
82 |
||
75472 | 83 |
|
84 |
/** repository commands **/ |
|
85 |
||
63997
e11ccb5aa82f
more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff
changeset
|
86 |
/* command-line syntax */ |
e11ccb5aa82f
more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff
changeset
|
87 |
|
63999 | 88 |
def optional(s: String, prefix: String = ""): String = |
64304 | 89 |
if (s == "") "" else " " + prefix + " " + Bash.string(s) |
63999 | 90 |
|
91 |
def opt_flag(flag: String, b: Boolean): String = if (b) " " + flag else "" |
|
92 |
def opt_rev(s: String): String = optional(s, "--rev") |
|
93 |
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
|
94 |
|
e11ccb5aa82f
more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff
changeset
|
95 |
|
73520
4cba4e250c28
clarified ISABELLE_ID: distribution vs. hg archive vs. hg repos;
wenzelm
parents:
73483
diff
changeset
|
96 |
/* repository archives */ |
4cba4e250c28
clarified ISABELLE_ID: distribution vs. hg archive vs. hg repos;
wenzelm
parents:
73483
diff
changeset
|
97 |
|
4cba4e250c28
clarified ISABELLE_ID: distribution vs. hg archive vs. hg repos;
wenzelm
parents:
73483
diff
changeset
|
98 |
private val Archive_Node = """^node: (\S{12}).*$""".r |
73521 | 99 |
private val Archive_Tag = """^tag: (\S+).*$""".r |
73520
4cba4e250c28
clarified ISABELLE_ID: distribution vs. hg archive vs. hg repos;
wenzelm
parents:
73483
diff
changeset
|
100 |
|
75393 | 101 |
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
|
102 |
def id: Option[String] = lines.collectFirst({ case Archive_Node(a) => a }) |
73521 | 103 |
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
|
104 |
} |
4cba4e250c28
clarified ISABELLE_ID: distribution vs. hg archive vs. hg repos;
wenzelm
parents:
73483
diff
changeset
|
105 |
|
75393 | 106 |
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
|
107 |
val path = root + Path.explode(".hg_archival.txt") |
4cba4e250c28
clarified ISABELLE_ID: distribution vs. hg archive vs. hg repos;
wenzelm
parents:
73483
diff
changeset
|
108 |
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
|
109 |
} |
4cba4e250c28
clarified ISABELLE_ID: distribution vs. hg archive vs. hg repos;
wenzelm
parents:
73483
diff
changeset
|
110 |
|
73521 | 111 |
def archive_id(root: Path): Option[String] = |
112 |
archive_info(root).flatMap(_.id) |
|
113 |
||
114 |
def archive_tags(root: Path): Option[String] = |
|
115 |
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
|
116 |
|
4cba4e250c28
clarified ISABELLE_ID: distribution vs. hg archive vs. hg repos;
wenzelm
parents:
73483
diff
changeset
|
117 |
|
63997
e11ccb5aa82f
more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff
changeset
|
118 |
/* repository access */ |
e11ccb5aa82f
more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff
changeset
|
119 |
|
66570 | 120 |
def is_repository(root: Path, ssh: SSH.System = SSH.Local): Boolean = |
121 |
ssh.is_dir(root + Path.explode(".hg")) && |
|
122 |
new Repository(root, ssh).command("root").ok |
|
64330 | 123 |
|
75393 | 124 |
def repository(root: Path, ssh: SSH.System = SSH.Local): Repository = { |
64168 | 125 |
val hg = new Repository(root, ssh) |
126 |
hg.command("root").check |
|
127 |
hg |
|
128 |
} |
|
129 |
||
75393 | 130 |
def find_repository(start: Path, ssh: SSH.System = SSH.Local): Option[Repository] = { |
71601 | 131 |
@tailrec def find(root: Path): Option[Repository] = |
65559 | 132 |
if (is_repository(root, ssh)) Some(repository(root, ssh = ssh)) |
133 |
else if (root.is_root) None |
|
134 |
else find(root + Path.parent) |
|
135 |
||
66570 | 136 |
find(ssh.expand_path(start)) |
65559 | 137 |
} |
138 |
||
75474 | 139 |
def the_repository(start: Path, ssh: SSH.System = SSH.Local): Repository = |
140 |
find_repository(start, ssh = ssh) getOrElse |
|
141 |
error("No repository found in " + start.absolute) |
|
142 |
||
75393 | 143 |
private def make_repository( |
144 |
root: Path, |
|
145 |
cmd: String, |
|
146 |
args: String, |
|
147 |
ssh: SSH.System = SSH.Local |
|
148 |
) : Repository = { |
|
64230 | 149 |
val hg = new Repository(root, ssh) |
72375 | 150 |
ssh.make_directory(hg.root.dir) |
67066 | 151 |
hg.command(cmd, args, repository = false).check |
64168 | 152 |
hg |
153 |
} |
|
63997
e11ccb5aa82f
more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff
changeset
|
154 |
|
67065 | 155 |
def init_repository(root: Path, ssh: SSH.System = SSH.Local): Repository = |
67066 | 156 |
make_repository(root, "init", ssh.bash_path(root), ssh = ssh) |
67065 | 157 |
|
158 |
def clone_repository(source: String, root: Path, |
|
159 |
rev: String = "", options: String = "", ssh: SSH.System = SSH.Local): Repository = |
|
160 |
make_repository(root, "clone", |
|
67066 | 161 |
options + " " + Bash.string(source) + " " + ssh.bash_path(root) + opt_rev(rev), ssh = ssh) |
67065 | 162 |
|
75393 | 163 |
def setup_repository(source: String, root: Path, ssh: SSH.System = SSH.Local): Repository = { |
66570 | 164 |
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
|
165 |
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
|
166 |
} |
64230 | 167 |
|
75393 | 168 |
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
|
169 |
hg => |
097d122222f6
support remote repositories via ssh command execution;
wenzelm
parents:
64162
diff
changeset
|
170 |
|
71312
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
171 |
val root: Path = ssh.expand_path(root_path) |
66570 | 172 |
def root_url: String = ssh.hg_url + root.implode |
64230 | 173 |
|
71562 | 174 |
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
|
175 |
|
75393 | 176 |
def command_line( |
177 |
name: String, |
|
178 |
args: String = "", |
|
179 |
options: String = "", |
|
180 |
repository: Boolean = true |
|
181 |
): String = { |
|
73611 | 182 |
"export LANG=C HGPLAIN=\n\"${HG:-hg}\" --config " + Bash.string("defaults." + name + "=") + |
183 |
(if (repository) " --repository " + ssh.bash_path(root) else "") + |
|
184 |
" --noninteractive " + name + " " + options + " " + args |
|
185 |
} |
|
186 |
||
187 |
def command( |
|
75393 | 188 |
name: String, |
189 |
args: String = "", |
|
190 |
options: String = "", |
|
191 |
repository: Boolean = true |
|
192 |
): Process_Result = { |
|
73611 | 193 |
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
|
194 |
} |
63999 | 195 |
|
67068 | 196 |
def add(files: List[Path]): Unit = |
71601 | 197 |
hg.command("add", files.map(ssh.bash_path).mkString(" ")) |
67068 | 198 |
|
64505 | 199 |
def archive(target: String, rev: String = "", options: String = ""): Unit = |
200 |
hg.command("archive", opt_rev(rev) + " " + Bash.string(target), options).check |
|
201 |
||
63999 | 202 |
def heads(template: String = "{node|short}\n", options: String = ""): List[String] = |
64168 | 203 |
hg.command("heads", opt_template(template), options).check.out_lines |
63999 | 204 |
|
64232
367d83d6030e
clarified hg.id operation, with explicit tip as default;
wenzelm
parents:
64230
diff
changeset
|
205 |
def identify(rev: String = "tip", options: String = ""): String = |
64168 | 206 |
hg.command("id", opt_rev(rev), options).check.out_lines.headOption getOrElse "" |
63998 | 207 |
|
64232
367d83d6030e
clarified hg.id operation, with explicit tip as default;
wenzelm
parents:
64230
diff
changeset
|
208 |
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
|
209 |
|
75393 | 210 |
def tags(rev: String = "tip"): String = { |
73524 | 211 |
val result = identify(rev, options = "-t") |
212 |
Library.space_explode(' ', result).filterNot(_ == "tip").mkString(" ") |
|
213 |
} |
|
73521 | 214 |
|
71313 | 215 |
def paths(args: String = "", options: String = ""): List[String] = |
216 |
hg.command("paths", args = args, options = options).check.out_lines |
|
217 |
||
63998 | 218 |
def manifest(rev: String = "", options: String = ""): List[String] = |
64168 | 219 |
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
|
220 |
|
64033 | 221 |
def log(rev: String = "", template: String = "", options: String = ""): String = |
64168 | 222 |
hg.command("log", opt_rev(rev) + opt_template(template), options).check.out |
64027 | 223 |
|
67755 | 224 |
def parent(): String = log(rev = "p1()", template = "{node|short}") |
225 |
||
73340 | 226 |
def push( |
75393 | 227 |
remote: String = "", |
228 |
rev: String = "", |
|
229 |
force: Boolean = false, |
|
230 |
options: String = "" |
|
231 |
): 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
|
232 |
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
|
233 |
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
|
234 |
} |
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
|
235 |
|
63999 | 236 |
def pull(remote: String = "", rev: String = "", options: String = ""): Unit = |
64168 | 237 |
hg.command("pull", opt_rev(rev) + optional(remote), options).check |
63999 | 238 |
|
64168 | 239 |
def update( |
75393 | 240 |
rev: String = "", |
241 |
clean: Boolean = false, |
|
242 |
check: Boolean = false, |
|
243 |
options: String = "" |
|
244 |
): Unit = { |
|
64168 | 245 |
hg.command("update", |
246 |
opt_rev(rev) + opt_flag("--clean", clean) + opt_flag("--check", check), options).check |
|
63999 | 247 |
} |
65819
ff3dc9325eaa
explore repository structure, with minimal assumptions about "hg log" output;
wenzelm
parents:
65818
diff
changeset
|
248 |
|
75470 | 249 |
def status(options: String = ""): List[String] = |
250 |
hg.command("status", options = options).check.out_lines |
|
251 |
||
252 |
def known_files(): List[String] = status(options = "--modified --added --clean --no-status") |
|
65819
ff3dc9325eaa
explore repository structure, with minimal assumptions about "hg log" output;
wenzelm
parents:
65818
diff
changeset
|
253 |
|
75474 | 254 |
def sync(target: String, |
255 |
progress: Progress = new Progress, |
|
256 |
verbose: Boolean = false, |
|
75487 | 257 |
thorough: Boolean = false, |
75474 | 258 |
dry_run: Boolean = false, |
75475 | 259 |
clean: Boolean = false, |
75477 | 260 |
filter: List[String] = Nil, |
75479 | 261 |
rev: String = "" |
75474 | 262 |
): Unit = { |
75480 | 263 |
require(ssh == SSH.Local, "local repository required") |
264 |
||
75491 | 265 |
Isabelle_System.with_tmp_dir("sync") { tmp_dir => |
75479 | 266 |
val (options, source) = |
267 |
if (rev.isEmpty) { |
|
268 |
val exclude_path = tmp_dir + Path.explode("exclude") |
|
269 |
val exclude = status(options = "--unknown --ignored --no-status") |
|
270 |
File.write(exclude_path, cat_lines((".hg" :: exclude).map("/" + _))) |
|
75477 | 271 |
|
75479 | 272 |
val options = List("--exclude-from=" + exclude_path.implode) |
75480 | 273 |
val source = File.standard_path(root) |
75479 | 274 |
(options, source) |
275 |
} |
|
276 |
else { |
|
277 |
val source = File.standard_path(tmp_dir + Path.explode("archive")) |
|
278 |
archive(source, rev = rev) |
|
279 |
(Nil, source) |
|
280 |
} |
|
75474 | 281 |
Isabelle_System.rsync( |
75487 | 282 |
progress = progress, verbose = verbose, thorough = thorough, |
283 |
dry_run = dry_run, clean = clean, filter = filter, |
|
75485 | 284 |
args = List("--prune-empty-dirs") ::: options ::: List("--", source + "/.", target)) |
75474 | 285 |
} |
286 |
} |
|
287 |
||
75393 | 288 |
def graph(): Graph = { |
65819
ff3dc9325eaa
explore repository structure, with minimal assumptions about "hg log" output;
wenzelm
parents:
65818
diff
changeset
|
289 |
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
|
290 |
val log_result = |
65825
11f87ab51ddb
more robust command invocation, without defaults from hgrc;
wenzelm
parents:
65822
diff
changeset
|
291 |
log(template = """node: {node|short} {p1node|short} {p2node|short}\n""") |
73359 | 292 |
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
|
293 |
case (graph, Node(x, y, z)) => |
ff3dc9325eaa
explore repository structure, with minimal assumptions about "hg log" output;
wenzelm
parents:
65818
diff
changeset
|
294 |
val deps = List(y, z).filterNot(s => s.forall(_ == '0')) |
73359 | 295 |
val graph1 = (x :: deps).foldLeft(graph)(_.default_node(_, ())) |
296 |
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
|
297 |
case (graph, _) => graph |
ff3dc9325eaa
explore repository structure, with minimal assumptions about "hg log" output;
wenzelm
parents:
65818
diff
changeset
|
298 |
} |
ff3dc9325eaa
explore repository structure, with minimal assumptions about "hg log" output;
wenzelm
parents:
65818
diff
changeset
|
299 |
} |
63997
e11ccb5aa82f
more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff
changeset
|
300 |
} |
65832
2fb85623c386
implicitly check for unknown files (not part of a Mercurial repository);
wenzelm
parents:
65825
diff
changeset
|
301 |
|
2fb85623c386
implicitly check for unknown files (not part of a Mercurial repository);
wenzelm
parents:
65825
diff
changeset
|
302 |
|
75472 | 303 |
|
304 |
/** check files **/ |
|
65832
2fb85623c386
implicitly check for unknown files (not part of a Mercurial repository);
wenzelm
parents:
65825
diff
changeset
|
305 |
|
75393 | 306 |
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
|
307 |
val outside = new mutable.ListBuffer[Path] |
65832
2fb85623c386
implicitly check for unknown files (not part of a Mercurial repository);
wenzelm
parents:
65825
diff
changeset
|
308 |
val unknown = new mutable.ListBuffer[Path] |
2fb85623c386
implicitly check for unknown files (not part of a Mercurial repository);
wenzelm
parents:
65825
diff
changeset
|
309 |
|
75393 | 310 |
@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
|
311 |
paths match { |
2fb85623c386
implicitly check for unknown files (not part of a Mercurial repository);
wenzelm
parents:
65825
diff
changeset
|
312 |
case path :: rest => |
2fb85623c386
implicitly check for unknown files (not part of a Mercurial repository);
wenzelm
parents:
65825
diff
changeset
|
313 |
find_repository(path, ssh) match { |
67782
7e223a05e6d8
clarified notion of unknown files: ignore files outside of a Mercurial repository;
wenzelm
parents:
67755
diff
changeset
|
314 |
case None => outside += path; check(rest) |
65832
2fb85623c386
implicitly check for unknown files (not part of a Mercurial repository);
wenzelm
parents:
65825
diff
changeset
|
315 |
case Some(hg) => |
2fb85623c386
implicitly check for unknown files (not part of a Mercurial repository);
wenzelm
parents:
65825
diff
changeset
|
316 |
val known = |
2fb85623c386
implicitly check for unknown files (not part of a Mercurial repository);
wenzelm
parents:
65825
diff
changeset
|
317 |
hg.known_files().iterator.map(name => |
65833 | 318 |
(hg.root + Path.explode(name)).canonical_file).toSet |
319 |
if (!known(path.canonical_file)) unknown += path |
|
320 |
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
|
321 |
} |
2fb85623c386
implicitly check for unknown files (not part of a Mercurial repository);
wenzelm
parents:
65825
diff
changeset
|
322 |
case Nil => |
2fb85623c386
implicitly check for unknown files (not part of a Mercurial repository);
wenzelm
parents:
65825
diff
changeset
|
323 |
} |
2fb85623c386
implicitly check for unknown files (not part of a Mercurial repository);
wenzelm
parents:
65825
diff
changeset
|
324 |
} |
2fb85623c386
implicitly check for unknown files (not part of a Mercurial repository);
wenzelm
parents:
65825
diff
changeset
|
325 |
|
2fb85623c386
implicitly check for unknown files (not part of a Mercurial repository);
wenzelm
parents:
65825
diff
changeset
|
326 |
check(files) |
67782
7e223a05e6d8
clarified notion of unknown files: ignore files outside of a Mercurial repository;
wenzelm
parents:
67755
diff
changeset
|
327 |
(outside.toList, unknown.toList) |
65832
2fb85623c386
implicitly check for unknown files (not part of a Mercurial repository);
wenzelm
parents:
65825
diff
changeset
|
328 |
} |
71312
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
329 |
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
330 |
|
75472 | 331 |
|
332 |
/** hg_setup **/ |
|
71312
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
333 |
|
75393 | 334 |
private def edit_hgrc(local_hg: Repository, path_name: String, source: String): Unit = { |
71313 | 335 |
val hgrc = local_hg.root + Path.explode(".hg/hgrc") |
336 |
def header(line: String): Boolean = line.startsWith("[paths]") |
|
337 |
val Entry = """^(\S+)\s*=\s*(.*)$""".r |
|
338 |
val new_entry = path_name + " = " + source |
|
339 |
||
75393 | 340 |
def commit(lines: List[String]): Boolean = { |
71320 | 341 |
File.write(hgrc, cat_lines(lines)) |
71313 | 342 |
true |
343 |
} |
|
344 |
val edited = |
|
345 |
hgrc.is_file && { |
|
346 |
val lines = split_lines(File.read(hgrc)) |
|
71383 | 347 |
lines.count(header) == 1 && { |
71313 | 348 |
if (local_hg.paths(options = "-q").contains(path_name)) { |
349 |
val old_source = local_hg.paths(args = path_name).head |
|
71320 | 350 |
val old_entry = path_name + ".old = " + old_source |
71313 | 351 |
val lines1 = |
352 |
lines.map { |
|
71320 | 353 |
case Entry(a, b) if a == path_name && b == old_source => |
354 |
new_entry + "\n" + old_entry |
|
71313 | 355 |
case line => line |
356 |
} |
|
357 |
lines != lines1 && commit(lines1) |
|
358 |
} |
|
359 |
else { |
|
360 |
val prefix = lines.takeWhile(line => !header(line)) |
|
361 |
val n = prefix.length |
|
362 |
commit(prefix ::: List(lines(n), new_entry) ::: lines.drop(n + 1)) |
|
363 |
} |
|
364 |
} |
|
365 |
} |
|
366 |
if (!edited) File.append(hgrc, "\n[paths]\n" + new_entry + "\n") |
|
367 |
} |
|
368 |
||
71312
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
369 |
val default_path_name = "default" |
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
370 |
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
371 |
def hg_setup( |
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
372 |
remote: String, |
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
373 |
local_path: Path, |
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
374 |
remote_name: String = "", |
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
375 |
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
|
376 |
remote_exists: Boolean = false, |
75393 | 377 |
progress: Progress = new Progress |
378 |
): Unit = { |
|
71312
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
379 |
/* local repository */ |
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
380 |
|
72375 | 381 |
Isabelle_System.make_directory(local_path) |
71312
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 |
val repos_name = |
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
384 |
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
|
385 |
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
386 |
val local_hg = |
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
387 |
if (is_repository(local_path)) repository(local_path) |
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
388 |
else init_repository(local_path) |
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
389 |
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
390 |
progress.echo("Local repository " + local_hg.root.absolute) |
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
391 |
|
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 |
/* remote repository */ |
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 |
val remote_url = |
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
396 |
remote match { |
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
397 |
case _ if remote.startsWith("ssh://") => |
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
398 |
val ssh_url = remote + "/" + repos_name |
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
399 |
|
71321
edf3210a61a2
added option -r: support more robust consolidation of local clones with varying names;
wenzelm
parents:
71320
diff
changeset
|
400 |
if (!remote_exists) { |
edf3210a61a2
added option -r: support more robust consolidation of local clones with varying names;
wenzelm
parents:
71320
diff
changeset
|
401 |
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
|
402 |
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
|
403 |
} |
71312
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
404 |
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
405 |
ssh_url |
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
406 |
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
407 |
case SSH.Target(user, host) => |
71314 | 408 |
val phabricator = Phabricator.API(user, host) |
71312
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
409 |
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
410 |
var repos = |
71321
edf3210a61a2
added option -r: support more robust consolidation of local clones with varying names;
wenzelm
parents:
71320
diff
changeset
|
411 |
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
|
412 |
if (remote_exists) { |
edf3210a61a2
added option -r: support more robust consolidation of local clones with varying names;
wenzelm
parents:
71320
diff
changeset
|
413 |
error("Remote repository " + |
edf3210a61a2
added option -r: support more robust consolidation of local clones with varying names;
wenzelm
parents:
71320
diff
changeset
|
414 |
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
|
415 |
} |
edf3210a61a2
added option -r: support more robust consolidation of local clones with varying names;
wenzelm
parents:
71320
diff
changeset
|
416 |
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
|
417 |
} |
71312
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
418 |
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
419 |
while (repos.importing) { |
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
420 |
progress.echo("Awaiting remote repository ...") |
73702
7202e12cb324
tuned signature --- following hints by IntelliJ IDEA;
wenzelm
parents:
73624
diff
changeset
|
421 |
Time.seconds(0.5).sleep() |
71312
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
422 |
repos = phabricator.the_repository(repos.phid) |
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
423 |
} |
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
424 |
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
425 |
repos.ssh_url |
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
426 |
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
427 |
case _ => error("Malformed remote specification " + quote(remote)) |
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
428 |
} |
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
429 |
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
430 |
progress.echo("Remote repository " + quote(remote_url)) |
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
431 |
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
432 |
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
433 |
/* synchronize local and remote state */ |
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
434 |
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
435 |
progress.echo("Synchronizing ...") |
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
436 |
|
71313 | 437 |
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
|
438 |
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
439 |
local_hg.pull(options = "-u") |
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
440 |
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
441 |
local_hg.push(remote = remote_url) |
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
442 |
} |
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
443 |
|
75474 | 444 |
val isabelle_tool1 = |
72763 | 445 |
Isabelle_Tool("hg_setup", "setup remote vs. local Mercurial repository", |
75394 | 446 |
Scala_Project.here, |
447 |
{ args => |
|
448 |
var remote_name = "" |
|
449 |
var path_name = default_path_name |
|
450 |
var remote_exists = false |
|
71312
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
451 |
|
75394 | 452 |
val getopts = Getopts(""" |
71322 | 453 |
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
|
454 |
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
455 |
Options are: |
71322 | 456 |
-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
|
457 |
-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
|
458 |
-r assume that remote repository already exists |
71312
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
459 |
|
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
460 |
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
|
461 |
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
|
462 |
""", |
75394 | 463 |
"n:" -> (arg => remote_name = arg), |
464 |
"p:" -> (arg => path_name = arg), |
|
465 |
"r" -> (_ => remote_exists = true)) |
|
71312
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
466 |
|
75394 | 467 |
val more_args = getopts(args) |
71312
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
468 |
|
75394 | 469 |
val (remote, local_path) = |
470 |
more_args match { |
|
471 |
case List(arg1, arg2) => (arg1, Path.explode(arg2)) |
|
472 |
case _ => getopts.usage() |
|
473 |
} |
|
71312
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
474 |
|
75394 | 475 |
val progress = new Console_Progress |
71312
937328d61436
added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents:
68566
diff
changeset
|
476 |
|
75394 | 477 |
hg_setup(remote, local_path, remote_name = remote_name, path_name = path_name, |
478 |
remote_exists = remote_exists, progress = progress) |
|
479 |
}) |
|
75474 | 480 |
|
481 |
||
482 |
||
483 |
/** hg_sync **/ |
|
484 |
||
485 |
val isabelle_tool2 = |
|
75479 | 486 |
Isabelle_Tool("hg_sync", "synchronize Mercurial repository with target directory", |
75474 | 487 |
Scala_Project.here, { args => |
488 |
var clean = false |
|
75489 | 489 |
var filter: List[String] = Nil |
75474 | 490 |
var root: Option[Path] = None |
75487 | 491 |
var thorough = false |
75474 | 492 |
var dry_run = false |
75479 | 493 |
var rev = "" |
75474 | 494 |
var verbose = false |
495 |
||
496 |
val getopts = Getopts(""" |
|
497 |
Usage: isabelle hg_sync [OPTIONS] TARGET |
|
498 |
||
499 |
Options are: |
|
500 |
-C clean all unknown/ignored files on target |
|
75476 | 501 |
(implies -n for testing; use option -f to confirm) |
75489 | 502 |
-F RULE add rsync filter RULE (e.g. "protect /foo" to avoid deletion) |
75474 | 503 |
-R ROOT explicit repository root directory |
504 |
(default: implicit from current directory) |
|
75487 | 505 |
-T thorough check of file content (default: time and size) |
75474 | 506 |
-f force changes: no dry-run |
507 |
-n no changes: dry-run |
|
75479 | 508 |
-r REV explicit revision (default: state of working directory) |
75474 | 509 |
-v verbose |
510 |
||
75479 | 511 |
Synchronize Mercurial repository with TARGET directory, |
75474 | 512 |
which can be local or remote (using notation of rsync). |
513 |
""", |
|
514 |
"C" -> { _ => clean = true; dry_run = true }, |
|
75489 | 515 |
"F:" -> (arg => filter = filter ::: List(arg)), |
75474 | 516 |
"R:" -> (arg => root = Some(Path.explode(arg))), |
75487 | 517 |
"T" -> (_ => thorough = true), |
75474 | 518 |
"f" -> (_ => dry_run = false), |
519 |
"n" -> (_ => dry_run = true), |
|
75479 | 520 |
"r:" -> (arg => rev = arg), |
75474 | 521 |
"v" -> (_ => verbose = true)) |
522 |
||
523 |
val more_args = getopts(args) |
|
524 |
val target = |
|
525 |
more_args match { |
|
526 |
case List(target) => target |
|
527 |
case _ => getopts.usage() |
|
528 |
} |
|
529 |
||
530 |
val progress = new Console_Progress |
|
531 |
val hg = |
|
532 |
root match { |
|
533 |
case Some(dir) => repository(dir) |
|
534 |
case None => the_repository(Path.current) |
|
535 |
} |
|
75487 | 536 |
hg.sync(target, progress = progress, verbose = verbose, thorough = thorough, |
75489 | 537 |
dry_run = dry_run, clean = clean, filter = filter, rev = rev) |
75474 | 538 |
} |
539 |
) |
|
63997
e11ccb5aa82f
more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff
changeset
|
540 |
} |