Created
December 14, 2023 04:46
-
-
Save brennie/72afc8e0d61a490023392e6b31608540 to your computer and use it in GitHub Desktop.
libgit2 push error demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include "git2/errors.h" | |
#include "git2/global.h" | |
#include "git2/remote.h" | |
#include "git2/repository.h" | |
#include "git2/strarray.h" | |
void git_die(const char *msg) { | |
const git_error *const err = git_error_last(); | |
fprintf(stderr, "%s: %s\n", msg, err->message); | |
exit(1); | |
} | |
#define GIT_TRY(fn, ...) \ | |
do { \ | |
if (fn(__VA_ARGS__) != 0) { \ | |
git_die(#fn); \ | |
} \ | |
} while (0) | |
int negotiate_push(const git_push_update **updates, size_t len, void *payload); | |
int main(int argc, char **argv) { | |
if (argc < 2) { | |
fprintf(stderr, "usage: %s REPOSITORY\n", argv[0]); | |
return 1; | |
} | |
git_libgit2_init(); | |
const char *const repository_path = argv[1]; | |
git_repository *repo = NULL; | |
GIT_TRY(git_repository_open, &repo, repository_path); | |
char *refspec = "+refs/heads/main:refs/heads/main"; | |
git_strarray refspecs = { | |
.count = 1, | |
.strings = &refspec, | |
}; | |
git_remote *remote; | |
GIT_TRY(git_remote_lookup, &remote, repo, "origin"); | |
git_push_options push_options = GIT_PUSH_OPTIONS_INIT; | |
push_options.callbacks.push_negotiation = negotiate_push; | |
push_options.callbacks.payload = NULL; | |
GIT_TRY(git_remote_push, remote, &refspecs, &push_options); | |
git_remote_free(remote); | |
git_repository_free(repo); | |
return 0; | |
} | |
int negotiate_push(const git_push_update **updates, size_t len, void *payload) { | |
return -1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Error: