summaryrefslogtreecommitdiff
path: root/src/reflog.c
diff options
context:
space:
mode:
authornulltoken <emeric.fermas@gmail.com>2012-11-17 18:29:51 -0800
committernulltoken <emeric.fermas@gmail.com>2012-11-17 18:30:35 -0800
commitb15df1d937128bb7051ab35084195ff1980a7869 (patch)
treeccac67d86df9f6f8f2276d2b4f4b43555e99b20e /src/reflog.c
parent1a764476d27ac1861d1433b02bc7fbff733d8942 (diff)
downloadlibgit2-b15df1d937128bb7051ab35084195ff1980a7869.tar.gz
reflog: make entry_byindex() and drop() git compliant
Passing 0 as the index now retrieves the most recent entry instead of the oldest one.
Diffstat (limited to 'src/reflog.c')
-rw-r--r--src/reflog.c41
1 files changed, 23 insertions, 18 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;