src/Pure/General/mercurial.scala
author wenzelm
Sun, 16 Oct 2016 17:10:24 +0200
changeset 64255 a9f540881611
parent 64252 e84cba30d7ff
child 64256 c3197aeae90b
permissions -rw-r--r--
more robust;
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
e11ccb5aa82f more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff changeset
     4
Support for Mercurial repositories.
e11ccb5aa82f more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff changeset
     5
*/
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
package isabelle
e11ccb5aa82f more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff changeset
     8
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
import java.io.{File => JFile}
e11ccb5aa82f more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff changeset
    11
e11ccb5aa82f more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff changeset
    12
e11ccb5aa82f more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff changeset
    13
object Mercurial
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
  /* command-line syntax */
e11ccb5aa82f more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff changeset
    16
63999
5649a993666d more operations;
wenzelm
parents: 63998
diff changeset
    17
  def optional(s: String, prefix: String = ""): String =
5649a993666d more operations;
wenzelm
parents: 63998
diff changeset
    18
    if (s == "") "" else " " + prefix + " " + File.bash_string(s)
5649a993666d more operations;
wenzelm
parents: 63998
diff changeset
    19
5649a993666d more operations;
wenzelm
parents: 63998
diff changeset
    20
  def opt_flag(flag: String, b: Boolean): String = if (b) " " + flag else ""
5649a993666d more operations;
wenzelm
parents: 63998
diff changeset
    21
  def opt_rev(s: String): String = optional(s, "--rev")
5649a993666d more operations;
wenzelm
parents: 63998
diff changeset
    22
  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
    23
e11ccb5aa82f more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff changeset
    24
e11ccb5aa82f more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff changeset
    25
  /* repository access */
e11ccb5aa82f more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff changeset
    26
64167
097d122222f6 support remote repositories via ssh command execution;
wenzelm
parents: 64162
diff changeset
    27
  def repository(root: Path, ssh: Option[SSH.Session] = None): Repository =
64168
e573b985390c added clone_repository;
wenzelm
parents: 64167
diff changeset
    28
  {
e573b985390c added clone_repository;
wenzelm
parents: 64167
diff changeset
    29
    val hg = new Repository(root, ssh)
e573b985390c added clone_repository;
wenzelm
parents: 64167
diff changeset
    30
    hg.command("root").check
e573b985390c added clone_repository;
wenzelm
parents: 64167
diff changeset
    31
    hg
e573b985390c added clone_repository;
wenzelm
parents: 64167
diff changeset
    32
  }
e573b985390c added clone_repository;
wenzelm
parents: 64167
diff changeset
    33
e573b985390c added clone_repository;
wenzelm
parents: 64167
diff changeset
    34
  def clone_repository(
64230
13a97c1d7d22 added setup_repository;
wenzelm
parents: 64198
diff changeset
    35
    source: String, root: Path, options: String = "", ssh: Option[SSH.Session] = None): Repository =
64168
e573b985390c added clone_repository;
wenzelm
parents: 64167
diff changeset
    36
  {
64230
13a97c1d7d22 added setup_repository;
wenzelm
parents: 64198
diff changeset
    37
    val hg = new Repository(root, ssh)
64255
a9f540881611 more robust;
wenzelm
parents: 64252
diff changeset
    38
    ssh match {
a9f540881611 more robust;
wenzelm
parents: 64252
diff changeset
    39
      case None => Isabelle_System.mkdirs(hg.root.dir)
a9f540881611 more robust;
wenzelm
parents: 64252
diff changeset
    40
      case Some(session) => using(session.sftp())(_.mkdirs(hg.root.dir))
a9f540881611 more robust;
wenzelm
parents: 64252
diff changeset
    41
    }
64230
13a97c1d7d22 added setup_repository;
wenzelm
parents: 64198
diff changeset
    42
    hg.command("clone", File.bash_string(source) + " " + File.bash_path(hg.root), options).check
64168
e573b985390c added clone_repository;
wenzelm
parents: 64167
diff changeset
    43
    hg
e573b985390c added clone_repository;
wenzelm
parents: 64167
diff changeset
    44
  }
63997
e11ccb5aa82f more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff changeset
    45
64230
13a97c1d7d22 added setup_repository;
wenzelm
parents: 64198
diff changeset
    46
  def setup_repository(source: String, root: Path, ssh: Option[SSH.Session] = None): Repository =
64252
e84cba30d7ff clarified setup_repository: more uniform pull vs. clone, without update;
wenzelm
parents: 64233
diff changeset
    47
  {
e84cba30d7ff clarified setup_repository: more uniform pull vs. clone, without update;
wenzelm
parents: 64233
diff changeset
    48
    val present =
e84cba30d7ff clarified setup_repository: more uniform pull vs. clone, without update;
wenzelm
parents: 64233
diff changeset
    49
      ssh match {
e84cba30d7ff clarified setup_repository: more uniform pull vs. clone, without update;
wenzelm
parents: 64233
diff changeset
    50
        case None => root.is_dir
e84cba30d7ff clarified setup_repository: more uniform pull vs. clone, without update;
wenzelm
parents: 64233
diff changeset
    51
        case Some(session) => using(session.sftp())(_.is_dir(root))
e84cba30d7ff clarified setup_repository: more uniform pull vs. clone, without update;
wenzelm
parents: 64233
diff changeset
    52
      }
e84cba30d7ff clarified setup_repository: more uniform pull vs. clone, without update;
wenzelm
parents: 64233
diff changeset
    53
    if (present) { val hg = repository(root, ssh = ssh); hg.pull(); hg }
e84cba30d7ff clarified setup_repository: more uniform pull vs. clone, without update;
wenzelm
parents: 64233
diff changeset
    54
    else clone_repository(source, root, options = "--noupdate", ssh = ssh)
e84cba30d7ff clarified setup_repository: more uniform pull vs. clone, without update;
wenzelm
parents: 64233
diff changeset
    55
  }
64230
13a97c1d7d22 added setup_repository;
wenzelm
parents: 64198
diff changeset
    56
13a97c1d7d22 added setup_repository;
wenzelm
parents: 64198
diff changeset
    57
  class Repository private[Mercurial](root_path: Path, ssh: Option[SSH.Session])
63997
e11ccb5aa82f more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff changeset
    58
  {
64167
097d122222f6 support remote repositories via ssh command execution;
wenzelm
parents: 64162
diff changeset
    59
    hg =>
097d122222f6 support remote repositories via ssh command execution;
wenzelm
parents: 64162
diff changeset
    60
64230
13a97c1d7d22 added setup_repository;
wenzelm
parents: 64198
diff changeset
    61
    val root =
13a97c1d7d22 added setup_repository;
wenzelm
parents: 64198
diff changeset
    62
      ssh match {
13a97c1d7d22 added setup_repository;
wenzelm
parents: 64198
diff changeset
    63
        case None => root_path.expand
13a97c1d7d22 added setup_repository;
wenzelm
parents: 64198
diff changeset
    64
        case Some(session) => using(session.sftp())(sftp => root_path.expand_env(sftp.settings))
13a97c1d7d22 added setup_repository;
wenzelm
parents: 64198
diff changeset
    65
      }
13a97c1d7d22 added setup_repository;
wenzelm
parents: 64198
diff changeset
    66
64167
097d122222f6 support remote repositories via ssh command execution;
wenzelm
parents: 64162
diff changeset
    67
    override def toString: String =
097d122222f6 support remote repositories via ssh command execution;
wenzelm
parents: 64162
diff changeset
    68
      ssh match {
097d122222f6 support remote repositories via ssh command execution;
wenzelm
parents: 64162
diff changeset
    69
        case None => root.implode
64169
wenzelm
parents: 64168
diff changeset
    70
        case Some(session) => session.toString + ":" + root.implode
64167
097d122222f6 support remote repositories via ssh command execution;
wenzelm
parents: 64162
diff changeset
    71
      }
63997
e11ccb5aa82f more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff changeset
    72
64168
e573b985390c added clone_repository;
wenzelm
parents: 64167
diff changeset
    73
    def command(name: String, args: String = "", options: String = ""): Process_Result =
64167
097d122222f6 support remote repositories via ssh command execution;
wenzelm
parents: 64162
diff changeset
    74
    {
097d122222f6 support remote repositories via ssh command execution;
wenzelm
parents: 64162
diff changeset
    75
      val cmdline =
64168
e573b985390c added clone_repository;
wenzelm
parents: 64167
diff changeset
    76
        "\"${HG:-hg}\"" +
64230
13a97c1d7d22 added setup_repository;
wenzelm
parents: 64198
diff changeset
    77
          (if (name == "clone") "" else " --repository " + File.bash_path(root)) +
64168
e573b985390c added clone_repository;
wenzelm
parents: 64167
diff changeset
    78
          " --noninteractive " + name + " " + options + " " + args
64167
097d122222f6 support remote repositories via ssh command execution;
wenzelm
parents: 64162
diff changeset
    79
      ssh match {
097d122222f6 support remote repositories via ssh command execution;
wenzelm
parents: 64162
diff changeset
    80
        case None => Isabelle_System.bash(cmdline)
097d122222f6 support remote repositories via ssh command execution;
wenzelm
parents: 64162
diff changeset
    81
        case Some(session) => session.execute(cmdline)
097d122222f6 support remote repositories via ssh command execution;
wenzelm
parents: 64162
diff changeset
    82
      }
097d122222f6 support remote repositories via ssh command execution;
wenzelm
parents: 64162
diff changeset
    83
    }
63999
5649a993666d more operations;
wenzelm
parents: 63998
diff changeset
    84
5649a993666d more operations;
wenzelm
parents: 63998
diff changeset
    85
    def heads(template: String = "{node|short}\n", options: String = ""): List[String] =
64168
e573b985390c added clone_repository;
wenzelm
parents: 64167
diff changeset
    86
      hg.command("heads", opt_template(template), options).check.out_lines
63999
5649a993666d more operations;
wenzelm
parents: 63998
diff changeset
    87
64232
367d83d6030e clarified hg.id operation, with explicit tip as default;
wenzelm
parents: 64230
diff changeset
    88
    def identify(rev: String = "tip", options: String = ""): String =
64168
e573b985390c added clone_repository;
wenzelm
parents: 64167
diff changeset
    89
      hg.command("id", opt_rev(rev), options).check.out_lines.headOption getOrElse ""
63998
2f38d8aff2f5 more operations;
wenzelm
parents: 63997
diff changeset
    90
64232
367d83d6030e clarified hg.id operation, with explicit tip as default;
wenzelm
parents: 64230
diff changeset
    91
    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
    92
63998
2f38d8aff2f5 more operations;
wenzelm
parents: 63997
diff changeset
    93
    def manifest(rev: String = "", options: String = ""): List[String] =
64168
e573b985390c added clone_repository;
wenzelm
parents: 64167
diff changeset
    94
      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
    95
64033
2989c1f2593a tuned signature;
wenzelm
parents: 64028
diff changeset
    96
    def log(rev: String = "", template: String = "", options: String = ""): String =
64168
e573b985390c added clone_repository;
wenzelm
parents: 64167
diff changeset
    97
      hg.command("log", opt_rev(rev) + opt_template(template), options).check.out
64027
4a33d740c9dc proper log output;
wenzelm
parents: 64020
diff changeset
    98
63999
5649a993666d more operations;
wenzelm
parents: 63998
diff changeset
    99
    def pull(remote: String = "", rev: String = "", options: String = ""): Unit =
64168
e573b985390c added clone_repository;
wenzelm
parents: 64167
diff changeset
   100
      hg.command("pull", opt_rev(rev) + optional(remote), options).check
63999
5649a993666d more operations;
wenzelm
parents: 63998
diff changeset
   101
64168
e573b985390c added clone_repository;
wenzelm
parents: 64167
diff changeset
   102
    def update(
e573b985390c added clone_repository;
wenzelm
parents: 64167
diff changeset
   103
      rev: String = "", clean: Boolean = false, check: Boolean = false, options: String = "")
63999
5649a993666d more operations;
wenzelm
parents: 63998
diff changeset
   104
    {
64168
e573b985390c added clone_repository;
wenzelm
parents: 64167
diff changeset
   105
      hg.command("update",
e573b985390c added clone_repository;
wenzelm
parents: 64167
diff changeset
   106
        opt_rev(rev) + opt_flag("--clean", clean) + opt_flag("--check", check), options).check
63999
5649a993666d more operations;
wenzelm
parents: 63998
diff changeset
   107
    }
63997
e11ccb5aa82f more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff changeset
   108
  }
e11ccb5aa82f more formal Mercurial support (with the potential to upgrade to command server);
wenzelm
parents:
diff changeset
   109
}