annotate repoint.sml @ 125:34e428693f5d vext

Vext -> Repoint
author Chris Cannam
date Thu, 14 Jun 2018 11:15:39 +0100
parents vext.sml@fc093b176444
children
rev   line source
Chris@122 1 (*
Chris@122 2 DO NOT EDIT THIS FILE.
Chris@122 3 This file is automatically generated from the individual
Chris@125 4 source files in the Repoint repository.
Chris@122 5 *)
Chris@122 6
Chris@122 7 (*
Chris@125 8 Repoint
Chris@122 9
Chris@122 10 A simple manager for third-party source code dependencies
Chris@122 11
Chris@125 12 Copyright 2018 Chris Cannam, Particular Programs Ltd,
Chris@122 13 and Queen Mary, University of London
Chris@122 14
Chris@122 15 Permission is hereby granted, free of charge, to any person
Chris@122 16 obtaining a copy of this software and associated documentation
Chris@122 17 files (the "Software"), to deal in the Software without
Chris@122 18 restriction, including without limitation the rights to use, copy,
Chris@122 19 modify, merge, publish, distribute, sublicense, and/or sell copies
Chris@122 20 of the Software, and to permit persons to whom the Software is
Chris@122 21 furnished to do so, subject to the following conditions:
Chris@122 22
Chris@122 23 The above copyright notice and this permission notice shall be
Chris@122 24 included in all copies or substantial portions of the Software.
Chris@122 25
Chris@122 26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Chris@122 27 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Chris@122 28 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Chris@122 29 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
Chris@122 30 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
Chris@122 31 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
Chris@122 32 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Chris@122 33
Chris@122 34 Except as contained in this notice, the names of Chris Cannam,
Chris@122 35 Particular Programs Ltd, and Queen Mary, University of London
Chris@122 36 shall not be used in advertising or otherwise to promote the sale,
Chris@122 37 use or other dealings in this Software without prior written
Chris@122 38 authorization.
Chris@122 39 *)
Chris@122 40
Chris@125 41 val repoint_version = "1.0"
Chris@122 42
Chris@122 43
Chris@122 44 datatype vcs =
Chris@122 45 HG |
Chris@125 46 GIT |
Chris@125 47 SVN
Chris@122 48
Chris@122 49 datatype source =
Chris@122 50 URL_SOURCE of string |
Chris@122 51 SERVICE_SOURCE of {
Chris@122 52 service : string,
Chris@122 53 owner : string option,
Chris@122 54 repo : string option
Chris@122 55 }
Chris@122 56
Chris@122 57 type id_or_tag = string
Chris@122 58
Chris@122 59 datatype pin =
Chris@122 60 UNPINNED |
Chris@122 61 PINNED of id_or_tag
Chris@122 62
Chris@122 63 datatype libstate =
Chris@122 64 ABSENT |
Chris@122 65 CORRECT |
Chris@122 66 SUPERSEDED |
Chris@122 67 WRONG
Chris@122 68
Chris@122 69 datatype localstate =
Chris@122 70 MODIFIED |
Chris@122 71 LOCK_MISMATCHED |
Chris@122 72 CLEAN
Chris@122 73
Chris@122 74 datatype branch =
Chris@122 75 BRANCH of string |
Chris@122 76 DEFAULT_BRANCH
Chris@122 77
Chris@122 78 (* If we can recover from an error, for example by reporting failure
Chris@122 79 for this one thing and going on to the next thing, then the error
Chris@122 80 should usually be returned through a result type rather than an
Chris@122 81 exception. *)
Chris@122 82
Chris@122 83 datatype 'a result =
Chris@122 84 OK of 'a |
Chris@122 85 ERROR of string
Chris@122 86
Chris@122 87 type libname = string
Chris@122 88
Chris@122 89 type libspec = {
Chris@122 90 libname : libname,
Chris@122 91 vcs : vcs,
Chris@122 92 source : source,
Chris@122 93 branch : branch,
Chris@122 94 project_pin : pin,
Chris@122 95 lock_pin : pin
Chris@122 96 }
Chris@122 97
Chris@122 98 type lock = {
Chris@122 99 libname : libname,
Chris@122 100 id_or_tag : id_or_tag
Chris@122 101 }
Chris@122 102
Chris@122 103 type remote_spec = {
Chris@122 104 anon : string option,
Chris@122 105 auth : string option
Chris@122 106 }
Chris@122 107
Chris@122 108 type provider = {
Chris@122 109 service : string,
Chris@122 110 supports : vcs list,
Chris@122 111 remote_spec : remote_spec
Chris@122 112 }
Chris@122 113
Chris@122 114 type account = {
Chris@122 115 service : string,
Chris@122 116 login : string
Chris@122 117 }
Chris@122 118
Chris@122 119 type context = {
Chris@122 120 rootpath : string,
Chris@122 121 extdir : string,
Chris@122 122 providers : provider list,
Chris@122 123 accounts : account list
Chris@122 124 }
Chris@122 125
Chris@122 126 type userconfig = {
Chris@122 127 providers : provider list,
Chris@122 128 accounts : account list
Chris@122 129 }
Chris@122 130
Chris@122 131 type project = {
Chris@122 132 context : context,
Chris@122 133 libs : libspec list
Chris@122 134 }
Chris@122 135
Chris@125 136 structure RepointFilenames = struct
Chris@125 137 val project_file = "repoint-project.json"
Chris@125 138 val project_lock_file = "repoint-lock.json"
Chris@125 139 val user_config_file = ".repoint.json"
Chris@125 140 val archive_dir = ".repoint-archive"
Chris@122 141 end
Chris@122 142
Chris@122 143 signature VCS_CONTROL = sig
Chris@122 144
Chris@125 145 (** Check whether the given VCS is installed and working *)
Chris@125 146 val is_working : context -> bool result
Chris@125 147
Chris@122 148 (** Test whether the library is present locally at all *)
Chris@122 149 val exists : context -> libname -> bool result
Chris@122 150
Chris@122 151 (** Return the id (hash) of the current revision for the library *)
Chris@122 152 val id_of : context -> libname -> id_or_tag result
Chris@122 153
Chris@122 154 (** Test whether the library is at the given id *)
Chris@122 155 val is_at : context -> libname * id_or_tag -> bool result
Chris@122 156
Chris@122 157 (** Test whether the library is on the given branch, i.e. is at
Chris@122 158 the branch tip or an ancestor of it *)
Chris@122 159 val is_on_branch : context -> libname * branch -> bool result
Chris@122 160
Chris@122 161 (** Test whether the library is at the newest revision for the
Chris@122 162 given branch. False may indicate that the branch has advanced
Chris@122 163 or that the library is not on the branch at all. This function
Chris@122 164 may use the network to check for new revisions *)
Chris@125 165 val is_newest : context -> libname * source * branch -> bool result
Chris@122 166
Chris@122 167 (** Test whether the library is at the newest revision available
Chris@122 168 locally for the given branch. False may indicate that the
Chris@122 169 branch has advanced or that the library is not on the branch
Chris@122 170 at all. This function must not use the network *)
Chris@122 171 val is_newest_locally : context -> libname * branch -> bool result
Chris@122 172
Chris@122 173 (** Test whether the library has been modified in the local
Chris@122 174 working copy *)
Chris@122 175 val is_modified_locally : context -> libname -> bool result
Chris@122 176
Chris@122 177 (** Check out, i.e. clone a fresh copy of, the repo for the given
Chris@122 178 library on the given branch *)
Chris@122 179 val checkout : context -> libname * source * branch -> unit result
Chris@122 180
Chris@125 181 (** Update the library to the given branch tip. Assumes that a
Chris@125 182 local copy of the library already exists *)
Chris@125 183 val update : context -> libname * source * branch -> unit result
Chris@122 184
Chris@122 185 (** Update the library to the given specific id or tag *)
Chris@125 186 val update_to : context -> libname * source * id_or_tag -> unit result
Chris@125 187
Chris@125 188 (** Return a URL from which the library can be cloned, given that
Chris@125 189 the local copy already exists. For a DVCS this can be the
Chris@125 190 local copy, but for a centralised VCS it will have to be the
Chris@125 191 remote repository URL. Used for archiving *)
Chris@125 192 val copy_url_for : context -> libname -> string result
Chris@122 193 end
Chris@122 194
Chris@122 195 signature LIB_CONTROL = sig
Chris@122 196 val review : context -> libspec -> (libstate * localstate) result
Chris@122 197 val status : context -> libspec -> (libstate * localstate) result
Chris@125 198 val update : context -> libspec -> unit result
Chris@122 199 val id_of : context -> libspec -> id_or_tag result
Chris@125 200 val is_working : context -> vcs -> bool result
Chris@122 201 end
Chris@122 202
Chris@122 203 structure FileBits :> sig
Chris@122 204 val extpath : context -> string
Chris@122 205 val libpath : context -> libname -> string
Chris@122 206 val subpath : context -> libname -> string -> string
Chris@122 207 val command_output : context -> libname -> string list -> string result
Chris@122 208 val command : context -> libname -> string list -> unit result
Chris@125 209 val file_url : string -> string
Chris@122 210 val file_contents : string -> string
Chris@122 211 val mydir : unit -> string
Chris@122 212 val homedir : unit -> string
Chris@122 213 val mkpath : string -> unit result
Chris@124 214 val rmpath : string -> unit result
Chris@125 215 val nonempty_dir_exists : string -> bool
Chris@122 216 val project_spec_path : string -> string
Chris@122 217 val project_lock_path : string -> string
Chris@122 218 val verbose : unit -> bool
Chris@122 219 end = struct
Chris@122 220
Chris@122 221 fun verbose () =
Chris@125 222 case OS.Process.getEnv "REPOINT_VERBOSE" of
Chris@122 223 SOME "0" => false
Chris@122 224 | SOME _ => true
Chris@122 225 | NONE => false
Chris@122 226
Chris@125 227 fun split_relative path desc =
Chris@125 228 case OS.Path.fromString path of
Chris@125 229 { isAbs = true, ... } => raise Fail (desc ^ " may not be absolute")
Chris@125 230 | { arcs, ... } => arcs
Chris@125 231
Chris@122 232 fun extpath ({ rootpath, extdir, ... } : context) =
Chris@122 233 let val { isAbs, vol, arcs } = OS.Path.fromString rootpath
Chris@122 234 in OS.Path.toString {
Chris@122 235 isAbs = isAbs,
Chris@122 236 vol = vol,
Chris@125 237 arcs = arcs @
Chris@125 238 split_relative extdir "extdir"
Chris@122 239 }
Chris@122 240 end
Chris@122 241
Chris@122 242 fun subpath ({ rootpath, extdir, ... } : context) libname remainder =
Chris@122 243 (* NB libname is allowed to be a path fragment, e.g. foo/bar *)
Chris@122 244 let val { isAbs, vol, arcs } = OS.Path.fromString rootpath
Chris@122 245 in OS.Path.toString {
Chris@122 246 isAbs = isAbs,
Chris@122 247 vol = vol,
Chris@125 248 arcs = arcs @
Chris@125 249 split_relative extdir "extdir" @
Chris@125 250 split_relative libname "library path" @
Chris@125 251 split_relative remainder "subpath"
Chris@122 252 }
Chris@122 253 end
Chris@122 254
Chris@122 255 fun libpath context "" =
Chris@122 256 extpath context
Chris@122 257 | libpath context libname =
Chris@122 258 subpath context libname ""
Chris@122 259
Chris@122 260 fun project_file_path rootpath filename =
Chris@122 261 let val { isAbs, vol, arcs } = OS.Path.fromString rootpath
Chris@122 262 in OS.Path.toString {
Chris@122 263 isAbs = isAbs,
Chris@122 264 vol = vol,
Chris@122 265 arcs = arcs @ [ filename ]
Chris@122 266 }
Chris@122 267 end
Chris@122 268
Chris@122 269 fun project_spec_path rootpath =
Chris@125 270 project_file_path rootpath (RepointFilenames.project_file)
Chris@122 271
Chris@122 272 fun project_lock_path rootpath =
Chris@125 273 project_file_path rootpath (RepointFilenames.project_lock_file)
Chris@122 274
Chris@122 275 fun trim str =
Chris@122 276 hd (String.fields (fn x => x = #"\n" orelse x = #"\r") str)
Chris@125 277
Chris@125 278 fun file_url path =
Chris@125 279 let val forward_path =
Chris@125 280 String.translate (fn #"\\" => "/" |
Chris@125 281 c => Char.toString c)
Chris@125 282 (OS.Path.mkCanonical path)
Chris@125 283 in
Chris@125 284 (* Path is expected to be absolute already, but if it
Chris@125 285 starts with a drive letter, we'll need an extra slash *)
Chris@125 286 case explode forward_path of
Chris@125 287 #"/"::rest => "file:///" ^ implode rest
Chris@125 288 | _ => "file:///" ^ forward_path
Chris@125 289 end
Chris@122 290
Chris@122 291 fun file_contents filename =
Chris@122 292 let val stream = TextIO.openIn filename
Chris@122 293 fun read_all str acc =
Chris@122 294 case TextIO.inputLine str of
Chris@122 295 SOME line => read_all str (trim line :: acc)
Chris@122 296 | NONE => rev acc
Chris@122 297 val contents = read_all stream []
Chris@122 298 val _ = TextIO.closeIn stream
Chris@122 299 in
Chris@122 300 String.concatWith "\n" contents
Chris@122 301 end
Chris@122 302
Chris@122 303 fun expand_commandline cmdlist =
Chris@125 304 (* We are quite strict about what we accept here, except
Chris@122 305 for the first element in cmdlist which is assumed to be a
Chris@125 306 known command location rather than arbitrary user input. *)
Chris@122 307 let open Char
Chris@122 308 fun quote arg =
Chris@122 309 if List.all
Chris@122 310 (fn c => isAlphaNum c orelse c = #"-" orelse c = #"_")
Chris@122 311 (explode arg)
Chris@122 312 then arg
Chris@122 313 else "\"" ^ arg ^ "\""
Chris@122 314 fun check arg =
Chris@125 315 let val valid = explode " /#:;?,._-{}@=+"
Chris@122 316 in
Chris@122 317 app (fn c =>
Chris@122 318 if isAlphaNum c orelse
Chris@125 319 List.exists (fn v => v = c) valid orelse
Chris@125 320 c > chr 127
Chris@122 321 then ()
Chris@122 322 else raise Fail ("Invalid character '" ^
Chris@122 323 (Char.toString c) ^
Chris@122 324 "' in command list"))
Chris@122 325 (explode arg);
Chris@122 326 arg
Chris@122 327 end
Chris@122 328 in
Chris@122 329 String.concatWith " "
Chris@122 330 (map quote
Chris@122 331 (hd cmdlist :: map check (tl cmdlist)))
Chris@122 332 end
Chris@122 333
Chris@122 334 val tick_cycle = ref 0
Chris@122 335 val tick_chars = Vector.fromList (map String.str (explode "|/-\\"))
Chris@122 336
Chris@122 337 fun tick libname cmdlist =
Chris@122 338 let val n = Vector.length tick_chars
Chris@122 339 fun pad_to n str =
Chris@122 340 if n <= String.size str then str
Chris@122 341 else pad_to n (str ^ " ")
Chris@122 342 val name = if libname <> "" then libname
Chris@122 343 else if cmdlist = nil then ""
Chris@122 344 else hd (rev cmdlist)
Chris@122 345 in
Chris@122 346 print (" " ^
Chris@122 347 Vector.sub(tick_chars, !tick_cycle) ^ " " ^
Chris@125 348 pad_to 70 name ^
Chris@122 349 "\r");
Chris@122 350 tick_cycle := (if !tick_cycle = n - 1 then 0 else 1 + !tick_cycle)
Chris@122 351 end
Chris@122 352
Chris@122 353 fun run_command context libname cmdlist redirect =
Chris@122 354 let open OS
Chris@122 355 val dir = libpath context libname
Chris@122 356 val cmd = expand_commandline cmdlist
Chris@122 357 val _ = if verbose ()
Chris@125 358 then print ("\n=== " ^ dir ^ "\n<<< " ^ cmd ^ "\n")
Chris@122 359 else tick libname cmdlist
Chris@122 360 val _ = FileSys.chDir dir
Chris@122 361 val status = case redirect of
Chris@122 362 NONE => Process.system cmd
Chris@122 363 | SOME file => Process.system (cmd ^ ">" ^ file)
Chris@122 364 in
Chris@122 365 if Process.isSuccess status
Chris@122 366 then OK ()
Chris@122 367 else ERROR ("Command failed: " ^ cmd ^ " (in dir " ^ dir ^ ")")
Chris@122 368 end
Chris@122 369 handle ex => ERROR ("Unable to run command: " ^ exnMessage ex)
Chris@122 370
Chris@122 371 fun command context libname cmdlist =
Chris@122 372 run_command context libname cmdlist NONE
Chris@122 373
Chris@122 374 fun command_output context libname cmdlist =
Chris@122 375 let open OS
Chris@122 376 val tmpFile = FileSys.tmpName ()
Chris@122 377 val result = run_command context libname cmdlist (SOME tmpFile)
Chris@122 378 val contents = file_contents tmpFile
Chris@125 379 val _ = if verbose ()
Chris@125 380 then print (">>> \"" ^ contents ^ "\"\n")
Chris@125 381 else ()
Chris@122 382 in
Chris@122 383 FileSys.remove tmpFile handle _ => ();
Chris@122 384 case result of
Chris@122 385 OK () => OK contents
Chris@122 386 | ERROR e => ERROR e
Chris@122 387 end
Chris@122 388
Chris@122 389 fun mydir () =
Chris@122 390 let open OS
Chris@122 391 val { dir, file } = Path.splitDirFile (CommandLine.name ())
Chris@122 392 in
Chris@122 393 FileSys.realPath
Chris@122 394 (if Path.isAbsolute dir
Chris@122 395 then dir
Chris@122 396 else Path.concat (FileSys.getDir (), dir))
Chris@122 397 end
Chris@122 398
Chris@122 399 fun homedir () =
Chris@122 400 (* Failure is not routine, so we use an exception here *)
Chris@122 401 case (OS.Process.getEnv "HOME",
Chris@122 402 OS.Process.getEnv "HOMEPATH") of
Chris@122 403 (SOME home, _) => home
Chris@122 404 | (NONE, SOME home) => home
Chris@122 405 | (NONE, NONE) =>
Chris@122 406 raise Fail "Failed to look up home directory from environment"
Chris@122 407
Chris@125 408 fun mkpath' path =
Chris@122 409 if OS.FileSys.isDir path handle _ => false
Chris@122 410 then OK ()
Chris@122 411 else case OS.Path.fromString path of
Chris@122 412 { arcs = nil, ... } => OK ()
Chris@122 413 | { isAbs = false, ... } => ERROR "mkpath requires absolute path"
Chris@122 414 | { isAbs, vol, arcs } =>
Chris@125 415 case mkpath' (OS.Path.toString { (* parent *)
Chris@125 416 isAbs = isAbs,
Chris@125 417 vol = vol,
Chris@125 418 arcs = rev (tl (rev arcs)) }) of
Chris@122 419 ERROR e => ERROR e
Chris@122 420 | OK () => ((OS.FileSys.mkDir path; OK ())
Chris@122 421 handle OS.SysErr (e, _) =>
Chris@122 422 ERROR ("Directory creation failed: " ^ e))
Chris@124 423
Chris@125 424 fun mkpath path =
Chris@125 425 mkpath' (OS.Path.mkCanonical path)
Chris@125 426
Chris@125 427 fun dir_contents dir =
Chris@124 428 let open OS
Chris@124 429 fun files_from dirstream =
Chris@124 430 case FileSys.readDir dirstream of
Chris@124 431 NONE => []
Chris@124 432 | SOME file =>
Chris@124 433 (* readDir is supposed to filter these,
Chris@124 434 but let's be extra cautious: *)
Chris@124 435 if file = Path.parentArc orelse file = Path.currentArc
Chris@124 436 then files_from dirstream
Chris@124 437 else file :: files_from dirstream
Chris@125 438 val stream = FileSys.openDir dir
Chris@125 439 val files = map (fn f => Path.joinDirFile
Chris@125 440 { dir = dir, file = f })
Chris@125 441 (files_from stream)
Chris@125 442 val _ = FileSys.closeDir stream
Chris@125 443 in
Chris@125 444 files
Chris@125 445 end
Chris@125 446
Chris@125 447 fun rmpath' path =
Chris@125 448 let open OS
Chris@124 449 fun remove path =
Chris@124 450 if FileSys.isLink path (* dangling links bother isDir *)
Chris@124 451 then FileSys.remove path
Chris@124 452 else if FileSys.isDir path
Chris@125 453 then (app remove (dir_contents path); FileSys.rmDir path)
Chris@124 454 else FileSys.remove path
Chris@124 455 in
Chris@124 456 (remove path; OK ())
Chris@124 457 handle SysErr (e, _) => ERROR ("Path removal failed: " ^ e)
Chris@124 458 end
Chris@125 459
Chris@125 460 fun rmpath path =
Chris@125 461 rmpath' (OS.Path.mkCanonical path)
Chris@125 462
Chris@125 463 fun nonempty_dir_exists path =
Chris@125 464 let open OS.FileSys
Chris@125 465 in
Chris@125 466 (not (isLink path) andalso
Chris@125 467 isDir path andalso
Chris@125 468 dir_contents path <> [])
Chris@125 469 handle _ => false
Chris@125 470 end
Chris@125 471
Chris@122 472 end
Chris@122 473
Chris@122 474 functor LibControlFn (V: VCS_CONTROL) :> LIB_CONTROL = struct
Chris@122 475
Chris@122 476 (* Valid states for unpinned libraries:
Chris@122 477
Chris@122 478 - CORRECT: We are on the right branch and are up-to-date with
Chris@122 479 it as far as we can tell. (If not using the network, this
Chris@122 480 should be reported to user as "Present" rather than "Correct"
Chris@122 481 as the remote repo may have advanced without us knowing.)
Chris@122 482
Chris@122 483 - SUPERSEDED: We are on the right branch but we can see that
Chris@122 484 there is a newer revision either locally or on the remote (in
Chris@122 485 Git terms, we are at an ancestor of the desired branch tip).
Chris@122 486
Chris@122 487 - WRONG: We are on the wrong branch (in Git terms, we are not
Chris@122 488 at the desired branch tip or any ancestor of it).
Chris@122 489
Chris@122 490 - ABSENT: Repo doesn't exist here at all.
Chris@122 491
Chris@122 492 Valid states for pinned libraries:
Chris@122 493
Chris@122 494 - CORRECT: We are at the pinned revision.
Chris@122 495
Chris@122 496 - WRONG: We are at any revision other than the pinned one.
Chris@122 497
Chris@122 498 - ABSENT: Repo doesn't exist here at all.
Chris@122 499 *)
Chris@122 500
Chris@122 501 fun check with_network context
Chris@125 502 ({ libname, source, branch,
Chris@125 503 project_pin, lock_pin, ... } : libspec) =
Chris@122 504 let fun check_unpinned () =
Chris@125 505 let val newest =
Chris@125 506 if with_network
Chris@125 507 then V.is_newest context (libname, source, branch)
Chris@125 508 else V.is_newest_locally context (libname, branch)
Chris@122 509 in
Chris@125 510 case newest of
Chris@122 511 ERROR e => ERROR e
Chris@122 512 | OK true => OK CORRECT
Chris@122 513 | OK false =>
Chris@122 514 case V.is_on_branch context (libname, branch) of
Chris@122 515 ERROR e => ERROR e
Chris@122 516 | OK true => OK SUPERSEDED
Chris@122 517 | OK false => OK WRONG
Chris@122 518 end
Chris@122 519 fun check_pinned target =
Chris@122 520 case V.is_at context (libname, target) of
Chris@122 521 ERROR e => ERROR e
Chris@122 522 | OK true => OK CORRECT
Chris@122 523 | OK false => OK WRONG
Chris@122 524 fun check_remote () =
Chris@122 525 case project_pin of
Chris@122 526 UNPINNED => check_unpinned ()
Chris@122 527 | PINNED target => check_pinned target
Chris@122 528 fun check_local () =
Chris@122 529 case V.is_modified_locally context libname of
Chris@122 530 ERROR e => ERROR e
Chris@122 531 | OK true => OK MODIFIED
Chris@122 532 | OK false =>
Chris@122 533 case lock_pin of
Chris@122 534 UNPINNED => OK CLEAN
Chris@122 535 | PINNED target =>
Chris@122 536 case V.is_at context (libname, target) of
Chris@122 537 ERROR e => ERROR e
Chris@122 538 | OK true => OK CLEAN
Chris@122 539 | OK false => OK LOCK_MISMATCHED
Chris@122 540 in
Chris@122 541 case V.exists context libname of
Chris@122 542 ERROR e => ERROR e
Chris@122 543 | OK false => OK (ABSENT, CLEAN)
Chris@122 544 | OK true =>
Chris@122 545 case (check_remote (), check_local ()) of
Chris@122 546 (ERROR e, _) => ERROR e
Chris@122 547 | (_, ERROR e) => ERROR e
Chris@122 548 | (OK r, OK l) => OK (r, l)
Chris@122 549 end
Chris@122 550
Chris@122 551 val review = check true
Chris@122 552 val status = check false
Chris@122 553
Chris@122 554 fun update context
Chris@122 555 ({ libname, source, branch,
Chris@122 556 project_pin, lock_pin, ... } : libspec) =
Chris@122 557 let fun update_unpinned () =
Chris@125 558 case V.is_newest context (libname, source, branch) of
Chris@122 559 ERROR e => ERROR e
Chris@125 560 | OK true => OK ()
Chris@125 561 | OK false => V.update context (libname, source, branch)
Chris@122 562 fun update_pinned target =
Chris@122 563 case V.is_at context (libname, target) of
Chris@122 564 ERROR e => ERROR e
Chris@125 565 | OK true => OK ()
Chris@125 566 | OK false => V.update_to context (libname, source, target)
Chris@122 567 fun update' () =
Chris@122 568 case lock_pin of
Chris@122 569 PINNED target => update_pinned target
Chris@122 570 | UNPINNED =>
Chris@122 571 case project_pin of
Chris@122 572 PINNED target => update_pinned target
Chris@122 573 | UNPINNED => update_unpinned ()
Chris@122 574 in
Chris@122 575 case V.exists context libname of
Chris@122 576 ERROR e => ERROR e
Chris@122 577 | OK true => update' ()
Chris@122 578 | OK false =>
Chris@122 579 case V.checkout context (libname, source, branch) of
Chris@122 580 ERROR e => ERROR e
Chris@122 581 | OK () => update' ()
Chris@122 582 end
Chris@122 583
Chris@122 584 fun id_of context ({ libname, ... } : libspec) =
Chris@122 585 V.id_of context libname
Chris@125 586
Chris@125 587 fun is_working context vcs =
Chris@125 588 V.is_working context
Chris@122 589
Chris@122 590 end
Chris@122 591
Chris@122 592 (* Simple Standard ML JSON parser
Chris@122 593 https://bitbucket.org/cannam/sml-simplejson
Chris@125 594 Copyright 2017 Chris Cannam. BSD licence.
Chris@122 595 Parts based on the JSON parser in the Ponyo library by Phil Eaton.
Chris@122 596 *)
Chris@122 597
Chris@122 598 signature JSON = sig
Chris@122 599
Chris@122 600 datatype json = OBJECT of (string * json) list
Chris@122 601 | ARRAY of json list
Chris@122 602 | NUMBER of real
Chris@122 603 | STRING of string
Chris@122 604 | BOOL of bool
Chris@122 605 | NULL
Chris@122 606
Chris@122 607 datatype 'a result = OK of 'a
Chris@122 608 | ERROR of string
Chris@122 609
Chris@122 610 val parse : string -> json result
Chris@122 611 val serialise : json -> string
Chris@122 612 val serialiseIndented : json -> string
Chris@122 613
Chris@122 614 end
Chris@122 615
Chris@122 616 structure Json :> JSON = struct
Chris@122 617
Chris@122 618 datatype json = OBJECT of (string * json) list
Chris@122 619 | ARRAY of json list
Chris@122 620 | NUMBER of real
Chris@122 621 | STRING of string
Chris@122 622 | BOOL of bool
Chris@122 623 | NULL
Chris@122 624
Chris@122 625 datatype 'a result = OK of 'a
Chris@122 626 | ERROR of string
Chris@122 627
Chris@122 628 structure T = struct
Chris@122 629 datatype token = NUMBER of char list
Chris@122 630 | STRING of string
Chris@122 631 | BOOL of bool
Chris@122 632 | NULL
Chris@122 633 | CURLY_L
Chris@122 634 | CURLY_R
Chris@122 635 | SQUARE_L
Chris@122 636 | SQUARE_R
Chris@122 637 | COLON
Chris@122 638 | COMMA
Chris@122 639
Chris@122 640 fun toString t =
Chris@122 641 case t of NUMBER digits => implode digits
Chris@122 642 | STRING s => s
Chris@122 643 | BOOL b => Bool.toString b
Chris@122 644 | NULL => "null"
Chris@122 645 | CURLY_L => "{"
Chris@122 646 | CURLY_R => "}"
Chris@122 647 | SQUARE_L => "["
Chris@122 648 | SQUARE_R => "]"
Chris@122 649 | COLON => ":"
Chris@122 650 | COMMA => ","
Chris@122 651 end
Chris@122 652
Chris@122 653 fun bmpToUtf8 cp = (* convert a codepoint in Unicode BMP to utf8 bytes *)
Chris@122 654 let open Word
Chris@122 655 infix 6 orb andb >>
Chris@122 656 in
Chris@122 657 map (Char.chr o toInt)
Chris@122 658 (if cp < 0wx80 then
Chris@122 659 [cp]
Chris@122 660 else if cp < 0wx800 then
Chris@122 661 [0wxc0 orb (cp >> 0w6), 0wx80 orb (cp andb 0wx3f)]
Chris@122 662 else if cp < 0wx10000 then
Chris@122 663 [0wxe0 orb (cp >> 0w12),
Chris@122 664 0wx80 orb ((cp >> 0w6) andb 0wx3f),
Chris@122 665 0wx80 orb (cp andb 0wx3f)]
Chris@122 666 else raise Fail ("Invalid BMP point " ^ (Word.toString cp)))
Chris@122 667 end
Chris@122 668
Chris@122 669 fun error pos text = ERROR (text ^ " at character position " ^
Chris@122 670 Int.toString (pos - 1))
Chris@122 671 fun token_error pos = error pos ("Unexpected token")
Chris@122 672
Chris@122 673 fun lexNull pos acc (#"u" :: #"l" :: #"l" :: xs) =
Chris@122 674 lex (pos + 3) (T.NULL :: acc) xs
Chris@122 675 | lexNull pos acc _ = token_error pos
Chris@122 676
Chris@122 677 and lexTrue pos acc (#"r" :: #"u" :: #"e" :: xs) =
Chris@122 678 lex (pos + 3) (T.BOOL true :: acc) xs
Chris@122 679 | lexTrue pos acc _ = token_error pos
Chris@122 680
Chris@122 681 and lexFalse pos acc (#"a" :: #"l" :: #"s" :: #"e" :: xs) =
Chris@122 682 lex (pos + 4) (T.BOOL false :: acc) xs
Chris@122 683 | lexFalse pos acc _ = token_error pos
Chris@122 684
Chris@122 685 and lexChar tok pos acc xs =
Chris@122 686 lex pos (tok :: acc) xs
Chris@122 687
Chris@122 688 and lexString pos acc cc =
Chris@122 689 let datatype escaped = ESCAPED | NORMAL
Chris@122 690 fun lexString' pos text ESCAPED [] =
Chris@122 691 error pos "End of input during escape sequence"
Chris@122 692 | lexString' pos text NORMAL [] =
Chris@122 693 error pos "End of input during string"
Chris@122 694 | lexString' pos text ESCAPED (x :: xs) =
Chris@122 695 let fun esc c = lexString' (pos + 1) (c :: text) NORMAL xs
Chris@122 696 in case x of
Chris@122 697 #"\"" => esc x
Chris@122 698 | #"\\" => esc x
Chris@122 699 | #"/" => esc x
Chris@122 700 | #"b" => esc #"\b"
Chris@122 701 | #"f" => esc #"\f"
Chris@122 702 | #"n" => esc #"\n"
Chris@122 703 | #"r" => esc #"\r"
Chris@122 704 | #"t" => esc #"\t"
Chris@122 705 | _ => error pos ("Invalid escape \\" ^
Chris@122 706 Char.toString x)
Chris@122 707 end
Chris@122 708 | lexString' pos text NORMAL (#"\\" :: #"u" ::a::b::c::d:: xs) =
Chris@122 709 if List.all Char.isHexDigit [a,b,c,d]
Chris@122 710 then case Word.fromString ("0wx" ^ (implode [a,b,c,d])) of
Chris@122 711 SOME w => (let val utf = rev (bmpToUtf8 w) in
Chris@122 712 lexString' (pos + 6) (utf @ text)
Chris@122 713 NORMAL xs
Chris@122 714 end
Chris@122 715 handle Fail err => error pos err)
Chris@122 716 | NONE => error pos "Invalid Unicode BMP escape sequence"
Chris@122 717 else error pos "Invalid Unicode BMP escape sequence"
Chris@122 718 | lexString' pos text NORMAL (x :: xs) =
Chris@122 719 if Char.ord x < 0x20
Chris@122 720 then error pos "Invalid unescaped control character"
Chris@122 721 else
Chris@122 722 case x of
Chris@122 723 #"\"" => OK (rev text, xs, pos + 1)
Chris@122 724 | #"\\" => lexString' (pos + 1) text ESCAPED xs
Chris@122 725 | _ => lexString' (pos + 1) (x :: text) NORMAL xs
Chris@122 726 in
Chris@122 727 case lexString' pos [] NORMAL cc of
Chris@122 728 OK (text, rest, newpos) =>
Chris@122 729 lex newpos (T.STRING (implode text) :: acc) rest
Chris@122 730 | ERROR e => ERROR e
Chris@122 731 end
Chris@122 732
Chris@122 733 and lexNumber firstChar pos acc cc =
Chris@122 734 let val valid = explode ".+-e"
Chris@122 735 fun lexNumber' pos digits [] = (rev digits, [], pos)
Chris@122 736 | lexNumber' pos digits (x :: xs) =
Chris@122 737 if x = #"E" then lexNumber' (pos + 1) (#"e" :: digits) xs
Chris@122 738 else if Char.isDigit x orelse List.exists (fn c => x = c) valid
Chris@122 739 then lexNumber' (pos + 1) (x :: digits) xs
Chris@122 740 else (rev digits, x :: xs, pos)
Chris@122 741 val (digits, rest, newpos) =
Chris@122 742 lexNumber' (pos - 1) [] (firstChar :: cc)
Chris@122 743 in
Chris@122 744 case digits of
Chris@122 745 [] => token_error pos
Chris@122 746 | _ => lex newpos (T.NUMBER digits :: acc) rest
Chris@122 747 end
Chris@122 748
Chris@122 749 and lex pos acc [] = OK (rev acc)
Chris@122 750 | lex pos acc (x::xs) =
Chris@122 751 (case x of
Chris@122 752 #" " => lex
Chris@122 753 | #"\t" => lex
Chris@122 754 | #"\n" => lex
Chris@122 755 | #"\r" => lex
Chris@122 756 | #"{" => lexChar T.CURLY_L
Chris@122 757 | #"}" => lexChar T.CURLY_R
Chris@122 758 | #"[" => lexChar T.SQUARE_L
Chris@122 759 | #"]" => lexChar T.SQUARE_R
Chris@122 760 | #":" => lexChar T.COLON
Chris@122 761 | #"," => lexChar T.COMMA
Chris@122 762 | #"\"" => lexString
Chris@122 763 | #"t" => lexTrue
Chris@122 764 | #"f" => lexFalse
Chris@122 765 | #"n" => lexNull
Chris@122 766 | x => lexNumber x) (pos + 1) acc xs
Chris@122 767
Chris@122 768 fun show [] = "end of input"
Chris@122 769 | show (tok :: _) = T.toString tok
Chris@122 770
Chris@122 771 fun parseNumber digits =
Chris@122 772 (* Note lexNumber already case-insensitised the E for us *)
Chris@122 773 let open Char
Chris@122 774
Chris@122 775 fun okExpDigits [] = false
Chris@122 776 | okExpDigits (c :: []) = isDigit c
Chris@122 777 | okExpDigits (c :: cs) = isDigit c andalso okExpDigits cs
Chris@122 778
Chris@122 779 fun okExponent [] = false
Chris@122 780 | okExponent (#"+" :: cs) = okExpDigits cs
Chris@122 781 | okExponent (#"-" :: cs) = okExpDigits cs
Chris@122 782 | okExponent cc = okExpDigits cc
Chris@122 783
Chris@122 784 fun okFracTrailing [] = true
Chris@122 785 | okFracTrailing (c :: cs) =
Chris@122 786 (isDigit c andalso okFracTrailing cs) orelse
Chris@122 787 (c = #"e" andalso okExponent cs)
Chris@122 788
Chris@122 789 fun okFraction [] = false
Chris@122 790 | okFraction (c :: cs) =
Chris@122 791 isDigit c andalso okFracTrailing cs
Chris@122 792
Chris@122 793 fun okPosTrailing [] = true
Chris@122 794 | okPosTrailing (#"." :: cs) = okFraction cs
Chris@122 795 | okPosTrailing (#"e" :: cs) = okExponent cs
Chris@122 796 | okPosTrailing (c :: cs) =
Chris@122 797 isDigit c andalso okPosTrailing cs
Chris@122 798
Chris@122 799 fun okPositive [] = false
Chris@122 800 | okPositive (#"0" :: []) = true
Chris@122 801 | okPositive (#"0" :: #"." :: cs) = okFraction cs
Chris@122 802 | okPositive (#"0" :: #"e" :: cs) = okExponent cs
Chris@122 803 | okPositive (#"0" :: cs) = false
Chris@122 804 | okPositive (c :: cs) = isDigit c andalso okPosTrailing cs
Chris@122 805
Chris@122 806 fun okNumber (#"-" :: cs) = okPositive cs
Chris@122 807 | okNumber cc = okPositive cc
Chris@122 808 in
Chris@122 809 if okNumber digits
Chris@122 810 then case Real.fromString (implode digits) of
Chris@122 811 NONE => ERROR "Number out of range"
Chris@122 812 | SOME r => OK r
Chris@122 813 else ERROR ("Invalid number \"" ^ (implode digits) ^ "\"")
Chris@122 814 end
Chris@122 815
Chris@122 816 fun parseObject (T.CURLY_R :: xs) = OK (OBJECT [], xs)
Chris@122 817 | parseObject tokens =
Chris@122 818 let fun parsePair (T.STRING key :: T.COLON :: xs) =
Chris@122 819 (case parseTokens xs of
Chris@122 820 ERROR e => ERROR e
Chris@122 821 | OK (j, xs) => OK ((key, j), xs))
Chris@122 822 | parsePair other =
Chris@122 823 ERROR ("Object key/value pair expected around \"" ^
Chris@122 824 show other ^ "\"")
Chris@122 825 fun parseObject' acc [] = ERROR "End of input during object"
Chris@122 826 | parseObject' acc tokens =
Chris@122 827 case parsePair tokens of
Chris@122 828 ERROR e => ERROR e
Chris@122 829 | OK (pair, T.COMMA :: xs) =>
Chris@122 830 parseObject' (pair :: acc) xs
Chris@122 831 | OK (pair, T.CURLY_R :: xs) =>
Chris@122 832 OK (OBJECT (rev (pair :: acc)), xs)
Chris@122 833 | OK (_, _) => ERROR "Expected , or } after object element"
Chris@122 834 in
Chris@122 835 parseObject' [] tokens
Chris@122 836 end
Chris@122 837
Chris@122 838 and parseArray (T.SQUARE_R :: xs) = OK (ARRAY [], xs)
Chris@122 839 | parseArray tokens =
Chris@122 840 let fun parseArray' acc [] = ERROR "End of input during array"
Chris@122 841 | parseArray' acc tokens =
Chris@122 842 case parseTokens tokens of
Chris@122 843 ERROR e => ERROR e
Chris@122 844 | OK (j, T.COMMA :: xs) => parseArray' (j :: acc) xs
Chris@122 845 | OK (j, T.SQUARE_R :: xs) => OK (ARRAY (rev (j :: acc)), xs)
Chris@122 846 | OK (_, _) => ERROR "Expected , or ] after array element"
Chris@122 847 in
Chris@122 848 parseArray' [] tokens
Chris@122 849 end
Chris@122 850
Chris@122 851 and parseTokens [] = ERROR "Value expected"
Chris@122 852 | parseTokens (tok :: xs) =
Chris@122 853 (case tok of
Chris@122 854 T.NUMBER d => (case parseNumber d of
Chris@122 855 OK r => OK (NUMBER r, xs)
Chris@122 856 | ERROR e => ERROR e)
Chris@122 857 | T.STRING s => OK (STRING s, xs)
Chris@122 858 | T.BOOL b => OK (BOOL b, xs)
Chris@122 859 | T.NULL => OK (NULL, xs)
Chris@122 860 | T.CURLY_L => parseObject xs
Chris@122 861 | T.SQUARE_L => parseArray xs
Chris@122 862 | _ => ERROR ("Unexpected token " ^ T.toString tok ^
Chris@122 863 " before " ^ show xs))
Chris@122 864
Chris@122 865 fun parse str =
Chris@122 866 case lex 1 [] (explode str) of
Chris@122 867 ERROR e => ERROR e
Chris@122 868 | OK tokens => case parseTokens tokens of
Chris@122 869 OK (value, []) => OK value
Chris@122 870 | OK (_, _) => ERROR "Extra data after input"
Chris@122 871 | ERROR e => ERROR e
Chris@122 872
Chris@122 873 fun stringEscape s =
Chris@122 874 let fun esc x = [x, #"\\"]
Chris@122 875 fun escape' acc [] = rev acc
Chris@122 876 | escape' acc (x :: xs) =
Chris@122 877 escape' (case x of
Chris@122 878 #"\"" => esc x @ acc
Chris@122 879 | #"\\" => esc x @ acc
Chris@122 880 | #"\b" => esc #"b" @ acc
Chris@122 881 | #"\f" => esc #"f" @ acc
Chris@122 882 | #"\n" => esc #"n" @ acc
Chris@122 883 | #"\r" => esc #"r" @ acc
Chris@122 884 | #"\t" => esc #"t" @ acc
Chris@122 885 | _ =>
Chris@122 886 let val c = Char.ord x
Chris@122 887 in
Chris@122 888 if c < 0x20
Chris@122 889 then let val hex = Word.toString (Word.fromInt c)
Chris@122 890 in (rev o explode) (if c < 0x10
Chris@122 891 then ("\\u000" ^ hex)
Chris@122 892 else ("\\u00" ^ hex))
Chris@122 893 end @ acc
Chris@122 894 else
Chris@122 895 x :: acc
Chris@122 896 end)
Chris@122 897 xs
Chris@122 898 in
Chris@122 899 implode (escape' [] (explode s))
Chris@122 900 end
Chris@122 901
Chris@122 902 fun serialise json =
Chris@122 903 case json of
Chris@122 904 OBJECT pp => "{" ^ String.concatWith
Chris@122 905 "," (map (fn (key, value) =>
Chris@122 906 serialise (STRING key) ^ ":" ^
Chris@122 907 serialise value) pp) ^
Chris@122 908 "}"
Chris@122 909 | ARRAY arr => "[" ^ String.concatWith "," (map serialise arr) ^ "]"
Chris@122 910 | NUMBER n => implode (map (fn #"~" => #"-" | c => c)
Chris@122 911 (explode (Real.toString n)))
Chris@122 912 | STRING s => "\"" ^ stringEscape s ^ "\""
Chris@122 913 | BOOL b => Bool.toString b
Chris@122 914 | NULL => "null"
Chris@122 915
Chris@122 916 fun serialiseIndented json =
Chris@122 917 let fun indent 0 = ""
Chris@122 918 | indent i = " " ^ indent (i - 1)
Chris@122 919 fun serialiseIndented' i json =
Chris@122 920 let val ser = serialiseIndented' (i + 1)
Chris@122 921 in
Chris@122 922 case json of
Chris@122 923 OBJECT [] => "{}"
Chris@122 924 | ARRAY [] => "[]"
Chris@122 925 | OBJECT pp => "{\n" ^ indent (i + 1) ^
Chris@122 926 String.concatWith
Chris@122 927 (",\n" ^ indent (i + 1))
Chris@122 928 (map (fn (key, value) =>
Chris@122 929 ser (STRING key) ^ ": " ^
Chris@122 930 ser value) pp) ^
Chris@122 931 "\n" ^ indent i ^ "}"
Chris@122 932 | ARRAY arr => "[\n" ^ indent (i + 1) ^
Chris@122 933 String.concatWith
Chris@122 934 (",\n" ^ indent (i + 1))
Chris@122 935 (map ser arr) ^
Chris@122 936 "\n" ^ indent i ^ "]"
Chris@122 937 | other => serialise other
Chris@122 938 end
Chris@122 939 in
Chris@122 940 serialiseIndented' 0 json ^ "\n"
Chris@122 941 end
Chris@122 942
Chris@122 943 end
Chris@122 944
Chris@122 945
Chris@122 946 structure JsonBits :> sig
Chris@125 947 exception Config of string
Chris@122 948 val load_json_from : string -> Json.json (* filename -> json *)
Chris@122 949 val save_json_to : string -> Json.json -> unit
Chris@122 950 val lookup_optional : Json.json -> string list -> Json.json option
Chris@122 951 val lookup_optional_string : Json.json -> string list -> string option
Chris@122 952 val lookup_mandatory : Json.json -> string list -> Json.json
Chris@122 953 val lookup_mandatory_string : Json.json -> string list -> string
Chris@122 954 end = struct
Chris@122 955
Chris@125 956 exception Config of string
Chris@125 957
Chris@122 958 fun load_json_from filename =
Chris@122 959 case Json.parse (FileBits.file_contents filename) of
Chris@122 960 Json.OK json => json
Chris@125 961 | Json.ERROR e => raise Config ("Failed to parse file: " ^ e)
Chris@122 962
Chris@122 963 fun save_json_to filename json =
Chris@122 964 (* using binary I/O to avoid ever writing CR/LF line endings *)
Chris@122 965 let val jstr = Json.serialiseIndented json
Chris@122 966 val stream = BinIO.openOut filename
Chris@122 967 in
Chris@122 968 BinIO.output (stream, Byte.stringToBytes jstr);
Chris@122 969 BinIO.closeOut stream
Chris@122 970 end
Chris@122 971
Chris@122 972 fun lookup_optional json kk =
Chris@122 973 let fun lookup key =
Chris@122 974 case json of
Chris@122 975 Json.OBJECT kvs =>
Chris@125 976 (case List.filter (fn (k, v) => k = key) kvs of
Chris@125 977 [] => NONE
Chris@125 978 | [(_,v)] => SOME v
Chris@125 979 | _ => raise Config ("Duplicate key: " ^
Chris@125 980 (String.concatWith " -> " kk)))
Chris@125 981 | _ => raise Config "Object expected"
Chris@122 982 in
Chris@122 983 case kk of
Chris@122 984 [] => NONE
Chris@122 985 | key::[] => lookup key
Chris@122 986 | key::kk => case lookup key of
Chris@122 987 NONE => NONE
Chris@122 988 | SOME j => lookup_optional j kk
Chris@122 989 end
Chris@122 990
Chris@122 991 fun lookup_optional_string json kk =
Chris@122 992 case lookup_optional json kk of
Chris@122 993 SOME (Json.STRING s) => SOME s
Chris@125 994 | SOME _ => raise Config ("Value (if present) must be string: " ^
Chris@125 995 (String.concatWith " -> " kk))
Chris@122 996 | NONE => NONE
Chris@122 997
Chris@122 998 fun lookup_mandatory json kk =
Chris@122 999 case lookup_optional json kk of
Chris@122 1000 SOME v => v
Chris@125 1001 | NONE => raise Config ("Value is mandatory: " ^
Chris@125 1002 (String.concatWith " -> " kk))
Chris@122 1003
Chris@122 1004 fun lookup_mandatory_string json kk =
Chris@122 1005 case lookup_optional json kk of
Chris@122 1006 SOME (Json.STRING s) => s
Chris@125 1007 | _ => raise Config ("Value must be string: " ^
Chris@125 1008 (String.concatWith " -> " kk))
Chris@122 1009 end
Chris@122 1010
Chris@122 1011 structure Provider :> sig
Chris@122 1012 val load_providers : Json.json -> provider list
Chris@122 1013 val load_more_providers : provider list -> Json.json -> provider list
Chris@122 1014 val remote_url : context -> vcs -> source -> libname -> string
Chris@122 1015 end = struct
Chris@122 1016
Chris@122 1017 val known_providers : provider list =
Chris@122 1018 [ {
Chris@122 1019 service = "bitbucket",
Chris@122 1020 supports = [HG, GIT],
Chris@122 1021 remote_spec = {
Chris@122 1022 anon = SOME "https://bitbucket.org/{owner}/{repository}",
Chris@122 1023 auth = SOME "ssh://{vcs}@bitbucket.org/{owner}/{repository}"
Chris@122 1024 }
Chris@122 1025 },
Chris@122 1026 {
Chris@122 1027 service = "github",
Chris@122 1028 supports = [GIT],
Chris@122 1029 remote_spec = {
Chris@122 1030 anon = SOME "https://github.com/{owner}/{repository}",
Chris@122 1031 auth = SOME "ssh://{vcs}@github.com/{owner}/{repository}"
Chris@122 1032 }
Chris@122 1033 }
Chris@122 1034 ]
Chris@122 1035
Chris@122 1036 fun vcs_name vcs =
Chris@125 1037 case vcs of HG => "hg"
Chris@125 1038 | GIT => "git"
Chris@125 1039 | SVN => "svn"
Chris@122 1040
Chris@122 1041 fun vcs_from_name name =
Chris@125 1042 case name of "hg" => HG
Chris@125 1043 | "git" => GIT
Chris@125 1044 | "svn" => SVN
Chris@122 1045 | other => raise Fail ("Unknown vcs name \"" ^ name ^ "\"")
Chris@122 1046
Chris@122 1047 fun load_more_providers previously_loaded json =
Chris@122 1048 let open JsonBits
Chris@122 1049 fun load pjson pname : provider =
Chris@122 1050 {
Chris@122 1051 service = pname,
Chris@122 1052 supports =
Chris@122 1053 case lookup_mandatory pjson ["vcs"] of
Chris@122 1054 Json.ARRAY vv =>
Chris@122 1055 map (fn (Json.STRING v) => vcs_from_name v
Chris@122 1056 | _ => raise Fail "Strings expected in vcs array")
Chris@122 1057 vv
Chris@122 1058 | _ => raise Fail "Array expected for vcs",
Chris@122 1059 remote_spec = {
Chris@122 1060 anon = lookup_optional_string pjson ["anonymous"],
Chris@122 1061 auth = lookup_optional_string pjson ["authenticated"]
Chris@122 1062 }
Chris@122 1063 }
Chris@122 1064 val loaded =
Chris@122 1065 case lookup_optional json ["services"] of
Chris@122 1066 NONE => []
Chris@122 1067 | SOME (Json.OBJECT pl) => map (fn (k, v) => load v k) pl
Chris@122 1068 | _ => raise Fail "Object expected for services in config"
Chris@122 1069 val newly_loaded =
Chris@122 1070 List.filter (fn p => not (List.exists (fn pp => #service p =
Chris@122 1071 #service pp)
Chris@122 1072 previously_loaded))
Chris@122 1073 loaded
Chris@122 1074 in
Chris@122 1075 previously_loaded @ newly_loaded
Chris@122 1076 end
Chris@122 1077
Chris@122 1078 fun load_providers json =
Chris@122 1079 load_more_providers known_providers json
Chris@122 1080
Chris@122 1081 fun expand_spec spec { vcs, service, owner, repo } login =
Chris@122 1082 (* ugly *)
Chris@122 1083 let fun replace str =
Chris@122 1084 case str of
Chris@122 1085 "vcs" => vcs_name vcs
Chris@122 1086 | "service" => service
Chris@122 1087 | "owner" =>
Chris@122 1088 (case owner of
Chris@122 1089 SOME ostr => ostr
Chris@122 1090 | NONE => raise Fail ("Owner not specified for service " ^
Chris@122 1091 service))
Chris@122 1092 | "repository" => repo
Chris@122 1093 | "account" =>
Chris@122 1094 (case login of
Chris@122 1095 SOME acc => acc
Chris@122 1096 | NONE => raise Fail ("Account not given for service " ^
Chris@122 1097 service))
Chris@122 1098 | other => raise Fail ("Unknown variable \"" ^ other ^
Chris@122 1099 "\" in spec for service " ^ service)
Chris@122 1100 fun expand' acc sstr =
Chris@122 1101 case Substring.splitl (fn c => c <> #"{") sstr of
Chris@122 1102 (pfx, sfx) =>
Chris@122 1103 if Substring.isEmpty sfx
Chris@122 1104 then rev (pfx :: acc)
Chris@122 1105 else
Chris@122 1106 case Substring.splitl (fn c => c <> #"}") sfx of
Chris@122 1107 (tok, remainder) =>
Chris@122 1108 if Substring.isEmpty remainder
Chris@122 1109 then rev (tok :: pfx :: acc)
Chris@122 1110 else let val replacement =
Chris@122 1111 replace
Chris@122 1112 (* tok begins with "{": *)
Chris@122 1113 (Substring.string
Chris@122 1114 (Substring.triml 1 tok))
Chris@122 1115 in
Chris@122 1116 expand' (Substring.full replacement ::
Chris@122 1117 pfx :: acc)
Chris@122 1118 (* remainder begins with "}": *)
Chris@122 1119 (Substring.triml 1 remainder)
Chris@122 1120 end
Chris@122 1121 in
Chris@122 1122 Substring.concat (expand' [] (Substring.full spec))
Chris@122 1123 end
Chris@122 1124
Chris@122 1125 fun provider_url req login providers =
Chris@122 1126 case providers of
Chris@122 1127 [] => raise Fail ("Unknown service \"" ^ (#service req) ^
Chris@122 1128 "\" for vcs \"" ^ (vcs_name (#vcs req)) ^ "\"")
Chris@122 1129 | ({ service, supports, remote_spec : remote_spec } :: rest) =>
Chris@122 1130 if service <> (#service req) orelse
Chris@122 1131 not (List.exists (fn v => v = (#vcs req)) supports)
Chris@122 1132 then provider_url req login rest
Chris@122 1133 else
Chris@122 1134 case (login, #auth remote_spec, #anon remote_spec) of
Chris@122 1135 (SOME _, SOME auth, _) => expand_spec auth req login
Chris@122 1136 | (SOME _, _, SOME anon) => expand_spec anon req NONE
Chris@122 1137 | (NONE, _, SOME anon) => expand_spec anon req NONE
Chris@122 1138 | _ => raise Fail ("No suitable anonymous or authenticated " ^
Chris@122 1139 "URL spec provided for service \"" ^
Chris@122 1140 service ^ "\"")
Chris@122 1141
Chris@122 1142 fun login_for ({ accounts, ... } : context) service =
Chris@122 1143 case List.find (fn a => service = #service a) accounts of
Chris@122 1144 SOME { login, ... } => SOME login
Chris@122 1145 | NONE => NONE
Chris@125 1146
Chris@125 1147 fun reponame_for path =
Chris@125 1148 case String.tokens (fn c => c = #"/") path of
Chris@125 1149 [] => raise Fail "Non-empty library path required"
Chris@125 1150 | toks => hd (rev toks)
Chris@125 1151
Chris@122 1152 fun remote_url (context : context) vcs source libname =
Chris@122 1153 case source of
Chris@122 1154 URL_SOURCE u => u
Chris@122 1155 | SERVICE_SOURCE { service, owner, repo } =>
Chris@122 1156 provider_url { vcs = vcs,
Chris@122 1157 service = service,
Chris@122 1158 owner = owner,
Chris@122 1159 repo = case repo of
Chris@122 1160 SOME r => r
Chris@125 1161 | NONE => reponame_for libname }
Chris@122 1162 (login_for context service)
Chris@122 1163 (#providers context)
Chris@122 1164 end
Chris@122 1165
Chris@122 1166 structure HgControl :> VCS_CONTROL = struct
Chris@125 1167
Chris@125 1168 (* Pulls always use an explicit URL, never just the default
Chris@125 1169 remote, in order to ensure we update properly if the location
Chris@125 1170 given in the project file changes. *)
Chris@125 1171
Chris@122 1172 type vcsstate = { id: string, modified: bool,
Chris@122 1173 branch: string, tags: string list }
Chris@122 1174
Chris@125 1175 val hg_program = "hg"
Chris@125 1176
Chris@125 1177 val hg_args = [ "--config", "ui.interactive=true",
Chris@125 1178 "--config", "ui.merge=:merge" ]
Chris@122 1179
Chris@122 1180 fun hg_command context libname args =
Chris@125 1181 FileBits.command context libname (hg_program :: hg_args @ args)
Chris@122 1182
Chris@122 1183 fun hg_command_output context libname args =
Chris@125 1184 FileBits.command_output context libname (hg_program :: hg_args @ args)
Chris@125 1185
Chris@125 1186 fun is_working context =
Chris@125 1187 case hg_command_output context "" ["--version"] of
Chris@125 1188 OK "" => OK false
Chris@125 1189 | OK _ => OK true
Chris@125 1190 | ERROR e => ERROR e
Chris@125 1191
Chris@122 1192 fun exists context libname =
Chris@122 1193 OK (OS.FileSys.isDir (FileBits.subpath context libname ".hg"))
Chris@122 1194 handle _ => OK false
Chris@122 1195
Chris@122 1196 fun remote_for context (libname, source) =
Chris@122 1197 Provider.remote_url context HG source libname
Chris@122 1198
Chris@122 1199 fun current_state context libname : vcsstate result =
Chris@122 1200 let fun is_branch text = text <> "" andalso #"(" = hd (explode text)
Chris@122 1201 and extract_branch b =
Chris@122 1202 if is_branch b (* need to remove enclosing parens *)
Chris@122 1203 then (implode o rev o tl o rev o tl o explode) b
Chris@122 1204 else "default"
Chris@122 1205 and is_modified id = id <> "" andalso #"+" = hd (rev (explode id))
Chris@122 1206 and extract_id id =
Chris@122 1207 if is_modified id (* need to remove trailing "+" *)
Chris@122 1208 then (implode o rev o tl o rev o explode) id
Chris@122 1209 else id
Chris@122 1210 and split_tags tags = String.tokens (fn c => c = #"/") tags
Chris@122 1211 and state_for (id, branch, tags) =
Chris@122 1212 OK { id = extract_id id,
Chris@122 1213 modified = is_modified id,
Chris@122 1214 branch = extract_branch branch,
Chris@122 1215 tags = split_tags tags }
Chris@122 1216 in
Chris@122 1217 case hg_command_output context libname ["id"] of
Chris@122 1218 ERROR e => ERROR e
Chris@122 1219 | OK out =>
Chris@122 1220 case String.tokens (fn x => x = #" ") out of
Chris@122 1221 [id, branch, tags] => state_for (id, branch, tags)
Chris@122 1222 | [id, other] => if is_branch other
Chris@122 1223 then state_for (id, other, "")
Chris@122 1224 else state_for (id, "", other)
Chris@122 1225 | [id] => state_for (id, "", "")
Chris@122 1226 | _ => ERROR ("Unexpected output from hg id: " ^ out)
Chris@122 1227 end
Chris@122 1228
Chris@122 1229 fun branch_name branch = case branch of
Chris@122 1230 DEFAULT_BRANCH => "default"
Chris@122 1231 | BRANCH "" => "default"
Chris@122 1232 | BRANCH b => b
Chris@122 1233
Chris@122 1234 fun id_of context libname =
Chris@122 1235 case current_state context libname of
Chris@122 1236 ERROR e => ERROR e
Chris@122 1237 | OK { id, ... } => OK id
Chris@122 1238
Chris@122 1239 fun is_at context (libname, id_or_tag) =
Chris@122 1240 case current_state context libname of
Chris@122 1241 ERROR e => ERROR e
Chris@122 1242 | OK { id, tags, ... } =>
Chris@122 1243 OK (String.isPrefix id_or_tag id orelse
Chris@122 1244 String.isPrefix id id_or_tag orelse
Chris@122 1245 List.exists (fn t => t = id_or_tag) tags)
Chris@122 1246
Chris@122 1247 fun is_on_branch context (libname, b) =
Chris@122 1248 case current_state context libname of
Chris@122 1249 ERROR e => ERROR e
Chris@122 1250 | OK { branch, ... } => OK (branch = branch_name b)
Chris@122 1251
Chris@122 1252 fun is_newest_locally context (libname, branch) =
Chris@122 1253 case hg_command_output context libname
Chris@122 1254 ["log", "-l1",
Chris@122 1255 "-b", branch_name branch,
Chris@122 1256 "--template", "{node}"] of
Chris@125 1257 ERROR e => OK false (* desired branch does not exist *)
Chris@122 1258 | OK newest_in_repo => is_at context (libname, newest_in_repo)
Chris@122 1259
Chris@125 1260 fun pull context (libname, source) =
Chris@125 1261 let val url = remote_for context (libname, source)
Chris@125 1262 in
Chris@125 1263 hg_command context libname
Chris@125 1264 (if FileBits.verbose ()
Chris@125 1265 then ["pull", url]
Chris@125 1266 else ["pull", "-q", url])
Chris@125 1267 end
Chris@122 1268
Chris@125 1269 fun is_newest context (libname, source, branch) =
Chris@122 1270 case is_newest_locally context (libname, branch) of
Chris@122 1271 ERROR e => ERROR e
Chris@122 1272 | OK false => OK false
Chris@122 1273 | OK true =>
Chris@125 1274 case pull context (libname, source) of
Chris@122 1275 ERROR e => ERROR e
Chris@122 1276 | _ => is_newest_locally context (libname, branch)
Chris@122 1277
Chris@122 1278 fun is_modified_locally context libname =
Chris@122 1279 case current_state context libname of
Chris@122 1280 ERROR e => ERROR e
Chris@122 1281 | OK { modified, ... } => OK modified
Chris@122 1282
Chris@122 1283 fun checkout context (libname, source, branch) =
Chris@122 1284 let val url = remote_for context (libname, source)
Chris@122 1285 in
Chris@125 1286 (* make the lib dir rather than just the ext dir, since
Chris@125 1287 the lib dir might be nested and hg will happily check
Chris@125 1288 out into an existing empty dir anyway *)
Chris@125 1289 case FileBits.mkpath (FileBits.libpath context libname) of
Chris@122 1290 ERROR e => ERROR e
Chris@122 1291 | _ => hg_command context ""
Chris@122 1292 ["clone", "-u", branch_name branch,
Chris@122 1293 url, libname]
Chris@122 1294 end
Chris@122 1295
Chris@125 1296 fun update context (libname, source, branch) =
Chris@125 1297 let val pull_result = pull context (libname, source)
Chris@122 1298 in
Chris@122 1299 case hg_command context libname ["update", branch_name branch] of
Chris@122 1300 ERROR e => ERROR e
Chris@122 1301 | _ =>
Chris@122 1302 case pull_result of
Chris@122 1303 ERROR e => ERROR e
Chris@125 1304 | _ => OK ()
Chris@122 1305 end
Chris@122 1306
Chris@125 1307 fun update_to context (libname, _, "") =
Chris@122 1308 ERROR "Non-empty id (tag or revision id) required for update_to"
Chris@125 1309 | update_to context (libname, source, id) =
Chris@125 1310 let val pull_result = pull context (libname, source)
Chris@122 1311 in
Chris@122 1312 case hg_command context libname ["update", "-r", id] of
Chris@125 1313 OK _ => OK ()
Chris@122 1314 | ERROR e =>
Chris@122 1315 case pull_result of
Chris@122 1316 ERROR e' => ERROR e' (* this was the ur-error *)
Chris@122 1317 | _ => ERROR e
Chris@122 1318 end
Chris@125 1319
Chris@125 1320 fun copy_url_for context libname =
Chris@125 1321 OK (FileBits.file_url (FileBits.libpath context libname))
Chris@125 1322
Chris@122 1323 end
Chris@122 1324
Chris@122 1325 structure GitControl :> VCS_CONTROL = struct
Chris@122 1326
Chris@122 1327 (* With Git repos we always operate in detached HEAD state. Even
Chris@125 1328 the master branch is checked out using a remote reference
Chris@125 1329 (repoint/master). The remote we use is always named repoint, and we
Chris@125 1330 update it to the expected URL each time we fetch, in order to
Chris@125 1331 ensure we update properly if the location given in the project
Chris@125 1332 file changes. The origin remote is unused. *)
Chris@122 1333
Chris@125 1334 val git_program = "git"
Chris@125 1335
Chris@122 1336 fun git_command context libname args =
Chris@125 1337 FileBits.command context libname (git_program :: args)
Chris@122 1338
Chris@122 1339 fun git_command_output context libname args =
Chris@125 1340 FileBits.command_output context libname (git_program :: args)
Chris@125 1341
Chris@125 1342 fun is_working context =
Chris@125 1343 case git_command_output context "" ["--version"] of
Chris@125 1344 OK "" => OK false
Chris@125 1345 | OK _ => OK true
Chris@125 1346 | ERROR e => ERROR e
Chris@122 1347
Chris@122 1348 fun exists context libname =
Chris@122 1349 OK (OS.FileSys.isDir (FileBits.subpath context libname ".git"))
Chris@122 1350 handle _ => OK false
Chris@122 1351
Chris@122 1352 fun remote_for context (libname, source) =
Chris@122 1353 Provider.remote_url context GIT source libname
Chris@122 1354
Chris@122 1355 fun branch_name branch = case branch of
Chris@122 1356 DEFAULT_BRANCH => "master"
Chris@122 1357 | BRANCH "" => "master"
Chris@122 1358 | BRANCH b => b
Chris@122 1359
Chris@125 1360 val our_remote = "repoint"
Chris@125 1361
Chris@125 1362 fun remote_branch_name branch = our_remote ^ "/" ^ branch_name branch
Chris@122 1363
Chris@122 1364 fun checkout context (libname, source, branch) =
Chris@122 1365 let val url = remote_for context (libname, source)
Chris@122 1366 in
Chris@125 1367 (* make the lib dir rather than just the ext dir, since
Chris@125 1368 the lib dir might be nested and git will happily check
Chris@125 1369 out into an existing empty dir anyway *)
Chris@125 1370 case FileBits.mkpath (FileBits.libpath context libname) of
Chris@122 1371 OK () => git_command context ""
Chris@125 1372 ["clone", "--origin", our_remote,
Chris@125 1373 "--branch", branch_name branch,
Chris@122 1374 url, libname]
Chris@122 1375 | ERROR e => ERROR e
Chris@122 1376 end
Chris@122 1377
Chris@125 1378 fun add_our_remote context (libname, source) =
Chris@125 1379 (* When we do the checkout ourselves (above), we add the
Chris@125 1380 remote at the same time. But if the repo was cloned by
Chris@125 1381 someone else, we'll need to do it after the fact. Git
Chris@125 1382 doesn't seem to have a means to add a remote or change its
Chris@125 1383 url if it already exists; seems we have to do this: *)
Chris@125 1384 let val url = remote_for context (libname, source)
Chris@125 1385 in
Chris@125 1386 case git_command context libname
Chris@125 1387 ["remote", "set-url", our_remote, url] of
Chris@125 1388 OK () => OK ()
Chris@125 1389 | ERROR e => git_command context libname
Chris@125 1390 ["remote", "add", "-f", our_remote, url]
Chris@125 1391 end
Chris@125 1392
Chris@122 1393 (* NB git rev-parse HEAD shows revision id of current checkout;
Chris@122 1394 git rev-list -1 <tag> shows revision id of revision with that tag *)
Chris@122 1395
Chris@122 1396 fun id_of context libname =
Chris@122 1397 git_command_output context libname ["rev-parse", "HEAD"]
Chris@122 1398
Chris@122 1399 fun is_at context (libname, id_or_tag) =
Chris@122 1400 case id_of context libname of
Chris@125 1401 ERROR e => OK false (* HEAD nonexistent, expected in empty repo *)
Chris@122 1402 | OK id =>
Chris@122 1403 if String.isPrefix id_or_tag id orelse
Chris@122 1404 String.isPrefix id id_or_tag
Chris@122 1405 then OK true
Chris@125 1406 else is_at_tag context (libname, id, id_or_tag)
Chris@125 1407
Chris@125 1408 and is_at_tag context (libname, id, tag) =
Chris@125 1409 (* For annotated tags (with message) show-ref returns the tag
Chris@125 1410 object ref rather than that of the revision being tagged;
Chris@125 1411 we need the subsequent rev-list to chase that up. In fact
Chris@125 1412 the rev-list on its own is enough to get us the id direct
Chris@125 1413 from the tag name, but it fails with an error if the tag
Chris@125 1414 doesn't exist, whereas we want to handle that quietly in
Chris@125 1415 case the tag simply hasn't been pulled yet *)
Chris@125 1416 case git_command_output context libname
Chris@125 1417 ["show-ref", "refs/tags/" ^ tag, "--"] of
Chris@125 1418 OK "" => OK false (* Not a tag *)
Chris@125 1419 | ERROR _ => OK false
Chris@125 1420 | OK s =>
Chris@125 1421 let val tag_ref = hd (String.tokens (fn c => c = #" ") s)
Chris@125 1422 in
Chris@122 1423 case git_command_output context libname
Chris@125 1424 ["rev-list", "-1", tag_ref] of
Chris@125 1425 OK tagged => OK (id = tagged)
Chris@122 1426 | ERROR _ => OK false
Chris@125 1427 end
Chris@125 1428
Chris@122 1429 fun branch_tip context (libname, branch) =
Chris@125 1430 (* We don't have access to the source info or the network
Chris@125 1431 here, as this is used by status (e.g. via is_on_branch) as
Chris@125 1432 well as review. It's possible the remote branch won't exist,
Chris@125 1433 e.g. if the repo was checked out by something other than
Chris@125 1434 Repoint, and if that's the case, we can't add it here; we'll
Chris@125 1435 just have to fail, since checking against local branches
Chris@125 1436 instead could produce the wrong result. *)
Chris@122 1437 git_command_output context libname
Chris@122 1438 ["rev-list", "-1",
Chris@125 1439 remote_branch_name branch, "--"]
Chris@122 1440
Chris@122 1441 fun is_newest_locally context (libname, branch) =
Chris@122 1442 case branch_tip context (libname, branch) of
Chris@125 1443 ERROR e => OK false
Chris@122 1444 | OK rev => is_at context (libname, rev)
Chris@122 1445
Chris@122 1446 fun is_on_branch context (libname, branch) =
Chris@122 1447 case branch_tip context (libname, branch) of
Chris@125 1448 ERROR e => OK false
Chris@122 1449 | OK rev =>
Chris@122 1450 case is_at context (libname, rev) of
Chris@122 1451 ERROR e => ERROR e
Chris@122 1452 | OK true => OK true
Chris@122 1453 | OK false =>
Chris@122 1454 case git_command context libname
Chris@122 1455 ["merge-base", "--is-ancestor",
Chris@122 1456 "HEAD", remote_branch_name branch] of
Chris@122 1457 ERROR e => OK false (* cmd returns non-zero for no *)
Chris@122 1458 | _ => OK true
Chris@122 1459
Chris@125 1460 fun fetch context (libname, source) =
Chris@125 1461 case add_our_remote context (libname, source) of
Chris@122 1462 ERROR e => ERROR e
Chris@125 1463 | _ => git_command context libname ["fetch", our_remote]
Chris@125 1464
Chris@125 1465 fun is_newest context (libname, source, branch) =
Chris@125 1466 case add_our_remote context (libname, source) of
Chris@125 1467 ERROR e => ERROR e
Chris@125 1468 | OK () =>
Chris@125 1469 case is_newest_locally context (libname, branch) of
Chris@122 1470 ERROR e => ERROR e
Chris@125 1471 | OK false => OK false
Chris@125 1472 | OK true =>
Chris@125 1473 case fetch context (libname, source) of
Chris@125 1474 ERROR e => ERROR e
Chris@125 1475 | _ => is_newest_locally context (libname, branch)
Chris@122 1476
Chris@122 1477 fun is_modified_locally context libname =
Chris@122 1478 case git_command_output context libname ["status", "--porcelain"] of
Chris@122 1479 ERROR e => ERROR e
Chris@122 1480 | OK "" => OK false
Chris@122 1481 | OK _ => OK true
Chris@122 1482
Chris@122 1483 (* This function updates to the latest revision on a branch rather
Chris@122 1484 than to a specific id or tag. We can't just checkout the given
Chris@122 1485 branch, as that will succeed even if the branch isn't up to
Chris@122 1486 date. We could checkout the branch and then fetch and merge,
Chris@122 1487 but it's perhaps cleaner not to maintain a local branch at all,
Chris@122 1488 but instead checkout the remote branch as a detached head. *)
Chris@122 1489
Chris@125 1490 fun update context (libname, source, branch) =
Chris@125 1491 case fetch context (libname, source) of
Chris@122 1492 ERROR e => ERROR e
Chris@122 1493 | _ =>
Chris@122 1494 case git_command context libname ["checkout", "--detach",
Chris@122 1495 remote_branch_name branch] of
Chris@122 1496 ERROR e => ERROR e
Chris@125 1497 | _ => OK ()
Chris@122 1498
Chris@122 1499 (* This function is dealing with a specific id or tag, so if we
Chris@122 1500 can successfully check it out (detached) then that's all we
Chris@122 1501 need to do, regardless of whether fetch succeeded or not. We do
Chris@122 1502 attempt the fetch first, though, purely in order to avoid ugly
Chris@122 1503 error messages in the common case where we're being asked to
Chris@122 1504 update to a new pin (from the lock file) that hasn't been
Chris@122 1505 fetched yet. *)
Chris@122 1506
Chris@125 1507 fun update_to context (libname, _, "") =
Chris@122 1508 ERROR "Non-empty id (tag or revision id) required for update_to"
Chris@125 1509 | update_to context (libname, source, id) =
Chris@125 1510 let val fetch_result = fetch context (libname, source)
Chris@122 1511 in
Chris@122 1512 case git_command context libname ["checkout", "--detach", id] of
Chris@125 1513 OK _ => OK ()
Chris@122 1514 | ERROR e =>
Chris@122 1515 case fetch_result of
Chris@122 1516 ERROR e' => ERROR e' (* this was the ur-error *)
Chris@122 1517 | _ => ERROR e
Chris@122 1518 end
Chris@125 1519
Chris@125 1520 fun copy_url_for context libname =
Chris@125 1521 OK (FileBits.file_url (FileBits.libpath context libname))
Chris@122 1522
Chris@122 1523 end
Chris@122 1524
Chris@125 1525 (* SubXml - A parser for a subset of XML
Chris@125 1526 https://bitbucket.org/cannam/sml-subxml
Chris@125 1527 Copyright 2018 Chris Cannam. BSD licence.
Chris@125 1528 *)
Chris@125 1529
Chris@125 1530 signature SUBXML = sig
Chris@125 1531
Chris@125 1532 datatype node = ELEMENT of { name : string, children : node list }
Chris@125 1533 | ATTRIBUTE of { name : string, value : string }
Chris@125 1534 | TEXT of string
Chris@125 1535 | CDATA of string
Chris@125 1536 | COMMENT of string
Chris@125 1537
Chris@125 1538 datatype document = DOCUMENT of { name : string, children : node list }
Chris@125 1539
Chris@125 1540 datatype 'a result = OK of 'a
Chris@125 1541 | ERROR of string
Chris@125 1542
Chris@125 1543 val parse : string -> document result
Chris@125 1544 val serialise : document -> string
Chris@125 1545
Chris@125 1546 end
Chris@125 1547
Chris@125 1548 structure SubXml :> SUBXML = struct
Chris@125 1549
Chris@125 1550 datatype node = ELEMENT of { name : string, children : node list }
Chris@125 1551 | ATTRIBUTE of { name : string, value : string }
Chris@125 1552 | TEXT of string
Chris@125 1553 | CDATA of string
Chris@125 1554 | COMMENT of string
Chris@125 1555
Chris@125 1556 datatype document = DOCUMENT of { name : string, children : node list }
Chris@125 1557
Chris@125 1558 datatype 'a result = OK of 'a
Chris@125 1559 | ERROR of string
Chris@125 1560
Chris@125 1561 structure T = struct
Chris@125 1562 datatype token = ANGLE_L
Chris@125 1563 | ANGLE_R
Chris@125 1564 | ANGLE_SLASH_L
Chris@125 1565 | SLASH_ANGLE_R
Chris@125 1566 | EQUAL
Chris@125 1567 | NAME of string
Chris@125 1568 | TEXT of string
Chris@125 1569 | CDATA of string
Chris@125 1570 | COMMENT of string
Chris@125 1571
Chris@125 1572 fun name t =
Chris@125 1573 case t of ANGLE_L => "<"
Chris@125 1574 | ANGLE_R => ">"
Chris@125 1575 | ANGLE_SLASH_L => "</"
Chris@125 1576 | SLASH_ANGLE_R => "/>"
Chris@125 1577 | EQUAL => "="
Chris@125 1578 | NAME s => "name \"" ^ s ^ "\""
Chris@125 1579 | TEXT s => "text"
Chris@125 1580 | CDATA _ => "CDATA section"
Chris@125 1581 | COMMENT _ => "comment"
Chris@125 1582 end
Chris@125 1583
Chris@125 1584 structure Lex :> sig
Chris@125 1585 val lex : string -> T.token list result
Chris@125 1586 end = struct
Chris@125 1587
Chris@125 1588 fun error pos text =
Chris@125 1589 ERROR (text ^ " at character position " ^ Int.toString (pos-1))
Chris@125 1590 fun tokenError pos token =
Chris@125 1591 error pos ("Unexpected token '" ^ Char.toString token ^ "'")
Chris@125 1592
Chris@125 1593 val nameEnd = explode " \t\n\r\"'</>!=?"
Chris@125 1594
Chris@125 1595 fun quoted quote pos acc cc =
Chris@125 1596 let fun quoted' pos text [] =
Chris@125 1597 error pos "Document ends during quoted string"
Chris@125 1598 | quoted' pos text (x::xs) =
Chris@125 1599 if x = quote
Chris@125 1600 then OK (rev text, xs, pos+1)
Chris@125 1601 else quoted' (pos+1) (x::text) xs
Chris@125 1602 in
Chris@125 1603 case quoted' pos [] cc of
Chris@125 1604 ERROR e => ERROR e
Chris@125 1605 | OK (text, rest, newpos) =>
Chris@125 1606 inside newpos (T.TEXT (implode text) :: acc) rest
Chris@125 1607 end
Chris@125 1608
Chris@125 1609 and name first pos acc cc =
Chris@125 1610 let fun name' pos text [] =
Chris@125 1611 error pos "Document ends during name"
Chris@125 1612 | name' pos text (x::xs) =
Chris@125 1613 if List.find (fn c => c = x) nameEnd <> NONE
Chris@125 1614 then OK (rev text, (x::xs), pos)
Chris@125 1615 else name' (pos+1) (x::text) xs
Chris@125 1616 in
Chris@125 1617 case name' (pos-1) [] (first::cc) of
Chris@125 1618 ERROR e => ERROR e
Chris@125 1619 | OK ([], [], pos) => error pos "Document ends before name"
Chris@125 1620 | OK ([], (x::xs), pos) => tokenError pos x
Chris@125 1621 | OK (text, rest, pos) =>
Chris@125 1622 inside pos (T.NAME (implode text) :: acc) rest
Chris@125 1623 end
Chris@125 1624
Chris@125 1625 and comment pos acc cc =
Chris@125 1626 let fun comment' pos text cc =
Chris@125 1627 case cc of
Chris@125 1628 #"-" :: #"-" :: #">" :: xs => OK (rev text, xs, pos+3)
Chris@125 1629 | x :: xs => comment' (pos+1) (x::text) xs
Chris@125 1630 | [] => error pos "Document ends during comment"
Chris@125 1631 in
Chris@125 1632 case comment' pos [] cc of
Chris@125 1633 ERROR e => ERROR e
Chris@125 1634 | OK (text, rest, pos) =>
Chris@125 1635 outside pos (T.COMMENT (implode text) :: acc) rest
Chris@125 1636 end
Chris@125 1637
Chris@125 1638 and instruction pos acc cc =
Chris@125 1639 case cc of
Chris@125 1640 #"?" :: #">" :: xs => outside (pos+2) acc xs
Chris@125 1641 | #">" :: _ => tokenError pos #">"
Chris@125 1642 | x :: xs => instruction (pos+1) acc xs
Chris@125 1643 | [] => error pos "Document ends during processing instruction"
Chris@125 1644
Chris@125 1645 and cdata pos acc cc =
Chris@125 1646 let fun cdata' pos text cc =
Chris@125 1647 case cc of
Chris@125 1648 #"]" :: #"]" :: #">" :: xs => OK (rev text, xs, pos+3)
Chris@125 1649 | x :: xs => cdata' (pos+1) (x::text) xs
Chris@125 1650 | [] => error pos "Document ends during CDATA section"
Chris@125 1651 in
Chris@125 1652 case cdata' pos [] cc of
Chris@125 1653 ERROR e => ERROR e
Chris@125 1654 | OK (text, rest, pos) =>
Chris@125 1655 outside pos (T.CDATA (implode text) :: acc) rest
Chris@125 1656 end
Chris@125 1657
Chris@125 1658 and doctype pos acc cc =
Chris@125 1659 case cc of
Chris@125 1660 #">" :: xs => outside (pos+1) acc xs
Chris@125 1661 | x :: xs => doctype (pos+1) acc xs
Chris@125 1662 | [] => error pos "Document ends during DOCTYPE"
Chris@125 1663
Chris@125 1664 and declaration pos acc cc =
Chris@125 1665 case cc of
Chris@125 1666 #"-" :: #"-" :: xs =>
Chris@125 1667 comment (pos+2) acc xs
Chris@125 1668 | #"[" :: #"C" :: #"D" :: #"A" :: #"T" :: #"A" :: #"[" :: xs =>
Chris@125 1669 cdata (pos+7) acc xs
Chris@125 1670 | #"D" :: #"O" :: #"C" :: #"T" :: #"Y" :: #"P" :: #"E" :: xs =>
Chris@125 1671 doctype (pos+7) acc xs
Chris@125 1672 | [] => error pos "Document ends during declaration"
Chris@125 1673 | _ => error pos "Unsupported declaration type"
Chris@125 1674
Chris@125 1675 and left pos acc cc =
Chris@125 1676 case cc of
Chris@125 1677 #"/" :: xs => inside (pos+1) (T.ANGLE_SLASH_L :: acc) xs
Chris@125 1678 | #"!" :: xs => declaration (pos+1) acc xs
Chris@125 1679 | #"?" :: xs => instruction (pos+1) acc xs
Chris@125 1680 | xs => inside pos (T.ANGLE_L :: acc) xs
Chris@125 1681
Chris@125 1682 and slash pos acc cc =
Chris@125 1683 case cc of
Chris@125 1684 #">" :: xs => outside (pos+1) (T.SLASH_ANGLE_R :: acc) xs
Chris@125 1685 | x :: _ => tokenError pos x
Chris@125 1686 | [] => error pos "Document ends before element closed"
Chris@125 1687
Chris@125 1688 and close pos acc xs = outside pos (T.ANGLE_R :: acc) xs
Chris@125 1689
Chris@125 1690 and equal pos acc xs = inside pos (T.EQUAL :: acc) xs
Chris@125 1691
Chris@125 1692 and outside pos acc [] = OK acc
Chris@125 1693 | outside pos acc cc =
Chris@125 1694 let fun textOf text = T.TEXT (implode (rev text))
Chris@125 1695 fun outside' pos [] acc [] = OK acc
Chris@125 1696 | outside' pos text acc [] = OK (textOf text :: acc)
Chris@125 1697 | outside' pos text acc (x::xs) =
Chris@125 1698 case x of
Chris@125 1699 #"<" => if text = []
Chris@125 1700 then left (pos+1) acc xs
Chris@125 1701 else left (pos+1) (textOf text :: acc) xs
Chris@125 1702 | x => outside' (pos+1) (x::text) acc xs
Chris@125 1703 in
Chris@125 1704 outside' pos [] acc cc
Chris@125 1705 end
Chris@125 1706
Chris@125 1707 and inside pos acc [] = error pos "Document ends within tag"
Chris@125 1708 | inside pos acc (#"<"::_) = tokenError pos #"<"
Chris@125 1709 | inside pos acc (x::xs) =
Chris@125 1710 (case x of
Chris@125 1711 #" " => inside | #"\t" => inside
Chris@125 1712 | #"\n" => inside | #"\r" => inside
Chris@125 1713 | #"\"" => quoted x | #"'" => quoted x
Chris@125 1714 | #"/" => slash | #">" => close | #"=" => equal
Chris@125 1715 | x => name x) (pos+1) acc xs
Chris@125 1716
Chris@125 1717 fun lex str =
Chris@125 1718 case outside 1 [] (explode str) of
Chris@125 1719 ERROR e => ERROR e
Chris@125 1720 | OK tokens => OK (rev tokens)
Chris@125 1721 end
Chris@125 1722
Chris@125 1723 structure Parse :> sig
Chris@125 1724 val parse : string -> document result
Chris@125 1725 end = struct
Chris@125 1726
Chris@125 1727 fun show [] = "end of input"
Chris@125 1728 | show (tok :: _) = T.name tok
Chris@125 1729
Chris@125 1730 fun error toks text = ERROR (text ^ " before " ^ show toks)
Chris@125 1731
Chris@125 1732 fun attribute elt name toks =
Chris@125 1733 case toks of
Chris@125 1734 T.EQUAL :: T.TEXT value :: xs =>
Chris@125 1735 namedElement {
Chris@125 1736 name = #name elt,
Chris@125 1737 children = ATTRIBUTE { name = name, value = value } ::
Chris@125 1738 #children elt
Chris@125 1739 } xs
Chris@125 1740 | T.EQUAL :: xs => error xs "Expected attribute value"
Chris@125 1741 | toks => error toks "Expected attribute assignment"
Chris@125 1742
Chris@125 1743 and content elt toks =
Chris@125 1744 case toks of
Chris@125 1745 T.ANGLE_SLASH_L :: T.NAME n :: T.ANGLE_R :: xs =>
Chris@125 1746 if n = #name elt
Chris@125 1747 then OK (elt, xs)
Chris@125 1748 else ERROR ("Closing tag </" ^ n ^ "> " ^
Chris@125 1749 "does not match opening <" ^ #name elt ^ ">")
Chris@125 1750 | T.TEXT text :: xs =>
Chris@125 1751 content {
Chris@125 1752 name = #name elt,
Chris@125 1753 children = TEXT text :: #children elt
Chris@125 1754 } xs
Chris@125 1755 | T.CDATA text :: xs =>
Chris@125 1756 content {
Chris@125 1757 name = #name elt,
Chris@125 1758 children = CDATA text :: #children elt
Chris@125 1759 } xs
Chris@125 1760 | T.COMMENT text :: xs =>
Chris@125 1761 content {
Chris@125 1762 name = #name elt,
Chris@125 1763 children = COMMENT text :: #children elt
Chris@125 1764 } xs
Chris@125 1765 | T.ANGLE_L :: xs =>
Chris@125 1766 (case element xs of
Chris@125 1767 ERROR e => ERROR e
Chris@125 1768 | OK (child, xs) =>
Chris@125 1769 content {
Chris@125 1770 name = #name elt,
Chris@125 1771 children = ELEMENT child :: #children elt
Chris@125 1772 } xs)
Chris@125 1773 | tok :: xs =>
Chris@125 1774 error xs ("Unexpected token " ^ T.name tok)
Chris@125 1775 | [] =>
Chris@125 1776 ERROR ("Document ends within element \"" ^ #name elt ^ "\"")
Chris@125 1777
Chris@125 1778 and namedElement elt toks =
Chris@125 1779 case toks of
Chris@125 1780 T.SLASH_ANGLE_R :: xs => OK (elt, xs)
Chris@125 1781 | T.NAME name :: xs => attribute elt name xs
Chris@125 1782 | T.ANGLE_R :: xs => content elt xs
Chris@125 1783 | x :: xs => error xs ("Unexpected token " ^ T.name x)
Chris@125 1784 | [] => ERROR "Document ends within opening tag"
Chris@125 1785
Chris@125 1786 and element toks =
Chris@125 1787 case toks of
Chris@125 1788 T.NAME name :: xs =>
Chris@125 1789 (case namedElement { name = name, children = [] } xs of
Chris@125 1790 ERROR e => ERROR e
Chris@125 1791 | OK ({ name, children }, xs) =>
Chris@125 1792 OK ({ name = name, children = rev children }, xs))
Chris@125 1793 | toks => error toks "Expected element name"
Chris@125 1794
Chris@125 1795 and document [] = ERROR "Empty document"
Chris@125 1796 | document (tok :: xs) =
Chris@125 1797 case tok of
Chris@125 1798 T.TEXT _ => document xs
Chris@125 1799 | T.COMMENT _ => document xs
Chris@125 1800 | T.ANGLE_L =>
Chris@125 1801 (case element xs of
Chris@125 1802 ERROR e => ERROR e
Chris@125 1803 | OK (elt, []) => OK (DOCUMENT elt)
Chris@125 1804 | OK (elt, (T.TEXT _ :: xs)) => OK (DOCUMENT elt)
Chris@125 1805 | OK (elt, xs) => error xs "Extra data after document")
Chris@125 1806 | _ => error xs ("Unexpected token " ^ T.name tok)
Chris@125 1807
Chris@125 1808 fun parse str =
Chris@125 1809 case Lex.lex str of
Chris@125 1810 ERROR e => ERROR e
Chris@125 1811 | OK tokens => document tokens
Chris@125 1812 end
Chris@125 1813
Chris@125 1814 structure Serialise :> sig
Chris@125 1815 val serialise : document -> string
Chris@125 1816 end = struct
Chris@125 1817
Chris@125 1818 fun attributes nodes =
Chris@125 1819 String.concatWith
Chris@125 1820 " "
Chris@125 1821 (map node (List.filter
Chris@125 1822 (fn ATTRIBUTE _ => true | _ => false)
Chris@125 1823 nodes))
Chris@125 1824
Chris@125 1825 and nonAttributes nodes =
Chris@125 1826 String.concat
Chris@125 1827 (map node (List.filter
Chris@125 1828 (fn ATTRIBUTE _ => false | _ => true)
Chris@125 1829 nodes))
Chris@125 1830
Chris@125 1831 and node n =
Chris@125 1832 case n of
Chris@125 1833 TEXT string =>
Chris@125 1834 string
Chris@125 1835 | CDATA string =>
Chris@125 1836 "<![CDATA[" ^ string ^ "]]>"
Chris@125 1837 | COMMENT string =>
Chris@125 1838 "<!-- " ^ string ^ "-->"
Chris@125 1839 | ATTRIBUTE { name, value } =>
Chris@125 1840 name ^ "=" ^ "\"" ^ value ^ "\"" (*!!!*)
Chris@125 1841 | ELEMENT { name, children } =>
Chris@125 1842 "<" ^ name ^
Chris@125 1843 (case (attributes children) of
Chris@125 1844 "" => ""
Chris@125 1845 | s => " " ^ s) ^
Chris@125 1846 (case (nonAttributes children) of
Chris@125 1847 "" => "/>"
Chris@125 1848 | s => ">" ^ s ^ "</" ^ name ^ ">")
Chris@125 1849
Chris@125 1850 fun serialise (DOCUMENT { name, children }) =
Chris@125 1851 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" ^
Chris@125 1852 node (ELEMENT { name = name, children = children })
Chris@125 1853 end
Chris@125 1854
Chris@125 1855 val parse = Parse.parse
Chris@125 1856 val serialise = Serialise.serialise
Chris@125 1857
Chris@125 1858 end
Chris@125 1859
Chris@125 1860
Chris@125 1861 structure SvnControl :> VCS_CONTROL = struct
Chris@125 1862
Chris@125 1863 val svn_program = "svn"
Chris@125 1864
Chris@125 1865 fun svn_command context libname args =
Chris@125 1866 FileBits.command context libname (svn_program :: args)
Chris@125 1867
Chris@125 1868 fun svn_command_output context libname args =
Chris@125 1869 FileBits.command_output context libname (svn_program :: args)
Chris@125 1870
Chris@125 1871 fun svn_command_lines context libname args =
Chris@125 1872 case svn_command_output context libname args of
Chris@125 1873 ERROR e => ERROR e
Chris@125 1874 | OK s => OK (String.tokens (fn c => c = #"\n" orelse c = #"\r") s)
Chris@125 1875
Chris@125 1876 fun split_line_pair line =
Chris@125 1877 let fun strip_leading_ws str = case explode str of
Chris@125 1878 #" "::rest => implode rest
Chris@125 1879 | _ => str
Chris@125 1880 in
Chris@125 1881 case String.tokens (fn c => c = #":") line of
Chris@125 1882 [] => ("", "")
Chris@125 1883 | first::rest =>
Chris@125 1884 (first, strip_leading_ws (String.concatWith ":" rest))
Chris@125 1885 end
Chris@125 1886
Chris@125 1887 fun is_working context =
Chris@125 1888 case svn_command_output context "" ["--version"] of
Chris@125 1889 OK "" => OK false
Chris@125 1890 | OK _ => OK true
Chris@125 1891 | ERROR e => ERROR e
Chris@125 1892
Chris@125 1893 structure X = SubXml
Chris@125 1894
Chris@125 1895 fun svn_info context libname route =
Chris@125 1896 (* SVN 1.9 has info --show-item which is just what we need,
Chris@125 1897 but at this point we still have 1.8 on the CI boxes so we
Chris@125 1898 might as well aim to support it. For that we really have to
Chris@125 1899 use the XML output format, since the default info output is
Chris@125 1900 localised. This is the only thing our mini-XML parser is
Chris@125 1901 used for though, so it would be good to trim it at some
Chris@125 1902 point *)
Chris@125 1903 let fun find elt [] = OK elt
Chris@125 1904 | find { children, ... } (first :: rest) =
Chris@125 1905 case List.find (fn (X.ELEMENT { name, ... }) => name = first
Chris@125 1906 | _ => false)
Chris@125 1907 children of
Chris@125 1908 NONE => ERROR ("No element \"" ^ first ^ "\" in SVN XML")
Chris@125 1909 | SOME (X.ELEMENT e) => find e rest
Chris@125 1910 | SOME _ => ERROR "Internal error"
Chris@125 1911 in
Chris@125 1912 case svn_command_output context libname ["info", "--xml"] of
Chris@125 1913 ERROR e => ERROR e
Chris@125 1914 | OK xml =>
Chris@125 1915 case X.parse xml of
Chris@125 1916 X.ERROR e => ERROR e
Chris@125 1917 | X.OK (X.DOCUMENT doc) => find doc route
Chris@125 1918 end
Chris@125 1919
Chris@125 1920 fun exists context libname =
Chris@125 1921 OK (OS.FileSys.isDir (FileBits.subpath context libname ".svn"))
Chris@125 1922 handle _ => OK false
Chris@125 1923
Chris@125 1924 fun remote_for context (libname, source) =
Chris@125 1925 Provider.remote_url context SVN source libname
Chris@125 1926
Chris@125 1927 (* Remote the checkout came from, not necessarily the one we want *)
Chris@125 1928 fun actual_remote_for context libname =
Chris@125 1929 case svn_info context libname ["entry", "url"] of
Chris@125 1930 ERROR e => ERROR e
Chris@125 1931 | OK { children, ... } =>
Chris@125 1932 case List.find (fn (X.TEXT _) => true | _ => false) children of
Chris@125 1933 NONE => ERROR "No content for URL in SVN info XML"
Chris@125 1934 | SOME (X.TEXT url) => OK url
Chris@125 1935 | SOME _ => ERROR "Internal error"
Chris@125 1936
Chris@125 1937 fun id_of context libname =
Chris@125 1938 case svn_info context libname ["entry"] of
Chris@125 1939 ERROR e => ERROR e
Chris@125 1940 | OK { children, ... } =>
Chris@125 1941 case List.find
Chris@125 1942 (fn (X.ATTRIBUTE { name = "revision", ... }) => true
Chris@125 1943 | _ => false)
Chris@125 1944 children of
Chris@125 1945 NONE => ERROR "No revision for entry in SVN info XML"
Chris@125 1946 | SOME (X.ATTRIBUTE { value, ... }) => OK value
Chris@125 1947 | SOME _ => ERROR "Internal error"
Chris@125 1948
Chris@125 1949 fun is_at context (libname, id_or_tag) =
Chris@125 1950 case id_of context libname of
Chris@125 1951 ERROR e => ERROR e
Chris@125 1952 | OK id => OK (id = id_or_tag)
Chris@125 1953
Chris@125 1954 fun is_on_branch context (libname, b) =
Chris@125 1955 OK (b = DEFAULT_BRANCH)
Chris@125 1956
Chris@125 1957 fun check_remote context (libname, source) =
Chris@125 1958 case (remote_for context (libname, source),
Chris@125 1959 actual_remote_for context libname) of
Chris@125 1960 (_, ERROR e) => ERROR e
Chris@125 1961 | (url, OK actual) =>
Chris@125 1962 if actual = url
Chris@125 1963 then OK ()
Chris@125 1964 else svn_command context libname ["relocate", url]
Chris@125 1965
Chris@125 1966 fun is_newest context (libname, source, branch) =
Chris@125 1967 case check_remote context (libname, source) of
Chris@125 1968 ERROR e => ERROR e
Chris@125 1969 | OK () =>
Chris@125 1970 case svn_command_lines context libname
Chris@125 1971 ["status", "--show-updates"] of
Chris@125 1972 ERROR e => ERROR e
Chris@125 1973 | OK lines =>
Chris@125 1974 case rev lines of
Chris@125 1975 [] => ERROR "No result returned for server status"
Chris@125 1976 | last_line::_ =>
Chris@125 1977 case rev (String.tokens (fn c => c = #" ") last_line) of
Chris@125 1978 [] => ERROR "No revision field found in server status"
Chris@125 1979 | server_id::_ => is_at context (libname, server_id)
Chris@125 1980
Chris@125 1981 fun is_newest_locally context (libname, branch) =
Chris@125 1982 OK true (* no local history *)
Chris@125 1983
Chris@125 1984 fun is_modified_locally context libname =
Chris@125 1985 case svn_command_output context libname ["status"] of
Chris@125 1986 ERROR e => ERROR e
Chris@125 1987 | OK "" => OK false
Chris@125 1988 | OK _ => OK true
Chris@125 1989
Chris@125 1990 fun checkout context (libname, source, branch) =
Chris@125 1991 let val url = remote_for context (libname, source)
Chris@125 1992 val path = FileBits.libpath context libname
Chris@125 1993 in
Chris@125 1994 if FileBits.nonempty_dir_exists path
Chris@125 1995 then (* Surprisingly, SVN itself has no problem with
Chris@125 1996 this. But for consistency with other VCSes we
Chris@125 1997 don't allow it *)
Chris@125 1998 ERROR ("Refusing checkout to nonempty dir \"" ^ path ^ "\"")
Chris@125 1999 else
Chris@125 2000 (* make the lib dir rather than just the ext dir, since
Chris@125 2001 the lib dir might be nested and svn will happily check
Chris@125 2002 out into an existing empty dir anyway *)
Chris@125 2003 case FileBits.mkpath (FileBits.libpath context libname) of
Chris@125 2004 ERROR e => ERROR e
Chris@125 2005 | _ => svn_command context "" ["checkout", url, libname]
Chris@125 2006 end
Chris@125 2007
Chris@125 2008 fun update context (libname, source, branch) =
Chris@125 2009 case check_remote context (libname, source) of
Chris@125 2010 ERROR e => ERROR e
Chris@125 2011 | OK () =>
Chris@125 2012 case svn_command context libname
Chris@125 2013 ["update", "--accept", "postpone"] of
Chris@125 2014 ERROR e => ERROR e
Chris@125 2015 | _ => OK ()
Chris@125 2016
Chris@125 2017 fun update_to context (libname, _, "") =
Chris@125 2018 ERROR "Non-empty id (tag or revision id) required for update_to"
Chris@125 2019 | update_to context (libname, source, id) =
Chris@125 2020 case check_remote context (libname, source) of
Chris@125 2021 ERROR e => ERROR e
Chris@125 2022 | OK () =>
Chris@125 2023 case svn_command context libname
Chris@125 2024 ["update", "-r", id, "--accept", "postpone"] of
Chris@125 2025 ERROR e => ERROR e
Chris@125 2026 | OK _ => OK ()
Chris@125 2027
Chris@125 2028 fun copy_url_for context libname =
Chris@125 2029 actual_remote_for context libname
Chris@125 2030
Chris@125 2031 end
Chris@125 2032
Chris@122 2033 structure AnyLibControl :> LIB_CONTROL = struct
Chris@122 2034
Chris@122 2035 structure H = LibControlFn(HgControl)
Chris@122 2036 structure G = LibControlFn(GitControl)
Chris@125 2037 structure S = LibControlFn(SvnControl)
Chris@122 2038
Chris@122 2039 fun review context (spec as { vcs, ... } : libspec) =
Chris@125 2040 (fn HG => H.review | GIT => G.review | SVN => S.review) vcs context spec
Chris@122 2041
Chris@122 2042 fun status context (spec as { vcs, ... } : libspec) =
Chris@125 2043 (fn HG => H.status | GIT => G.status | SVN => S.status) vcs context spec
Chris@122 2044
Chris@122 2045 fun update context (spec as { vcs, ... } : libspec) =
Chris@125 2046 (fn HG => H.update | GIT => G.update | SVN => S.update) vcs context spec
Chris@122 2047
Chris@122 2048 fun id_of context (spec as { vcs, ... } : libspec) =
Chris@125 2049 (fn HG => H.id_of | GIT => G.id_of | SVN => S.id_of) vcs context spec
Chris@125 2050
Chris@125 2051 fun is_working context vcs =
Chris@125 2052 (fn HG => H.is_working | GIT => G.is_working | SVN => S.is_working)
Chris@125 2053 vcs context vcs
Chris@125 2054
Chris@122 2055 end
Chris@122 2056
Chris@124 2057
Chris@124 2058 type exclusions = string list
Chris@124 2059
Chris@124 2060 structure Archive :> sig
Chris@124 2061
Chris@124 2062 val archive : string * exclusions -> project -> OS.Process.status
Chris@124 2063
Chris@124 2064 end = struct
Chris@124 2065
Chris@124 2066 (* The idea of "archive" is to replace hg/git archive, which won't
Chris@125 2067 include files, like the Repoint-introduced external libraries,
Chris@124 2068 that are not under version control with the main repo.
Chris@124 2069
Chris@124 2070 The process goes like this:
Chris@124 2071
Chris@124 2072 - Make sure we have a target filename from the user, and take
Chris@124 2073 its basename as our archive directory name
Chris@124 2074
Chris@124 2075 - Make an "archive root" subdir of the project repo, named
Chris@125 2076 typically .repoint-archive
Chris@124 2077
Chris@124 2078 - Identify the VCS used for the project repo. Note that any
Chris@124 2079 explicit references to VCS type in this structure are to
Chris@125 2080 the VCS used for the project (something Repoint doesn't
Chris@124 2081 otherwise care about), not for an individual library
Chris@124 2082
Chris@125 2083 - Synthesise a Repoint project with the archive root as its
Chris@124 2084 root path, "." as its extdir, with one library whose
Chris@124 2085 name is the user-supplied basename and whose explicit
Chris@124 2086 source URL is the original project root; update that
Chris@124 2087 project -- thus cloning the original project to a subdir
Chris@124 2088 of the archive root
Chris@124 2089
Chris@125 2090 - Synthesise a Repoint project identical to the original one for
Chris@124 2091 this project, but with the newly-cloned copy as its root
Chris@124 2092 path; update that project -- thus checking out clean copies
Chris@124 2093 of the external library dirs
Chris@124 2094
Chris@124 2095 - Call out to an archive program to archive up the new copy,
Chris@124 2096 running e.g.
Chris@124 2097 tar cvzf project-release.tar.gz \
Chris@124 2098 --exclude=.hg --exclude=.git project-release
Chris@124 2099 in the archive root dir
Chris@124 2100
Chris@125 2101 - (We also omit the repoint-project.json file and any trace of
Chris@125 2102 Repoint. It can't properly be run in a directory where the
Chris@124 2103 external project folders already exist but their repo history
Chris@125 2104 does not. End users shouldn't get to see Repoint)
Chris@124 2105
Chris@124 2106 - Clean up by deleting the new copy
Chris@124 2107 *)
Chris@124 2108
Chris@125 2109 fun project_vcs_id_and_url dir =
Chris@124 2110 let val context = {
Chris@124 2111 rootpath = dir,
Chris@124 2112 extdir = ".",
Chris@124 2113 providers = [],
Chris@124 2114 accounts = []
Chris@124 2115 }
Chris@124 2116 val vcs_maybe =
Chris@124 2117 case [HgControl.exists context ".",
Chris@125 2118 GitControl.exists context ".",
Chris@125 2119 SvnControl.exists context "."] of
Chris@125 2120 [OK true, OK false, OK false] => OK HG
Chris@125 2121 | [OK false, OK true, OK false] => OK GIT
Chris@125 2122 | [OK false, OK false, OK true] => OK SVN
Chris@124 2123 | _ => ERROR ("Unable to identify VCS for directory " ^ dir)
Chris@124 2124 in
Chris@124 2125 case vcs_maybe of
Chris@124 2126 ERROR e => ERROR e
Chris@124 2127 | OK vcs =>
Chris@125 2128 case (fn HG => HgControl.id_of
Chris@125 2129 | GIT => GitControl.id_of
Chris@125 2130 | SVN => SvnControl.id_of)
Chris@124 2131 vcs context "." of
Chris@125 2132 ERROR e => ERROR ("Unable to find id of project repo: " ^ e)
Chris@125 2133 | OK id =>
Chris@125 2134 case (fn HG => HgControl.copy_url_for
Chris@125 2135 | GIT => GitControl.copy_url_for
Chris@125 2136 | SVN => SvnControl.copy_url_for)
Chris@125 2137 vcs context "." of
Chris@125 2138 ERROR e => ERROR ("Unable to find URL of project repo: "
Chris@125 2139 ^ e)
Chris@125 2140 | OK url => OK (vcs, id, url)
Chris@124 2141 end
Chris@124 2142
Chris@124 2143 fun make_archive_root (context : context) =
Chris@124 2144 let val path = OS.Path.joinDirFile {
Chris@124 2145 dir = #rootpath context,
Chris@125 2146 file = RepointFilenames.archive_dir
Chris@124 2147 }
Chris@124 2148 in
Chris@124 2149 case FileBits.mkpath path of
Chris@124 2150 ERROR e => raise Fail ("Failed to create archive directory \""
Chris@124 2151 ^ path ^ "\": " ^ e)
Chris@124 2152 | OK () => path
Chris@124 2153 end
Chris@124 2154
Chris@124 2155 fun archive_path archive_dir target_name =
Chris@124 2156 OS.Path.joinDirFile {
Chris@124 2157 dir = archive_dir,
Chris@124 2158 file = target_name
Chris@124 2159 }
Chris@124 2160
Chris@124 2161 fun check_nonexistent path =
Chris@124 2162 case SOME (OS.FileSys.fileSize path) handle OS.SysErr _ => NONE of
Chris@124 2163 NONE => ()
Chris@124 2164 | _ => raise Fail ("Path " ^ path ^ " exists, not overwriting")
Chris@124 2165
Chris@125 2166 fun make_archive_copy target_name (vcs, project_id, source_url)
Chris@124 2167 ({ context, ... } : project) =
Chris@124 2168 let val archive_root = make_archive_root context
Chris@124 2169 val synthetic_context = {
Chris@124 2170 rootpath = archive_root,
Chris@124 2171 extdir = ".",
Chris@124 2172 providers = [],
Chris@124 2173 accounts = []
Chris@124 2174 }
Chris@124 2175 val synthetic_library = {
Chris@124 2176 libname = target_name,
Chris@124 2177 vcs = vcs,
Chris@125 2178 source = URL_SOURCE source_url,
Chris@124 2179 branch = DEFAULT_BRANCH, (* overridden by pinned id below *)
Chris@124 2180 project_pin = PINNED project_id,
Chris@124 2181 lock_pin = PINNED project_id
Chris@124 2182 }
Chris@124 2183 val path = archive_path archive_root target_name
Chris@124 2184 val _ = print ("Cloning original project to " ^ path
Chris@124 2185 ^ " at revision " ^ project_id ^ "...\n");
Chris@124 2186 val _ = check_nonexistent path
Chris@124 2187 in
Chris@124 2188 case AnyLibControl.update synthetic_context synthetic_library of
Chris@124 2189 ERROR e => ERROR ("Failed to clone original project to "
Chris@124 2190 ^ path ^ ": " ^ e)
Chris@124 2191 | OK _ => OK archive_root
Chris@124 2192 end
Chris@124 2193
Chris@124 2194 fun update_archive archive_root target_name
Chris@124 2195 (project as { context, ... } : project) =
Chris@124 2196 let val synthetic_context = {
Chris@124 2197 rootpath = archive_path archive_root target_name,
Chris@124 2198 extdir = #extdir context,
Chris@124 2199 providers = #providers context,
Chris@124 2200 accounts = #accounts context
Chris@124 2201 }
Chris@124 2202 in
Chris@124 2203 foldl (fn (lib, acc) =>
Chris@124 2204 case acc of
Chris@124 2205 ERROR e => ERROR e
Chris@125 2206 | OK () => AnyLibControl.update synthetic_context lib)
Chris@125 2207 (OK ())
Chris@124 2208 (#libs project)
Chris@124 2209 end
Chris@124 2210
Chris@124 2211 datatype packer = TAR
Chris@124 2212 | TAR_GZ
Chris@124 2213 | TAR_BZ2
Chris@124 2214 | TAR_XZ
Chris@124 2215 (* could add other packers, e.g. zip, if we knew how to
Chris@124 2216 handle the file omissions etc properly in pack_archive *)
Chris@124 2217
Chris@124 2218 fun packer_and_basename path =
Chris@124 2219 let val extensions = [ (".tar", TAR),
Chris@124 2220 (".tar.gz", TAR_GZ),
Chris@124 2221 (".tar.bz2", TAR_BZ2),
Chris@124 2222 (".tar.xz", TAR_XZ)]
Chris@124 2223 val filename = OS.Path.file path
Chris@124 2224 in
Chris@124 2225 foldl (fn ((ext, packer), acc) =>
Chris@124 2226 if String.isSuffix ext filename
Chris@124 2227 then SOME (packer,
Chris@124 2228 String.substring (filename, 0,
Chris@124 2229 String.size filename -
Chris@124 2230 String.size ext))
Chris@124 2231 else acc)
Chris@124 2232 NONE
Chris@124 2233 extensions
Chris@124 2234 end
Chris@124 2235
Chris@124 2236 fun pack_archive archive_root target_name target_path packer exclusions =
Chris@124 2237 case FileBits.command {
Chris@124 2238 rootpath = archive_root,
Chris@124 2239 extdir = ".",
Chris@124 2240 providers = [],
Chris@124 2241 accounts = []
Chris@124 2242 } "" ([
Chris@124 2243 "tar",
Chris@124 2244 case packer of
Chris@124 2245 TAR => "cf"
Chris@124 2246 | TAR_GZ => "czf"
Chris@124 2247 | TAR_BZ2 => "cjf"
Chris@124 2248 | TAR_XZ => "cJf",
Chris@124 2249 target_path,
Chris@124 2250 "--exclude=.hg",
Chris@124 2251 "--exclude=.git",
Chris@125 2252 "--exclude=.svn",
Chris@125 2253 "--exclude=repoint",
Chris@125 2254 "--exclude=repoint.sml",
Chris@125 2255 "--exclude=repoint.ps1",
Chris@125 2256 "--exclude=repoint.bat",
Chris@125 2257 "--exclude=repoint-project.json",
Chris@125 2258 "--exclude=repoint-lock.json"
Chris@124 2259 ] @ (map (fn e => "--exclude=" ^ e) exclusions) @
Chris@124 2260 [ target_name ])
Chris@124 2261 of
Chris@124 2262 ERROR e => ERROR e
Chris@124 2263 | OK _ => FileBits.rmpath (archive_path archive_root target_name)
Chris@124 2264
Chris@124 2265 fun archive (target_path, exclusions) (project : project) =
Chris@124 2266 let val _ = check_nonexistent target_path
Chris@124 2267 val (packer, name) =
Chris@124 2268 case packer_and_basename target_path of
Chris@124 2269 NONE => raise Fail ("Unsupported archive file extension in "
Chris@124 2270 ^ target_path)
Chris@124 2271 | SOME pn => pn
Chris@124 2272 val details =
Chris@125 2273 case project_vcs_id_and_url (#rootpath (#context project)) of
Chris@124 2274 ERROR e => raise Fail e
Chris@124 2275 | OK details => details
Chris@124 2276 val archive_root =
Chris@124 2277 case make_archive_copy name details project of
Chris@124 2278 ERROR e => raise Fail e
Chris@124 2279 | OK archive_root => archive_root
Chris@124 2280 val outcome =
Chris@124 2281 case update_archive archive_root name project of
Chris@124 2282 ERROR e => ERROR e
Chris@124 2283 | OK _ =>
Chris@124 2284 case pack_archive archive_root name
Chris@124 2285 target_path packer exclusions of
Chris@124 2286 ERROR e => ERROR e
Chris@124 2287 | OK _ => OK ()
Chris@124 2288 in
Chris@124 2289 case outcome of
Chris@124 2290 ERROR e => raise Fail e
Chris@124 2291 | OK () => OS.Process.success
Chris@124 2292 end
Chris@124 2293
Chris@124 2294 end
Chris@124 2295
Chris@122 2296 val libobjname = "libraries"
Chris@122 2297
Chris@122 2298 fun load_libspec spec_json lock_json libname : libspec =
Chris@122 2299 let open JsonBits
Chris@122 2300 val libobj = lookup_mandatory spec_json [libobjname, libname]
Chris@122 2301 val vcs = lookup_mandatory_string libobj ["vcs"]
Chris@122 2302 val retrieve = lookup_optional_string libobj
Chris@122 2303 val service = retrieve ["service"]
Chris@122 2304 val owner = retrieve ["owner"]
Chris@122 2305 val repo = retrieve ["repository"]
Chris@122 2306 val url = retrieve ["url"]
Chris@122 2307 val branch = retrieve ["branch"]
Chris@122 2308 val project_pin = case retrieve ["pin"] of
Chris@122 2309 NONE => UNPINNED
Chris@122 2310 | SOME p => PINNED p
Chris@122 2311 val lock_pin = case lookup_optional lock_json [libobjname, libname] of
Chris@122 2312 NONE => UNPINNED
Chris@122 2313 | SOME ll => case lookup_optional_string ll ["pin"] of
Chris@122 2314 SOME p => PINNED p
Chris@122 2315 | NONE => UNPINNED
Chris@122 2316 in
Chris@122 2317 {
Chris@122 2318 libname = libname,
Chris@122 2319 vcs = case vcs of
Chris@122 2320 "hg" => HG
Chris@122 2321 | "git" => GIT
Chris@125 2322 | "svn" => SVN
Chris@122 2323 | other => raise Fail ("Unknown version-control system \"" ^
Chris@122 2324 other ^ "\""),
Chris@122 2325 source = case (url, service, owner, repo) of
Chris@122 2326 (SOME u, NONE, _, _) => URL_SOURCE u
Chris@122 2327 | (NONE, SOME ss, owner, repo) =>
Chris@122 2328 SERVICE_SOURCE { service = ss, owner = owner, repo = repo }
Chris@122 2329 | _ => raise Fail ("Must have exactly one of service " ^
Chris@122 2330 "or url string"),
Chris@122 2331 project_pin = project_pin,
Chris@122 2332 lock_pin = lock_pin,
Chris@122 2333 branch = case branch of
Chris@125 2334 NONE => DEFAULT_BRANCH
Chris@125 2335 | SOME b =>
Chris@125 2336 case vcs of
Chris@125 2337 "svn" => raise Fail ("Branches not supported for " ^
Chris@125 2338 "svn repositories; change " ^
Chris@125 2339 "URL instead")
Chris@125 2340 | _ => BRANCH b
Chris@122 2341 }
Chris@122 2342 end
Chris@122 2343
Chris@122 2344 fun load_userconfig () : userconfig =
Chris@122 2345 let val home = FileBits.homedir ()
Chris@122 2346 val conf_json =
Chris@122 2347 JsonBits.load_json_from
Chris@122 2348 (OS.Path.joinDirFile {
Chris@122 2349 dir = home,
Chris@125 2350 file = RepointFilenames.user_config_file })
Chris@122 2351 handle IO.Io _ => Json.OBJECT []
Chris@122 2352 in
Chris@122 2353 {
Chris@122 2354 accounts = case JsonBits.lookup_optional conf_json ["accounts"] of
Chris@122 2355 NONE => []
Chris@122 2356 | SOME (Json.OBJECT aa) =>
Chris@122 2357 map (fn (k, (Json.STRING v)) =>
Chris@122 2358 { service = k, login = v }
Chris@122 2359 | _ => raise Fail
Chris@122 2360 "String expected for account name")
Chris@122 2361 aa
Chris@122 2362 | _ => raise Fail "Array expected for accounts",
Chris@122 2363 providers = Provider.load_providers conf_json
Chris@122 2364 }
Chris@122 2365 end
Chris@122 2366
Chris@122 2367 datatype pintype =
Chris@122 2368 NO_LOCKFILE |
Chris@122 2369 USE_LOCKFILE
Chris@122 2370
Chris@122 2371 fun load_project (userconfig : userconfig) rootpath pintype : project =
Chris@122 2372 let val spec_file = FileBits.project_spec_path rootpath
Chris@122 2373 val lock_file = FileBits.project_lock_path rootpath
Chris@122 2374 val _ = if OS.FileSys.access (spec_file, [OS.FileSys.A_READ])
Chris@122 2375 handle OS.SysErr _ => false
Chris@122 2376 then ()
Chris@122 2377 else raise Fail ("Failed to open project spec file " ^
Chris@125 2378 (RepointFilenames.project_file) ^ " in " ^
Chris@122 2379 rootpath ^
Chris@122 2380 ".\nPlease ensure the spec file is in the " ^
Chris@122 2381 "project root and run this from there.")
Chris@122 2382 val spec_json = JsonBits.load_json_from spec_file
Chris@122 2383 val lock_json = if pintype = USE_LOCKFILE
Chris@122 2384 then JsonBits.load_json_from lock_file
Chris@122 2385 handle IO.Io _ => Json.OBJECT []
Chris@122 2386 else Json.OBJECT []
Chris@122 2387 val extdir = JsonBits.lookup_mandatory_string spec_json
Chris@122 2388 ["config", "extdir"]
Chris@122 2389 val spec_libs = JsonBits.lookup_optional spec_json [libobjname]
Chris@122 2390 val lock_libs = JsonBits.lookup_optional lock_json [libobjname]
Chris@122 2391 val providers = Provider.load_more_providers
Chris@122 2392 (#providers userconfig) spec_json
Chris@122 2393 val libnames = case spec_libs of
Chris@122 2394 NONE => []
Chris@122 2395 | SOME (Json.OBJECT ll) => map (fn (k, v) => k) ll
Chris@122 2396 | _ => raise Fail "Object expected for libs"
Chris@122 2397 in
Chris@122 2398 {
Chris@122 2399 context = {
Chris@122 2400 rootpath = rootpath,
Chris@122 2401 extdir = extdir,
Chris@122 2402 providers = providers,
Chris@122 2403 accounts = #accounts userconfig
Chris@122 2404 },
Chris@122 2405 libs = map (load_libspec spec_json lock_json) libnames
Chris@122 2406 }
Chris@122 2407 end
Chris@122 2408
Chris@122 2409 fun save_lock_file rootpath locks =
Chris@122 2410 let val lock_file = FileBits.project_lock_path rootpath
Chris@122 2411 open Json
Chris@122 2412 val lock_json =
Chris@122 2413 OBJECT [
Chris@122 2414 (libobjname,
Chris@122 2415 OBJECT (map (fn { libname, id_or_tag } =>
Chris@122 2416 (libname,
Chris@122 2417 OBJECT [ ("pin", STRING id_or_tag) ]))
Chris@122 2418 locks))
Chris@122 2419 ]
Chris@122 2420 in
Chris@122 2421 JsonBits.save_json_to lock_file lock_json
Chris@122 2422 end
Chris@122 2423
Chris@122 2424 fun pad_to n str =
Chris@122 2425 if n <= String.size str then str
Chris@122 2426 else pad_to n (str ^ " ")
Chris@122 2427
Chris@122 2428 fun hline_to 0 = ""
Chris@122 2429 | hline_to n = "-" ^ hline_to (n-1)
Chris@122 2430
Chris@125 2431 val libname_width = 28
Chris@122 2432 val libstate_width = 11
Chris@122 2433 val localstate_width = 17
Chris@122 2434 val notes_width = 5
Chris@122 2435 val divider = " | "
Chris@122 2436 val clear_line = "\r" ^ pad_to 80 "";
Chris@122 2437
Chris@122 2438 fun print_status_header () =
Chris@122 2439 print (clear_line ^ "\n " ^
Chris@122 2440 pad_to libname_width "Library" ^ divider ^
Chris@122 2441 pad_to libstate_width "State" ^ divider ^
Chris@122 2442 pad_to localstate_width "Local" ^ divider ^
Chris@122 2443 "Notes" ^ "\n " ^
Chris@122 2444 hline_to libname_width ^ "-+-" ^
Chris@122 2445 hline_to libstate_width ^ "-+-" ^
Chris@122 2446 hline_to localstate_width ^ "-+-" ^
Chris@122 2447 hline_to notes_width ^ "\n")
Chris@122 2448
Chris@122 2449 fun print_outcome_header () =
Chris@122 2450 print (clear_line ^ "\n " ^
Chris@122 2451 pad_to libname_width "Library" ^ divider ^
Chris@122 2452 pad_to libstate_width "Outcome" ^ divider ^
Chris@122 2453 "Notes" ^ "\n " ^
Chris@122 2454 hline_to libname_width ^ "-+-" ^
Chris@122 2455 hline_to libstate_width ^ "-+-" ^
Chris@122 2456 hline_to notes_width ^ "\n")
Chris@122 2457
Chris@125 2458 fun print_status with_network (lib : libspec, status) =
Chris@122 2459 let val libstate_str =
Chris@122 2460 case status of
Chris@122 2461 OK (ABSENT, _) => "Absent"
Chris@122 2462 | OK (CORRECT, _) => if with_network then "Correct" else "Present"
Chris@122 2463 | OK (SUPERSEDED, _) => "Superseded"
Chris@122 2464 | OK (WRONG, _) => "Wrong"
Chris@122 2465 | ERROR _ => "Error"
Chris@122 2466 val localstate_str =
Chris@122 2467 case status of
Chris@122 2468 OK (_, MODIFIED) => "Modified"
Chris@122 2469 | OK (_, LOCK_MISMATCHED) => "Differs from Lock"
Chris@122 2470 | OK (_, CLEAN) => "Clean"
Chris@122 2471 | ERROR _ => ""
Chris@122 2472 val error_str =
Chris@122 2473 case status of
Chris@122 2474 ERROR e => e
Chris@122 2475 | _ => ""
Chris@122 2476 in
Chris@122 2477 print (" " ^
Chris@125 2478 pad_to libname_width (#libname lib) ^ divider ^
Chris@122 2479 pad_to libstate_width libstate_str ^ divider ^
Chris@122 2480 pad_to localstate_width localstate_str ^ divider ^
Chris@122 2481 error_str ^ "\n")
Chris@122 2482 end
Chris@122 2483
Chris@125 2484 fun print_update_outcome (lib : libspec, outcome) =
Chris@122 2485 let val outcome_str =
Chris@122 2486 case outcome of
Chris@122 2487 OK id => "Ok"
Chris@122 2488 | ERROR e => "Failed"
Chris@122 2489 val error_str =
Chris@122 2490 case outcome of
Chris@122 2491 ERROR e => e
Chris@122 2492 | _ => ""
Chris@122 2493 in
Chris@122 2494 print (" " ^
Chris@125 2495 pad_to libname_width (#libname lib) ^ divider ^
Chris@122 2496 pad_to libstate_width outcome_str ^ divider ^
Chris@122 2497 error_str ^ "\n")
Chris@122 2498 end
Chris@122 2499
Chris@125 2500 fun vcs_name HG = ("Mercurial", "hg")
Chris@125 2501 | vcs_name GIT = ("Git", "git")
Chris@125 2502 | vcs_name SVN = ("Subversion", "svn")
Chris@125 2503
Chris@125 2504 fun print_problem_summary context lines =
Chris@125 2505 let val failed_vcs =
Chris@125 2506 foldl (fn (({ vcs, ... } : libspec, ERROR _), acc) => vcs::acc
Chris@125 2507 | (_, acc) => acc) [] lines
Chris@125 2508 fun report_nonworking vcs error =
Chris@125 2509 print ((if error = "" then "" else error ^ "\n\n") ^
Chris@125 2510 "Error: The project uses the " ^ (#1 (vcs_name vcs)) ^
Chris@125 2511 " version control system, but its\n" ^
Chris@125 2512 "executable program (" ^ (#2 (vcs_name vcs)) ^
Chris@125 2513 ") does not appear to be installed in the program path\n\n")
Chris@125 2514 fun check_working [] checked = ()
Chris@125 2515 | check_working (vcs::rest) checked =
Chris@125 2516 if List.exists (fn v => vcs = v) checked
Chris@125 2517 then check_working rest checked
Chris@125 2518 else
Chris@125 2519 case AnyLibControl.is_working context vcs of
Chris@125 2520 OK true => check_working rest checked
Chris@125 2521 | OK false => (report_nonworking vcs "";
Chris@125 2522 check_working rest (vcs::checked))
Chris@125 2523 | ERROR e => (report_nonworking vcs e;
Chris@125 2524 check_working rest (vcs::checked))
Chris@125 2525 in
Chris@125 2526 print "\nError: Some operations failed\n\n";
Chris@125 2527 check_working failed_vcs []
Chris@125 2528 end
Chris@125 2529
Chris@125 2530 fun act_and_print action print_header print_line context (libs : libspec list) =
Chris@125 2531 let val lines = map (fn lib => (lib, action lib)) libs
Chris@125 2532 val imperfect = List.exists (fn (_, ERROR _) => true | _ => false) lines
Chris@122 2533 val _ = print_header ()
Chris@122 2534 in
Chris@122 2535 app print_line lines;
Chris@125 2536 if imperfect then print_problem_summary context lines else ();
Chris@122 2537 lines
Chris@122 2538 end
Chris@122 2539
Chris@122 2540 fun return_code_for outcomes =
Chris@122 2541 foldl (fn ((_, result), acc) =>
Chris@122 2542 case result of
Chris@122 2543 ERROR _ => OS.Process.failure
Chris@122 2544 | _ => acc)
Chris@122 2545 OS.Process.success
Chris@122 2546 outcomes
Chris@122 2547
Chris@122 2548 fun status_of_project ({ context, libs } : project) =
Chris@122 2549 return_code_for (act_and_print (AnyLibControl.status context)
Chris@122 2550 print_status_header (print_status false)
Chris@125 2551 context libs)
Chris@122 2552
Chris@122 2553 fun review_project ({ context, libs } : project) =
Chris@122 2554 return_code_for (act_and_print (AnyLibControl.review context)
Chris@122 2555 print_status_header (print_status true)
Chris@125 2556 context libs)
Chris@122 2557
Chris@122 2558 fun lock_project ({ context, libs } : project) =
Chris@125 2559 let val _ = if FileBits.verbose ()
Chris@125 2560 then print ("Scanning IDs for lock file...\n")
Chris@125 2561 else ()
Chris@125 2562 val outcomes = map (fn lib => (lib, AnyLibControl.id_of context lib))
Chris@122 2563 libs
Chris@122 2564 val locks =
Chris@122 2565 List.concat
Chris@125 2566 (map (fn (lib : libspec, result) =>
Chris@122 2567 case result of
Chris@122 2568 ERROR _ => []
Chris@125 2569 | OK id => [{ libname = #libname lib,
Chris@125 2570 id_or_tag = id }])
Chris@122 2571 outcomes)
Chris@122 2572 val return_code = return_code_for outcomes
Chris@122 2573 val _ = print clear_line
Chris@122 2574 in
Chris@122 2575 if OS.Process.isSuccess return_code
Chris@122 2576 then save_lock_file (#rootpath context) locks
Chris@122 2577 else ();
Chris@122 2578 return_code
Chris@122 2579 end
Chris@125 2580
Chris@125 2581 fun update_project (project as { context, libs }) =
Chris@125 2582 let val outcomes = act_and_print
Chris@125 2583 (AnyLibControl.update context)
Chris@125 2584 print_outcome_header print_update_outcome
Chris@125 2585 context libs
Chris@125 2586 val _ = if List.exists (fn (_, OK _) => true | _ => false) outcomes
Chris@125 2587 then lock_project project
Chris@125 2588 else OS.Process.success
Chris@125 2589 in
Chris@125 2590 return_code_for outcomes
Chris@125 2591 end
Chris@124 2592
Chris@122 2593 fun load_local_project pintype =
Chris@122 2594 let val userconfig = load_userconfig ()
Chris@122 2595 val rootpath = OS.FileSys.getDir ()
Chris@122 2596 in
Chris@122 2597 load_project userconfig rootpath pintype
Chris@122 2598 end
Chris@122 2599
Chris@122 2600 fun with_local_project pintype f =
Chris@125 2601 let open OS.Process
Chris@125 2602 val return_code =
Chris@125 2603 f (load_local_project pintype)
Chris@125 2604 handle Fail msg =>
Chris@125 2605 failure before print ("Error: " ^ msg)
Chris@125 2606 | JsonBits.Config msg =>
Chris@125 2607 failure before print ("Error in configuration: " ^ msg)
Chris@125 2608 | e =>
Chris@125 2609 failure before print ("Error: " ^ exnMessage e)
Chris@122 2610 val _ = print "\n";
Chris@122 2611 in
Chris@122 2612 return_code
Chris@122 2613 end
Chris@122 2614
Chris@122 2615 fun review () = with_local_project USE_LOCKFILE review_project
Chris@122 2616 fun status () = with_local_project USE_LOCKFILE status_of_project
Chris@122 2617 fun update () = with_local_project NO_LOCKFILE update_project
Chris@122 2618 fun lock () = with_local_project NO_LOCKFILE lock_project
Chris@122 2619 fun install () = with_local_project USE_LOCKFILE update_project
Chris@122 2620
Chris@122 2621 fun version () =
Chris@125 2622 (print ("v" ^ repoint_version ^ "\n");
Chris@122 2623 OS.Process.success)
Chris@122 2624
Chris@122 2625 fun usage () =
Chris@125 2626 (print "\nRepoint ";
Chris@122 2627 version ();
Chris@125 2628 print ("\n A simple manager for third-party source code dependencies.\n"
Chris@125 2629 ^ " http://all-day-breakfast.com/repoint/\n\n"
Chris@122 2630 ^ "Usage:\n\n"
Chris@125 2631 ^ " repoint <command>\n\n"
Chris@122 2632 ^ "where <command> is one of:\n\n"
Chris@122 2633 ^ " status print quick report on local status only, without using network\n"
Chris@122 2634 ^ " review check configured libraries against their providers, and report\n"
Chris@122 2635 ^ " install update configured libraries according to project specs and lock file\n"
Chris@122 2636 ^ " update update configured libraries and lock file according to project specs\n"
Chris@125 2637 ^ " lock rewrite lock file to match local library status\n"
Chris@125 2638 ^ " archive pack up project and all libraries into an archive file:\n"
Chris@125 2639 ^ " invoke as 'repoint archive targetfile.tar.gz --exclude unwanted.txt'\n"
Chris@125 2640 ^ " version print the Repoint version number and exit\n\n");
Chris@122 2641 OS.Process.failure)
Chris@122 2642
Chris@124 2643 fun archive target args =
Chris@124 2644 case args of
Chris@124 2645 [] =>
Chris@124 2646 with_local_project USE_LOCKFILE (Archive.archive (target, []))
Chris@124 2647 | "--exclude"::xs =>
Chris@124 2648 with_local_project USE_LOCKFILE (Archive.archive (target, xs))
Chris@124 2649 | _ => usage ()
Chris@124 2650
Chris@125 2651 fun repoint args =
Chris@122 2652 let val return_code =
Chris@122 2653 case args of
Chris@122 2654 ["review"] => review ()
Chris@122 2655 | ["status"] => status ()
Chris@122 2656 | ["install"] => install ()
Chris@122 2657 | ["update"] => update ()
Chris@122 2658 | ["lock"] => lock ()
Chris@122 2659 | ["version"] => version ()
Chris@124 2660 | "archive"::target::args => archive target args
Chris@125 2661 | arg::_ => (print ("Error: unknown argument \"" ^ arg ^ "\"\n");
Chris@125 2662 usage ())
Chris@122 2663 | _ => usage ()
Chris@122 2664 in
Chris@122 2665 OS.Process.exit return_code;
Chris@122 2666 ()
Chris@122 2667 end
Chris@122 2668
Chris@122 2669 fun main () =
Chris@125 2670 repoint (CommandLine.arguments ())