Pergunta

I would like to program something similar to "git diff blobish blobish" with libgit2

here is my code so far

git_repository* repo;
git_repository_open(&repo, path/to/repository);
git_blob *oldBlob, *newBlob;
git_oid old_oid, new_oid;
const char* oldBlobHash = "92fd12351a4e4d6f10d30516149c624e6b9e3dc9";
git_oid_fromstr(&old_oid, oldBlobHash);
git_blob_lookup(&oldBlob, repo, &old_oid);
const char* newBlobHash = "80bf2770cdbb9580bc040d49775fccd405f534dc";
git_oid_fromstr(&new_oid, newBlobHash);
git_blob_lookup(&newBlob, repo, &new_oid);

git_diff_blobs(oldBlob,
        NULL,
        newBlob,
        NULL,
        NULL,?, ?, ?, ?); //what should I put in the question marks?

.... // how do I continue from here?

when I run "git diff 92fd1235 80bf2770" on the command line I get

diff --git a/92fd12351a4e4d6f10d30516149c624e6b9e3dc9 b/80bf2770cdbb9580bc040d49775fccd405f534dc
index 92fd123..80bf277 100644
--- a/92fd12351a4e4d6f10d30516149c624e6b9e3dc9
+++ b/80bf2770cdbb9580bc040d49775fccd405f534dc
@@ -1 +1 @@
-file1 v2
\ No newline at end of file
+file1 v4
\ No newline at end of file

how can I complete my code to get the same output as git?

Foi útil?

Solução

git_diff is the genreic interface for line-based differences, so you would put your functions for dealing with the individual lines and files.

If you're looking for something that gives you textual output as produced by git diff and similar utilities, it would be easier to go through the git_patch API.

You can call

git_patch_from_blobs()

which would give you a git_patch object which you can then pass to

git_patch_to_buf()

which will write out the content in unified diff format to a git_buf which you can use to print to whatever file/screen you want.

You can also do it by hand by providing some callbacks which would add information to some data structure of your choosing which you can then transform into unified diff or side-by-side or whatever, but for the user-case you mention, going through the patch API is much easier.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top