Пример #1
0
void test_refs_unicode__create_and_lookup(void)
{
	git_reference *ref0, *ref1, *ref2;
	git_repository *repo2;

	const char *REFNAME = "refs/heads/" "\305" "ngstr" "\366" "m";
	const char *master = "refs/heads/master";

	/* Create the reference */
	cl_git_pass(git_reference_lookup(&ref0, repo, master));
	cl_git_pass(git_reference_create_oid(&ref1, repo, REFNAME, git_reference_oid(ref0), 0));
	cl_assert(strcmp(REFNAME, git_reference_name(ref1)) == 0);

	/* Lookup the reference in a different instance of the repository */
	cl_git_pass(git_repository_open(&repo2, "testrepo.git"));
	cl_git_pass(git_reference_lookup(&ref2, repo2, REFNAME));

	cl_assert(git_oid_cmp(git_reference_oid(ref1), git_reference_oid(ref2)) == 0);
	cl_assert(strcmp(REFNAME, git_reference_name(ref2)) == 0);

	git_reference_free(ref0);
	git_reference_free(ref1);
	git_reference_free(ref2);
	git_repository_free(repo2);
}
Пример #2
0
PyObject *
Repository_create_reference(Repository *self,  PyObject *args)
{
    PyObject *py_oid;
    git_reference *c_reference;
    char *c_name;
    git_oid oid;
    int err;

    /* 1- Get the C variables */
    if (!PyArg_ParseTuple(args, "sO", &c_name, &py_oid))
        return NULL;

    err = py_str_to_git_oid_expand(self->repo, py_oid, &oid);
    if (err < 0)
        return Error_set(err);

    /* 2- Create the reference */
    err = git_reference_create_oid(&c_reference, self->repo, c_name, &oid, 0);
    if (err < 0)
        return Error_set(err);

    /* 3- Make an instance of Reference and return it */
    return wrap_reference(c_reference);
}
Пример #3
0
int git_branch_create(
		git_reference **ref_out,
		git_repository *repository,
		const char *branch_name,
		const git_object *target,
		int force)
{
	git_object *commit = NULL;
	git_reference *branch = NULL;
	git_buf canonical_branch_name = GIT_BUF_INIT;
	int error = -1;

	assert(branch_name && target && ref_out);
	assert(git_object_owner(target) == repository);

	if (git_object_peel(&commit, (git_object *)target, GIT_OBJ_COMMIT) < 0)
		return create_error_invalid("The given target does not resolve to a commit");

	if (git_buf_joinpath(&canonical_branch_name, GIT_REFS_HEADS_DIR, branch_name) < 0)
		goto cleanup;

	error = git_reference_create_oid(&branch, repository,
		git_buf_cstr(&canonical_branch_name), git_object_id(commit), force);

	if (!error)
		*ref_out = branch;

cleanup:
	git_object_free(commit);
	git_buf_free(&canonical_branch_name);
	return error;
}
Пример #4
0
int git_branch_create(
		git_oid *oid_out,
		git_repository *repo,
		const char *branch_name,
		const git_object *target,
		int force)
{
	git_otype target_type = GIT_OBJ_BAD;
	git_object *commit = NULL;
	git_reference *branch = NULL;
	git_buf canonical_branch_name = GIT_BUF_INIT;
	int error = -1;

	assert(repo && branch_name && target && oid_out);

	if (git_object_owner(target) != repo)
		return create_error_invalid("The given target does not belong to this repository");

	target_type = git_object_type(target);

	switch (target_type)
	{
	case GIT_OBJ_TAG:
		if (git_tag_peel(&commit, (git_tag *)target) < 0)
			goto cleanup;

		if (git_object_type(commit) != GIT_OBJ_COMMIT) {
			create_error_invalid("The given target does not resolve to a commit");
			goto cleanup;
		}
		break;

	case GIT_OBJ_COMMIT:
		commit = (git_object *)target;
		break;

	default:
		return create_error_invalid("Only git_tag and git_commit objects are valid targets.");
	}

	if (git_buf_joinpath(&canonical_branch_name, GIT_REFS_HEADS_DIR, branch_name) < 0)
		goto cleanup;

	if (git_reference_create_oid(&branch, repo, git_buf_cstr(&canonical_branch_name), git_object_id(commit), force) < 0)
		goto cleanup;

	git_oid_cpy(oid_out, git_reference_oid(branch));
	error = 0;

cleanup:
	if (target_type == GIT_OBJ_TAG)
		git_object_free(commit);

	git_reference_free(branch);
	git_buf_free(&canonical_branch_name);
	return error;
}
Пример #5
0
void test_refs_branches_listall__initialize(void)
{
	git_oid id;

	cl_fixture_sandbox("testrepo.git");
	cl_git_pass(git_repository_open(&repo, "testrepo.git"));

	cl_git_pass(git_oid_fromstr(&id, "be3563ae3f795b2b4353bcce3a527ad0a4f7f644"));
	cl_git_pass(git_reference_create_oid(&fake_remote, repo, "refs/remotes/nulltoken/master", &id, 0));
}
Пример #6
0
void test_refs_branches_delete__can_delete_a_branch_pointed_at_by_detached_HEAD(void)
{
	git_reference *master, *head;

	/* Detach HEAD and make it target the commit that "master" points to */
	cl_git_pass(git_reference_lookup(&master, repo, "refs/heads/master"));
	cl_git_pass(git_reference_create_oid(&head, repo, "HEAD", git_reference_oid(master), 1));
	git_reference_free(head);
	git_reference_free(master);

	cl_git_pass(git_branch_delete(repo, "master", GIT_BRANCH_LOCAL));
}
Пример #7
0
void test_repo_head__head_detached(void)
{
	git_reference *ref;
	git_oid oid;

	cl_assert(git_repository_head_detached(repo) == 0);

	/* detach the HEAD */
	git_oid_fromstr(&oid, "c47800c7266a2be04c571c04d5a6614691ea99bd");
	cl_git_pass(git_reference_create_oid(&ref, repo, "HEAD", &oid, 1));
	cl_assert(git_repository_head_detached(repo) == 1);
	git_reference_free(ref);

	/* take the reop back to it's original state */
	cl_git_pass(git_reference_create_symbolic(&ref, repo, "HEAD", "refs/heads/master", 1));
	cl_assert(git_repository_head_detached(repo) == 0);

	git_reference_free(ref);
}
Пример #8
0
	git_reference *new_reference, *looked_up_ref;
	git_repository *repo, *repo2;
	git_oid id;
	char ref_path[GIT_PATH_MAX];

	const char *new_head = "refs/heads/new-head";

	git_oid_fromstr(&id, current_master_tip);

	must_pass(open_temp_repo(&repo, REPOSITORY_FOLDER));

	/* Retrieve the physical path to the symbolic ref for further cleaning */
	git_path_join(ref_path, repo->path_repository, new_head);

	/* Create and write the new object id reference */
	must_pass(git_reference_create_oid(&new_reference, repo, new_head, &id, 0));

	/* Ensure the reference can be looked-up... */
	must_pass(git_reference_lookup(&looked_up_ref, repo, new_head));
	must_be_true(looked_up_ref->type & GIT_REF_OID);
	must_be_true((looked_up_ref->type & GIT_REF_PACKED) == 0);
	must_be_true(strcmp(looked_up_ref->name, new_head) == 0);

	/* ...and that it points to the current master tip */
	must_be_true(git_oid_cmp(&id, git_reference_oid(looked_up_ref)) == 0);

	git_repository_free(repo);

	/* Similar test with a fresh new repository */
	must_pass(git_repository_open(&repo2, TEMP_REPO_FOLDER));
Пример #9
0
int git_commit_create(
		git_oid *oid,
		git_repository *repo,
		const char *update_ref,
		const git_signature *author,
		const git_signature *committer,
		const char *message_encoding,
		const char *message,
		const git_tree *tree,
		int parent_count,
		const git_commit *parents[])
{
	git_buf commit = GIT_BUF_INIT;
	int error, i;

	if (git_object_owner((const git_object *)tree) != repo)
		return git__throw(GIT_EINVALIDARGS, "The given tree does not belong to this repository");

	git_oid__writebuf(&commit, "tree ", git_object_id((const git_object *)tree));

	for (i = 0; i < parent_count; ++i) {
		if (git_object_owner((const git_object *)parents[i]) != repo) {
			error = git__throw(GIT_EINVALIDARGS, "The given parent does not belong to this repository");
			goto cleanup;
		}

		git_oid__writebuf(&commit, "parent ", git_object_id((const git_object *)parents[i]));
	}

	git_signature__writebuf(&commit, "author ", author);
	git_signature__writebuf(&commit, "committer ", committer);

	if (message_encoding != NULL)
		git_buf_printf(&commit, "encoding %s\n", message_encoding);

	git_buf_putc(&commit, '\n');
	git_buf_puts(&commit, message);

	if (git_buf_oom(&commit)) {
		error = git__throw(GIT_ENOMEM, "Not enough memory to build the commit data");
		goto cleanup;
	}

	error = git_odb_write(oid, git_repository_database(repo), commit.ptr, commit.size, GIT_OBJ_COMMIT);
	git_buf_free(&commit);

	if (error == GIT_SUCCESS && update_ref != NULL) {
		git_reference *head;

		error = git_reference_lookup(&head, repo, update_ref);
		if (error < GIT_SUCCESS)
			return git__rethrow(error, "Failed to create commit");

		error = git_reference_resolve(&head, head);
		if (error < GIT_SUCCESS) {
			if (error != GIT_ENOTFOUND)
				return git__rethrow(error, "Failed to create commit");
		/*
		 * The target of the reference was not found. This can happen
		 * just after a repository has been initialized (the master
		 * branch doesn't exist yet, as it doesn't have anything to
		 * point to) or after an orphan checkout, so if the target
		 * branch doesn't exist yet, create it and return.
		 */
			return git_reference_create_oid(&head, repo, git_reference_target(head), oid, 1);
		}

		error = git_reference_set_oid(head, oid);
	}

	if (error < GIT_SUCCESS)
		return git__rethrow(error, "Failed to create commit");

	return GIT_SUCCESS;

cleanup:
	git_buf_free(&commit);
	return error;
}
Пример #10
0
int main(int argc, char **argv)
{
    git_repository *repo;
    git_odb *odb;
    git_oid oid, tree_id, commit;
    git_index *index;
    char hash[41];
    int error;
    char *folder;
    int folder_size;
    git_signature *author, *committer;
    git_tree *tree;
    git_reference *master;

    hash[40] = '\0';

    if(argc != 2)    {
        printf("usage: migit <path-to-git-repo>\n");
        return 1;
    }

    folder_size = strlen(argv[1]) + 6;
    folder = malloc(folder_size);
    sprintf(folder, "%s/.git", argv[1]);
    folder[folder_size - 1] = '\0';

    create_sample_file(argv[1]);

    error = git_repository_open(&repo, folder);
    if(error) {
        die("could not open repository", error);
    }

    odb = git_repository_database(repo);
    git_odb_write(&oid, odb, content, strlen(content) * sizeof(char),
                  GIT_OBJ_BLOB);


    error = git_repository_index(&index, repo);
    if(error) {
        die("could not open index", error);
    }

    error = git_index_add(index, fname, 0);
    if(error) {
        die("could not add file to index", error);
    }
    assert(git_index_entrycount(index) == 1);

    error = git_tree_create_fromindex(&tree_id, index);
    if(error) {
        die("could not create tree from index", error);
    }

    error = git_tree_lookup(&tree, repo, &tree_id);
    if(error) {
        die("could not create tree from id", error);
    }

    error = git_signature_new(&author, "helino", "*****@*****.**", 12345, 0);
    if(error) {
        die("could not create signature for author", error);
    }
    error = git_signature_new(&committer, "irock", "*****@*****.**", 12345, 0);
    if(error) {
        die("could not create signature for committer", error);
    }

    error = git_commit_create(&commit, repo, NULL, author, committer,
                              NULL, "hello git", tree, 0, NULL);
    if(error) {
        die("could not commit", error);
    }

    git_oid_fmt(hash, &commit);
    printf("commit: %s\n", hash);

    error = git_reference_create_oid(&master, repo, "refs/heads/master", &commit, 1);
    if(error) {
        die("could not create master reference", error);
    }

    git_repository_free(repo);
    free(folder);

    return 0;
}