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); }
static int mne_git_get_tag_tree(git_tree **tag_tree, git_reference **tag_ref, const char *ref_name) { int err = git_reference_lookup(tag_ref, repo, ref_name); mne_check_error("git_reference_lookup()", err, __FILE__, __LINE__); const git_oid *tag_oid = git_reference_oid(*tag_ref); assert(tag_oid != NULL); git_tag *tag; err = git_tag_lookup(&tag, repo, tag_oid); const git_oid *tag_commit_oid; if (err == GIT_ENOTFOUND) { /* Not a tag, must be a commit. */ tag_commit_oid = tag_oid; } else { err = mne_git_get_tag_commit_oid(&tag_commit_oid, tag); git_tag_free(tag); if (err != GIT_OK) return err; } git_commit *tag_commit; err = git_commit_lookup(&tag_commit, repo, tag_commit_oid); mne_check_error("git_commit_lookup()", err, __FILE__, __LINE__); err = git_commit_tree(tag_tree, tag_commit); mne_check_error("git_commit_tree()", err, __FILE__, __LINE__); git_commit_free(tag_commit); return MNE_GIT_OK; }
static int revparse_lookup_object(git_object **out, git_repository *repo, const char *spec) { int error; git_reference *ref; error = maybe_describe(out, repo, spec); if (!error) return 0; if (error < 0 && error != GIT_ENOTFOUND) return error; error = disambiguate_refname(&ref, repo, spec); if (!error) { error = git_object_lookup(out, repo, git_reference_oid(ref), GIT_OBJ_ANY); git_reference_free(ref); return error; } if (error < 0 && error != GIT_ENOTFOUND) return error; error = maybe_sha_or_abbrev(out, repo, spec); if (!error) return 0; if (error < 0 && error != GIT_ENOTFOUND) return error; giterr_set(GITERR_REFERENCE, "Refspec '%s' not found.", spec); return GIT_ENOTFOUND; }
static int retrieve_revobject_from_reflog(git_object **out, git_reference **base_ref, git_repository *repo, const char *identifier, unsigned int position) { git_reference *ref; git_oid oid; int error = -1; if (*base_ref == NULL) { if ((error = disambiguate_refname(&ref, repo, identifier)) < 0) return error; } else { ref = *base_ref; *base_ref = NULL; } if (position == 0) { error = git_object_lookup(out, repo, git_reference_oid(ref), GIT_OBJ_ANY); goto cleanup; } if ((error = retrieve_oid_from_reflog(&oid, ref, position)) < 0) goto cleanup; error = git_object_lookup(out, repo, &oid, GIT_OBJ_ANY); cleanup: git_reference_free(ref); return error; }
void test_object_tag_write__lightweight(void) { // write a lightweight tag to the repository and read it again git_oid target_id, object_id; git_reference *ref_tag; git_object *target; git_oid_fromstr(&target_id, tagged_commit); cl_git_pass(git_object_lookup(&target, g_repo, &target_id, GIT_OBJ_COMMIT)); cl_git_pass(git_tag_create_lightweight( &object_id, g_repo, "light-tag", target, 0)); git_object_free(target); cl_assert(git_oid_cmp(&object_id, &target_id) == 0); cl_git_pass(git_reference_lookup(&ref_tag, g_repo, "refs/tags/light-tag")); cl_assert(git_oid_cmp(git_reference_oid(ref_tag), &target_id) == 0); cl_git_pass(git_tag_delete(g_repo, "light-tag")); git_reference_free(ref_tag); }
const git_oid *lookup_master_tree(git_repository *repo) { int ret; git_reference *ref; const git_oid *commit_oid; const git_oid *tree_oid; git_commit *commit; ret = git_reference_lookup(&ref, repo, "refs/heads/master"); if (ret) { printf("git_reference_lookup() error: %s\n", git_strerror(ret)); return NULL; } if (git_reference_type(ref) != GIT_REF_OID) { perror("reference_type is not oid reference."); return NULL; } commit_oid = git_reference_oid(ref); ret = git_commit_lookup(&commit, repo, commit_oid); tree_oid = git_commit_tree_oid(commit); git_commit_free(commit); return tree_oid; }
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; }
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)); }
static int object_from_reference(git_object **object, git_reference *reference) { git_reference *resolved = NULL; int error; if (git_reference_resolve(&resolved, reference) < 0) return -1; error = git_object_lookup(object, reference->owner, git_reference_oid(resolved), GIT_OBJ_ANY); git_reference_free(resolved); return error; }
static void assert_head_is_correctly_detached(void) { git_reference *head; git_object *commit; cl_assert_equal_i(true, git_repository_head_detached(repo)); cl_git_pass(git_repository_head(&head, repo)); cl_git_pass(git_object_lookup(&commit, repo, git_reference_oid(head), GIT_OBJ_COMMIT)); git_object_free(commit); git_reference_free(head); }
void test_object_tag_write__replace(void) { // Replace an already existing tag git_oid target_id, tag_id, old_tag_id; git_signature *tagger; git_reference *ref_tag; git_object *target; git_oid_fromstr(&target_id, tagged_commit); cl_git_pass(git_object_lookup(&target, g_repo, &target_id, GIT_OBJ_COMMIT)); cl_git_pass(git_reference_lookup(&ref_tag, g_repo, "refs/tags/e90810b")); git_oid_cpy(&old_tag_id, git_reference_oid(ref_tag)); git_reference_free(ref_tag); /* create signature */ cl_git_pass(git_signature_new(&tagger, tagger_name, tagger_email, 123456789, 60)); cl_git_pass(git_tag_create( &tag_id, /* out id */ g_repo, "e90810b", target, tagger, tagger_message, 1)); git_object_free(target); git_signature_free(tagger); cl_git_pass(git_reference_lookup(&ref_tag, g_repo, "refs/tags/e90810b")); cl_assert(git_oid_cmp(git_reference_oid(ref_tag), &tag_id) == 0); cl_assert(git_oid_cmp(git_reference_oid(ref_tag), &old_tag_id) != 0); git_reference_free(ref_tag); }
void test_object_tag_write__basic(void) { // write a tag to the repository and read it again git_tag *tag; git_oid target_id, tag_id; git_signature *tagger; const git_signature *tagger1; git_reference *ref_tag; git_object *target; git_oid_fromstr(&target_id, tagged_commit); cl_git_pass(git_object_lookup(&target, g_repo, &target_id, GIT_OBJ_COMMIT)); /* create signature */ cl_git_pass(git_signature_new(&tagger, tagger_name, tagger_email, 123456789, 60)); cl_git_pass( git_tag_create(&tag_id, g_repo, "the-tag", target, tagger, tagger_message, 0) ); git_object_free(target); git_signature_free(tagger); cl_git_pass(git_tag_lookup(&tag, g_repo, &tag_id)); cl_assert(git_oid_cmp(git_tag_target_oid(tag), &target_id) == 0); /* Check attributes were set correctly */ tagger1 = git_tag_tagger(tag); cl_assert(tagger1 != NULL); cl_assert_equal_s(tagger1->name, tagger_name); cl_assert_equal_s(tagger1->email, tagger_email); cl_assert(tagger1->when.time == 123456789); cl_assert(tagger1->when.offset == 60); cl_assert_equal_s(git_tag_message(tag), tagger_message); cl_git_pass(git_reference_lookup(&ref_tag, g_repo, "refs/tags/the-tag")); cl_assert(git_oid_cmp(git_reference_oid(ref_tag), &tag_id) == 0); cl_git_pass(git_reference_delete(ref_tag)); git_tag_free(tag); }
static void mne_git_walk_head(mne_git_walk_ctx *ctx) { git_reference *head_ref; int err = git_repository_head(&head_ref, repo); mne_check_error("git_repository_head()", err, __FILE__, __LINE__); const git_oid *head_oid = git_reference_oid(head_ref); git_commit *head_commit; git_commit_lookup(&head_commit, repo, head_oid); git_tree *head_tree; git_commit_tree(&head_tree, head_commit); git_commit_free(head_commit); mne_git_walk_tree(head_tree, head_ref, ctx); git_tree_free(head_tree); git_reference_free(head_ref); }
PyObject * Repository_head(Repository *self) { git_reference *head; const git_oid *oid; int err; err = git_repository_head(&head, self->repo); if(err < 0) { if(err == GIT_ENOTFOUND) PyErr_SetString(GitError, "head reference does not exist"); else Error_set(err); return NULL; } oid = git_reference_oid(head); return lookup_object(self, oid, GIT_OBJ_COMMIT); }
int git_repository_head_detached(git_repository *repo) { git_reference *ref; git_odb *odb = NULL; int exists; if (git_repository_odb__weakptr(&odb, repo) < 0) return -1; if (git_reference_lookup(&ref, repo, GIT_HEAD_FILE) < 0) return -1; if (git_reference_type(ref) == GIT_REF_SYMBOLIC) { git_reference_free(ref); return 0; } exists = git_odb_exists(odb, git_reference_oid(ref)); git_reference_free(ref); return exists; }
static const char *non_existing_tag_ref_name = "refs/tags/i-do-not-exist"; BEGIN_TEST(readtag0, "lookup a loose tag reference") git_repository *repo; git_reference *reference; git_object *object; char ref_name_from_tag_name[GIT_REFNAME_MAX]; must_pass(git_repository_open(&repo, REPOSITORY_FOLDER)); must_pass(git_reference_lookup(&reference, repo, loose_tag_ref_name)); must_be_true(reference->type & GIT_REF_OID); must_be_true((reference->type & GIT_REF_PACKED) == 0); must_be_true(strcmp(reference->name, loose_tag_ref_name) == 0); must_pass(git_object_lookup(&object, repo, git_reference_oid(reference), GIT_OBJ_ANY)); must_be_true(object != NULL); must_be_true(git_object_type(object) == GIT_OBJ_TAG); /* Ensure the name of the tag matches the name of the reference */ git_path_join(ref_name_from_tag_name, GIT_REFS_TAGS_DIR, git_tag_name((git_tag *)object)); must_be_true(strcmp(ref_name_from_tag_name, loose_tag_ref_name) == 0); git_object_close(object); git_repository_free(repo); END_TEST BEGIN_TEST(readtag1, "lookup a loose tag reference that doesn't exist") git_repository *repo; git_reference *reference;
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, ".git")) { 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, ".git")) { libgit_error(); } int i = 0; /* 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; }
static int add_ref(const char *name, git_repository *repo, git_vector *vec) { const char peeled[] = "^{}"; git_remote_head *head; git_reference *ref; git_object *obj = NULL; int error = GIT_SUCCESS, peel_len, ret; head = git__malloc(sizeof(git_remote_head)); if (head == NULL) return GIT_ENOMEM; head->name = git__strdup(name); if (head->name == NULL) { error = GIT_ENOMEM; goto out; } error = git_reference_lookup(&ref, repo, name); if (error < GIT_SUCCESS) goto out; error = git_reference_resolve(&ref, ref); if (error < GIT_SUCCESS) goto out; git_oid_cpy(&head->oid, git_reference_oid(ref)); error = git_vector_insert(vec, head); if (error < GIT_SUCCESS) goto out; /* If it's not a tag, we don't need to try to peel it */ if (git__prefixcmp(name, GIT_REFS_TAGS_DIR)) goto out; error = git_object_lookup(&obj, repo, &head->oid, GIT_OBJ_ANY); if (error < GIT_SUCCESS) { git__rethrow(error, "Failed to lookup object"); } /* If it's not an annotated tag, just get out */ if (git_object_type(obj) != GIT_OBJ_TAG) goto out; /* And if it's a tag, peel it, and add it to the list */ head = git__malloc(sizeof(git_remote_head)); peel_len = strlen(name) + STRLEN(peeled); head->name = git__malloc(peel_len + 1); ret = snprintf(head->name, peel_len + 1, "%s%s", name, peeled); if (ret >= peel_len + 1) { error = git__throw(GIT_ERROR, "The string is magically to long"); } git_oid_cpy(&head->oid, git_tag_target_oid((git_tag *) obj)); error = git_vector_insert(vec, head); if (error < GIT_SUCCESS) goto out; out: git_object_close(obj); if (error < GIT_SUCCESS) { free(head->name); free(head); } return error; }
int main (int argc, char** argv) { // ### Opening the Repository // There are a couple of methods for opening a repository, this being the simplest. // There are also [methods][me] for specifying the index file and work tree locations, here // we are assuming they are in the normal places. // // [me]: http://libgit2.github.com/libgit2/#HEAD/group/repository git_repository *repo; if (argc > 1) { git_repository_open(&repo, argv[1]); } else { git_repository_open(&repo, "/opt/libgit2-test/.git"); } // ### SHA-1 Value Conversions // For our first example, we will convert a 40 character hex value to the 20 byte raw SHA1 value. printf("*Hex to Raw*\n"); char hex[] = "fd6e612585290339ea8bf39c692a7ff6a29cb7c3"; // The `git_oid` is the structure that keeps the SHA value. We will use this throughout the example // for storing the value of the current SHA key we're working with. git_oid oid; git_oid_fromstr(&oid, hex); // Once we've converted the string into the oid value, we can get the raw value of the SHA. printf("Raw 20 bytes: [%.20s]\n", (&oid)->id); // Next we will convert the 20 byte raw SHA1 value to a human readable 40 char hex value. printf("\n*Raw to Hex*\n"); char out[41]; out[40] = '\0'; // If you have a oid, you can easily get the hex value of the SHA as well. git_oid_fmt(out, &oid); printf("SHA hex string: %s\n", out); // ### Working with the Object Database // **libgit2** provides [direct access][odb] to the object database. // The object database is where the actual objects are stored in Git. For // working with raw objects, we'll need to get this structure from the // repository. // [odb]: http://libgit2.github.com/libgit2/#HEAD/group/odb git_odb *odb; git_repository_odb(&odb, repo); // #### Raw Object Reading printf("\n*Raw Object Read*\n"); git_odb_object *obj; git_otype otype; const unsigned char *data; const char *str_type; int error; // We can read raw objects directly from the object database if we have the oid (SHA) // of the object. This allows us to access objects without knowing thier type and inspect // the raw bytes unparsed. error = git_odb_read(&obj, odb, &oid); // A raw object only has three properties - the type (commit, blob, tree or tag), the size // of the raw data and the raw, unparsed data itself. For a commit or tag, that raw data // is human readable plain ASCII text. For a blob it is just file contents, so it could be // text or binary data. For a tree it is a special binary format, so it's unlikely to be // hugely helpful as a raw object. data = (const unsigned char *)git_odb_object_data(obj); otype = git_odb_object_type(obj); // We provide methods to convert from the object type which is an enum, to a string // representation of that value (and vice-versa). str_type = git_object_type2string(otype); printf("object length and type: %d, %s\n", (int)git_odb_object_size(obj), str_type); // For proper memory management, close the object when you are done with it or it will leak // memory. git_odb_object_free(obj); // #### Raw Object Writing printf("\n*Raw Object Write*\n"); // You can also write raw object data to Git. This is pretty cool because it gives you // direct access to the key/value properties of Git. Here we'll write a new blob object // that just contains a simple string. Notice that we have to specify the object type as // the `git_otype` enum. git_odb_write(&oid, odb, "test data", sizeof("test data") - 1, GIT_OBJ_BLOB); // Now that we've written the object, we can check out what SHA1 was generated when the // object was written to our database. git_oid_fmt(out, &oid); printf("Written Object: %s\n", out); // ### Object Parsing // libgit2 has methods to parse every object type in Git so you don't have to work directly // with the raw data. This is much faster and simpler than trying to deal with the raw data // yourself. // #### Commit Parsing // [Parsing commit objects][pco] is simple and gives you access to all the data in the commit // - the // author (name, email, datetime), committer (same), tree, message, encoding and parent(s). // [pco]: http://libgit2.github.com/libgit2/#HEAD/group/commit printf("\n*Commit Parsing*\n"); git_commit *commit; git_oid_fromstr(&oid, "f0877d0b841d75172ec404fc9370173dfffc20d1"); error = git_commit_lookup(&commit, repo, &oid); const git_signature *author, *cmtter; const char *message; time_t ctime; unsigned int parents, p; // Each of the properties of the commit object are accessible via methods, including commonly // needed variations, such as `git_commit_time` which returns the author time and `_message` // which gives you the commit message. message = git_commit_message(commit); author = git_commit_author(commit); cmtter = git_commit_committer(commit); ctime = git_commit_time(commit); // The author and committer methods return [git_signature] structures, which give you name, email // and `when`, which is a `git_time` structure, giving you a timestamp and timezone offset. printf("Author: %s (%s)\n", author->name, author->email); // Commits can have zero or more parents. The first (root) commit will have no parents, most commits // will have one, which is the commit it was based on, and merge commits will have two or more. // Commits can technically have any number, though it's pretty rare to have more than two. parents = git_commit_parentcount(commit); for (p = 0;p < parents;p++) { git_commit *parent; git_commit_parent(&parent, commit, p); git_oid_fmt(out, git_commit_id(parent)); printf("Parent: %s\n", out); git_commit_free(parent); } // Don't forget to close the object to prevent memory leaks. You will have to do this for // all the objects you open and parse. git_commit_free(commit); // #### Writing Commits // // libgit2 provides a couple of methods to create commit objects easily as well. There are four // different create signatures, we'll just show one of them here. You can read about the other // ones in the [commit API docs][cd]. // [cd]: http://libgit2.github.com/libgit2/#HEAD/group/commit printf("\n*Commit Writing*\n"); git_oid tree_id, parent_id, commit_id; git_tree *tree; git_commit *parent; // Creating signatures for an authoring identity and time is pretty simple - you will need to have // this to create a commit in order to specify who created it and when. Default values for the name // and email should be found in the `user.name` and `user.email` configuration options. See the `config` // section of this example file to see how to access config values. git_signature_new((git_signature **)&author, "Scott Chacon", "*****@*****.**", 123456789, 60); git_signature_new((git_signature **)&cmtter, "Scott A Chacon", "*****@*****.**", 987654321, 90); // Commit objects need a tree to point to and optionally one or more parents. Here we're creating oid // objects to create the commit with, but you can also use git_oid_fromstr(&tree_id, "28873d96b4e8f4e33ea30f4c682fd325f7ba56ac"); git_tree_lookup(&tree, repo, &tree_id); git_oid_fromstr(&parent_id, "f0877d0b841d75172ec404fc9370173dfffc20d1"); git_commit_lookup(&parent, repo, &parent_id); // Here we actually create the commit object with a single call with all the values we need to create // the commit. The SHA key is written to the `commit_id` variable here. git_commit_create_v( &commit_id, /* out id */ repo, NULL, /* do not update the HEAD */ author, cmtter, NULL, /* use default message encoding */ "example commit", tree, 1, parent); // Now we can take a look at the commit SHA we've generated. git_oid_fmt(out, &commit_id); printf("New Commit: %s\n", out); // #### Tag Parsing // You can parse and create tags with the [tag management API][tm], which functions very similarly // to the commit lookup, parsing and creation methods, since the objects themselves are very similar. // [tm]: http://libgit2.github.com/libgit2/#HEAD/group/tag printf("\n*Tag Parsing*\n"); git_tag *tag; const char *tmessage, *tname; git_otype ttype; // We create an oid for the tag object if we know the SHA and look it up in the repository the same // way that we would a commit (or any other) object. git_oid_fromstr(&oid, "bc422d45275aca289c51d79830b45cecebff7c3a"); error = git_tag_lookup(&tag, repo, &oid); // Now that we have the tag object, we can extract the information it generally contains: the target // (usually a commit object), the type of the target object (usually 'commit'), the name ('v1.0'), // the tagger (a git_signature - name, email, timestamp), and the tag message. git_tag_target((git_object **)&commit, tag); tname = git_tag_name(tag); // "test" ttype = git_tag_type(tag); // GIT_OBJ_COMMIT (otype enum) tmessage = git_tag_message(tag); // "tag message\n" printf("Tag Message: %s\n", tmessage); git_commit_free(commit); // #### Tree Parsing // [Tree parsing][tp] is a bit different than the other objects, in that we have a subtype which is the // tree entry. This is not an actual object type in Git, but a useful structure for parsing and // traversing tree entries. // // [tp]: http://libgit2.github.com/libgit2/#HEAD/group/tree printf("\n*Tree Parsing*\n"); const git_tree_entry *entry; git_object *objt; // Create the oid and lookup the tree object just like the other objects. git_oid_fromstr(&oid, "2a741c18ac5ff082a7caaec6e74db3075a1906b5"); git_tree_lookup(&tree, repo, &oid); // Getting the count of entries in the tree so you can iterate over them if you want to. int cnt = git_tree_entrycount(tree); // 3 printf("tree entries: %d\n", cnt); entry = git_tree_entry_byindex(tree, 0); printf("Entry name: %s\n", git_tree_entry_name(entry)); // "hello.c" // You can also access tree entries by name if you know the name of the entry you're looking for. entry = git_tree_entry_byname(tree, "hello.c"); git_tree_entry_name(entry); // "hello.c" // Once you have the entry object, you can access the content or subtree (or commit, in the case // of submodules) that it points to. You can also get the mode if you want. git_tree_entry_to_object(&objt, repo, entry); // blob // Remember to close the looked-up object once you are done using it git_object_free(objt); // #### Blob Parsing // // The last object type is the simplest and requires the least parsing help. Blobs are just file // contents and can contain anything, there is no structure to it. The main advantage to using the // [simple blob api][ba] is that when you're creating blobs you don't have to calculate the size // of the content. There is also a helper for reading a file from disk and writing it to the db and // getting the oid back so you don't have to do all those steps yourself. // // [ba]: http://libgit2.github.com/libgit2/#HEAD/group/blob printf("\n*Blob Parsing*\n"); git_blob *blob; git_oid_fromstr(&oid, "af7574ea73f7b166f869ef1a39be126d9a186ae0"); git_blob_lookup(&blob, repo, &oid); // You can access a buffer with the raw contents of the blob directly. // Note that this buffer may not be contain ASCII data for certain blobs (e.g. binary files): // do not consider the buffer a NULL-terminated string, and use the `git_blob_rawsize` attribute to // find out its exact size in bytes printf("Blob Size: %ld\n", git_blob_rawsize(blob)); // 8 git_blob_rawcontent(blob); // "content" // ### Revwalking // // The libgit2 [revision walking api][rw] provides methods to traverse the directed graph created // by the parent pointers of the commit objects. Since all commits point back to the commit that // came directly before them, you can walk this parentage as a graph and find all the commits that // were ancestors of (reachable from) a given starting point. This can allow you to create `git log` // type functionality. // // [rw]: http://libgit2.github.com/libgit2/#HEAD/group/revwalk printf("\n*Revwalking*\n"); git_revwalk *walk; git_commit *wcommit; git_oid_fromstr(&oid, "f0877d0b841d75172ec404fc9370173dfffc20d1"); // To use the revwalker, create a new walker, tell it how you want to sort the output and then push // one or more starting points onto the walker. If you want to emulate the output of `git log` you // would push the SHA of the commit that HEAD points to into the walker and then start traversing them. // You can also 'hide' commits that you want to stop at or not see any of their ancestors. So if you // want to emulate `git log branch1..branch2`, you would push the oid of `branch2` and hide the oid // of `branch1`. git_revwalk_new(&walk, repo); git_revwalk_sorting(walk, GIT_SORT_TOPOLOGICAL | GIT_SORT_REVERSE); git_revwalk_push(walk, &oid); const git_signature *cauth; const char *cmsg; // Now that we have the starting point pushed onto the walker, we can start asking for ancestors. It // will return them in the sorting order we asked for as commit oids. // We can then lookup and parse the commited pointed at by the returned OID; // note that this operation is specially fast since the raw contents of the commit object will // be cached in memory while ((git_revwalk_next(&oid, walk)) == 0) { error = git_commit_lookup(&wcommit, repo, &oid); cmsg = git_commit_message(wcommit); cauth = git_commit_author(wcommit); printf("%s (%s)\n", cmsg, cauth->email); git_commit_free(wcommit); } // Like the other objects, be sure to free the revwalker when you're done to prevent memory leaks. // Also, make sure that the repository being walked it not deallocated while the walk is in // progress, or it will result in undefined behavior git_revwalk_free(walk); // ### Index File Manipulation // // The [index file API][gi] allows you to read, traverse, update and write the Git index file // (sometimes thought of as the staging area). // // [gi]: http://libgit2.github.com/libgit2/#HEAD/group/index printf("\n*Index Walking*\n"); git_index *index; unsigned int i, ecount; // You can either open the index from the standard location in an open repository, as we're doing // here, or you can open and manipulate any index file with `git_index_open_bare()`. The index // for the repository will be located and loaded from disk. git_repository_index(&index, repo); // For each entry in the index, you can get a bunch of information including the SHA (oid), path // and mode which map to the tree objects that are written out. It also has filesystem properties // to help determine what to inspect for changes (ctime, mtime, dev, ino, uid, gid, file_size and flags) // All these properties are exported publicly in the `git_index_entry` struct ecount = git_index_entrycount(index); for (i = 0; i < ecount; ++i) { git_index_entry *e = git_index_get(index, i); printf("path: %s\n", e->path); printf("mtime: %d\n", (int)e->mtime.seconds); printf("fs: %d\n", (int)e->file_size); } git_index_free(index); // ### References // // The [reference API][ref] allows you to list, resolve, create and update references such as // branches, tags and remote references (everything in the .git/refs directory). // // [ref]: http://libgit2.github.com/libgit2/#HEAD/group/reference printf("\n*Reference Listing*\n"); // Here we will implement something like `git for-each-ref` simply listing out all available // references and the object SHA they resolve to. git_strarray ref_list; git_reference_list(&ref_list, repo, GIT_REF_LISTALL); const char *refname; git_reference *ref; // Now that we have the list of reference names, we can lookup each ref one at a time and // resolve them to the SHA, then print both values out. for (i = 0; i < ref_list.count; ++i) { refname = ref_list.strings[i]; git_reference_lookup(&ref, repo, refname); switch (git_reference_type(ref)) { case GIT_REF_OID: git_oid_fmt(out, git_reference_oid(ref)); printf("%s [%s]\n", refname, out); break; case GIT_REF_SYMBOLIC: printf("%s => %s\n", refname, git_reference_target(ref)); break; default: fprintf(stderr, "Unexpected reference type\n"); exit(1); } } git_strarray_free(&ref_list); // ### Config Files // // The [config API][config] allows you to list and updatee config values in // any of the accessible config file locations (system, global, local). // // [config]: http://libgit2.github.com/libgit2/#HEAD/group/config printf("\n*Config Listing*\n"); const char *email; int32_t j; git_config *cfg; // Open a config object so we can read global values from it. git_config_open_ondisk(&cfg, "~/.gitconfig"); git_config_get_int32(cfg, "help.autocorrect", &j); printf("Autocorrect: %d\n", j); git_config_get_string(cfg, "user.email", &email); printf("Email: %s\n", email); // Finally, when you're done with the repository, you can free it as well. git_repository_free(repo); return 0; }
must_pass(git_tag_lookup(&tag, repo, &tag_id)); must_be_true(git_oid_cmp(git_tag_target_oid(tag), &target_id) == 0); /* Check attributes were set correctly */ tagger = git_tag_tagger(tag); must_be_true(tagger != NULL); must_be_true(strcmp(tagger->name, TAGGER_NAME) == 0); must_be_true(strcmp(tagger->email, TAGGER_EMAIL) == 0); must_be_true(tagger->when.time == 123456789); must_be_true(tagger->when.offset == 60); must_be_true(strcmp(git_tag_message(tag), TAGGER_MESSAGE) == 0); must_pass(git_reference_lookup(&ref_tag, repo, "refs/tags/the-tag")); must_be_true(git_oid_cmp(git_reference_oid(ref_tag), &tag_id) == 0); must_pass(git_reference_delete(ref_tag)); must_pass(remove_loose_object(REPOSITORY_FOLDER, (git_object *)tag)); git_tag_close(tag); git_repository_free(repo); END_TEST BEGIN_TEST(write1, "write a tag to the repository which points to an unknown oid should fail") git_repository *repo; git_oid target_id, tag_id; const git_signature *tagger; must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));