diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/reflog.c | 41 | ||||
-rw-r--r-- | src/revparse.c | 6 | ||||
-rw-r--r-- | src/stash.c | 4 |
3 files changed, 28 insertions, 23 deletions
diff --git a/src/reflog.c b/src/reflog.c index 0e333aa6f..7b07c6a9f 100644 --- a/src/reflog.c +++ b/src/reflog.c @@ -291,8 +291,8 @@ success: int git_reflog_append(git_reflog *reflog, const git_oid *new_oid, const git_signature *committer, const char *msg) { - int count; git_reflog_entry *entry; + const git_reflog_entry *previous; const char *newline; assert(reflog && new_oid && committer); @@ -319,16 +319,12 @@ int git_reflog_append(git_reflog *reflog, const git_oid *new_oid, } } - count = git_reflog_entrycount(reflog); + previous = git_reflog_entry_byindex(reflog, 0); - if (count == 0) + if (previous == NULL) git_oid_fromstr(&entry->oid_old, GIT_OID_HEX_ZERO); - else { - const git_reflog_entry *previous; - - previous = git_reflog_entry_byindex(reflog, count -1); + else git_oid_cpy(&entry->oid_old, &previous->oid_cur); - } git_oid_cpy(&entry->oid_cur, new_oid); @@ -417,8 +413,16 @@ unsigned int git_reflog_entrycount(git_reflog *reflog) const git_reflog_entry * git_reflog_entry_byindex(git_reflog *reflog, size_t idx) { + int pos; + assert(reflog); - return git_vector_get(&reflog->entries, idx); + + pos = git_reflog_entrycount(reflog) - (idx + 1); + + if (pos < 0) + return NULL; + + return git_vector_get(&reflog->entries, pos); } const git_oid * git_reflog_entry_oidold(const git_reflog_entry *entry) @@ -447,7 +451,7 @@ char * git_reflog_entry_msg(const git_reflog_entry *entry) int git_reflog_drop( git_reflog *reflog, - unsigned int idx, + size_t idx, int rewrite_previous_entry) { unsigned int entrycount; @@ -457,30 +461,31 @@ int git_reflog_drop( entrycount = git_reflog_entrycount(reflog); - if (idx >= entrycount) + entry = (git_reflog_entry *)git_reflog_entry_byindex(reflog, idx); + + if (entry == NULL) return GIT_ENOTFOUND; - entry = git_vector_get(&reflog->entries, idx); reflog_entry_free(entry); - if (git_vector_remove(&reflog->entries, idx) < 0) + if (git_vector_remove(&reflog->entries, entrycount - (idx + 1)) < 0) return -1; if (!rewrite_previous_entry) return 0; /* No need to rewrite anything when removing the most recent entry */ - if (idx == entrycount - 1) + if (idx == 0) return 0; - /* There are no more entries in the log */ + /* Have the latest entry just been dropped? */ if (entrycount == 1) return 0; - entry = (git_reflog_entry *)git_reflog_entry_byindex(reflog, idx); + entry = (git_reflog_entry *)git_reflog_entry_byindex(reflog, idx - 1); /* If the oldest entry has just been removed... */ - if (idx == 0) { + if (idx == entrycount - 1) { /* ...clear the oid_old member of the "new" oldest entry */ if (git_oid_fromstr(&entry->oid_old, GIT_OID_HEX_ZERO) < 0) return -1; @@ -488,7 +493,7 @@ int git_reflog_drop( return 0; } - previous = (git_reflog_entry *)git_reflog_entry_byindex(reflog, idx - 1); + previous = (git_reflog_entry *)git_reflog_entry_byindex(reflog, idx); git_oid_cpy(&entry->oid_old, &previous->oid_cur); return 0; diff --git a/src/revparse.c b/src/revparse.c index 83eea7d3f..6a7587d6a 100644 --- a/src/revparse.c +++ b/src/revparse.c @@ -201,7 +201,7 @@ static int retrieve_previously_checked_out_branch_or_revision(git_object **out, numentries = git_reflog_entrycount(reflog); - for (i = numentries - 1; i >= 0; i--) { + for (i = 0; i < numentries; i++) { entry = git_reflog_entry_byindex(reflog, i); msg = git_reflog_entry_msg(entry); @@ -263,7 +263,7 @@ static int retrieve_oid_from_reflog(git_oid *oid, git_reference *ref, unsigned i } entry = git_reflog_entry_byindex(reflog, identifier); - git_oid_cpy(oid, git_reflog_entry_oidold(entry)); + git_oid_cpy(oid, git_reflog_entry_oidnew(entry)); error = 0; goto cleanup; @@ -271,7 +271,7 @@ static int retrieve_oid_from_reflog(git_oid *oid, git_reference *ref, unsigned i int i; git_time commit_time; - for (i = numentries - 1; i >= 0; i--) { + for (i = 0; i < numentries; i++) { entry = git_reflog_entry_byindex(reflog, i); commit_time = git_reflog_entry_committer(entry)->when; diff --git a/src/stash.c b/src/stash.c index 627d271f4..b74429aca 100644 --- a/src/stash.c +++ b/src/stash.c @@ -600,7 +600,7 @@ int git_stash_foreach( max = git_reflog_entrycount(reflog); for (i = 0; i < max; i++) { - entry = git_reflog_entry_byindex(reflog, max - i - 1); + entry = git_reflog_entry_byindex(reflog, i); if (callback(i, git_reflog_entry_msg(entry), @@ -642,7 +642,7 @@ int git_stash_drop( goto cleanup; } - if ((error = git_reflog_drop(reflog, max - index - 1, true)) < 0) + if ((error = git_reflog_drop(reflog, index, true)) < 0) goto cleanup; if ((error = git_reflog_write(reflog)) < 0) |