| 1 |
(**************************************************************************) |
| 2 |
(* *) |
| 3 |
(* Copyright 2012-2019 OCamlPro *) |
| 4 |
(* Copyright 2012 INRIA *) |
| 5 |
(* *) |
| 6 |
(* All rights reserved. This file is distributed under the terms of the *) |
| 7 |
(* GNU Lesser General Public License version 2.1, with the special *) |
| 8 |
(* exception on linking described in the file LICENSE. *) |
| 9 |
(* *) |
| 10 |
(**************************************************************************) |
| 11 |
|
| 12 |
open OpamStd.Op |
| 13 |
|
| 14 |
type version_control = [ `git | `darcs | `hg ] |
| 15 |
|
| 16 |
type backend = [ `http | `rsync | version_control ] |
| 17 |
|
| 18 |
type t = { |
| 19 |
transport: string; |
| 20 |
path: string; |
| 21 |
hash: string option; |
| 22 |
backend: backend; |
| 23 |
} |
| 24 |
|
| 25 |
let empty = { |
| 26 |
backend = `http; |
| 27 |
transport = "https"; |
| 28 |
path = ""; |
| 29 |
hash = None; |
| 30 |
} |
| 31 |
|
| 32 |
let compare {transport; path; hash; backend} u = |
| 33 |
let transport = String.compare transport u.transport in |
| 34 |
if transport <> 0 then transport else |
| 35 |
let path = String.compare path u.path in |
| 36 |
if path <> 0 then path else |
| 37 |
let hash = OpamStd.Option.compare String.compare hash u.hash in |
| 38 |
if hash <> 0 then hash else |
| 39 |
compare backend u.backend |
| 40 |
|
| 41 |
let equal u v = compare u v = 0 |
| 42 |
|
| 43 |
exception Parse_error of string |
| 44 |
let parse_error s = raise (Parse_error s) |
| 45 |
|
| 46 |
let split_url = |
| 47 |
let re = |
| 48 |
Re.(compile @@ whole_string @@ seq [ |
| 49 |
(* Parse the scheme, which is either backend+protocol or just a protocol *) |
| 50 |
opt @@ seq [ |
| 51 |
(* Backend *) |
| 52 |
opt @@ seq [ group @@ rep @@ diff any (set "+:"); |
| 53 |
alt [ char '+'; str "://"] ]; |
| 54 |
(* Protocol *) |
| 55 |
group @@ rep @@ diff any (char ':'); |
| 56 |
(* Separator *) |
| 57 |
str "://" |
| 58 |
]; |
| 59 |
(* Parse the path, with is either path or path.suffix (suffix contains no .) *) |
| 60 |
group @@ seq [ |
| 61 |
non_greedy @@ rep @@ diff any (char '#'); |
| 62 |
(* If there's a .suffix, group it separately (used for backend guessing) *) |
| 63 |
opt @@ seq [ char '.'; group @@ rep1 @@ diff any (set "\\/.#")] |
| 64 |
]; |
| 65 |
(* Parse the fragment (git branch, etc.) *) |
| 66 |
opt @@ seq [ char '#'; group @@ rep any ]; |
| 67 |
]) |
| 68 |
in |
| 69 |
fun u -> |
| 70 |
match Re.Group.all (Re.exec re u) with |
| 71 |
| [| _; vc; transport; path; suffix; hash |] -> |
| 72 |
let opt = function "" -> None | s -> Some s in |
| 73 |
opt vc, opt transport, path, opt suffix, opt hash |
| 74 |
| _ -> assert false |
| 75 |
|
| 76 |
let vc_of_string = function |
| 77 |
| "git" -> `git |
| 78 |
| "hg" -> `hg |
| 79 |
| "darcs" -> `darcs |
| 80 |
| vc -> |
| 81 |
parse_error (Printf.sprintf "unsupported version control system %s" |
| 82 |
(OpamConsole.colorise `underline vc)) |
| 83 |
|
| 84 |
let string_of_vc = function |
| 85 |
| `git -> "git" |
| 86 |
| `darcs -> "darcs" |
| 87 |
| `hg -> "hg" |
| 88 |
|
| 89 |
let string_of_backend = function |
| 90 |
| `http -> "http" |
| 91 |
| `rsync -> "rsync" |
| 92 |
| #version_control as vc -> string_of_vc vc |
| 93 |
|
| 94 |
let backend_of_string = function |
| 95 |
| "http" | "https" | "ftp" | "wget" | "curl" -> `http |
| 96 |
| "file" -> `rsync |
| 97 |
| "git" -> `git |
| 98 |
| "darcs" -> `darcs |
| 99 |
| "hg" -> `hg |
| 100 |
| "path" | "local" | "rsync" | "ssh" | "scp" | "sftp" -> `rsync |
| 101 |
| p -> |
| 102 |
parse_error (Printf.sprintf "unsupported protocol %s" |
| 103 |
(OpamConsole.colorise `underline p)) |
| 104 |
|
| 105 |
|
| 106 |
let looks_like_ssh_path = |
| 107 |
(* ':' before any '/' : assume ssh, like git does. Exception for 'x:' with |
| 108 |
single char, because Windows *) |
| 109 |
let re = |
| 110 |
Re.(compile @@ seq [ |
| 111 |
group @@ repn (diff any (set "/:")) 2 None; |
| 112 |
char ':'; |
| 113 |
opt @@ char '/'; |
| 114 |
opt @@ group @@ seq [ |
| 115 |
alt [ |
| 116 |
diff any digit; |
| 117 |
seq [rep digit; compl [digit; char '/']] |
| 118 |
]; |
| 119 |
rep any; |
| 120 |
]; |
| 121 |
eos; |
| 122 |
]) |
| 123 |
in |
| 124 |
fun path -> |
| 125 |
try |
| 126 |
let sub = Re.exec re path in |
| 127 |
Some (Re.Group.get sub 1 ^ |
| 128 |
try "/" ^ Re.Group.get sub 2 with Not_found -> "") |
| 129 |
with Not_found -> None |
| 130 |
|
| 131 |
let parse ?backend ?(handle_suffix=true) ?(from_file=true) s = |
| 132 |
let vc, transport, path, suffix, hash = split_url s in |
| 133 |
let backend = |
| 134 |
match backend with |
| 135 |
| Some b -> b |
| 136 |
| None -> |
| 137 |
match vc with |
| 138 |
| Some vc -> vc_of_string vc |
| 139 |
| None -> |
| 140 |
let of_suffix ~default = |
| 141 |
if not handle_suffix then default else |
| 142 |
match suffix with |
| 143 |
| Some sf -> (try vc_of_string sf with Parse_error _ -> default) |
| 144 |
| None -> |
| 145 |
match OpamStd.String.cut_at path '@' with |
| 146 |
| Some (user, _) -> |
| 147 |
(try vc_of_string user with Parse_error _ -> default) |
| 148 |
| None -> default |
| 149 |
in |
| 150 |
match transport with |
| 151 |
| None -> of_suffix ~default:`rsync |
| 152 |
| Some tr -> |
| 153 |
try vc_of_string tr with Parse_error _ -> |
| 154 |
of_suffix ~default:(backend_of_string tr) |
| 155 |
in |
| 156 |
let transport, path = |
| 157 |
match backend, transport, looks_like_ssh_path path with |
| 158 |
| `http, None, _ -> |
| 159 |
"http", path |
| 160 |
| _, (None | Some ("git"|"hg"|"darcs")), Some path -> |
| 161 |
"ssh", path |
| 162 |
| _, (None | Some ("hg"|"darcs")), None -> |
| 163 |
"file", OpamSystem.real_path path |> OpamSystem.back_to_forward |
| 164 |
| `rsync, Some "file", _ when not from_file -> |
| 165 |
"file", OpamSystem.real_path path |> OpamSystem.back_to_forward |
| 166 |
| _, Some tr, _ -> |
| 167 |
tr, path |
| 168 |
in |
| 169 |
{ |
| 170 |
transport; |
| 171 |
path; |
| 172 |
hash; |
| 173 |
backend; |
| 174 |
} |
| 175 |
|
| 176 |
let parse_opt ?(quiet=false) ?backend ?handle_suffix ?from_file s = |
| 177 |
try |
| 178 |
Some (parse ?backend ?handle_suffix ?from_file s) |
| 179 |
with Parse_error pe -> |
| 180 |
if not quiet then |
| 181 |
OpamConsole.warning "URL parsing error on %s: %s" |
| 182 |
(OpamConsole.colorise `underline s) pe; |
| 183 |
None |
| 184 |
|
| 185 |
let of_string url = parse ~handle_suffix:false url |
| 186 |
|
| 187 |
let to_string_t ?subpath url = |
| 188 |
let hash = match url.hash with Some h -> "#" ^ h | None -> "" in |
| 189 |
let subpath = |
| 190 |
match subpath with |
| 191 |
| Some sb -> |
| 192 |
Printf.sprintf "directory /%s in " |
| 193 |
(OpamFilename.SubPath.normalised_string sb) |
| 194 |
| None -> "" |
| 195 |
in |
| 196 |
match url.backend with |
| 197 |
| #version_control as vc -> |
| 198 |
let vc = string_of_backend vc in |
| 199 |
if url.transport = vc then (* Don't be redundant on e.g git:// protocols *) |
| 200 |
Printf.sprintf "%s%s://%s%s" subpath vc url.path hash |
| 201 |
else |
| 202 |
Printf.sprintf "%s%s+%s://%s%s" subpath vc url.transport url.path hash |
| 203 |
| `rsync | `http -> |
| 204 |
Printf.sprintf "%s%s://%s%s" subpath url.transport url.path hash |
| 205 |
|
| 206 |
let to_string url = to_string_t url |
| 207 |
let to_string_w_subpath subpath = to_string_t ?subpath |
| 208 |
|
| 209 |
let base_url url = |
| 210 |
match url.transport with |
| 211 |
| "" -> url.path |
| 212 |
| tr -> Printf.sprintf "%s://%s" tr url.path |
| 213 |
|
| 214 |
let local_path = function |
| 215 |
| { transport = ("file"|"path"|"local"|"rsync"); path; |
| 216 |
hash = _; backend = (#version_control | `rsync); } |
| 217 |
when looks_like_ssh_path path = None -> |
| 218 |
Some path |
| 219 |
| _ -> None |
| 220 |
|
| 221 |
let local_dir url = |
| 222 |
let open OpamStd.Option.Op in |
| 223 |
local_path url >>| |
| 224 |
OpamFilename.Dir.of_string >>= fun d -> |
| 225 |
if OpamFilename.exists_dir d then Some d |
| 226 |
else None |
| 227 |
|
| 228 |
let local_file url = |
| 229 |
let open OpamStd.Option.Op in |
| 230 |
local_path url >>| |
| 231 |
OpamFilename.of_string >>= fun f -> |
| 232 |
if OpamFilename.exists f then Some f |
| 233 |
else None |
| 234 |
|
| 235 |
let guess_version_control s = |
| 236 |
let vc,transport,path,_,_ = split_url s in |
| 237 |
if vc = None && transport = None && looks_like_ssh_path path = None then |
| 238 |
let open OpamFilename in |
| 239 |
let open Op in |
| 240 |
let dir = Dir.of_string path in |
| 241 |
if exists_dir (dir / ".git") || exists (dir // ".git") |
| 242 |
then Some`git else |
| 243 |
if exists_dir (dir / ".hg") then Some `hg else |
| 244 |
if exists_dir (dir / "_darcs") then Some `darcs else |
| 245 |
None |
| 246 |
else |
| 247 |
None |
| 248 |
|
| 249 |
let basename = |
| 250 |
let re = |
| 251 |
Re.(compile @@ seq [ |
| 252 |
opt @@ seq [rep any; char '/']; |
| 253 |
group @@ rep1 (diff any (char '/')); |
| 254 |
rep @@ char '/'; |
| 255 |
]) |
| 256 |
in |
| 257 |
fun t -> |
| 258 |
try |
| 259 |
Re.Group.get (Re.exec re t.path) 1 |
| 260 |
with Not_found -> "" |
| 261 |
|
| 262 |
let root = |
| 263 |
let re = Re.(compile @@ seq [char '/'; rep any]) in |
| 264 |
fun t -> |
| 265 |
let path = |
| 266 |
(* The special-casing of "file" is needed for Windows *) |
| 267 |
if t.transport = "file" then |
| 268 |
"" |
| 269 |
else |
| 270 |
Re.replace_string re ~by:"" t.path |
| 271 |
in |
| 272 |
{ t with path} |
| 273 |
|
| 274 |
let has_trailing_slash url = |
| 275 |
OpamStd.String.ends_with ~suffix:"/" url.path |
| 276 |
|
| 277 |
let to_json url = `String (to_string url) |
| 278 |
let of_json = function |
| 279 |
| `String s -> (try Some (of_string s) with _ -> None) |
| 280 |
| _ -> None |
| 281 |
|
| 282 |
type url = t |
| 283 |
|
| 284 |
let map_file_url f url = |
| 285 |
if url.transport = "file" then |
| 286 |
{url with path = f url.path} |
| 287 |
else |
| 288 |
url |
| 289 |
|
| 290 |
module O = struct |
| 291 |
type t = url |
| 292 |
let to_string = to_string |
| 293 |
let to_json = to_json |
| 294 |
let of_json = of_json |
| 295 |
let compare = compare |
| 296 |
end |
| 297 |
|
| 298 |
module Set = OpamStd.Set.Make(O) |
| 299 |
|
| 300 |
module Map = OpamStd.Map.Make(O) |
| 301 |
|
| 302 |
module Op = struct |
| 303 |
|
| 304 |
(** appending to an url path *) |
| 305 |
let ( / ) url dir = |
| 306 |
let url = |
| 307 |
if Filename.is_relative dir then |
| 308 |
url |
| 309 |
else |
| 310 |
root url |
| 311 |
in |
| 312 |
(* Even on Windows, a file:// _should_ use slash *) |
| 313 |
let dir = OpamSystem.back_to_forward dir in |
| 314 |
let path = |
| 315 |
if has_trailing_slash url || url.path = "" then url.path ^ dir |
| 316 |
else url.path ^ "/" ^ dir |
| 317 |
in |
| 318 |
{url with path } |
| 319 |
|
| 320 |
end |