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