GitTest: Commit: Do not allow empty commits

This commit is contained in:
Vishesh Handa
2019-05-15 13:07:10 +02:00
parent 6cca6a5c4a
commit aa45ab07a8

View File

@ -5,13 +5,22 @@
#include <git2.h> #include <git2.h>
#define GJ_ERR_EMPTY_COMMIT -954
int handle_error(int err) int handle_error(int err)
{ {
if (err != 0) if (err != 0)
{ {
const git_error *e = giterr_last(); const git_error *e = giterr_last();
if (e)
{
printf("Error %d/%d: %s\n", err, e->klass, e->message); printf("Error %d/%d: %s\n", err, e->klass, e->message);
} }
else
{
printf("Unknown Error: %d\n", err);
}
}
return err; return err;
} }
@ -108,10 +117,6 @@ int gj_git_commit(char *git_base_path, char *author_name, char *author_email, ch
git_oid parent_id; git_oid parent_id;
git_commit *parent_commit = NULL; git_commit *parent_commit = NULL;
err = git_signature_now(&sig, author_name, author_email);
if (err < 0)
goto cleanup;
err = git_repository_open(&repo, git_base_path); err = git_repository_open(&repo, git_base_path);
if (err < 0) if (err < 0)
goto cleanup; goto cleanup;
@ -120,6 +125,17 @@ int gj_git_commit(char *git_base_path, char *author_name, char *author_email, ch
if (err < 0) if (err < 0)
goto cleanup; goto cleanup;
int numOps = git_index_entrycount(index);
if (numOps == 0)
{
err = GJ_ERR_EMPTY_COMMIT;
goto cleanup;
}
err = git_signature_now(&sig, author_name, author_email);
if (err < 0)
goto cleanup;
err = git_index_write_tree(&tree_id, index); err = git_index_write_tree(&tree_id, index);
if (err < 0) if (err < 0)
goto cleanup; goto cleanup;
@ -159,7 +175,7 @@ cleanup:
git_repository_free(repo); git_repository_free(repo);
git_signature_free(sig); git_signature_free(sig);
return 0; return err;
} }
int fetch_progress(const git_transfer_progress *stats, void *payload) int fetch_progress(const git_transfer_progress *stats, void *payload)
@ -252,10 +268,11 @@ int gj_git_pull()
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
char *git_base_path = "/tmp/journal_test"; int err;
char *git_base_path = "/tmp/foo";
//char *clone_url = "https://github.com/GitJournal/journal_test.git"; //char *clone_url = "https://github.com/GitJournal/journal_test.git";
//char *clone_url = "git@github.com:GitJournal/journal_test.git"; char *clone_url = "git@github.com:GitJournal/journal_test.git";
char *clone_url = "root@pi.local:git/test"; //char *clone_url = "root@pi.local:git/test";
char *add_pattern = "."; char *add_pattern = ".";
char *author_name = "TestMan"; char *author_name = "TestMan";
char *author_email = "TestMan@example.com"; char *author_email = "TestMan@example.com";
@ -264,14 +281,17 @@ int main(int argc, char *argv[])
git_libgit2_init(); git_libgit2_init();
//gj_git_init(git_base_path); //gj_git_init(git_base_path);
//gj_git_commit(git_base_path, author_name, author_email, message); err = gj_git_commit(git_base_path, author_name, author_email, message);
if (err < 0)
handle_error(err);
gj_git_clone(clone_url, git_base_path); //gj_git_clone(clone_url, git_base_path);
printf("We seem to be done\n"); printf("We seem to be done\n");
git_libgit2_shutdown(); git_libgit2_shutdown();
/*
int features = git_libgit2_features(); int features = git_libgit2_features();
bool supports_threading = features & GIT_FEATURE_THREADS; bool supports_threading = features & GIT_FEATURE_THREADS;
bool supports_https2 = features & GIT_FEATURE_HTTPS; bool supports_https2 = features & GIT_FEATURE_HTTPS;
@ -279,6 +299,6 @@ int main(int argc, char *argv[])
printf("Threading: %d\n", supports_threading); printf("Threading: %d\n", supports_threading);
printf("Https: %d\n", supports_https2); printf("Https: %d\n", supports_https2);
printf("SSH: %d\n", supports_ssh); printf("SSH: %d\n", supports_ssh);
*/
return 0; return 0;
} }