Jump to content
aegger

Minimal working Example for libgit2-delphi

Recommended Posts

Good evening!

 

I am trying to set up a minimal working example for libgit2-delphi (GitHub - todaysoftware/libgit2-delphi: libgit2 bindings for Delphi/Free Pascal).
As I am relatively new to this aspect of Delphi (Pointers, cdecl; external etc.), I would appreciate some help - thank you!

 

With the compiled *.dll and source from the release I can call the functions, but I get an error when calling even basic functionality like git_repository_init(...).
I am sure there is something fundamental I am overlooking.

program GetItWorking;
{$APPTYPE CONSOLE}
{$R *.res}
uses
  System.SysUtils,
  libgit2 in 'libgit2-delphi-main\src\libgit2.pas';
var
  statusRepository : integer;
  repoPP : PPgit_repository;
  gitError : Pgit_error;
const
  repoPath : PAnsiChar = 'C:/TEMP/';
begin
  try
    InitLibgit2;  //with this setup in "LibGit2.pas" you can only init a single repository at a time... (Singleton Pattern)
    statusRepository := git_repository_init(repoPP, repoPath, 0); //function git_repository_init(out_: PPgit_repository; path: PAnsiChar; is_bare: Cardinal): Integer; cdecl; external libgit2_dll;
    if statusRepository <> 0 then begin
      gitError := git_error_last(); //message: invalid argument: "out"
    end;
    //How to look into the repo?
      //ToDo
    ShutdownLibgit2;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
  //keep console open
  readln;
end.

 

LibGit2_GetItWorking.zip

Share this post


Link to post

Try changing the variable "repoPP:PPgit_repository" to: "repoPP:PGit_repository" and call    "git_repository_init(@repoPP..."

 


 

 

  • Thanks 1

Share this post


Link to post

Seems to do the trick!
@Darian Miller Thank you very much for helping out a novice.

I will take a closer look at the fundamentals.

Share this post


Link to post
On 3/16/2024 at 3:58 PM, aegger said:

repoPP : PPgit_repository;

Your original declaration declared a pointer, but never assigned it to anything. Since it is a local variable on the stack, it contained a garbage/random value rather than a pointer to actual data.

 

On 3/16/2024 at 9:02 PM, Darian Miller said:

 "repoPP:PGit_repository" and call    "git_repository_init(@repoPP..."

Darian's suggestion changed your declaration to an instance of data. Using @repoPP then passed the memory address (pointer) of the data to the DLL. See The @ Operator

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×