src/Pure/General/mercurial.scala
author wenzelm
Mon, 30 May 2022 11:02:13 +0200
changeset 75491 47d790984e82
parent 75489 f08fd5048df3
child 75493 f775dfb55655
permissions -rw-r--r--
tuned names;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
wenzelm
parents: 64256
diff changeset
     4
Support for Mercurial repositories, with local or remote repository clone
wenzelm
parents: 64256
diff changeset
     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
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73702
diff changeset
    15
object Mercurial {
66895
e378e0468ef2 tuned: build hg_graph only once;
wenzelm
parents: 66570
diff changeset
    16
  type Graph = isabelle.Graph[String, Unit]
e378e0468ef2 tuned: build hg_graph only once;
wenzelm
parents: 66570
diff changeset
    17
e378e0468ef2 tuned: build hg_graph only once;
wenzelm
parents: 66570
diff changeset
    18
75472
ff6d4b48f23b tuned comments;
wenzelm
parents: 75470
diff changeset
    19
ff6d4b48f23b tuned comments;
wenzelm
parents: 75470
diff changeset
    20
  /** HTTP server **/
73611
cc36841eeff6 clarified signature: more operations;
wenzelm
parents: 73610
diff changeset
    21
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73702
diff changeset
    22
  object Server {
73611
cc36841eeff6 clarified signature: more operations;
wenzelm
parents: 73610
diff changeset
    23
    def apply(root: String): Server = new Server(root)
cc36841eeff6 clarified signature: more operations;
wenzelm
parents: 73610
diff changeset
    24
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73702
diff changeset
    25
    def start(root: Path): Server = {
73611
cc36841eeff6 clarified signature: more operations;
wenzelm
parents: 73610
diff changeset
    26
      val hg = repository(root)
73609
58b17dca57ef clarified signature;
wenzelm
parents: 73524
diff changeset
    27
73611
cc36841eeff6 clarified signature: more operations;
wenzelm
parents: 73610
diff changeset
    28
      val server_process = Future.promise[Bash.Process]
cc36841eeff6 clarified signature: more operations;
wenzelm
parents: 73610
diff changeset
    29
      val server_root = Future.promise[String]
cc36841eeff6 clarified signature: more operations;
wenzelm
parents: 73610
diff changeset
    30
      Isabelle_Thread.fork("hg") {
cc36841eeff6 clarified signature: more operations;
wenzelm
parents: 73610
diff changeset
    31
        val process =
cc36841eeff6 clarified signature: more operations;
wenzelm
parents: 73610
diff changeset
    32
          Exn.capture { Bash.process(hg.command_line("serve", options = "--port 0 --print-url")) }
cc36841eeff6 clarified signature: more operations;
wenzelm
parents: 73610
diff changeset
    33
        server_process.fulfill_result(process)
cc36841eeff6 clarified signature: more operations;
wenzelm
parents: 73610
diff changeset
    34
        Exn.release(process).result(progress_stdout =
cc36841eeff6 clarified signature: more operations;
wenzelm
parents: 73610
diff changeset
    35
          line => if (!server_root.is_finished) {
cc36841eeff6 clarified signature: more operations;
wenzelm
parents: 73610
diff changeset
    36
            server_root.fulfill(Library.try_unsuffix("/", line).getOrElse(line))
cc36841eeff6 clarified signature: more operations;
wenzelm
parents: 73610
diff changeset
    37
          })
cc36841eeff6 clarified signature: more operations;
wenzelm
parents: 73610
diff changeset
    38
      }
cc36841eeff6 clarified signature: more operations;
wenzelm
parents: 73610
diff changeset
    39
      server_process.join
cc36841eeff6 clarified signature: more operations;
wenzelm
parents: 73610
diff changeset
    40
cc36841eeff6 clarified signature: more operations;
wenzelm
parents: 73610
diff changeset
    41
      new Server(server_root.join) {
cc36841eeff6 clarified signature: more operations;
wenzelm
parents: 73610
diff changeset
    42
        override def close(): Unit = server_process.join.terminate()
cc36841eeff6 clarified signature: more operations;
wenzelm
parents: 73610
diff changeset
    43
      }
cc36841eeff6 clarified signature: more operations;
wenzelm
parents: 73610
diff changeset
    44
    }
73609
58b17dca57ef clarified signature;
wenzelm
parents: 73524
diff changeset
    45
  }
58b17dca57ef clarified signature;
wenzelm
parents: 73524
diff changeset
    46
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73702
diff changeset
    47
  class Server private(val root: String) extends AutoCloseable {
73609
58b17dca57ef clarified signature;
wenzelm
parents: 73524
diff changeset
    48
    override def toString: String = root
58b17dca57ef clarified signature;
wenzelm
parents: 73524
diff changeset
    49
73611
cc36841eeff6 clarified signature: more operations;
wenzelm
parents: 73610
diff changeset
    50
    def close(): Unit = ()
cc36841eeff6 clarified signature: more operations;
wenzelm
parents: 73610
diff changeset
    51
73610
6ba5f9d18c56 clarified signature: more operations;
wenzelm
parents: 73609
diff changeset
    52
    def changeset(rev: String = "tip", raw: Boolean = false): String =
6ba5f9d18c56 clarified signature: more operations;
wenzelm
parents: 73609
diff changeset
    53
      root + (if (raw) "/raw-rev/" else "/rev/") + rev
6ba5f9d18c56 clarified signature: more operations;
wenzelm
parents: 73609
diff changeset
    54
6ba5f9d18c56 clarified signature: more operations;
wenzelm
parents: 73609
diff changeset
    55
    def file(path: Path, rev: String = "tip", raw: Boolean = false): String =
6ba5f9d18c56 clarified signature: more operations;
wenzelm
parents: 73609
diff changeset
    56
      root + (if (raw) "/raw-file/" else "/file/") + rev + "/" + path.expand.implode
6ba5f9d18c56 clarified signature: more operations;
wenzelm
parents: 73609
diff changeset
    57
6ba5f9d18c56 clarified signature: more operations;
wenzelm
parents: 73609
diff changeset
    58
    def archive(rev: String = "tip"): String =
6ba5f9d18c56 clarified signature: more operations;
wenzelm
parents: 73609
diff changeset
    59
      root + "/archive/" + rev + ".tar.gz"
6ba5f9d18c56 clarified signature: more operations;
wenzelm
parents: 73609
diff changeset
    60
6ba5f9d18c56 clarified signature: more operations;
wenzelm
parents: 73609
diff changeset
    61
    def read_changeset(rev: String = "tip"): String =
6ba5f9d18c56 clarified signature: more operations;
wenzelm
parents: 73609
diff changeset
    62
      Url.read(changeset(rev = rev, raw = true))
6ba5f9d18c56 clarified signature: more operations;
wenzelm
parents: 73609
diff changeset
    63
6ba5f9d18c56 clarified signature: more operations;
wenzelm
parents: 73609
diff changeset
    64
    def read_file(path: Path, rev: String = "tip"): String =
6ba5f9d18c56 clarified signature: more operations;
wenzelm
parents: 73609
diff changeset
    65
      Url.read(file(path, rev = rev, raw = true))
6ba5f9d18c56 clarified signature: more operations;
wenzelm
parents: 73609
diff changeset
    66
6ba5f9d18c56 clarified signature: more operations;
wenzelm
parents: 73609
diff changeset
    67
    def download_archive(rev: String = "tip", progress: Progress = new Progress): HTTP.Content =
6ba5f9d18c56 clarified signature: more operations;
wenzelm
parents: 73609
diff changeset
    68
      Isabelle_System.download(archive(rev = rev), progress = progress)
6ba5f9d18c56 clarified signature: more operations;
wenzelm
parents: 73609
diff changeset
    69
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73702
diff changeset
    70
    def download_dir(dir: Path, rev: String = "tip", progress: Progress = new Progress): Unit = {
73610
6ba5f9d18c56 clarified signature: more operations;
wenzelm
parents: 73609
diff changeset
    71
      Isabelle_System.new_directory(dir)
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
    72
      Isabelle_System.with_tmp_file("rev", ext = ".tar.gz") { archive_path =>
73610
6ba5f9d18c56 clarified signature: more operations;
wenzelm
parents: 73609
diff changeset
    73
        val content = download_archive(rev = rev, progress = progress)
6ba5f9d18c56 clarified signature: more operations;
wenzelm
parents: 73609
diff changeset
    74
        Bytes.write(archive_path, content.bytes)
6ba5f9d18c56 clarified signature: more operations;
wenzelm
parents: 73609
diff changeset
    75
        progress.echo("Unpacking " + rev + ".tar.gz")
73624
f033d4f661e9 tuned signature;
wenzelm
parents: 73611
diff changeset
    76
        Isabelle_System.gnutar("-xzf " + File.bash_path(archive_path),
f033d4f661e9 tuned signature;
wenzelm
parents: 73611
diff changeset
    77
          dir = dir, original_owner = true, strip = 1).check
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
    78
      }
73610
6ba5f9d18c56 clarified signature: more operations;
wenzelm
parents: 73609
diff changeset
    79
    }
73609
58b17dca57ef clarified signature;
wenzelm
parents: 73524
diff changeset
    80
  }
58b17dca57ef clarified signature;
wenzelm
parents: 73524
diff changeset
    81
58b17dca57ef clarified signature;
wenzelm
parents: 73524
diff changeset
    82
75472
ff6d4b48f23b tuned comments;
wenzelm
parents: 75470
diff changeset
    83
ff6d4b48f23b tuned comments;
wenzelm
parents: 75470
diff changeset
    84
  /** repository commands **/
ff6d4b48f23b tuned comments;
wenzelm
parents: 75470
diff changeset
    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
5649a993666d more operations;
wenzelm
parents: 63998
diff changeset
    88
  def optional(s: String, prefix: String = ""): String =
64304
96bc94c87a81 clarified modules;
wenzelm
parents: 64280
diff changeset
    89
    if (s == "") "" else " " + prefix + " " + Bash.string(s)
63999
5649a993666d more operations;
wenzelm
parents: 63998
diff changeset
    90
5649a993666d more operations;
wenzelm
parents: 63998
diff changeset
    91
  def opt_flag(flag: String, b: Boolean): String = if (b) " " + flag else ""
5649a993666d more operations;
wenzelm
parents: 63998
diff changeset
    92
  def opt_rev(s: String): String = optional(s, "--rev")
5649a993666d more operations;
wenzelm
parents: 63998
diff changeset
    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
a6ca869af096 more robust and uniform ISABELLE_TAGS;
wenzelm
parents: 73520
diff changeset
    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
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73702
diff changeset
   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
a6ca869af096 more robust and uniform ISABELLE_TAGS;
wenzelm
parents: 73520
diff changeset
   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
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73702
diff changeset
   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
a6ca869af096 more robust and uniform ISABELLE_TAGS;
wenzelm
parents: 73520
diff changeset
   111
  def archive_id(root: Path): Option[String] =
a6ca869af096 more robust and uniform ISABELLE_TAGS;
wenzelm
parents: 73520
diff changeset
   112
    archive_info(root).flatMap(_.id)
a6ca869af096 more robust and uniform ISABELLE_TAGS;
wenzelm
parents: 73520
diff changeset
   113
a6ca869af096 more robust and uniform ISABELLE_TAGS;
wenzelm
parents: 73520
diff changeset
   114
  def archive_tags(root: Path): Option[String] =
a6ca869af096 more robust and uniform ISABELLE_TAGS;
wenzelm
parents: 73520
diff changeset
   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
9af879e222cc clarified signature;
wenzelm
parents: 66569
diff changeset
   120
  def is_repository(root: Path, ssh: SSH.System = SSH.Local): Boolean =
9af879e222cc clarified signature;
wenzelm
parents: 66569
diff changeset
   121
    ssh.is_dir(root + Path.explode(".hg")) &&
9af879e222cc clarified signature;
wenzelm
parents: 66569
diff changeset
   122
    new Repository(root, ssh).command("root").ok
64330
d9a9ae3956f6 more operations;
wenzelm
parents: 64304
diff changeset
   123
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73702
diff changeset
   124
  def repository(root: Path, ssh: SSH.System = SSH.Local): Repository = {
64168
e573b985390c added clone_repository;
wenzelm
parents: 64167
diff changeset
   125
    val hg = new Repository(root, ssh)
e573b985390c added clone_repository;
wenzelm
parents: 64167
diff changeset
   126
    hg.command("root").check
e573b985390c added clone_repository;
wenzelm
parents: 64167
diff changeset
   127
    hg
e573b985390c added clone_repository;
wenzelm
parents: 64167
diff changeset
   128
  }
e573b985390c added clone_repository;
wenzelm
parents: 64167
diff changeset
   129
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73702
diff changeset
   130
  def find_repository(start: Path, ssh: SSH.System = SSH.Local): Option[Repository] = {
71601
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 71562
diff changeset
   131
    @tailrec def find(root: Path): Option[Repository] =
65559
7ff7781913a4 more operations;
wenzelm
parents: 64505
diff changeset
   132
      if (is_repository(root, ssh)) Some(repository(root, ssh = ssh))
7ff7781913a4 more operations;
wenzelm
parents: 64505
diff changeset
   133
      else if (root.is_root) None
7ff7781913a4 more operations;
wenzelm
parents: 64505
diff changeset
   134
      else find(root + Path.parent)
7ff7781913a4 more operations;
wenzelm
parents: 64505
diff changeset
   135
66570
9af879e222cc clarified signature;
wenzelm
parents: 66569
diff changeset
   136
    find(ssh.expand_path(start))
65559
7ff7781913a4 more operations;
wenzelm
parents: 64505
diff changeset
   137
  }
7ff7781913a4 more operations;
wenzelm
parents: 64505
diff changeset
   138
75474
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   139
  def the_repository(start: Path, ssh: SSH.System = SSH.Local): Repository =
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   140
    find_repository(start, ssh = ssh) getOrElse
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   141
      error("No repository found in " + start.absolute)
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   142
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73702
diff changeset
   143
  private def make_repository(
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73702
diff changeset
   144
    root: Path,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73702
diff changeset
   145
    cmd: String,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73702
diff changeset
   146
    args: String,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73702
diff changeset
   147
    ssh: SSH.System = SSH.Local
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73702
diff changeset
   148
  ) : Repository = {
64230
13a97c1d7d22 added setup_repository;
wenzelm
parents: 64198
diff changeset
   149
    val hg = new Repository(root, ssh)
72375
e48d93811ed7 clarified signature;
wenzelm
parents: 71726
diff changeset
   150
    ssh.make_directory(hg.root.dir)
67066
1645cef7a49c proper ssh.bash_path;
wenzelm
parents: 67065
diff changeset
   151
    hg.command(cmd, args, repository = false).check
64168
e573b985390c added clone_repository;
wenzelm
parents: 64167
diff changeset
   152
    hg
e573b985390c added clone_repository;
wenzelm
parents: 64167
diff changeset
   153
  }
63997
e11ccb5aa82f more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff changeset
   154
67065
d9a347af82ab more operations;
wenzelm
parents: 66895
diff changeset
   155
  def init_repository(root: Path, ssh: SSH.System = SSH.Local): Repository =
67066
1645cef7a49c proper ssh.bash_path;
wenzelm
parents: 67065
diff changeset
   156
    make_repository(root, "init", ssh.bash_path(root), ssh = ssh)
67065
d9a347af82ab more operations;
wenzelm
parents: 66895
diff changeset
   157
d9a347af82ab more operations;
wenzelm
parents: 66895
diff changeset
   158
  def clone_repository(source: String, root: Path,
d9a347af82ab more operations;
wenzelm
parents: 66895
diff changeset
   159
      rev: String = "", options: String = "", ssh: SSH.System = SSH.Local): Repository =
d9a347af82ab more operations;
wenzelm
parents: 66895
diff changeset
   160
    make_repository(root, "clone",
67066
1645cef7a49c proper ssh.bash_path;
wenzelm
parents: 67065
diff changeset
   161
      options + " " + Bash.string(source) + " " + ssh.bash_path(root) + opt_rev(rev), ssh = ssh)
67065
d9a347af82ab more operations;
wenzelm
parents: 66895
diff changeset
   162
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73702
diff changeset
   163
  def setup_repository(source: String, root: Path, ssh: SSH.System = SSH.Local): Repository = {
66570
9af879e222cc clarified signature;
wenzelm
parents: 66569
diff changeset
   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
13a97c1d7d22 added setup_repository;
wenzelm
parents: 64198
diff changeset
   167
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73702
diff changeset
   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
9af879e222cc clarified signature;
wenzelm
parents: 66569
diff changeset
   172
    def root_url: String = ssh.hg_url + root.implode
64230
13a97c1d7d22 added setup_repository;
wenzelm
parents: 64198
diff changeset
   173
71562
794c8b0ad8f1 clarified output;
wenzelm
parents: 71383
diff changeset
   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
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73702
diff changeset
   176
    def command_line(
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73702
diff changeset
   177
      name: String,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73702
diff changeset
   178
      args: String = "",
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73702
diff changeset
   179
      options: String = "",
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73702
diff changeset
   180
      repository: Boolean = true
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73702
diff changeset
   181
    ): String = {
73611
cc36841eeff6 clarified signature: more operations;
wenzelm
parents: 73610
diff changeset
   182
      "export LANG=C HGPLAIN=\n\"${HG:-hg}\" --config " + Bash.string("defaults." + name + "=") +
cc36841eeff6 clarified signature: more operations;
wenzelm
parents: 73610
diff changeset
   183
        (if (repository) " --repository " + ssh.bash_path(root) else "") +
cc36841eeff6 clarified signature: more operations;
wenzelm
parents: 73610
diff changeset
   184
        " --noninteractive " + name + " " + options + " " + args
cc36841eeff6 clarified signature: more operations;
wenzelm
parents: 73610
diff changeset
   185
    }
cc36841eeff6 clarified signature: more operations;
wenzelm
parents: 73610
diff changeset
   186
cc36841eeff6 clarified signature: more operations;
wenzelm
parents: 73610
diff changeset
   187
    def command(
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73702
diff changeset
   188
      name: String,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73702
diff changeset
   189
      args: String = "",
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73702
diff changeset
   190
      options: String = "",
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73702
diff changeset
   191
      repository: Boolean = true
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73702
diff changeset
   192
    ): Process_Result = {
73611
cc36841eeff6 clarified signature: more operations;
wenzelm
parents: 73610
diff changeset
   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
5649a993666d more operations;
wenzelm
parents: 63998
diff changeset
   195
67068
46ce32fd5f53 more operations;
wenzelm
parents: 67066
diff changeset
   196
    def add(files: List[Path]): Unit =
71601
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 71562
diff changeset
   197
      hg.command("add", files.map(ssh.bash_path).mkString(" "))
67068
46ce32fd5f53 more operations;
wenzelm
parents: 67066
diff changeset
   198
64505
545a7ab3c35f optional component setup;
wenzelm
parents: 64451
diff changeset
   199
    def archive(target: String, rev: String = "", options: String = ""): Unit =
545a7ab3c35f optional component setup;
wenzelm
parents: 64451
diff changeset
   200
      hg.command("archive", opt_rev(rev) + " " + Bash.string(target), options).check
545a7ab3c35f optional component setup;
wenzelm
parents: 64451
diff changeset
   201
63999
5649a993666d more operations;
wenzelm
parents: 63998
diff changeset
   202
    def heads(template: String = "{node|short}\n", options: String = ""): List[String] =
64168
e573b985390c added clone_repository;
wenzelm
parents: 64167
diff changeset
   203
      hg.command("heads", opt_template(template), options).check.out_lines
63999
5649a993666d more operations;
wenzelm
parents: 63998
diff changeset
   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
e573b985390c added clone_repository;
wenzelm
parents: 64167
diff changeset
   206
      hg.command("id", opt_rev(rev), options).check.out_lines.headOption getOrElse ""
63998
2f38d8aff2f5 more operations;
wenzelm
parents: 63997
diff changeset
   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
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73702
diff changeset
   210
    def tags(rev: String = "tip"): String = {
73524
c52d819499a1 clarified: follow "isabelle version -t";
wenzelm
parents: 73521
diff changeset
   211
      val result = identify(rev, options = "-t")
c52d819499a1 clarified: follow "isabelle version -t";
wenzelm
parents: 73521
diff changeset
   212
      Library.space_explode(' ', result).filterNot(_ == "tip").mkString(" ")
c52d819499a1 clarified: follow "isabelle version -t";
wenzelm
parents: 73521
diff changeset
   213
    }
73521
a6ca869af096 more robust and uniform ISABELLE_TAGS;
wenzelm
parents: 73520
diff changeset
   214
71313
c7bf771cdfb5 more ambitious edit_hgrc;
wenzelm
parents: 71312
diff changeset
   215
    def paths(args: String = "", options: String = ""): List[String] =
c7bf771cdfb5 more ambitious edit_hgrc;
wenzelm
parents: 71312
diff changeset
   216
      hg.command("paths", args = args, options = options).check.out_lines
c7bf771cdfb5 more ambitious edit_hgrc;
wenzelm
parents: 71312
diff changeset
   217
63998
2f38d8aff2f5 more operations;
wenzelm
parents: 63997
diff changeset
   218
    def manifest(rev: String = "", options: String = ""): List[String] =
64168
e573b985390c added clone_repository;
wenzelm
parents: 64167
diff changeset
   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
2989c1f2593a tuned signature;
wenzelm
parents: 64028
diff changeset
   221
    def log(rev: String = "", template: String = "", options: String = ""): String =
64168
e573b985390c added clone_repository;
wenzelm
parents: 64167
diff changeset
   222
      hg.command("log", opt_rev(rev) + opt_template(template), options).check.out
64027
4a33d740c9dc proper log output;
wenzelm
parents: 64020
diff changeset
   223
67755
208235e594f6 more operations;
wenzelm
parents: 67068
diff changeset
   224
    def parent(): String = log(rev = "p1()", template = "{node|short}")
208235e594f6 more operations;
wenzelm
parents: 67068
diff changeset
   225
73340
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 72763
diff changeset
   226
    def push(
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73702
diff changeset
   227
      remote: String = "",
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73702
diff changeset
   228
      rev: String = "",
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73702
diff changeset
   229
      force: Boolean = false,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73702
diff changeset
   230
      options: String = ""
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73702
diff changeset
   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
5649a993666d more operations;
wenzelm
parents: 63998
diff changeset
   236
    def pull(remote: String = "", rev: String = "", options: String = ""): Unit =
64168
e573b985390c added clone_repository;
wenzelm
parents: 64167
diff changeset
   237
      hg.command("pull", opt_rev(rev) + optional(remote), options).check
63999
5649a993666d more operations;
wenzelm
parents: 63998
diff changeset
   238
64168
e573b985390c added clone_repository;
wenzelm
parents: 64167
diff changeset
   239
    def update(
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73702
diff changeset
   240
      rev: String = "",
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73702
diff changeset
   241
      clean: Boolean = false,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73702
diff changeset
   242
      check: Boolean = false,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73702
diff changeset
   243
      options: String = ""
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73702
diff changeset
   244
    ): Unit = {
64168
e573b985390c added clone_repository;
wenzelm
parents: 64167
diff changeset
   245
      hg.command("update",
e573b985390c added clone_repository;
wenzelm
parents: 64167
diff changeset
   246
        opt_rev(rev) + opt_flag("--clean", clean) + opt_flag("--check", check), options).check
63999
5649a993666d more operations;
wenzelm
parents: 63998
diff changeset
   247
    }
65819
ff3dc9325eaa explore repository structure, with minimal assumptions about "hg log" output;
wenzelm
parents: 65818
diff changeset
   248
75470
e3f753ef0b5c tuned signature;
wenzelm
parents: 75394
diff changeset
   249
    def status(options: String = ""): List[String] =
e3f753ef0b5c tuned signature;
wenzelm
parents: 75394
diff changeset
   250
      hg.command("status", options = options).check.out_lines
e3f753ef0b5c tuned signature;
wenzelm
parents: 75394
diff changeset
   251
e3f753ef0b5c tuned signature;
wenzelm
parents: 75394
diff changeset
   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
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   254
    def sync(target: String,
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   255
      progress: Progress = new Progress,
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   256
      verbose: Boolean = false,
75487
167660a8f99e support thorough check of file content;
wenzelm
parents: 75485
diff changeset
   257
      thorough: Boolean = false,
75474
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   258
      dry_run: Boolean = false,
75475
f1d204a4d795 support filter rules, notably "protect";
wenzelm
parents: 75474
diff changeset
   259
      clean: Boolean = false,
75477
wenzelm
parents: 75476
diff changeset
   260
      filter: List[String] = Nil,
75479
4363ad65ad36 support option -r;
wenzelm
parents: 75477
diff changeset
   261
      rev: String = ""
75474
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   262
    ): Unit = {
75480
6c93c13ba3c8 more robust: local repository required;
wenzelm
parents: 75479
diff changeset
   263
      require(ssh == SSH.Local, "local repository required")
6c93c13ba3c8 more robust: local repository required;
wenzelm
parents: 75479
diff changeset
   264
75491
47d790984e82 tuned names;
wenzelm
parents: 75489
diff changeset
   265
      Isabelle_System.with_tmp_dir("sync") { tmp_dir =>
75479
4363ad65ad36 support option -r;
wenzelm
parents: 75477
diff changeset
   266
        val (options, source) =
4363ad65ad36 support option -r;
wenzelm
parents: 75477
diff changeset
   267
          if (rev.isEmpty) {
4363ad65ad36 support option -r;
wenzelm
parents: 75477
diff changeset
   268
            val exclude_path = tmp_dir + Path.explode("exclude")
4363ad65ad36 support option -r;
wenzelm
parents: 75477
diff changeset
   269
            val exclude = status(options = "--unknown --ignored --no-status")
4363ad65ad36 support option -r;
wenzelm
parents: 75477
diff changeset
   270
            File.write(exclude_path, cat_lines((".hg" :: exclude).map("/" + _)))
75477
wenzelm
parents: 75476
diff changeset
   271
75479
4363ad65ad36 support option -r;
wenzelm
parents: 75477
diff changeset
   272
            val options = List("--exclude-from=" + exclude_path.implode)
75480
6c93c13ba3c8 more robust: local repository required;
wenzelm
parents: 75479
diff changeset
   273
            val source = File.standard_path(root)
75479
4363ad65ad36 support option -r;
wenzelm
parents: 75477
diff changeset
   274
            (options, source)
4363ad65ad36 support option -r;
wenzelm
parents: 75477
diff changeset
   275
          }
4363ad65ad36 support option -r;
wenzelm
parents: 75477
diff changeset
   276
          else {
4363ad65ad36 support option -r;
wenzelm
parents: 75477
diff changeset
   277
            val source = File.standard_path(tmp_dir + Path.explode("archive"))
4363ad65ad36 support option -r;
wenzelm
parents: 75477
diff changeset
   278
            archive(source, rev = rev)
4363ad65ad36 support option -r;
wenzelm
parents: 75477
diff changeset
   279
            (Nil, source)
4363ad65ad36 support option -r;
wenzelm
parents: 75477
diff changeset
   280
          }
75474
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   281
        Isabelle_System.rsync(
75487
167660a8f99e support thorough check of file content;
wenzelm
parents: 75485
diff changeset
   282
          progress = progress, verbose = verbose, thorough = thorough,
167660a8f99e support thorough check of file content;
wenzelm
parents: 75485
diff changeset
   283
          dry_run = dry_run, clean = clean, filter = filter,
75485
d8ee3e4d74ef clarified signature;
wenzelm
parents: 75480
diff changeset
   284
          args = List("--prune-empty-dirs") ::: options ::: List("--", source + "/.", target))
75474
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   285
      }
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   286
    }
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   287
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73702
diff changeset
   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
d8a0e996614b tuned --- fewer warnings;
wenzelm
parents: 73340
diff changeset
   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
d8a0e996614b tuned --- fewer warnings;
wenzelm
parents: 73340
diff changeset
   295
          val graph1 = (x :: deps).foldLeft(graph)(_.default_node(_, ()))
d8a0e996614b tuned --- fewer warnings;
wenzelm
parents: 73340
diff changeset
   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
ff6d4b48f23b tuned comments;
wenzelm
parents: 75470
diff changeset
   303
ff6d4b48f23b tuned comments;
wenzelm
parents: 75470
diff changeset
   304
  /** check files **/
65832
2fb85623c386 implicitly check for unknown files (not part of a Mercurial repository);
wenzelm
parents: 65825
diff changeset
   305
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73702
diff changeset
   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
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73702
diff changeset
   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
95fd3b9888e6 tuned signature;
wenzelm
parents: 65832
diff changeset
   318
                  (hg.root + Path.explode(name)).canonical_file).toSet
95fd3b9888e6 tuned signature;
wenzelm
parents: 65832
diff changeset
   319
              if (!known(path.canonical_file)) unknown += path
95fd3b9888e6 tuned signature;
wenzelm
parents: 65832
diff changeset
   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
ff6d4b48f23b tuned comments;
wenzelm
parents: 75470
diff changeset
   331
ff6d4b48f23b tuned comments;
wenzelm
parents: 75470
diff changeset
   332
  /** hg_setup **/
71312
937328d61436 added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents: 68566
diff changeset
   333
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73702
diff changeset
   334
  private def edit_hgrc(local_hg: Repository, path_name: String, source: String): Unit = {
71313
c7bf771cdfb5 more ambitious edit_hgrc;
wenzelm
parents: 71312
diff changeset
   335
    val hgrc = local_hg.root + Path.explode(".hg/hgrc")
c7bf771cdfb5 more ambitious edit_hgrc;
wenzelm
parents: 71312
diff changeset
   336
    def header(line: String): Boolean = line.startsWith("[paths]")
c7bf771cdfb5 more ambitious edit_hgrc;
wenzelm
parents: 71312
diff changeset
   337
    val Entry = """^(\S+)\s*=\s*(.*)$""".r
c7bf771cdfb5 more ambitious edit_hgrc;
wenzelm
parents: 71312
diff changeset
   338
    val new_entry = path_name + " = " + source
c7bf771cdfb5 more ambitious edit_hgrc;
wenzelm
parents: 71312
diff changeset
   339
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73702
diff changeset
   340
    def commit(lines: List[String]): Boolean = {
71320
1e2e68984a9f clarified backup;
wenzelm
parents: 71319
diff changeset
   341
      File.write(hgrc, cat_lines(lines))
71313
c7bf771cdfb5 more ambitious edit_hgrc;
wenzelm
parents: 71312
diff changeset
   342
      true
c7bf771cdfb5 more ambitious edit_hgrc;
wenzelm
parents: 71312
diff changeset
   343
    }
c7bf771cdfb5 more ambitious edit_hgrc;
wenzelm
parents: 71312
diff changeset
   344
    val edited =
c7bf771cdfb5 more ambitious edit_hgrc;
wenzelm
parents: 71312
diff changeset
   345
      hgrc.is_file && {
c7bf771cdfb5 more ambitious edit_hgrc;
wenzelm
parents: 71312
diff changeset
   346
        val lines = split_lines(File.read(hgrc))
71383
8313dca6dee9 misc tuning, following hint by IntelliJ;
wenzelm
parents: 71322
diff changeset
   347
        lines.count(header) == 1 && {
71313
c7bf771cdfb5 more ambitious edit_hgrc;
wenzelm
parents: 71312
diff changeset
   348
          if (local_hg.paths(options = "-q").contains(path_name)) {
c7bf771cdfb5 more ambitious edit_hgrc;
wenzelm
parents: 71312
diff changeset
   349
            val old_source = local_hg.paths(args = path_name).head
71320
1e2e68984a9f clarified backup;
wenzelm
parents: 71319
diff changeset
   350
            val old_entry = path_name + ".old = " + old_source
71313
c7bf771cdfb5 more ambitious edit_hgrc;
wenzelm
parents: 71312
diff changeset
   351
            val lines1 =
c7bf771cdfb5 more ambitious edit_hgrc;
wenzelm
parents: 71312
diff changeset
   352
              lines.map {
71320
1e2e68984a9f clarified backup;
wenzelm
parents: 71319
diff changeset
   353
                case Entry(a, b) if a == path_name && b == old_source =>
1e2e68984a9f clarified backup;
wenzelm
parents: 71319
diff changeset
   354
                  new_entry + "\n" + old_entry
71313
c7bf771cdfb5 more ambitious edit_hgrc;
wenzelm
parents: 71312
diff changeset
   355
                case line => line
c7bf771cdfb5 more ambitious edit_hgrc;
wenzelm
parents: 71312
diff changeset
   356
              }
c7bf771cdfb5 more ambitious edit_hgrc;
wenzelm
parents: 71312
diff changeset
   357
            lines != lines1 && commit(lines1)
c7bf771cdfb5 more ambitious edit_hgrc;
wenzelm
parents: 71312
diff changeset
   358
          }
c7bf771cdfb5 more ambitious edit_hgrc;
wenzelm
parents: 71312
diff changeset
   359
          else {
c7bf771cdfb5 more ambitious edit_hgrc;
wenzelm
parents: 71312
diff changeset
   360
            val prefix = lines.takeWhile(line => !header(line))
c7bf771cdfb5 more ambitious edit_hgrc;
wenzelm
parents: 71312
diff changeset
   361
            val n = prefix.length
c7bf771cdfb5 more ambitious edit_hgrc;
wenzelm
parents: 71312
diff changeset
   362
            commit(prefix ::: List(lines(n), new_entry) ::: lines.drop(n + 1))
c7bf771cdfb5 more ambitious edit_hgrc;
wenzelm
parents: 71312
diff changeset
   363
          }
c7bf771cdfb5 more ambitious edit_hgrc;
wenzelm
parents: 71312
diff changeset
   364
        }
c7bf771cdfb5 more ambitious edit_hgrc;
wenzelm
parents: 71312
diff changeset
   365
      }
c7bf771cdfb5 more ambitious edit_hgrc;
wenzelm
parents: 71312
diff changeset
   366
    if (!edited) File.append(hgrc, "\n[paths]\n" + new_entry + "\n")
c7bf771cdfb5 more ambitious edit_hgrc;
wenzelm
parents: 71312
diff changeset
   367
  }
c7bf771cdfb5 more ambitious edit_hgrc;
wenzelm
parents: 71312
diff changeset
   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
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73702
diff changeset
   377
    progress: Progress = new Progress
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73702
diff changeset
   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
e48d93811ed7 clarified signature;
wenzelm
parents: 71726
diff changeset
   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
5b68cc73f8b1 clarified signature;
wenzelm
parents: 71313
diff changeset
   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
c7bf771cdfb5 more ambitious edit_hgrc;
wenzelm
parents: 71312
diff changeset
   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
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   444
  val isabelle_tool1 =
72763
3cc73d00553c added document antiquotation @{tool};
wenzelm
parents: 72375
diff changeset
   445
    Isabelle_Tool("hg_setup", "setup remote vs. local Mercurial repository",
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   446
      Scala_Project.here,
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   447
      { args =>
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   448
        var remote_name = ""
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   449
        var path_name = default_path_name
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   450
        var remote_exists = false
71312
937328d61436 added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents: 68566
diff changeset
   451
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   452
        val getopts = Getopts("""
71322
0256ce61f405 more documentation;
wenzelm
parents: 71321
diff changeset
   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
0256ce61f405 more documentation;
wenzelm
parents: 71321
diff changeset
   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
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   463
          "n:" -> (arg => remote_name = arg),
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   464
          "p:" -> (arg => path_name = arg),
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   465
          "r" -> (_ => remote_exists = true))
71312
937328d61436 added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents: 68566
diff changeset
   466
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   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
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   469
        val (remote, local_path) =
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   470
          more_args match {
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   471
            case List(arg1, arg2) => (arg1, Path.explode(arg2))
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   472
            case _ => getopts.usage()
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   473
          }
71312
937328d61436 added command hg_setup: setup remote vs. local Mercurial repository;
wenzelm
parents: 68566
diff changeset
   474
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   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
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   477
        hg_setup(remote, local_path, remote_name = remote_name, path_name = path_name,
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   478
          remote_exists = remote_exists, progress = progress)
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
   479
      })
75474
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   480
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   481
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   482
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   483
  /** hg_sync **/
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   484
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   485
  val isabelle_tool2 =
75479
4363ad65ad36 support option -r;
wenzelm
parents: 75477
diff changeset
   486
    Isabelle_Tool("hg_sync", "synchronize Mercurial repository with target directory",
75474
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   487
      Scala_Project.here, { args =>
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   488
        var clean = false
75489
f08fd5048df3 clarified command-line options;
wenzelm
parents: 75487
diff changeset
   489
        var filter: List[String] = Nil
75474
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   490
        var root: Option[Path] = None
75487
167660a8f99e support thorough check of file content;
wenzelm
parents: 75485
diff changeset
   491
        var thorough = false
75474
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   492
        var dry_run = false
75479
4363ad65ad36 support option -r;
wenzelm
parents: 75477
diff changeset
   493
        var rev = ""
75474
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   494
        var verbose = false
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   495
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   496
        val getopts = Getopts("""
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   497
Usage: isabelle hg_sync [OPTIONS] TARGET
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   498
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   499
  Options are:
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   500
    -C           clean all unknown/ignored files on target
75476
1148c190eb9b more documentation;
wenzelm
parents: 75475
diff changeset
   501
                 (implies -n for testing; use option -f to confirm)
75489
f08fd5048df3 clarified command-line options;
wenzelm
parents: 75487
diff changeset
   502
    -F RULE      add rsync filter RULE (e.g. "protect /foo" to avoid deletion)
75474
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   503
    -R ROOT      explicit repository root directory
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   504
                 (default: implicit from current directory)
75487
167660a8f99e support thorough check of file content;
wenzelm
parents: 75485
diff changeset
   505
    -T           thorough check of file content (default: time and size)
75474
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   506
    -f           force changes: no dry-run
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   507
    -n           no changes: dry-run
75479
4363ad65ad36 support option -r;
wenzelm
parents: 75477
diff changeset
   508
    -r REV       explicit revision (default: state of working directory)
75474
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   509
    -v           verbose
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   510
75479
4363ad65ad36 support option -r;
wenzelm
parents: 75477
diff changeset
   511
  Synchronize Mercurial repository with TARGET directory,
75474
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   512
  which can be local or remote (using notation of rsync).
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   513
""",
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   514
          "C" -> { _ => clean = true; dry_run = true },
75489
f08fd5048df3 clarified command-line options;
wenzelm
parents: 75487
diff changeset
   515
          "F:" -> (arg => filter = filter ::: List(arg)),
75474
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   516
          "R:" -> (arg => root = Some(Path.explode(arg))),
75487
167660a8f99e support thorough check of file content;
wenzelm
parents: 75485
diff changeset
   517
          "T" -> (_ => thorough = true),
75474
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   518
          "f" -> (_ => dry_run = false),
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   519
          "n" -> (_ => dry_run = true),
75479
4363ad65ad36 support option -r;
wenzelm
parents: 75477
diff changeset
   520
          "r:" -> (arg => rev = arg),
75474
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   521
          "v" -> (_ => verbose = true))
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   522
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   523
        val more_args = getopts(args)
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   524
        val target =
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   525
          more_args match {
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   526
            case List(target) => target
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   527
            case _ => getopts.usage()
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   528
          }
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   529
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   530
        val progress = new Console_Progress
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   531
        val hg =
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   532
          root match {
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   533
            case Some(dir) => repository(dir)
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   534
            case None => the_repository(Path.current)
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   535
          }
75487
167660a8f99e support thorough check of file content;
wenzelm
parents: 75485
diff changeset
   536
        hg.sync(target, progress = progress, verbose = verbose, thorough = thorough,
75489
f08fd5048df3 clarified command-line options;
wenzelm
parents: 75487
diff changeset
   537
          dry_run = dry_run, clean = clean, filter = filter, rev = rev)
75474
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   538
      }
d16dd2d1b50a support for "isabelle hg_sync";
wenzelm
parents: 75472
diff changeset
   539
    )
63997
e11ccb5aa82f more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff changeset
   540
}