Beispiel #1
0
void test_repo_open__force_bare(void)
{
	/* need to have both repo dir and workdir set up correctly */
	git_repository *repo = cl_git_sandbox_init("empty_standard_repo");
	git_repository *barerepo;

	make_gitlink_dir("alternate", "gitdir: ../empty_standard_repo/.git");

	cl_assert(!git_repository_is_bare(repo));

	cl_git_pass(git_repository_open(&barerepo, "alternate"));
	cl_assert(!git_repository_is_bare(barerepo));
	git_repository_free(barerepo);

	cl_git_pass(git_repository_open_bare(
		&barerepo, "empty_standard_repo/.git"));
	cl_assert(git_repository_is_bare(barerepo));
	git_repository_free(barerepo);

	cl_git_fail(git_repository_open_bare(&barerepo, "alternate/.git"));

	cl_git_pass(git_repository_open_ext(
		&barerepo, "alternate/.git", GIT_REPOSITORY_OPEN_BARE, NULL));
	cl_assert(git_repository_is_bare(barerepo));
	git_repository_free(barerepo);

	cl_git_pass(p_mkdir("empty_standard_repo/subdir", 0777));
	cl_git_mkfile("empty_standard_repo/subdir/something.txt", "something");

	cl_git_fail(git_repository_open_bare(
		&barerepo, "empty_standard_repo/subdir"));

	cl_git_pass(git_repository_open_ext(
		&barerepo, "empty_standard_repo/subdir", GIT_REPOSITORY_OPEN_BARE, NULL));
	cl_assert(git_repository_is_bare(barerepo));
	git_repository_free(barerepo);

	cl_git_pass(p_mkdir("alternate/subdir", 0777));
	cl_git_pass(p_mkdir("alternate/subdir/sub2", 0777));
	cl_git_mkfile("alternate/subdir/sub2/something.txt", "something");

	cl_git_fail(git_repository_open_bare(&barerepo, "alternate/subdir/sub2"));

	cl_git_pass(git_repository_open_ext(
		&barerepo, "alternate/subdir/sub2", GIT_REPOSITORY_OPEN_BARE, NULL));
	cl_assert(git_repository_is_bare(barerepo));
	git_repository_free(barerepo);
}
Beispiel #2
0
static int read_file_git(const char *repo_path, const char *name, void **out, size_t *outlen) {
    int ret, retcode = -1;

    git_repository *repo;
    ret = git_repository_open_bare(&repo, repo_path);
    if(check_lg2(ret, "opening repo"))
        goto out;

    git_object *master;
    ret = git_revparse_single(&master, repo, "master");
    if(check_lg2(ret, "getting master branch"))
        goto out_repo;

    if(git_object_type(master) != GIT_OBJ_COMMIT) {
        debug("master is not a commit");
        goto out_master;
    }

    git_tree *tree;
    ret = git_commit_tree(&tree, (git_commit*)master);
    if(check_lg2(ret, "getting tree from commit"))
        goto out_master;

    const git_tree_entry *entry = git_tree_entry_byname(tree, name);
    if(!entry) {
        debug("entry %s not found", name);
        goto out_tree;
    }

    if(git_tree_entry_type(entry) != GIT_OBJ_BLOB) {
        debug("entry is not a blob");
        goto out_tree;
    }

    git_object *file;
    ret = git_tree_entry_to_object(&file, repo, entry);
    if(check_lg2(ret, "getting file from tree entry"))
        goto out_tree;

    const void *data = git_blob_rawcontent((git_blob*)file);
    *outlen = git_blob_rawsize((git_blob*)file);

    *out = malloc(*outlen);
    memcpy(*out, data, *outlen);

    retcode = 0;

    git_object_free(file);
out_tree:
    git_tree_free(tree);
out_master:
    git_object_free(master);
out_repo:
    git_repository_free(repo);
out:
    return retcode;
}
Beispiel #3
0
bool BaseRefsLookup(const char *repodir, std::string &refcontent)
{
	StringBuffer buffer;
	if (!refcontent.empty()) {
		refcontent.clear();
	}
	git_repository *repo;
	auto er = git_repository_open_bare(&repo, repodir);
	if (!er) {
		auto e = giterr_last();
		refcontent.append("0000fata: ");
		refcontent.append(e->message);
		return false;
	}
	git_oid oid;
	er = git_reference_name_to_id(&oid, repo, "HEAD");
	if (er == GIT_ENOTFOUND) {

	} else if (er == GIT_EINVALIDSPEC) {

	} else if (er == GIT_EUNBORNBRANCH) {
		/////Init bare Repository
	} else {

	}
	std::string firstLine;
	char out[GIT_OID_HEXSZ + 1];
	out[GIT_OID_HEXSZ] = '\0';
	git_oid_fmt(out, &oid);
	firstLine.append(out);
	firstLine.append("HEAD");
	firstLine.append(0);
	firstLine.append("multi_ack thin-pack side-band side-band-64k ofs-delta shallow no-progress include-tag multi_ack_detailed no-done symref=HEAD:");
	git_strarray refs = { 0 };
	const char *refname;
	int len = 0;
	git_reference *ref;
	if (git_reference_list(&refs, repo) == 0) {
		size_t i = 0;
		for (; i<refs.count; i++) {
			refname = refs.strings[i];
			git_reference_lookup(&ref, repo, refname);
			switch (git_reference_type(ref)) {
				case GIT_REF_OID:
				git_oid_fmt(out, git_reference_target(ref));
				len = strlen(refname) + 46;
				buffer.Format2(512,"%04x%s %s\n", len, out, refname);
				break;
				case GIT_REF_SYMBOLIC:
				printf("%s => %s\n", refname, git_reference_symbolic_target(ref));
				break;
				default:
				fprintf(stderr, "Unexpected reference type\n");
				exit(1);
			}
		}
		git_strarray_free(&refs);
	}
	git_repository_free(repo);
	return true;
}