Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
git2
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
lvzhengyang
git2
Commits
59341a5d
Commit
59341a5d
authored
Jul 16, 2012
by
nulltoken
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
reflog: introduce git_reflog_entry_drop()
parent
7c458e3a
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
189 additions
and
4 deletions
+189
-4
include/git2/reflog.h
+20
-0
src/reflog.c
+58
-4
tests-clar/refs/reflog/drop.c
+111
-0
No files found.
include/git2/reflog.h
View file @
59341a5d
...
...
@@ -87,6 +87,26 @@ GIT_EXTERN(unsigned int) git_reflog_entrycount(git_reflog *reflog);
GIT_EXTERN
(
const
git_reflog_entry
*
)
git_reflog_entry_byindex
(
git_reflog
*
reflog
,
unsigned
int
idx
);
/**
* Remove an entry from the reflog by its index
*
* To ensure there's no gap in the log history, set the `rewrite_previosu_entry` to 1.
* When deleting entry `n`, member old_oid of entry `n-1` (if any) will be updated with
* the value of memeber new_oid of entry `n+1`.
*
* @param reflog a previously loaded reflog.
*
* @param idx the position of the entry to remove.
*
* @param rewrite_previous_entry 1 to rewrite the history; 0 otherwise.
*
* @return 0 on success or an error code.
*/
GIT_EXTERN
(
int
)
git_reflog_entry_drop
(
git_reflog
*
reflog
,
unsigned
int
idx
,
int
rewrite_previous_entry
);
/**
* Get the old oid
*
* @param entry a reflog entry
...
...
src/reflog.c
View file @
59341a5d
...
...
@@ -160,6 +160,14 @@ fail:
return
-
1
;
}
static
void
reflog_entry_free
(
git_reflog_entry
*
entry
)
{
git_signature_free
(
entry
->
committer
);
git__free
(
entry
->
msg
);
git__free
(
entry
);
}
void
git_reflog_free
(
git_reflog
*
reflog
)
{
unsigned
int
i
;
...
...
@@ -168,10 +176,7 @@ void git_reflog_free(git_reflog *reflog)
for
(
i
=
0
;
i
<
reflog
->
entries
.
length
;
i
++
)
{
entry
=
git_vector_get
(
&
reflog
->
entries
,
i
);
git_signature_free
(
entry
->
committer
);
git__free
(
entry
->
msg
);
git__free
(
entry
);
reflog_entry_free
(
entry
);
}
git_vector_free
(
&
reflog
->
entries
);
...
...
@@ -370,3 +375,52 @@ char * git_reflog_entry_msg(const git_reflog_entry *entry)
assert
(
entry
);
return
entry
->
msg
;
}
int
git_reflog_entry_drop
(
git_reflog
*
reflog
,
unsigned
int
idx
,
int
rewrite_previous_entry
)
{
unsigned
int
entrycount
;
git_reflog_entry
*
entry
,
*
previous
;
assert
(
reflog
);
entrycount
=
git_reflog_entrycount
(
reflog
);
if
(
idx
>=
entrycount
)
return
GIT_ENOTFOUND
;
entry
=
git_vector_get
(
&
reflog
->
entries
,
idx
);
reflog_entry_free
(
entry
);
if
(
git_vector_remove
(
&
reflog
->
entries
,
idx
)
<
0
)
return
-
1
;
if
(
!
rewrite_previous_entry
)
return
0
;
/* No need to rewrite anything when removing the first entry */
if
(
idx
==
0
)
return
0
;
/* There are no more entries in the log */
if
(
entrycount
==
1
)
return
0
;
entry
=
(
git_reflog_entry
*
)
git_reflog_entry_byindex
(
reflog
,
idx
-
1
);
/* If the last entry has just been removed... */
if
(
idx
==
entrycount
-
1
)
{
/* ...clear the oid_old member of the "new" last entry */
if
(
git_oid_fromstr
(
&
entry
->
oid_old
,
GIT_OID_HEX_ZERO
)
<
0
)
return
-
1
;
return
0
;
}
previous
=
(
git_reflog_entry
*
)
git_reflog_entry_byindex
(
reflog
,
idx
);
git_oid_cpy
(
&
entry
->
oid_old
,
&
previous
->
oid_cur
);
return
0
;
}
tests-clar/refs/reflog/drop.c
0 → 100644
View file @
59341a5d
#include "clar_libgit2.h"
#include "reflog.h"
static
git_repository
*
g_repo
;
static
git_reflog
*
g_reflog
;
static
unsigned
int
entrycount
;
void
test_refs_reflog_drop__initialize
(
void
)
{
git_reference
*
ref
;
g_repo
=
cl_git_sandbox_init
(
"testrepo.git"
);
cl_git_pass
(
git_reference_lookup
(
&
ref
,
g_repo
,
"HEAD"
));
git_reflog_read
(
&
g_reflog
,
ref
);
entrycount
=
git_reflog_entrycount
(
g_reflog
);
git_reference_free
(
ref
);
}
void
test_refs_reflog_drop__cleanup
(
void
)
{
git_reflog_free
(
g_reflog
);
cl_git_sandbox_cleanup
();
}
void
test_refs_reflog_drop__dropping_a_non_exisiting_entry_from_the_log_returns_ENOTFOUND
(
void
)
{
cl_assert_equal_i
(
GIT_ENOTFOUND
,
git_reflog_entry_drop
(
g_reflog
,
entrycount
,
0
));
cl_assert_equal_i
(
entrycount
,
git_reflog_entrycount
(
g_reflog
));
}
void
test_refs_reflog_drop__can_drop_an_entry
(
void
)
{
cl_assert
(
entrycount
>
4
);
cl_git_pass
(
git_reflog_entry_drop
(
g_reflog
,
2
,
0
));
cl_assert_equal_i
(
entrycount
-
1
,
git_reflog_entrycount
(
g_reflog
));
}
void
test_refs_reflog_drop__can_drop_an_entry_and_rewrite_the_log_history
(
void
)
{
const
git_reflog_entry
*
before_previous
,
*
before_next
;
const
git_reflog_entry
*
after_next
;
git_oid
before_next_old_oid
;
cl_assert
(
entrycount
>
4
);
before_previous
=
git_reflog_entry_byindex
(
g_reflog
,
3
);
before_next
=
git_reflog_entry_byindex
(
g_reflog
,
1
);
git_oid_cpy
(
&
before_next_old_oid
,
&
before_next
->
oid_old
);
cl_git_pass
(
git_reflog_entry_drop
(
g_reflog
,
2
,
1
));
cl_assert_equal_i
(
entrycount
-
1
,
git_reflog_entrycount
(
g_reflog
));
after_next
=
git_reflog_entry_byindex
(
g_reflog
,
1
);
cl_assert_equal_i
(
0
,
git_oid_cmp
(
&
before_next
->
oid_cur
,
&
after_next
->
oid_cur
));
cl_assert
(
git_oid_cmp
(
&
before_next_old_oid
,
&
after_next
->
oid_old
)
!=
0
);
cl_assert_equal_i
(
0
,
git_oid_cmp
(
&
before_previous
->
oid_cur
,
&
after_next
->
oid_old
));
}
void
test_refs_reflog_drop__can_drop_the_first_entry
(
void
)
{
cl_assert
(
entrycount
>
2
);
cl_git_pass
(
git_reflog_entry_drop
(
g_reflog
,
0
,
0
));
cl_assert_equal_i
(
entrycount
-
1
,
git_reflog_entrycount
(
g_reflog
));
}
void
test_refs_reflog_drop__can_drop_the_last_entry
(
void
)
{
const
git_reflog_entry
*
entry
;
cl_assert
(
entrycount
>
2
);
cl_git_pass
(
git_reflog_entry_drop
(
g_reflog
,
entrycount
-
1
,
0
));
cl_assert_equal_i
(
entrycount
-
1
,
git_reflog_entrycount
(
g_reflog
));
entry
=
git_reflog_entry_byindex
(
g_reflog
,
entrycount
-
2
);
cl_assert
(
git_oid_streq
(
&
entry
->
oid_old
,
GIT_OID_HEX_ZERO
)
!=
0
);
}
void
test_refs_reflog_drop__can_drop_the_last_entry_and_rewrite_the_log_history
(
void
)
{
const
git_reflog_entry
*
entry
;
cl_assert
(
entrycount
>
2
);
cl_git_pass
(
git_reflog_entry_drop
(
g_reflog
,
entrycount
-
1
,
1
));
cl_assert_equal_i
(
entrycount
-
1
,
git_reflog_entrycount
(
g_reflog
));
entry
=
git_reflog_entry_byindex
(
g_reflog
,
entrycount
-
2
);
cl_assert
(
git_oid_streq
(
&
entry
->
oid_old
,
GIT_OID_HEX_ZERO
)
==
0
);
}
void
test_refs_reflog_drop__can_drop_all_the_entries
(
void
)
{
cl_assert
(
--
entrycount
>
0
);
do
{
cl_git_pass
(
git_reflog_entry_drop
(
g_reflog
,
--
entrycount
,
1
));
}
while
(
entrycount
>
0
);
cl_git_pass
(
git_reflog_entry_drop
(
g_reflog
,
0
,
1
));
cl_assert_equal_i
(
0
,
git_reflog_entrycount
(
g_reflog
));
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment