void test_refs_reflog_drop__cleanup(void) { git_reflog_free(g_reflog); cl_git_sandbox_cleanup(); }
void RefLogIter_dealloc(RefLogIter *self) { git_reflog_free(self->reflog); PyObject_Del(self); }
void test_rebase_merge__commit(void) { git_rebase *rebase; git_reference *branch_ref, *upstream_ref; git_annotated_commit *branch_head, *upstream_head; git_rebase_operation *rebase_operation; git_checkout_options checkout_opts = GIT_CHECKOUT_OPTIONS_INIT; git_oid commit_id, tree_id, parent_id; git_signature *author; git_commit *commit; git_reflog *reflog; const git_reflog_entry *reflog_entry; checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE; cl_git_pass(git_reference_lookup(&branch_ref, repo, "refs/heads/beef")); cl_git_pass(git_reference_lookup(&upstream_ref, repo, "refs/heads/master")); cl_git_pass(git_annotated_commit_from_ref(&branch_head, repo, branch_ref)); cl_git_pass(git_annotated_commit_from_ref(&upstream_head, repo, upstream_ref)); cl_git_pass(git_rebase_init(&rebase, repo, branch_head, upstream_head, NULL, signature, NULL)); cl_git_pass(git_rebase_next(&rebase_operation, rebase, &checkout_opts)); cl_git_pass(git_rebase_commit(&commit_id, rebase, NULL, signature, NULL, NULL)); cl_git_pass(git_commit_lookup(&commit, repo, &commit_id)); git_oid_fromstr(&parent_id, "efad0b11c47cb2f0220cbd6f5b0f93bb99064b00"); cl_assert_equal_i(1, git_commit_parentcount(commit)); cl_assert_equal_oid(&parent_id, git_commit_parent_id(commit, 0)); git_oid_fromstr(&tree_id, "4461379789c777d2a6c1f2ee0e9d6c86731b9992"); cl_assert_equal_oid(&tree_id, git_commit_tree_id(commit)); cl_assert_equal_s(NULL, git_commit_message_encoding(commit)); cl_assert_equal_s("Modification 1 to beef\n", git_commit_message(commit)); cl_git_pass(git_signature_new(&author, "Edward Thomson", "*****@*****.**", 1405621769, 0-(4*60))); cl_assert(git_signature__equal(author, git_commit_author(commit))); cl_assert(git_signature__equal(signature, git_commit_committer(commit))); /* Make sure the reflogs are updated appropriately */ cl_git_pass(git_reflog_read(&reflog, repo, "HEAD")); cl_assert(reflog_entry = git_reflog_entry_byindex(reflog, 0)); cl_assert_equal_oid(&parent_id, git_reflog_entry_id_old(reflog_entry)); cl_assert_equal_oid(&commit_id, git_reflog_entry_id_new(reflog_entry)); cl_assert_equal_s("rebase: Modification 1 to beef", git_reflog_entry_message(reflog_entry)); git_reflog_free(reflog); git_signature_free(author); git_commit_free(commit); git_annotated_commit_free(branch_head); git_annotated_commit_free(upstream_head); git_reference_free(branch_ref); git_reference_free(upstream_ref); git_rebase_free(rebase); }
int lgit_current::discover( git_repository *repo ) { int error, retval = 0; const git_oid *oidp; git_oid oid; const char *sym; std::string symbolic_name; cur_repo_ = repo; clear(); // Mark one branch as the checked out one git_reference *ref; if ( ( error = git_repository_head( &ref, repo ) ) < 0 ) { state_ = CURRENT_INVALID_MAIN; MessagesI.AppendMessageT( "Could not determine which branch is currently checked out" ); } else { switch ( git_reference_type( ref ) ) { /* GIT_REF_OID */ case GIT_REF_OID: // Prepare data for .tip_sha oidp = git_reference_target( ref ); if ( !oidp ) { state_ = CURRENT_INVALID_OID; break; } state_ = CURRENT_OID; oid = *oidp; break; /* GIT_REF_SYMBOLIC */ case GIT_REF_SYMBOLIC: sym = git_reference_symbolic_target( ref ); if ( !sym ) { state_ = CURRENT_INVALID_SYM; break; } state_ = CURRENT_SYM; symbolic_name = std::string( sym ); // Also establish OID if ( ( error = git_reference_name_to_id( &oid, repo, symbolic_name.c_str() ) ) < 0 ) { state_ = CURRENT_INVALID_OID2; break; } break; /* DEFAULT */ /* GIT_REF_INVALID */ /* GIT_REF_LISTALL */ default: state_ = CURRENT_INVALID_OTHER; break; } /* * AFTER SWITCH() */ if ( state_ == CURRENT_OID || state_ == CURRENT_SYM ) { char sha[ GIT_OID_HEXSZ + 1 ]; git_oid_fmt( sha, &oid ); sha[ GIT_OID_HEXSZ ] = '\0'; current_oid_ = std::string( sha ); type_ = CURRENT_TYPE_OID; } if ( state_ == CURRENT_OID ) { // Does the reference have any name? const char* refname_cstring = git_reference_name( ref ); if( !refname_cstring ) { refname_cstring = ""; } QString refname = QString::fromUtf8( refname_cstring ).trimmed(); if( refname.isEmpty() || refname == "HEAD" ) { git_reflog *reflog; const git_reflog_entry *reflog_entry; if ( ( error = git_reflog_read( &reflog, repo, "HEAD" ) ) < 0 ) { MessagesI.AppendMessageT( QString( "Could not read reflog, current HEAD will be established as SHA-only" ) + QString( " (no tag name resolution, error code: %1)" ) . arg( error ) ); } else { int count = git_reflog_entrycount( reflog ); int limit_count = 0; for ( int i = 0; i < count; i ++ ) { reflog_entry = git_reflog_entry_byindex( reflog, i ); if( !reflog_entry ) { MessagesI.AppendMessageT( "Problems when reading reflog" ); continue; } oidp = git_reflog_entry_id_new( reflog_entry ); if( 0 == git_oid_cmp( oidp, &oid ) ) { const char* message_cstring = git_reflog_entry_message( reflog_entry ); if( !message_cstring ) { MessagesI.AppendMessageT( "Problems when reading reflog (2)" ); continue; } QString message = QString( message_cstring ); QRegExp rx("checkout: moving from.*to (.*)$"); if( rx.indexIn( message ) != -1 ) { // The target of the catched move message must not be current HEAD's OID prefix if( ! QString::fromStdString( current_oid_ ).startsWith( rx.cap( 1 ) ) ) { current_tag_ = rx.cap( 1 ).toStdString(); type_ = CURRENT_TYPE_TAG; } break; } // Search only 5 OID-matching entries if( ++ limit_count >= 5 ) { break; } } } git_reflog_free( reflog ); } } else { QStringList parts = refname.split( "/", QString::SkipEmptyParts ); current_branch_ = parts.last().toStdString(); type_ = CURRENT_TYPE_BRANCH; } } else if ( state_ == CURRENT_SYM ) { current_branch_ = symbolic_name; } else { MessagesI.AppendMessageT( QString( "Could not determine which branch is currently checked out (error code: %1)" ).arg( state_ ) ); } git_reference_free( ref ); } return retval; }
static int retrieve_previously_checked_out_branch_or_revision(git_object **out, git_reference **base_ref, git_repository *repo, const char *identifier, size_t position) { git_reference *ref = NULL; git_reflog *reflog = NULL; regex_t preg; int error = -1; size_t i, numentries, cur; const git_reflog_entry *entry; const char *msg; regmatch_t regexmatches[2]; git_buf buf = GIT_BUF_INIT; cur = position; if (*identifier != '\0' || *base_ref != NULL) return GIT_EINVALIDSPEC; if (build_regex(&preg, "checkout: moving from (.*) to .*") < 0) return -1; if (git_reference_lookup(&ref, repo, GIT_HEAD_FILE) < 0) goto cleanup; if (git_reflog_read(&reflog, repo, GIT_HEAD_FILE) < 0) goto cleanup; numentries = git_reflog_entrycount(reflog); for (i = 0; i < numentries; i++) { entry = git_reflog_entry_byindex(reflog, i); msg = git_reflog_entry_message(entry); if (regexec(&preg, msg, 2, regexmatches, 0)) continue; cur--; if (cur > 0) continue; git_buf_put(&buf, msg+regexmatches[1].rm_so, regexmatches[1].rm_eo - regexmatches[1].rm_so); if ((error = git_reference_dwim(base_ref, repo, git_buf_cstr(&buf))) == 0) goto cleanup; if (error < 0 && error != GIT_ENOTFOUND) goto cleanup; error = maybe_abbrev(out, repo, git_buf_cstr(&buf)); goto cleanup; } error = GIT_ENOTFOUND; cleanup: git_reference_free(ref); git_buf_free(&buf); regfree(&preg); git_reflog_free(reflog); return error; }
// create a root commit void test_commit_write__root(void) { git_oid tree_id, commit_id; const git_oid *branch_oid; git_signature *author, *committer; const char *branch_name = "refs/heads/root-commit-branch"; git_tree *tree; git_reflog *log; const git_reflog_entry *entry; git_oid_fromstr(&tree_id, tree_oid); cl_git_pass(git_tree_lookup(&tree, g_repo, &tree_id)); /* create signatures */ cl_git_pass(git_signature_new(&committer, committer_name, committer_email, 123456789, 60)); cl_git_pass(git_signature_new(&author, committer_name, committer_email, 987654321, 90)); /* First we need to update HEAD so it points to our non-existant branch */ cl_git_pass(git_reference_lookup(&head, g_repo, "HEAD")); cl_assert(git_reference_type(head) == GIT_REF_SYMBOLIC); head_old = git__strdup(git_reference_symbolic_target(head)); cl_assert(head_old != NULL); git_reference_free(head); cl_git_pass(git_reference_symbolic_create(&head, g_repo, "HEAD", branch_name, 1, NULL, NULL)); cl_git_pass(git_commit_create_v( &commit_id, /* out id */ g_repo, "HEAD", author, committer, NULL, root_commit_message, tree, 0)); git_object_free((git_object *)tree); git_signature_free(author); /* * The fact that creating a commit works has already been * tested. Here we just make sure it's our commit and that it was * written as a root commit. */ cl_git_pass(git_commit_lookup(&commit, g_repo, &commit_id)); cl_assert(git_commit_parentcount(commit) == 0); cl_git_pass(git_reference_lookup(&branch, g_repo, branch_name)); branch_oid = git_reference_target(branch); cl_git_pass(git_oid_cmp(branch_oid, &commit_id)); cl_assert_equal_s(root_commit_message, git_commit_message(commit)); cl_git_pass(git_reflog_read(&log, g_repo, branch_name)); cl_assert_equal_i(1, git_reflog_entrycount(log)); entry = git_reflog_entry_byindex(log, 0); cl_assert_equal_s(committer->email, git_reflog_entry_committer(entry)->email); cl_assert_equal_s(committer->name, git_reflog_entry_committer(entry)->name); cl_assert_equal_s(root_reflog_message, git_reflog_entry_message(entry)); git_signature_free(committer); git_reflog_free(log); }