Skip to content

Commit

Permalink
Merge pull request #213 from lefessan/z-2023-05-10-latest-version
Browse files Browse the repository at this point in the history
Support for LATEST_VERSIONS
  • Loading branch information
lefessan committed May 10, 2023
2 parents b2bc2e6 + d8ac4a5 commit 4a67af7
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 31 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
`path/file.ext.drom-tpl` exists, in which case it will use it as
a template for the file. It is possible to use
`_drom/skeleton/path/file.ext` as an example to create such a file.
* Support for LATEST_VERSIONS in drom-share: the LATEST_VERSIONS file
indicates, for any version of `drom`, the latest version of the
drom-share repo that should be used. It makes the `LATEST` file
obsolete (as it was incomplete in that matter)

## v0.9.1
* Fix Sys_error docs/style.css because of missing file
Expand Down
1 change: 1 addition & 0 deletions src/drom_lib/call.ml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ let after_hook ?args ~command () =
let tmpfile () =
Filename.temp_file "tmpfile" ".tmp"

(* Does not print anything, except in case of error. Exception on error *)
let silent ?print_args args =
let out_file = tmpfile () in
let fd = Unix.openfile out_file
Expand Down
2 changes: 2 additions & 0 deletions src/drom_lib/call.mli
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ val call :
?stdout:Unix.file_descr ->
?stderr:Unix.file_descr ->
?print_args:string list -> string list -> unit

(* Does not print anything, except in case of error. Exception on error *)
val silent : ?print_args:string list -> string list -> unit

val call_get_fst_line : string -> string option
Expand Down
113 changes: 82 additions & 31 deletions src/drom_lib/share.ml
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ let load ?(args=default_args()) ?p () =
end;
git ~cd:share_dir cmd args
in
let git_silent_fail cmd args =
let git_fail_ok cmd args =
try git cmd args with _ -> ()
in
let git_fetch_all () =
Expand All @@ -139,32 +139,80 @@ let load ?(args=default_args()) ?p () =
if branch = "master" then
Error.raise "Don't use 'master' as a development branch of the share-repo.";
git "checkout" [ "master" ];
git_silent_fail "branch" [ "-D" ; branch ];
git_fail_ok "branch" [ "-D" ; branch ];
git "checkout" [ "-b"; branch ; "--track" ; remote ^ "/" ^ branch ];
in
let read_first_line file =
let ic = open_in file in
let line = input_line ic in
close_in ic;
String.trim line
in
let get_version () =
String.trim ( EzFile.read_file ( share_dir // "VERSION" ))
String.trim ( read_first_line ( share_dir // "VERSION" ))
in
let get_latest () =
String.trim ( EzFile.read_file ( share_dir // "LATEST" ))
let get_latest ?(version = Version.version) () =
let filename = share_dir // "LATEST_VERSIONS" in
if Sys.file_exists filename then
let lines = EzFile.read_lines_to_list filename in
(* format:
* '#' at 0 for line comment
* 'dev $VERSION' for VERSION is the LATEST version
* '$DROM_VERSION $VERSION' for $VERSION is for all drom versions
before $DROM_VERSION
*)
let rec iter share_version lines =
match lines with
[] -> begin
match share_version with
| None ->
failwith "LATEST_VERSIONS does not contain a matching version"
| Some share_version -> share_version
end
| line :: lines ->
let len = String.length line in
if len > 0 && line.[0] = '#' then (* allow comments *)
iter share_version lines
else
let drom_version, new_share_version = EzString.cut_at line ' ' in
if
drom_version <> "dev" &&
VersionCompare.compare version drom_version >= 0 then
iter share_version []
else
let share_version = String.trim new_share_version in
iter ( Some share_version ) lines
in
iter None lines
else
String.trim ( read_first_line ( share_dir // "LATEST" ))
in
let get_drom_version () =
String.trim ( EzFile.read_file ( share_dir // "DROM_VERSION" ))
String.trim ( read_first_line ( share_dir // "DROM_VERSION" ))
in
let git_checkout_latest () =
(* Some testing of the algorithm...
List.iter (fun version ->
let latest = get_latest ~version () in
Printf.eprintf "Latest for %s is %s\n%!" version latest)
[ "0.8.0"; "0.8.1"; "0.9.0" ; "0.9.1" ; "0.9.2" ; "0.9.3" ];
*)
let latest = get_latest () in
git "checkout" [ latest ];
let version = get_version () in
if version <> latest then
Error.raise
"Version %S in VERSION does not match tag version %S"
version latest;
version
in
let share_version = match version with
| None
| Some "latest" ->
git_fetch_all ();
git "checkout" [ "master" ];
git "merge" [ "--ff-only" ];
let latest = get_latest () in
git "checkout" [ latest ];
let version = get_version () in
if version <> latest then
Error.raise
"Version %S in VERSION does not match tag version %S"
version latest;
version
git_checkout_latest ()

| Some version ->

Expand All @@ -177,26 +225,29 @@ let load ?(args=default_args()) ?p () =
git_fetch_all () ;
git_checkout_branch ~remote branch;
version
| [ "branch" ; remote; branch ; "latest" ] ->
git_fetch_all () ;
git_checkout_branch ~remote branch;
let _latest_version = git_checkout_latest () in
version
| _ ->
(* always checkout the version by tag. *)
begin
try
git "checkout" [ version ];
let current_version = get_version () in
if current_version <> version then
failwith "Probably buggy version, try refetch"
with _ ->
git_fetch_all ();
(* TODO: in case of error, the version does not exit ? *)
git "checkout" [ version ];
end;
let current_version = get_version () in
if current_version <> version then begin
begin
try
git "checkout" [ version ];
let current_version = get_version () in
if current_version <> version then
failwith "Probably buggy version, try refetch"
with _ ->
git_fetch_all ();
(* TODO: in case of error, the version does not exit ? *)
git "checkout" [ version ];
end;
let current_version = get_version () in
if current_version <> version then begin
Error.raise "Version %S does not seem to exist (latest seems to be %S).\nCheck in repo %s"
version current_version
share_dir
end;
Error.raise "Version %S does not seem to exist (latest seems to be %S).\nCheck in repo %s"
version current_version
share_dir
end;
version
in
Expand Down

0 comments on commit 4a67af7

Please sign in to comment.