Exemple #1
0
PyObject *
Repository_create_commit(Repository *self, PyObject *args)
{
    Signature *py_author, *py_committer;
    PyObject *py_oid, *py_message, *py_parents, *py_parent;
    PyObject *py_result = NULL;
    PyObject *tmessage;
    const char *message = NULL;
    char *update_ref = NULL;
    char *encoding = NULL;
    git_oid oid;
    git_tree *tree = NULL;
    int parent_count;
    git_commit **parents = NULL;
    int err = 0, i = 0;
    size_t len;

    if (!PyArg_ParseTuple(args, "zO!O!OOO!|s",
                          &update_ref,
                          &SignatureType, &py_author,
                          &SignatureType, &py_committer,
                          &py_message,
                          &py_oid,
                          &PyList_Type, &py_parents,
                          &encoding))
        return NULL;

    len = py_oid_to_git_oid(py_oid, &oid);
    if (len == 0)
        return NULL;

    message = py_str_borrow_c_str(&tmessage, py_message, encoding);
    if (message == NULL)
        return NULL;

    err = git_tree_lookup_prefix(&tree, self->repo, &oid, len);
    if (err < 0) {
        Error_set(err);
        goto out;
    }

    parent_count = (int)PyList_Size(py_parents);
    parents = malloc(parent_count * sizeof(git_commit*));
    if (parents == NULL) {
        PyErr_SetNone(PyExc_MemoryError);
        goto out;
    }
    for (; i < parent_count; i++) {
        py_parent = PyList_GET_ITEM(py_parents, i);
        len = py_oid_to_git_oid(py_parent, &oid);
        if (len == 0)
            goto out;
        err = git_commit_lookup_prefix(&parents[i], self->repo, &oid, len);
        if (err < 0) {
            Error_set(err);
            goto out;
        }
    }

    err = git_commit_create(&oid, self->repo, update_ref,
                            py_author->signature, py_committer->signature,
                            encoding, message, tree, parent_count,
                            (const git_commit**)parents);
    if (err < 0) {
        Error_set(err);
        goto out;
    }

    py_result = git_oid_to_python(&oid);

out:
    Py_DECREF(tmessage);
    git_tree_free(tree);
    while (i > 0) {
        i--;
        git_commit_free(parents[i]);
    }
    free(parents);
    return py_result;
}
Exemple #2
0
int cmd_rev_list(git_repository *repo, int argc, char **argv)
{
	const char *commit_string = NULL;
	const char *format = NULL;
	int i = 0;
	git_oid commit_oid;
	git_oid current_oid;
	char current_oid_str[GIT_OID_HEXSZ+1];
	int err = 0;
	int rc = EXIT_FAILURE;
	git_commit *commit;
	git_revwalk *walk;

	for (i = 1; i < argc; i++)
	{
		if (*argv[i] == '-')
		{
			if (!prefixcmp(argv[i], "--pretty="))
			{
				format = argv[i] + strlen("--pretty=");
			} else
			{
				fprintf(stderr,"Unknown option \"%s\"\n",argv[i]);
				goto out;
			}
		} else
		{
			commit_string = argv[i];
		}
	}

	if (!commit_string)
	{
		fprintf(stderr,"No commit id given!\n");
		goto out;
	}

	if (format)
	{
		fprintf(stderr,"Format \"%s\" not supported!\n",format);
		goto out;
	}

	if ((err = git_oid_fromstrn(&commit_oid,commit_string,strlen(commit_string))) != GIT_OK)
		goto out;

	if ((err = git_commit_lookup_prefix(&commit,repo,&commit_oid,strlen(commit_string))) != GIT_OK)
		goto out;

	if ((err = git_revwalk_new(&walk, repo)) != GIT_OK)
		goto out;

	git_revwalk_sorting(walk, GIT_SORT_TIME);

	if ((err = git_revwalk_push(walk, &commit_oid)) != GIT_OK)
		goto out;

	while ((git_revwalk_next(&current_oid, walk)) == GIT_OK)
	{
		struct git_commit *wcommit;

		if (git_commit_lookup(&wcommit, repo, &current_oid) != 0)
			continue;

		git_oid_tostr(current_oid_str,sizeof(current_oid_str),&current_oid);
		printf("%s\n",current_oid_str);
		git_commit_free(wcommit);
	}

	git_revwalk_free(walk);
out:
	if (err) libgit_error();
	if (commit) git_commit_free(commit);
	return rc;
}