/** Initialize describe_options struct */ static void describe_options_init(describe_options *opts) { memset(opts, 0, sizeof(*opts)); opts->commits = NULL; opts->commit_count = 0; git_describe_init_options(&opts->describe_options, GIT_DESCRIBE_OPTIONS_VERSION); git_describe_init_format_options(&opts->format_options, GIT_DESCRIBE_FORMAT_OPTIONS_VERSION); }
//-----------------------------------------------------------------------------------------// int AdVersion::getGitVersion(QString gitDirectory, QString &version) { int ierr; QFile dir(gitDirectory+"/.git"); QByteArray tempData; git_repository *repo; git_describe_result *description; git_describe_options options; git_describe_format_options format; git_object *headObject; git_buf buffer = { 0 }; version = QString(); if(!dir.exists()) return -1; tempData = gitDirectory.toLatin1(); const char *cgitDirectory = tempData.data(); git_libgit2_init(); ierr = git_repository_open(&repo,cgitDirectory); if(ierr<0) { git_repository_free(repo); git_libgit2_shutdown(); return ierr; } ierr = git_describe_init_options(&options,GIT_DESCRIBE_OPTIONS_VERSION); options.show_commit_oid_as_fallback = 1; if(ierr<0) { git_repository_free(repo); git_libgit2_shutdown(); return ierr; } ierr = git_describe_init_format_options(&format,GIT_DESCRIBE_FORMAT_OPTIONS_VERSION); if(ierr<0) { git_repository_free(repo); git_libgit2_shutdown(); return ierr; } ierr = git_revparse_single(&headObject,repo,"HEAD"); if(ierr<0) { git_object_free(headObject); git_repository_free(repo); git_libgit2_shutdown(); return ierr; } ierr = git_describe_commit(&description,headObject,&options); if(ierr<0) { git_object_free(headObject); git_repository_free(repo); git_libgit2_shutdown(); return ierr; } ierr = git_describe_format(&buffer,description,&format); if(ierr<0) { git_object_free(headObject); git_describe_result_free(description); git_repository_free(repo); git_libgit2_shutdown(); return ierr; } version.sprintf("%s",buffer.ptr); git_object_free(headObject); git_describe_result_free(description); git_repository_free(repo); git_libgit2_shutdown(); return ERROR_NOERROR; }