示例#1
0
/* Given a commit and a path in it, create a new origin structure. */
static int make_origin(git_blame__origin **out, git_commit *commit, const char *path)
{
	git_blame__origin *o;
	git_object *blob;
	size_t path_len = strlen(path), alloc_len;
	int error = 0;

	if ((error = git_object_lookup_bypath(&blob, (git_object*)commit,
			path, GIT_OBJECT_BLOB)) < 0)
		return error;

	GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, sizeof(*o), path_len);
	GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, 1);
	o = git__calloc(1, alloc_len);
	GIT_ERROR_CHECK_ALLOC(o);

	o->commit = commit;
	o->blob = (git_blob *) blob;
	o->refcnt = 1;
	strcpy(o->path, path);

	*out = o;

	return 0;
}
示例#2
0
/* Given a commit and a path in it, create a new origin structure. */
static int make_origin(git_blame__origin **out, git_commit *commit, const char *path)
{
	int error = 0;
	git_blame__origin *o;

	o = (git_blame__origin *) git__calloc(1, sizeof(*o) + strlen(path) + 1);
	GITERR_CHECK_ALLOC(o);
	o->commit = commit;
	o->refcnt = 1;
	strcpy(o->path, path);

	if (!(error = git_object_lookup_bypath((git_object**)&o->blob, (git_object*)commit,
			path, GIT_OBJ_BLOB))) {
		*out = o;
	} else {
		origin_decref(o);
	}
	return error;
}
示例#3
0
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;
}