Skip to content

Commit

Permalink
Merge pull request #201 from lefessan/z-2023-03-15-better-git-support
Browse files Browse the repository at this point in the history
better support for git
  • Loading branch information
lefessan authored Mar 15, 2023
2 parents 1410c44 + 3c614cd commit 0a94109
Show file tree
Hide file tree
Showing 14 changed files with 113 additions and 59 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
* If a file `$PROJECT-deps.opam.locked` is available at the root of the source
tree, it will be provided to `opam install` (projects with binaries should
`git add` this file, while other projects should `.gitignore` it)
* Configuration file `$HOME/.config/drom/config`:
* Add option `git-stage: true/false` to decide whether `drom` should
call `git add` and `git rm` after every modification
* Support for git conflicts in `.drom`: accept multiple hash for a file
to decide whether it has been modified by the user or by drom
* Requires use of `opam>=2.1`

## v0.8.0
Expand Down
14 changes: 8 additions & 6 deletions src/drom_lib/build.ml
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ let build ~args ?(setup_opam = true) ?(build_deps = true)
switch p.min_edition
| _ -> () ) );

let config = Config.config () in
let config = Config.get () in

let share = Share.load ~p () in
begin
Expand All @@ -111,7 +111,7 @@ let build ~args ?(setup_opam = true) ?(build_deps = true)
if
match Hashes.get hashes "." with
| exception Not_found -> true
| old_hash ->
| old_hashes ->
let files =
( match p.file with
| None -> assert false
Expand All @@ -124,9 +124,11 @@ let build ~args ?(setup_opam = true) ?(build_deps = true)
| Some file -> [ file ] )
p.packages )
in
old_hash
<> Update.compute_config_hash
(List.map (fun file -> (file, EzFile.read_file file)) files)
let new_hash =
Update.compute_config_hash
(List.map (fun file -> (file, EzFile.read_file file)) files)
in
List.for_all ( (<>) new_hash ) old_hashes
then
if config.config_auto_upgrade <> Some false then
Update.update_files share ~twice:false ~create:false ~git:true p
Expand Down Expand Up @@ -323,7 +325,7 @@ had_switch: %b
let to_install =
let extra_packages =
if force_dev_deps then
let config = Config.config () in
let config = Config.get () in
( match config.config_dev_tools with
| None -> [ "merlin"; "ocp-indent" ]
| Some dev_tools -> dev_tools )
Expand Down
2 changes: 1 addition & 1 deletion src/drom_lib/commandLock.ml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ let action ~args () =
let opam_basename = p.package.name ^ "-deps.opam" in
let opam_filename = Globals.drom_dir // opam_basename in
Opam.run [ "lock" ] [ opam_filename ];
Git.call "add" [ opam_basename ^ ".locked" ];
Git.add [ opam_basename ^ ".locked" ];
()

let cmd =
Expand Down
2 changes: 1 addition & 1 deletion src/drom_lib/commandNew.ml
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ Available skeletons are: %s
|> String.concat " " );
exit 2
| Some name -> (
let config = Config.config () in
let config = Config.get () in
let project = Project.find () in
match project with
| None -> create_project ~config ~name ~skeleton ~dir ~inplace ~args
Expand Down
2 changes: 1 addition & 1 deletion src/drom_lib/commandPackage.ml
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ let upgrade_package package licenses ~upgrade ~kind ~files =

EzFile.write_file new_file content )
files;
Call.call ([ "git"; "add" ] @ List.rev !new_files)
Git.add (List.rev !new_files)
end;
()

Expand Down
2 changes: 1 addition & 1 deletion src/drom_lib/commandPublish.ml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ let select =
()

let action ~force ~opam_repo ~use_md5 () =
let config = Config.config () in
let config = Config.get () in
let opam_repo =
match !opam_repo with
| Some repo -> repo
Expand Down
19 changes: 15 additions & 4 deletions src/drom_lib/config.ml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ let config_of_toml filename =
let config_auto_opam_yes =
EzToml.get_bool_option table [ "user"; "auto-opam-yes" ]
in
let config_git_stage =
EzToml.get_bool_option table [ "user"; "git-stage" ]
in
{ config_author;
config_github_organization;
config_share_repo;
Expand All @@ -49,7 +52,8 @@ let config_of_toml filename =
config_opam_repo;
config_dev_tools;
config_auto_upgrade;
config_auto_opam_yes
config_auto_opam_yes;
config_git_stage;
}

let config_template =
Expand All @@ -66,6 +70,8 @@ let config_template =
# auto-upgrade = false
## Do not call opam with -y for local opam switches:
# auto-opam-yes = false
## Do not automatically stage files with git
# git-stage = false
|}

let update_with oldc newc =
Expand Down Expand Up @@ -106,7 +112,11 @@ let update_with oldc newc =
config_auto_opam_yes =
( match (newc.config_auto_opam_yes, oldc.config_auto_opam_yes) with
| None, oldc -> oldc
| newc, _ -> newc )
| newc, _ -> newc );
config_git_stage =
( match (newc.config_git_stage, oldc.config_git_stage) with
| None, oldc -> oldc
| newc, _ -> newc );
}

let getenv_opt v =
Expand Down Expand Up @@ -139,7 +149,8 @@ let load () =
config_share_repo = getenv_opt "DROM_SHARE_REPO";
config_dev_tools = None;
config_auto_upgrade = getenv_bool_opt "DROM_AUTO_UPGRADE";
config_auto_opam_yes = getenv_bool_opt "DROM_AUTO_OPAM_YES"
config_auto_opam_yes = getenv_bool_opt "DROM_AUTO_OPAM_YES";
config_git_stage = getenv_bool_opt "DROM_GIT_STAGE";
}
in
let path = Sys.getcwd () in
Expand All @@ -163,4 +174,4 @@ let load () =
iter path

let config = lazy (load ())
let config () = Lazy.force config
let get () = Lazy.force config
2 changes: 1 addition & 1 deletion src/drom_lib/config.mli
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
(* *)
(**************************************************************************)

val config : unit -> Types.config
val get : unit -> Types.config
31 changes: 26 additions & 5 deletions src/drom_lib/git.ml
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,32 @@ let update_submodules () =
if Sys.file_exists ".gitmodules" then
silent_fail "submodule" [ "update"; "--init"; "--recursive" ]

let remove dir =
Call.call [ "rm"; "-rf"; dir ];
silent_fail "rm" [ "-rf"; dir ]
let add ?(silent=false) files =
let config = Config.get () in
match config.config_git_stage with
| Some false -> ()
| None | Some true ->
if silent then
silent_fail "add" files
else
call "add" files

let remove ?(silent=false) files =
Call.call ("rm" :: "-rf" :: files );
let config = Config.get () in
match config.config_git_stage with
| Some false -> ()
| None | Some true ->
if silent then
silent_fail "rm" ("-rf" :: files)
else
call "rm" ("-rf" :: files)

let rename old_dir new_dir =
Call.call [ "mv"; old_dir; new_dir ];
silent_fail "rm" [ "-rf"; old_dir ];
silent_fail "add" [ new_dir ]
let config = Config.get () in
match config.config_git_stage with
| Some false -> ()
| None | Some true ->
remove ~silent:true [ old_dir ];
add ~silent:true [ new_dir ]
69 changes: 41 additions & 28 deletions src/drom_lib/hashes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ end
include HASH

type t =
{ mutable hashes : hash StringMap.t;
{ mutable hashes : hash list StringMap.t;
mutable modified : bool;
mutable files : (bool * string * int) StringMap.t;
(* for git *)
Expand All @@ -72,26 +72,36 @@ let load () =
(* Printf.eprintf "Loading .drom\n%!"; *)
Array.iteri
(fun i line ->
try
if line <> "" && line.[0] <> '#' then
let digest, filename =
if String.contains line ':' then
EzString.cut_at line ':'
else
EzString.cut_at line ' '
(* only for backward compat *)
in
if digest = "version" then
version := Some filename
else
let digest = HASH.from_hex digest in
map := StringMap.add filename digest !map
with
| exn ->
Printf.eprintf "Error loading .drom at line %d: %s\n%!" (i + 1)
(Printexc.to_string exn);
Printf.eprintf " on line: %s\n%!" line;
exit 2 )
try
let len = String.length line in
if len > 2 && match line.[0] with
| '#'
| '=' | '<' | '>' (* git conflict ! *)
-> false
| _ -> true then
let digest, filename =
if String.contains line ':' then
EzString.cut_at line ':'
else
EzString.cut_at line ' '
(* only for backward compat *)
in
if digest = "version" then
version := Some filename
else
let digest = HASH.from_hex digest in
let hashes =
match StringMap.find filename !map with
| exception Not_found -> []
| hashes -> hashes
in
map := StringMap.add filename (digest :: hashes) !map
with
| exn ->
Printf.eprintf "Error loading .drom at line %d: %s\n%!" (i + 1)
(Printexc.to_string exn);
Printf.eprintf " on line: %s\n%!" line;
exit 2 )
(EzFile.read_lines ".drom");
!map
) else
Expand All @@ -116,8 +126,8 @@ let read t ~file =

let get t file = StringMap.find file t.hashes

let update ?(git = true) t file hash =
t.hashes <- StringMap.add file hash t.hashes;
let update ?(git = true) t file hashes =
t.hashes <- StringMap.add file hashes t.hashes;
if git then t.to_add <- StringSet.add file t.to_add;
t.modified <- true

Expand All @@ -144,7 +154,7 @@ let save ?(git = true) t =
if not (Sys.file_exists dirname) then EzFile.make_dir ~p:true dirname;
EzFile.write_file file content;
Unix.chmod file perm;
if record then update t file (digest_content ~file ~perm ~content ()) )
if record then update t file [digest_content ~file ~perm ~content ()] )
t.files;

let b = Buffer.create 1000 in
Expand All @@ -156,7 +166,7 @@ let save ?(git = true) t =
| Some version -> version);
Printf.bprintf b "# end version\n%!";
StringMap.iter
(fun filename hash ->
(fun filename hashes ->
if Sys.file_exists filename then begin
if filename = "." then begin
Printf.bprintf b "\n# hash of toml configuration files\n";
Expand All @@ -165,7 +175,9 @@ let save ?(git = true) t =
Printf.bprintf b "\n# begin context for %s\n" filename;
Printf.bprintf b "# file %s\n" filename
end;
Printf.bprintf b "%s:%s\n" (HASH.to_hex hash) filename;
List.iter (fun hash ->
Printf.bprintf b "%s:%s\n" (HASH.to_hex hash) filename
) (List.rev hashes);
Printf.bprintf b "# end context for %s\n" filename
end )
t.hashes;
Expand All @@ -177,13 +189,14 @@ let save ?(git = true) t =
(fun file ->
if not (Sys.file_exists file) then to_remove := file :: !to_remove )
t.to_remove;
if !to_remove <> [] then Git.silent_fail "rm" ("-f" :: !to_remove);
if !to_remove <> [] then
Git.remove ~silent:true ("-f" :: !to_remove);

let to_add = ref [] in
StringSet.iter
(fun file -> if Sys.file_exists file then to_add := file :: !to_add)
t.to_add;
Git.silent_fail "add" (".drom" :: !to_add)
Git.add ~silent:true (".drom" :: !to_add)
);
t.to_add <- StringSet.empty;
t.to_remove <- StringSet.empty;
Expand Down
4 changes: 2 additions & 2 deletions src/drom_lib/hashes.mli
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ type t
val load : unit -> t

val remove : t -> string -> unit
val get : t -> string -> hash
val get : t -> string -> hash list
val write : t -> record:bool -> perm:int -> file:string -> content:string -> unit
val rename : t -> src:string -> dst:string -> unit
val with_ctxt : ?git:bool -> (t -> 'a) -> 'a
val update : ?git:bool -> t -> string -> hash -> unit
val update : ?git:bool -> t -> string -> hash list -> unit

(* read file either from Hashes of from disk *)
val read : t -> file:string -> string
Expand Down
4 changes: 2 additions & 2 deletions src/drom_lib/share.ml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ open Types
let share_repo_default = "https://github.com/OCamlPro/drom-share"

let share_repo_default () =
match ( Config.config () ). config_share_repo with
match ( Config.get () ). config_share_repo with
| None -> share_repo_default
| Some repo -> repo

Expand Down Expand Up @@ -103,7 +103,7 @@ let load ?(args=default_args()) ?p () =
(* version = None => use latest version *)

(* Used to ensure `shares_dir` is created *)
let _config = Config.config () in
let _config = Config.get () in

let hash = Digest.to_hex (Digest.string repo) in
let shares_dir = Globals.config_dir // "shares" in
Expand Down
3 changes: 2 additions & 1 deletion src/drom_lib/types.ml
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ type config =
config_opam_repo : string option;
config_dev_tools : string list option;
config_auto_upgrade : bool option;
config_auto_opam_yes : bool option
config_auto_opam_yes : bool option;
config_git_stage : bool option;
}

type opam_kind =
Expand Down
Loading

0 comments on commit 0a94109

Please sign in to comment.