From 7a4743d7d406474971bd0cb44eb023f013e328fa Mon Sep 17 00:00:00 2001 From: Edgar Aroutiounian Date: Thu, 28 May 2015 18:47:51 -0400 Subject: [PATCH] Updated Makefile, Readme and starting to handle errors --- .gitignore | 4 +++ Makefile | 16 +++++++--- README.md | 25 +++++++++++++++ git.ml | 94 ++++++++++++++++++++++++++++++++++++++++++++++++------ 4 files changed, 125 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index 7a98001..f1fa99f 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,7 @@ Git_test README.org git.cmo a.out +scratch.cmi +scratch.cmt +scratch.cmx +scratch.o diff --git a/Makefile b/Makefile index 4a3c06a..e0ed2db 100644 --- a/Makefile +++ b/Makefile @@ -5,11 +5,18 @@ SRC := git.ml .PHONY: clean +all:install + native:libgit set_meta ocamlfind ocamlopt -package $(PACKAGES) \ - -cclib `ocamlfind query ocaml-libgit2`/$(CCLIB) -linkpkg $(SRC) -o Git_test + -cclib `ocamlfind query ocaml-libgit2`/$(CCLIB) -linkpkg $(SRC) + +bytecode:libgit set_meta + ocamlfind ocamlc -package $(PACKAGES) \ + -cclib `ocamlfind query ocaml-libgit2`/$(CCLIB) -linkpkg $(SRC) libgit: + # Not sure why this doesn't work with opam yet. # @echo "Updating libgit2 itself" # git submodule update --remote ocamlfind install ocaml-libgit2 META @@ -24,10 +31,9 @@ set_meta: echo "linkopts = \"-cclib" `ocamlfind query ocaml-libgit2`/$(CCLIB)"\"" >> \ `ocamlfind query ocaml-libgit2`/META -install: - ocamlfind install ocaml-libgit2 -add git.cmi git.cmt git.o git.cmx - +install:native bytecode + ocamlfind install ocaml-libgit2 -add git.cmo git.cmi git.cmt git.o git.cmx uninstall: ocamlfind remove ocaml-libgit2 clean: - rm -rf *.cmi *.cmt *.cmx *.cmo *.o git_test libgit_gen_* a.out + rm -rf *.cmi *.cmt *.cmx *.cmo *.o libgit_gen_* a.out diff --git a/README.md b/README.md index f7a179f..5dc038b 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,31 @@ the `Ctypes` library. Since I work on OS X primarily, my first goal is to have this working transparently and cleanly on that platform first. +I haven't submitted to opam yet, but this works right now for OS X +users. + + git clone --recursive https://github.com/fxfactorial/ocaml-libgit2 + cd ocaml-libgit2 + make install + +To do a sanity check of everything working, try the following. + +in *test.ml* + + open Git + + let () = + init (); + "libgit's version is: " ^ git_library_version () |> print_endline + +…and then compile that with + + ocamlfind ocamlopt -package ocaml-libgit2 -linkpkg test.ml -o SanityCheck + ./SanityCheck + +If all went well, no DLL errors, then congrats. This most likely means +everything is correct. + # Documentation I'm trying to expose as much of the API as I can and the naming is 1-1 diff --git a/git.ml b/git.ml index 5ba377d..8a77a9d 100644 --- a/git.ml +++ b/git.ml @@ -1,7 +1,6 @@ open Ctypes open Foreign - module Common = struct let git_feature_t = typedef int64_t "git_feature_t" @@ -958,11 +957,94 @@ module Global = struct (void @-> returning int) end +(** Now the helpers *) +module Results = struct + + exception Git_Errored of string + type git_return_code = + | GIT_OK + | GIT_ERROR of string + | GIT_ENOTFOUND of string + | GIT_EEXISTS of string + | GIT_EAMBIGUOUS of string + | GIT_EBUFS of string + | GIT_EUSER of string + | GIT_EBAREREPO of string + | GIT_EUNBORNBRANCH of string + | GIT_EUNMERGED of string + | GIT_ENONFASTFORWARD of string + | GIT_EINVALIDSPEC of string + | GIT_EMERGECONFLICT of string + | GIT_ELOCKED of string + | GIT_EMODIFIED of string + | GIT_EAUTH of string + | GIT_ECERTIFICATE of string + | GIT_EAPPLIED of string + | GIT_EPEEL of string + | GIT_EEOF of string + | GIT_PASSTHROUGH of string + | GIT_ITEROVER of string + + let of_int = function + | -1 -> GIT_ERROR "Generic Error" + | -3 -> GIT_ENOTFOUND "Requested object could not be found" + | -4 -> GIT_EEXISTS "Object exists preventing operation" + | -5 -> GIT_EAMBIGUOUS "More than one object matches" + | -6 -> GIT_EBUFS "Output buffer too short to hold data" + | -7 -> + GIT_EUSER "GIT_EUSER is a special error that is never generated by libgit2\ + You can return it from a callback (e.g to stop an iteration)\ + to know that it was generated by the callback and \ + not by libgit2." + | -8 -> GIT_EBAREREPO "Operation not allowed on bare repository" + | -9 -> GIT_EUNBORNBRANCH "HEAD refers to branch with no commits" + | -10 -> GIT_EUNMERGED "Merge in progress prevented operation" + | -11 -> GIT_ENONFASTFORWARD "Reference was not fast-forwardable" + | -12 -> GIT_EINVALIDSPEC "Name/ref spec was not in a valid format" + | -13 -> GIT_EMERGECONFLICT "Merge conflicts prevented operation" + | -14 -> GIT_ELOCKED "Lock file prevented operation" + | -15 -> GIT_EMODIFIED "Reference value does not match expected" + | -16 -> GIT_EAUTH "Authentication error" + | -17 -> GIT_ECERTIFICATE "Server certificate is invalid" + | -18 -> GIT_EAPPLIED "Patch/merge has already been applied" + | -19 -> GIT_EPEEL "The requested peel operation is not possible" + | -20 -> GIT_EEOF "Unexpected EOF" + | -30 -> GIT_PASSTHROUGH "Internal only" + | -31 -> GIT_ITEROVER "Signals end of iteration with iterator" + | _ -> GIT_OK + + let continue = function + | GIT_OK -> () + | GIT_ERROR a -> raise (Git_Errored a) + | GIT_ENOTFOUND a -> raise (Git_Errored a) + | GIT_EEXISTS a -> raise (Git_Errored a) + | GIT_EAMBIGUOUS a -> raise (Git_Errored a) + | GIT_EBUFS a -> raise (Git_Errored a) + | GIT_EUSER a -> raise (Git_Errored a) + | GIT_EBAREREPO a -> raise (Git_Errored a) + | GIT_EUNBORNBRANCH a -> raise (Git_Errored a) + | GIT_EUNMERGED a -> raise (Git_Errored a) + | GIT_ENONFASTFORWARD a -> raise (Git_Errored a) + | GIT_EINVALIDSPEC a -> raise (Git_Errored a) + | GIT_EMERGECONFLICT a -> raise (Git_Errored a) + | GIT_ELOCKED a -> raise (Git_Errored a) + | GIT_EMODIFIED a -> raise (Git_Errored a) + | GIT_EAUTH a -> raise (Git_Errored a) + | GIT_ECERTIFICATE a -> raise (Git_Errored a) + | GIT_EAPPLIED a -> raise (Git_Errored a) + | GIT_EPEEL a -> raise (Git_Errored a) + | GIT_EEOF a -> raise (Git_Errored a) + | GIT_PASSTHROUGH a -> raise (Git_Errored a) + | GIT_ITEROVER a -> raise (Git_Errored a) + end + +open Results + let init () = - Global.git_libgit2_init () + Global.git_libgit2_init () |> of_int |> continue let shutdown () = - Global.git_libgit2_shutdown () + Global.git_libgit2_shutdown () |> of_int |> continue let init_repo ?(is_bare=true) ?init_options ~repo_path:repo_path = let a_repo = allocate_n ~count:1 (ptr Types.git_repository) in @@ -995,13 +1077,7 @@ let clone_simple ?path ~repo_url:url = let find_repo ~path:p = let root = make Buffer.git_buf in - (* Will have to come back, check errors, Result.t? *) ignore(Repository.git_repository_discover (addr root) p 0 None); let result = getf root Buffer.ptr_ in let length = getf root Buffer.size_ |> Unsigned.Size_t.to_int in string_from_ptr result length - -(* let () = *) -(* init (); *) -(* find_repo "/Users/Edgar/Repos/" |> print_endline *) -