void test_repo_open__bad_gitlinks(void) { git_repository *repo; static const char *bad_links[] = { "garbage\n", "gitdir", "gitdir:\n", "gitdir: foobar", "gitdir: ../invalid", "gitdir: ../invalid2", "gitdir: ../attr/.git with extra stuff", NULL }; const char **scan; cl_git_sandbox_init("attr"); cl_git_pass(p_mkdir("alternate", 0777)); cl_git_pass(p_mkdir("invalid", 0777)); cl_git_pass(git_futils_mkdir_r("invalid2/.git", NULL, 0777)); for (scan = bad_links; *scan != NULL; scan++) { cl_git_rewritefile("alternate/.git", *scan); cl_git_fail(git_repository_open_ext(&repo, "alternate", 0, NULL)); } git_futils_rmdir_r("invalid", NULL, GIT_DIRREMOVAL_FILES_AND_DIRS); git_futils_rmdir_r("invalid2", NULL, GIT_DIRREMOVAL_FILES_AND_DIRS); }
git_repository * open_repo(char * dir){ git_repository *repo = NULL; int ret; ret = git_repository_open_ext(&repo, dir, 0, NULL); if (ret < 0){ fprintf(stderr, "Error opening repository\n"); } return repo; }
int main(int argc, char **argv) { struct stat st; struct timeval start, end; char filepath[1024], repodir[64]; char *dirpath, *filename, *ref = NULL; if (argc != 3) { fprintf(stderr, "Usage: ./log dirpath filename\n"); exit(-1); } dirpath = argv[1]; filename = argv[2]; strcpy(repodir, dirpath); strcat(repodir, "/.git"); strcpy(filepath, dirpath); strcat(filepath, "/"); strcat(filepath, filename); if (stat(filepath, &st) < 0) { fprintf(stderr, "Not valid file path: \"%s\"!\n", filepath); exit(-2); } memset(&s, 0, sizeof(s)); s.sorting = GIT_SORT_TIME; s.hide = 0; s.repodir = strlen(repodir) > 0 ? repodir : "/tmp/git/.git"; s.ref = ref ? ref : "refs/heads/master"; /* Init libgit2 library */ git_libgit2_init(); /* Open repo. */ check_lg2(git_repository_open_ext(&s.repo, s.repodir, 0, NULL), "Could not open repository", s.repodir); /* Create revwalker. */ check_lg2(git_revwalk_new(&s.walker, s.repo), "Could not create revision walker", NULL); git_revwalk_sorting(s.walker, s.sorting); /* Show file's latest commit. */ printf("filename: %s\n", filename); gettimeofday(&start, NULL); git_show_last_commit(filename); gettimeofday(&end, NULL); printf("time span: %ld(ms)\n", (end.tv_sec - start.tv_sec) * 1000 + \ (end.tv_usec - start.tv_usec) / 1000); git_revwalk_free(s.walker); git_repository_free(s.repo); git_libgit2_shutdown(); return 0; }
void MainWindow::on_pushButtonDir_clicked() { QFileDialog dialog; dialog.setFileMode(QFileDialog::Directory); dialog.setOption(QFileDialog::ShowDirsOnly); int res = dialog.exec(); if (res) { QString mypath = dialog.selectedFiles()[0]; ui->textRepoLocation->setText(mypath); hasDir = true; const char* repo_dir = ui->textRepoLocation->text().toStdString().c_str(); if (git_repository_open_ext( NULL, repo_dir, GIT_REPOSITORY_OPEN_NO_SEARCH, NULL) == 0) { //!!!! so apparently the repo_dir value gets changed //! when you call open above to check things: .git gets appended to it, so ... //! this is ugly sandbox code anyway... repo_dir = ui->textRepoLocation->text().toStdString().c_str(); int ret = git_repository_open(&repo, repo_dir); qDebug() << "opening existing repo at " << ui->textRepoLocation->text() << "ret = " << ret; ui->pushButtonInit->setDisabled(true); //load em tags ui->listRevisions->clear(); git_strarray tags = {0}; int error = git_tag_list(&tags, repo); qDebug() <<"found numTags =" <<tags.count; if (error == 0) { QString text_str; for (int i=0; i<tags.count; i++) { text_str+=tags.strings[i]; text_str+="\n"; ui->listRevisions->addItem(tags.strings[i]); } } //look up commit info lookupCommits(); } else { ui->pushButtonInit->setEnabled(true); } } }
NotesModel::NotesModel(QObject *parent) : QAbstractListModel(parent) { m_categories=QStringList(QString("All")); m_filter=QString("All"); QDir notesdir = notesFolder(); if (!(git_repository_open_ext( NULL, notesdir.absolutePath().toUtf8().constData() , GIT_REPOSITORY_OPEN_NO_SEARCH, NULL) == 0)) { git_repository *repo = NULL; // Init Git Repository if not int gerr = git_repository_init(&repo, notesdir.absolutePath().toUtf8().constData(), false); if (gerr < 0) { const git_error *e = giterr_last(); qDebug() << "Libgit error : %s\n" << e->message; emit error(QString().fromLatin1(e->message)); } //Create Initial Commit git_signature *sig; git_index *index; git_oid tree_id, commit_id; git_tree *tree; gerr = git_signature_now(&sig, "sparkleNotes", "*****@*****.**"); gerr = git_repository_index(&index, repo); if (!checkGitErr(gerr)) { gerr = git_index_write_tree(&tree_id, index); if (!checkGitErr(gerr)) { git_index_free(index); gerr = git_tree_lookup(&tree, repo, &tree_id); if (!checkGitErr(gerr)) { gerr = git_commit_create_v( &commit_id, repo, "HEAD", sig, sig, NULL, "Initial commit", tree, 0); } git_tree_free(tree); } } git_signature_free(sig); git_repository_free(repo); } // Add already existing files in case created outside sparkleNotes updateGitStatus(); QtConcurrent::run(this, &NotesModel::pullMergePush); }
int main (int argc, char** argv) { git_repository *repo; git_index *index; unsigned int i, ecount; char *dir = "."; char out[41]; out[40] = '\0'; if (argc > 1) dir = argv[1]; if (argc > 2) { fprintf(stderr, "usage: showindex [<repo-dir>]\n"); return 1; } if (git_repository_open_ext(&repo, dir, 0, NULL) < 0) { fprintf(stderr, "could not open repository: %s\n", dir); return 1; } git_repository_index(&index, repo); git_index_read(index); ecount = git_index_entrycount(index); if (!ecount) printf("Empty index\n"); for (i = 0; i < ecount; ++i) { const git_index_entry *e = git_index_get_byindex(index, i); git_oid_fmt(out, &e->oid); printf("File Path: %s\n", e->path); printf(" Stage: %d\n", git_index_entry_stage(e)); printf(" Blob SHA: %s\n", out); printf("File Size: %d\n", (int)e->file_size); printf(" Device: %d\n", (int)e->dev); printf(" Inode: %d\n", (int)e->ino); printf(" UID: %d\n", (int)e->uid); printf(" GID: %d\n", (int)e->gid); printf(" ctime: %d\n", (int)e->ctime.seconds); printf(" mtime: %d\n", (int)e->mtime.seconds); printf("\n"); } git_index_free(index); git_repository_free(repo); return 0; }
static int parse_revision(struct parse_state *ps, const char *revstr) { git_revspec rs; char str[GIT_OID_HEXSZ + 1]; if (!ps->repo) { if (!ps->repodir) ps->repodir = "."; check(git_repository_open_ext(&ps->repo, ps->repodir, 0, NULL), "Could not open repository from", ps->repodir); } check(git_revparse(&rs, ps->repo, revstr), "Could not parse", revstr); if ((rs.flags & GIT_REVPARSE_SINGLE) != 0) { git_oid_tostr(str, sizeof(str), git_object_id(rs.from)); printf("%s\n", str); git_object_free(rs.from); } else if ((rs.flags & GIT_REVPARSE_RANGE) != 0) { git_oid_tostr(str, sizeof(str), git_object_id(rs.to)); printf("%s\n", str); git_object_free(rs.to); if ((rs.flags & GIT_REVPARSE_MERGE_BASE) != 0) { git_oid base; check(git_merge_base(&base, ps->repo, git_object_id(rs.from), git_object_id(rs.to)), "Could not find merge base", revstr); git_oid_tostr(str, sizeof(str), &base); printf("%s\n", str); } git_oid_tostr(str, sizeof(str), git_object_id(rs.from)); printf("^%s\n", str); git_object_free(rs.from); } else { check(0, "Invalid results from git_revparse", revstr); } return 0; }
bool gitCore::is_git_dir(QString path) { git_repository *e; int res = git_repository_open_ext( &e, path.toStdString().data(), 0, "."); switch( res ) { case 0: { // RepositoryNode *r = new RepositoryNode (e, path); // qDebug () << r->getDirPath() << r->getDisplayName (); _Model->addRepository(e, path); // _repoList.append( r ); // emit newRepository(r); return true; } break; default: qDebug() << "Not found git repository"; } }
int main(int argc, char **argv) { git_repository *repo; describe_options opts; git_libgit2_init(); check_lg2(git_repository_open_ext(&repo, ".", 0, NULL), "Could not open repository", NULL); describe_options_init(&opts); parse_options(&opts, argc, argv); do_describe(repo, &opts); git_repository_free(repo); git_libgit2_shutdown(); return 0; }
int main (int argc, char **argv) { git_repository *repo; git_revwalk *walk; git_oid oid; char buf[GIT_OID_HEXSZ+1]; git_libgit2_init(); check_lg2(git_repository_open_ext(&repo, ".", 0, NULL), "opening repository", NULL); check_lg2(git_revwalk_new(&walk, repo), "allocating revwalk", NULL); check_lg2(revwalk_parseopts(repo, walk, argc-1, argv+1), "parsing options", NULL); while (!git_revwalk_next(&oid, walk)) { git_oid_fmt(buf, &oid); buf[GIT_OID_HEXSZ] = '\0'; printf("%s\n", buf); } git_libgit2_shutdown(); return 0; }
void test_repo_open__open_with_discover(void) { static const char *variants[] = { "attr", "attr/", "attr/.git", "attr/.git/", "attr/sub", "attr/sub/", "attr/sub/sub", "attr/sub/sub/", NULL }; git_repository *repo; const char **scan; cl_fixture_sandbox("attr"); cl_git_pass(p_rename("attr/.gitted", "attr/.git")); for (scan = variants; *scan != NULL; scan++) { cl_git_pass(git_repository_open_ext(&repo, *scan, 0, NULL)); cl_assert(git__suffixcmp(git_repository_path(repo), "attr/.git/") == 0); cl_assert(git__suffixcmp(git_repository_workdir(repo), "attr/") == 0); git_repository_free(repo); } cl_fixture_cleanup("attr"); }
int main(int argc, char **argv) { git_repository *repo; tag_options opts; tag_action action; tag_state state; git_threads_init(); check_lg2(git_repository_open_ext(&repo, ".", 0, NULL), "Could not open repository", NULL); tag_options_init(&opts); parse_options(&action, &opts, argc, argv); state.repo = repo; state.opts = &opts; action(&state); git_repository_free(repo); git_threads_shutdown(); return 0; }
/** Entry point for this command */ int main(int argc, char *argv[]) { git_repository *repo; struct opts o = { ".", NULL, 0, 0 }; git_object *obj = NULL; char oidstr[GIT_OID_HEXSZ + 1]; git_libgit2_init(); parse_opts(&o, argc, argv); check_lg2(git_repository_open_ext(&repo, o.dir, 0, NULL), "Could not open repository", NULL); check_lg2(git_revparse_single(&obj, repo, o.rev), "Could not resolve", o.rev); if (o.verbose) { char oidstr[GIT_OID_HEXSZ + 1]; git_oid_tostr(oidstr, sizeof(oidstr), git_object_id(obj)); printf("%s %s\n--\n", git_object_type2string(git_object_type(obj)), oidstr); } switch (o.action) { case SHOW_TYPE: printf("%s\n", git_object_type2string(git_object_type(obj))); break; case SHOW_SIZE: { git_odb *odb; git_odb_object *odbobj; check_lg2(git_repository_odb(&odb, repo), "Could not open ODB", NULL); check_lg2(git_odb_read(&odbobj, odb, git_object_id(obj)), "Could not find obj", NULL); printf("%ld\n", (long)git_odb_object_size(odbobj)); git_odb_object_free(odbobj); git_odb_free(odb); } break; case SHOW_NONE: /* just want return result */ break; case SHOW_PRETTY: switch (git_object_type(obj)) { case GIT_OBJ_BLOB: show_blob((const git_blob *)obj); break; case GIT_OBJ_COMMIT: show_commit((const git_commit *)obj); break; case GIT_OBJ_TREE: show_tree((const git_tree *)obj); break; case GIT_OBJ_TAG: show_tag((const git_tag *)obj); break; default: printf("unknown %s\n", oidstr); break; } break; } git_object_free(obj); git_repository_free(repo); git_libgit2_shutdown(); return 0; }
static void env_fail_(const char *path, const char *file, int line) { git_repository *repo; cl_git_fail_at_line(git_repository_open_ext(NULL, path, GIT_REPOSITORY_OPEN_FROM_ENV, NULL), file, line); cl_git_fail_at_line(git_repository_open_ext(&repo, path, GIT_REPOSITORY_OPEN_FROM_ENV, NULL), file, line); }
void test_repo_env__open(void) { git_repository *repo = NULL; git_buf repo_dir_buf = GIT_BUF_INIT; const char *repo_dir = NULL; git_index *index = NULL; const char *t_obj = "testrepo.git/objects"; const char *p_obj = "peeled.git/objects"; clear_git_env(); cl_fixture_sandbox("attr"); cl_fixture_sandbox("testrepo.git"); cl_fixture_sandbox("peeled.git"); cl_git_pass(p_rename("attr/.gitted", "attr/.git")); cl_git_pass(git_path_prettify_dir(&repo_dir_buf, "attr", NULL)); repo_dir = git_buf_cstr(&repo_dir_buf); /* GIT_DIR that doesn't exist */ cl_setenv("GIT_DIR", "does-not-exist"); env_fail(NULL); /* Explicit start_path overrides GIT_DIR */ env_pass("attr"); env_pass("attr/.git"); env_pass("attr/sub"); env_pass("attr/sub/sub"); /* GIT_DIR with relative paths */ cl_setenv("GIT_DIR", "attr/.git"); env_pass(NULL); cl_setenv("GIT_DIR", "attr"); env_fail(NULL); cl_setenv("GIT_DIR", "attr/sub"); env_fail(NULL); cl_setenv("GIT_DIR", "attr/sub/sub"); env_fail(NULL); /* GIT_DIR with absolute paths */ cl_setenv_printf("GIT_DIR", "%s/.git", repo_dir); env_pass(NULL); cl_setenv("GIT_DIR", repo_dir); env_fail(NULL); cl_setenv_printf("GIT_DIR", "%s/sub", repo_dir); env_fail(NULL); cl_setenv_printf("GIT_DIR", "%s/sub/sub", repo_dir); env_fail(NULL); cl_setenv("GIT_DIR", NULL); /* Searching from the current directory */ env_cd_pass("attr"); env_cd_pass("attr/.git"); env_cd_pass("attr/sub"); env_cd_pass("attr/sub/sub"); /* A ceiling directory blocks searches from ascending into that * directory, but doesn't block the start_path itself. */ cl_setenv("GIT_CEILING_DIRECTORIES", repo_dir); env_cd_pass("attr"); env_cd_fail("attr/sub"); env_cd_fail("attr/sub/sub"); cl_setenv_printf("GIT_CEILING_DIRECTORIES", "%s/sub", repo_dir); env_cd_pass("attr"); env_cd_pass("attr/sub"); env_cd_fail("attr/sub/sub"); /* Multiple ceiling directories */ cl_setenv_printf("GIT_CEILING_DIRECTORIES", "123%c%s/sub%cabc", GIT_PATH_LIST_SEPARATOR, repo_dir, GIT_PATH_LIST_SEPARATOR); env_cd_pass("attr"); env_cd_pass("attr/sub"); env_cd_fail("attr/sub/sub"); cl_setenv_printf("GIT_CEILING_DIRECTORIES", "%s%c%s/sub", repo_dir, GIT_PATH_LIST_SEPARATOR, repo_dir); env_cd_pass("attr"); env_cd_fail("attr/sub"); env_cd_fail("attr/sub/sub"); cl_setenv_printf("GIT_CEILING_DIRECTORIES", "%s/sub%c%s", repo_dir, GIT_PATH_LIST_SEPARATOR, repo_dir); env_cd_pass("attr"); env_cd_fail("attr/sub"); env_cd_fail("attr/sub/sub"); cl_setenv_printf("GIT_CEILING_DIRECTORIES", "%s%c%s/sub/sub", repo_dir, GIT_PATH_LIST_SEPARATOR, repo_dir); env_cd_pass("attr"); env_cd_fail("attr/sub"); env_cd_fail("attr/sub/sub"); cl_setenv("GIT_CEILING_DIRECTORIES", NULL); /* Index files */ cl_setenv("GIT_INDEX_FILE", cl_fixture("gitgit.index")); cl_git_pass(git_repository_open_ext(&repo, "attr", GIT_REPOSITORY_OPEN_FROM_ENV, NULL)); cl_git_pass(git_repository_index(&index, repo)); cl_assert_equal_s(git_index_path(index), cl_fixture("gitgit.index")); cl_assert_equal_i(git_index_entrycount(index), 1437); git_index_free(index); git_repository_free(repo); cl_setenv("GIT_INDEX_FILE", NULL); /* Namespaces */ cl_setenv("GIT_NAMESPACE", "some-namespace"); cl_git_pass(git_repository_open_ext(&repo, "attr", GIT_REPOSITORY_OPEN_FROM_ENV, NULL)); cl_assert_equal_s(git_repository_get_namespace(repo), "some-namespace"); git_repository_free(repo); cl_setenv("GIT_NAMESPACE", NULL); /* Object directories and alternates */ env_check_objects(true, false, false); cl_setenv("GIT_OBJECT_DIRECTORY", t_obj); env_check_objects(false, true, false); cl_setenv("GIT_OBJECT_DIRECTORY", NULL); cl_setenv("GIT_ALTERNATE_OBJECT_DIRECTORIES", t_obj); env_check_objects(true, true, false); cl_setenv("GIT_ALTERNATE_OBJECT_DIRECTORIES", NULL); cl_setenv("GIT_OBJECT_DIRECTORY", p_obj); env_check_objects(false, false, true); cl_setenv("GIT_OBJECT_DIRECTORY", NULL); cl_setenv("GIT_OBJECT_DIRECTORY", t_obj); cl_setenv("GIT_ALTERNATE_OBJECT_DIRECTORIES", p_obj); env_check_objects(false, true, true); cl_setenv("GIT_ALTERNATE_OBJECT_DIRECTORIES", NULL); cl_setenv("GIT_OBJECT_DIRECTORY", NULL); cl_setenv_printf("GIT_ALTERNATE_OBJECT_DIRECTORIES", "%s%c%s", t_obj, GIT_PATH_LIST_SEPARATOR, p_obj); env_check_objects(true, true, true); cl_setenv("GIT_ALTERNATE_OBJECT_DIRECTORIES", NULL); cl_setenv_printf("GIT_ALTERNATE_OBJECT_DIRECTORIES", "%s%c%s", p_obj, GIT_PATH_LIST_SEPARATOR, t_obj); env_check_objects(true, true, true); cl_setenv("GIT_ALTERNATE_OBJECT_DIRECTORIES", NULL); cl_fixture_cleanup("peeled.git"); cl_fixture_cleanup("testrepo.git"); cl_fixture_cleanup("attr"); git_buf_free(&repo_dir_buf); clear_git_env(); }
QStringList KateProjectWorker::filesFromGit(const QDir &dir, bool recursive) { // init libgit2, we require at least 0.22 which has this function! // do this here to have init in this thread done, shutdown afterwards again! git_libgit2_init(); QStringList files; git_repository *repo = nullptr; git_object *root_tree = nullptr, *tree = nullptr; // check if the repo can be opened. // git_repository_open_ext() will return 0 if everything is OK; // if not, return an empty files list const QByteArray repoPathUtf8 = dir.path().toUtf8(); if (git_repository_open_ext(&repo, repoPathUtf8.constData(), 0, NULL)) { git_libgit2_shutdown(); return files; } // get the working directory of the repo // if none was found, return an empty files list const char *working_dir = nullptr; if ((working_dir = git_repository_workdir(repo)) == nullptr) { git_repository_free(repo); git_libgit2_shutdown(); return files; } if (git_revparse_single(&root_tree, repo, "HEAD^{tree}")) { git_repository_free(repo); git_libgit2_shutdown(); return files; } QDir workdir; workdir.setPath(QString::fromUtf8(working_dir)); const QByteArray relpathUtf8 = workdir.relativeFilePath(dir.path()).toUtf8(); if (relpathUtf8.isEmpty() || relpathUtf8 == ".") { // git_object_lookup_bypath is not able to resolv "." as path tree = root_tree; } else { if (git_object_lookup_bypath(&tree, root_tree, relpathUtf8.constData(), GIT_OBJ_TREE)) { git_object_free(root_tree); git_repository_free(repo); git_libgit2_shutdown(); return files; } } QString path = workdir.absolutePath() + QDir::separator(); files.append(gitSearchTree(tree, path, recursive)); if (recursive && relpathUtf8.isEmpty()) { files.append(gitSearchSubmodules(repo, path)); } files.append(gitSearchStatusList(repo, path)); if (tree != root_tree) { git_object_free(tree); } git_object_free(root_tree); git_repository_free(repo); git_libgit2_shutdown(); return files; }
int main(int argc, char *argv[]) { const char *dir = ".", *rev = NULL; int i, action = 0, verbose = 0; git_object *obj = NULL; char oidstr[GIT_OID_HEXSZ + 1]; git_threads_init(); for (i = 1; i < argc; ++i) { char *a = argv[i]; if (a[0] != '-') { if (rev != NULL) usage("Only one rev should be provided", NULL); else rev = a; } else if (!strcmp(a, "-t")) action = SHOW_TYPE; else if (!strcmp(a, "-s")) action = SHOW_SIZE; else if (!strcmp(a, "-e")) action = SHOW_NONE; else if (!strcmp(a, "-p")) action = SHOW_PRETTY; else if (!strcmp(a, "-q")) verbose = 0; else if (!strcmp(a, "-v")) verbose = 1; else if (!strcmp(a, "--help") || !strcmp(a, "-h")) usage(NULL, NULL); else if (!check_str_param(a, "--git-dir=", &dir)) usage("Unknown option", a); } if (!action || !rev) usage(NULL, NULL); check(git_repository_open_ext(&g_repo, dir, 0, NULL), "Could not open repository"); if (git_revparse_single(&obj, g_repo, rev) < 0) { fprintf(stderr, "Could not resolve '%s'\n", rev); exit(1); } if (verbose) { char oidstr[GIT_OID_HEXSZ + 1]; git_oid_tostr(oidstr, sizeof(oidstr), git_object_id(obj)); printf("%s %s\n--\n", git_object_type2string(git_object_type(obj)), oidstr); } switch (action) { case SHOW_TYPE: printf("%s\n", git_object_type2string(git_object_type(obj))); break; case SHOW_SIZE: { git_odb *odb; git_odb_object *odbobj; check(git_repository_odb(&odb, g_repo), "Could not open ODB"); check(git_odb_read(&odbobj, odb, git_object_id(obj)), "Could not find obj"); printf("%ld\n", (long)git_odb_object_size(odbobj)); git_odb_object_free(odbobj); git_odb_free(odb); } break; case SHOW_NONE: /* just want return result */ break; case SHOW_PRETTY: switch (git_object_type(obj)) { case GIT_OBJ_BLOB: show_blob((const git_blob *)obj); break; case GIT_OBJ_COMMIT: show_commit((const git_commit *)obj); break; case GIT_OBJ_TREE: show_tree((const git_tree *)obj); break; case GIT_OBJ_TAG: show_tag((const git_tag *)obj); break; default: printf("unknown %s\n", oidstr); break; } break; } git_object_free(obj); git_repository_free(g_repo); git_threads_shutdown(); return 0; }
static gboolean submain (struct EvTag *self, int argc, char **argv, GError **error) { gboolean ret = FALSE; git_status_options statusopts = GIT_STATUS_OPTIONS_INIT; GCancellable *cancellable = NULL; const char *command_name = NULL; Subcommand *command; char *prgname = NULL; int in, out; int r; /* * Parse the global options. We rearrange the options as * necessary, in order to pass relevant options through * to the commands, but also have them take effect globally. */ for (in = 1, out = 1; in < argc; in++, out++) { /* The non-option is the command, take it out of the arguments */ if (argv[in][0] != '-') { if (command_name == NULL) { command_name = argv[in]; out--; continue; } } else if (g_str_equal (argv[in], "--")) { break; } argv[out] = argv[in]; } argc = out; command = commands; while (command->name) { if (g_strcmp0 (command_name, command->name) == 0) break; command++; } if (!command->fn) { GOptionContext *context; char *help; context = option_context_new_with_commands (commands); /* This will not return for some options (e.g. --version). */ if (option_context_parse (context, NULL, &argc, &argv, cancellable, error)) { if (command_name == NULL) { g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, "No command specified"); } else { g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Unknown command '%s'", command_name); } } help = g_option_context_get_help (context, FALSE, NULL); g_printerr ("%s", help); g_option_context_free (context); goto out; } prgname = g_strdup_printf ("%s %s", g_get_prgname (), command_name); g_set_prgname (prgname); r = git_repository_open_ext (&self->top_repo, ".", 0, NULL); if (!handle_libgit_ret (r, error)) goto out; r = git_status_init_options (&statusopts, GIT_STATUS_OPTIONS_VERSION); if (!handle_libgit_ret (r, error)) goto out; { struct TreeWalkData twdata = { FALSE, self, self->top_repo, NULL, cancellable, error }; r = git_status_foreach_ext (self->top_repo, &statusopts, status_cb, &twdata); if (twdata.caught_error) goto out; if (!handle_libgit_ret (r, error)) goto out; } self->checksum = g_checksum_new (G_CHECKSUM_SHA512); if (!command->fn (self, argc, argv, cancellable, error)) goto out; ret = TRUE; out: return ret; }
int git_repository_open(git_repository **repo_out, const char *path) { return git_repository_open_ext( repo_out, path, GIT_REPOSITORY_OPEN_NO_SEARCH, NULL); }
Repository::Repository(std::string const & dir) { if (git_repository_open_ext(&repo_, dir.c_str(), 0, NULL)) throw repository_open_error(); }
int main(int argc, char *argv[]) { int r = 0; const char *command; enum Error command_error = ERROR_NONE; int i = 0; int end = 0; if(argc < 2) { usage_instructions(); goto error_usage; } command = argv[1]; argc -= 2; argv = &argv[2]; for(i = 0, end = sizeof(commands) / sizeof(commands[0]); i != end; ++i) { const struct Command *cmp_command = &commands[i]; if(i == end) { fprintf(stderr, "Unrecognised command \"%s\"\n", command); goto error_invalid_command; } else if(strcmp(cmp_command->command, command) == 0) { int error = 0; error = git_repository_open_ext(&repo_handle, ".", 0, NULL); if(error != 0) { fprintf(stderr, "Invalid git repository\n"); r = 1; goto error_git_repository_open_ext; } command_error = cmp_command->function(argc, argv); break; } } switch(command_error) { case ERROR_SILENT: r = 1; case ERROR_NONE: break; case ERROR_INTERNAL: { const git_error *git_error = giterr_last(); if(git_error) { fprintf(stderr, "Internal Git Error: %s\n", git_error->message); } } default: fprintf(stderr, "git-big encountered an error:\n" "%s\n", error_string_table[command_error]); r = 1; break; } git_repository_free(repo_handle); error_git_repository_open_ext: error_invalid_command: error_usage: return r; }