Skip to content

Commit

Permalink
Updated Makefile, Readme and starting to handle errors
Browse files Browse the repository at this point in the history
  • Loading branch information
fxfactorial committed May 28, 2015
1 parent 8a56f70 commit 7a4743d
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 14 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ Git_test
README.org
git.cmo
a.out
scratch.cmi
scratch.cmt
scratch.cmx
scratch.o
16 changes: 11 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
94 changes: 85 additions & 9 deletions git.ml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
open Ctypes
open Foreign


module Common = struct

let git_feature_t = typedef int64_t "git_feature_t"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 *)

0 comments on commit 7a4743d

Please sign in to comment.