コード例 #1
0
ファイル: git-ls-files.c プロジェクト: chris-y/git2
int cmd_ls_files(int argc, const char **argv)
{
	/* Delete the following line once git tests pass */
	please_git_do_it_for_me();

	int show_cached = 1;

	/* options parsing */
	if (argc > 1) {
		if (argc > 2)
			please_git_do_it_for_me();

		if (strcmp(argv[1], "--stage") == 0 || strcmp(argv[1], "-s") == 0)
			show_cached = 0;
		else if (strcmp(argv[1], "--cached") == 0 || strcmp(argv[1], "-c") == 0)
			show_cached = 1;
		else
			please_git_do_it_for_me();
	}


	git_repository *repo = get_git_repository();

	git_index *index_cur;
	int e = git_repository_index(&index_cur, repo);
	if (e) libgit_error();

	char buf[GIT_OID_HEXSZ+1];

	const char *prefix = get_git_prefix();
	size_t prefix_len = strlen(prefix);

	for (unsigned i = 0; i < git_index_entrycount(index_cur); i++) {
		git_index_entry *gie = git_index_get(index_cur, i);

		if (prefixcmp(gie->path, prefix))
			continue;

		if (!show_cached)
			printf("%06o %s %i\t", gie->mode, git_oid_tostr(buf, GIT_OID_HEXSZ+1, &gie->oid), git_index_entry_stage(gie));

		write_name_quoted(gie->path + prefix_len, stdout, '\n');
	}

	git_index_free(index_cur);

	return EXIT_SUCCESS;
}
コード例 #2
0
ファイル: git-read-tree.c プロジェクト: Romain-Geissler/git2
int cmd_read_tree(int argc, const char **argv)
{
	please_git_do_it_for_me();
	if (argc != 2)
		please_git_do_it_for_me();

	/* Find the current repository */
	repo = get_git_repository();

	/*Find the tree*/
	git_tree *tree;
	git_oid oid_tree;

	switch (git_oid_fromstr(&oid_tree, (const char *)argv[argc-1])) {
		case GIT_ENOTOID:
			please_git_do_it_for_me();
			break;
		case GIT_SUCCESS:
			break;
		default:
			libgit_error();
	}
	
	e = git_tree_lookup(&tree, repo, &oid_tree);
	if (e) {
		if (e == GIT_EINVALIDTYPE || e == GIT_ENOTFOUND) {
			error("Tree object not found");
		} else {
			libgit_error();
		}
	}

	/* Open the index */
	if (git_repository_index(&index_cur, repo) < 0)
		libgit_error();

	/* Clear the index */
	git_index_clear(index_cur);

	/* add objects of the tree to the index */
	add_tree_to_index(tree, "");

	/* write the index */
	git_index_write(index_cur);

	return EXIT_SUCCESS;
}
コード例 #3
0
ファイル: git-write-tree.c プロジェクト: chris-y/git2
int cmd_write_tree(int argc, const char **argv)
{
	please_git_do_it_for_me();

	int verify_index = 1;
	if (argc == 1)
		verify_index = 1;
	else if (argc == 2 && strcmp(argv[1], "--missing-ok") == 0 )
		verify_index = 0;
	else
		please_git_do_it_for_me();
	

	char sha1buf[GIT_OID_HEXSZ + 1];

	git_repository *repo = get_git_repository();
	git_index *index_cur;
	int e = git_repository_index(&index_cur, repo);
	if(e != GIT_OK)
		libgit_error();

	/* check the index */
	if (verify_index) {
		git_odb * odb;
		git_repository_odb(&odb, repo);
		for (unsigned i = 0; i < git_index_entrycount(index_cur); i++) {
			git_index_entry *gie = git_index_get(index_cur, i);

			if (git_odb_exists(odb, &gie->oid) != 1) {
				printf("error: invalid object %06o %s for '%s'\n", gie->mode, git_oid_tostr(sha1buf, GIT_OID_HEXSZ+1, &gie->oid), gie->path);
				printf("fatal: git-write-tree: error building trees\n");
				return EXIT_FAILURE;
			}
		}
	}
	
	/* create the tree */
	git_oid oid;
	e = git_tree_create_fromindex(&oid, index_cur);
	if(e != GIT_OK)
		libgit_error();

	printf("%s\n", git_oid_tostr(sha1buf, GIT_OID_HEXSZ+1, &oid));

	return EXIT_SUCCESS;
}
コード例 #4
0
ファイル: git-cat-file.c プロジェクト: chris-y/git2
int cmd_cat_file(int argc, const char **argv)
{
	/* Uncomment when it passes the tests */
	please_git_do_it_for_me();

	char opt;
	if (argc != 2 && argc !=3) 
		please_git_do_it_for_me();

	if (argc == 3){
		if ((strcmp(argv[1], "blob") == 0) || (strcmp(argv[1], "tree") == 0) || (strcmp(argv[1], "commit") == 0) || (strcmp(argv[1], "tag") == 0 ))
			opt = '0';
		else
			opt = argv[1][1];
	}
	else if (argc == 2 && (strcmp(argv[1], "--batch") == 0))
		opt = 'a';
	

	
	git_repository *repo = get_git_repository();

	git_oid oid;
	if (git_oid_fromstr(&oid, (const char *)argv[argc-1]))
		please_git_do_it_for_me();

	git_odb *odb;
	git_repository_odb(&odb, repo);
	git_odb_object *odb_object;
	if(git_odb_read(&odb_object, odb, &oid) == GIT_ENOTFOUND)
		libgit_error();

	size_t size = git_odb_object_size(odb_object);
	git_otype type = git_odb_object_type(odb_object);

	const char *type_string = git_object_type2string(type);

	switch (opt) {
		case 'p':
			if (strcmp(type_string, "tree") == 0) {
				printf("vous etes là\n");

 				char ** arg = malloc(2);
 				strcpy(arg[0], "ls-tree");
 				strcpy(arg[1], argv[2]);
 				int e = cmd_ls_tree(2, (const char **)arg);
 				if (e == 0 )
 					printf("succes\n");
 				else
 					printf("echec\n");
			}
			else {
				for(int i=0; i < (int)size; i++)
					printf("%c", *((char *)git_odb_object_data(odb_object)+i));
			}
			break;
		case 't':
			printf("%s\n",type_string);
			break;
		case 's':
			printf("%zu\n",size);
			break;
		case 'e':
			if(git_odb_exists(odb, &oid) == 1) {
				return 0;
			}
			break;
		case '0' :
			if (strcmp(type_string, argv[1]) == 0) {
				for (int i=0; i < (int)size; i++)
					printf("%c", *((char *)git_odb_object_data(odb_object)+i));
			}
 			else
 				please_git_do_it_for_me();
			break;
		case 'a' :
			please_git_do_it_for_me();
// 			if (strbuf_read(&buf, 0, 4096) < 0)
// 				die_errno("could not read from stdin");
// 
// 			git_oid id;
// 			if (git_oid_mkstr(&id, buf.buf))
// 				please_git_do_it_for_me();
// 
// 			git_odb_object *odb_object1;
// 			if(git_odb_read(&odb_object1, odb, &id) == GIT_ENOTFOUND)
// 				libgit_error();
// 			size_t size1 = git_odb_object_size(odb_object1);
// 			git_otype type1 = git_odb_object_type(odb_object1);
// 
// 			printf("bla bla %s %s %zu\n",buf.buf, git_object_type2string(type1), size1);
// 			for(int i=0; i < (int)size1; i++)
// 				printf("%c", *((char *)git_odb_object_data(odb_object1)+i));
// 			fflush(stdout);
			break;
	}
	//git_odb_object_close(odb_object);
	return EXIT_SUCCESS;
}
コード例 #5
0
ファイル: git-rev-list.c プロジェクト: chris-y/git2
int cmd_rev_list(int argc, const char **argv)
{
    /* Doesn't pass the tests due to bug in libgit2 apparently */
    please_git_do_it_for_me();

    const char *commit_string = NULL;
    const char *format = NULL;
    int i = 0;
    git_repository *repository=NULL;
    git_oid commit_oid;
    git_oid current_oid;
    char commit_oid_string[GIT_OID_HEXSZ+1];
    size_t len;
    int e;

    /* For now, we only implement --format=oneline */
    if (argc != 3) {
        please_git_do_it_for_me();
    }

    for (i = 1; i < 3; ++i) {
        if (*argv[i] == '-') {
            if (!prefixcmp(argv[i], "--pretty=")) {
                format = argv[i] + strlen("--pretty=");
            }
        } else {
            commit_string = argv[i];
        }
    }

    if (!commit_string) {
        /* Show usage : ask git for now */
        please_git_do_it_for_me();
    }
    if (!format) {
        /* No option or not handled option */
        please_git_do_it_for_me();
    }
    if (strcmp(format, "oneline")) {
        /* Format not supported for now */
        please_git_do_it_for_me();
    }

    /* Supported object specifications are full or short oid */
    /* Create the partial oid (filled with '0's) from the given argument */
    len = strlen(commit_string);
    if (len > GIT_OID_HEXSZ) {
        /* It's not a sha1 */
        please_git_do_it_for_me();
    }
    memcpy(commit_oid_string, commit_string, len * sizeof(char));
    memset(commit_oid_string + len, '0', (GIT_OID_HEXSZ - len) * sizeof(char));
    commit_oid_string[GIT_OID_HEXSZ] = '\0';
    e = git_oid_fromstr(&commit_oid, commit_oid_string);
    if (e) {
        /* Not an OID. The object can be specified in a lot
         * of different ways (not supported by libgit2 yet).
         * Fall back to git.
         */
        please_git_do_it_for_me();
    }

    repository = get_git_repository();

    /* Lookup the commit object */
    e = git_object_lookup_prefix((git_object **)&commit, repository, &commit_oid, len, GIT_OBJ_ANY);
    if (e != GIT_OK) {
        if (e == GIT_ENOTFOUND) {
            /* Maybe the given argument is not a sha1
             * but a tag name (which looks like a sha1)
             * Fall back to git.
             */
            please_git_do_it_for_me();
        } else if (e == GIT_EAMBIGUOUS) {
            error("%s is an ambiguous prefix", commit_string);
        } else {
            libgit_error();
        }
    }
    if (git_object_type((git_object *)commit) != GIT_OBJ_COMMIT) {
        /* Not a commit : nothing to do */
        cleanup();

        return EXIT_SUCCESS;
    }

    if (git_revwalk_new(&walk, repository)) {
        cleanup();
        libgit_error();
    }

    git_revwalk_sorting(walk, GIT_SORT_TIME);

    if (git_revwalk_push(walk, &commit_oid)) {
        cleanup();
        libgit_error();
    }

    git_commit_close(commit);
    if ((git_revwalk_next(&current_oid, walk)) == GIT_OK) {
        char oid_string[GIT_OID_HEXSZ+1];
        oid_string[GIT_OID_HEXSZ] = '\0';
        const char *cmsg;

        while (1) {
            git_oid_fmt(oid_string, &current_oid);
            if (git_commit_lookup(&commit, repository, &current_oid)) {
                libgit_error();
            }

            cmsg  = git_commit_message(commit);

            git_oid_fmt(oid_string, git_commit_id(commit));
            printf("%s %s\n", oid_string, cmsg);

            if ((git_revwalk_next(&current_oid, walk)) != GIT_OK)
                break;
            git_commit_close(commit);
        }
    }

    cleanup();

    return EXIT_SUCCESS;
}
コード例 #6
0
ファイル: git-checkout.c プロジェクト: Romain-Geissler/git2
int cmd_checkout(int argc, const char **argv) 
{
	/* Delete the following line once gits tests pass */
	please_git_do_it_for_me();

	if (argc != 1)
		please_git_do_it_for_me();
	
	git_index *index;
	git_repository *repo;
	git_index_entry *index_entry;
	git_oid id;
	
	 /* Open the repository */
	if (git_repository_open(&repo, argv[argc-1])) {
		libgit_error();
	}

	/* Get the Index file of a Git repository */
	if (git_repository_index(&index,repo)) {
		libgit_error();
	}
	
	/* Find the first index of any entries which point to given path in the Git index */
	if (git_index_find (index, argv[argc-1])) {
		libgit_error();
	}

	int i;

	/* get a pointer to one of the entries in the index */
	index_entry = git_index_get(index, i);
	if (index_entry == NULL)
		printf("Out of bound");
	else
		id = index_entry->oid;
	(void)id;
	
	git_reference *symbolic_ref;
	if (git_reference_lookup(&symbolic_ref, repo, "HEAD"))
		libgit_error();

	git_reference *direct_ref;
	if (git_reference_resolve(&direct_ref, symbolic_ref))
		libgit_error();

	const git_oid *oid;
	oid = git_reference_oid(direct_ref);
	if (oid == NULL) {
		printf("Internal error: reference is not direct\n");
		return EXIT_FAILURE;
	}
	
	git_tree *tree;
	/* Lookup a tree object from the repository */
	if (git_tree_lookup(&tree, repo, oid))
		libgit_error();
	
	/* Update the index ?? */
	if (git_index_read(index))
		libgit_error();
	
	git_index_free(index);
	git_tree_close(tree);
	
	return EXIT_SUCCESS;
}