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
40c75652
Commit
40c75652
authored
Jul 21, 2012
by
nulltoken
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
reflog: prevent git_reflog_append() from persisting the reflog back to disk
parent
ae833178
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
98 additions
and
139 deletions
+98
-139
include/git2/reflog.h
+4
-9
src/reflog.c
+59
-81
tests-clar/refs/reflog/reflog.c
+35
-49
No files found.
include/git2/reflog.h
View file @
40c75652
...
@@ -46,22 +46,17 @@ GIT_EXTERN(int) git_reflog_read(git_reflog **reflog, git_reference *ref);
...
@@ -46,22 +46,17 @@ GIT_EXTERN(int) git_reflog_read(git_reflog **reflog, git_reference *ref);
GIT_EXTERN
(
int
)
git_reflog_write
(
git_reflog
*
reflog
);
GIT_EXTERN
(
int
)
git_reflog_write
(
git_reflog
*
reflog
);
/**
/**
* Add a new entry to the reflog for the given reference
* Add a new entry to the reflog.
*
* If there is no reflog file for the given
* reference yet, it will be created.
*
* `oid_old` may be NULL in case it's a new reference.
*
*
* `msg` is optional and can be NULL.
* `msg` is optional and can be NULL.
*
*
* @param ref
the changed reference
* @param ref
log an existing reflog object
* @param
oid_old the OID the reference was
pointing to
* @param
new_oid the OID the reference is now
pointing to
* @param committer the signature of the committer
* @param committer the signature of the committer
* @param msg the reflog message
* @param msg the reflog message
* @return 0 or an error code
* @return 0 or an error code
*/
*/
GIT_EXTERN
(
int
)
git_reflog_append
(
git_ref
erence
*
ref
,
const
git_oid
*
oid_ol
d
,
const
git_signature
*
committer
,
const
char
*
msg
);
GIT_EXTERN
(
int
)
git_reflog_append
(
git_ref
log
*
reflog
,
const
git_oid
*
new_oi
d
,
const
git_signature
*
committer
,
const
char
*
msg
);
/**
/**
* Rename the reflog for the given reference
* Rename the reflog for the given reference
...
...
src/reflog.c
View file @
40c75652
...
@@ -59,18 +59,8 @@ static int serialize_reflog_entry(
...
@@ -59,18 +59,8 @@ static int serialize_reflog_entry(
git_buf_rtrim
(
buf
);
git_buf_rtrim
(
buf
);
if
(
msg
)
{
if
(
msg
)
{
const
char
*
newline
=
strchr
(
msg
,
'\n'
);
if
(
newline
&&
newline
[
1
]
!=
'\0'
)
{
giterr_set
(
GITERR_INVALID
,
"Reflog message cannot contain newline"
);
return
-
1
;
}
git_buf_putc
(
buf
,
'\t'
);
git_buf_putc
(
buf
,
'\t'
);
git_buf_puts
(
buf
,
msg
);
git_buf_puts
(
buf
,
msg
);
/* drop potential trailing LF */
git_buf_rtrim
(
buf
);
}
}
git_buf_putc
(
buf
,
'\n'
);
git_buf_putc
(
buf
,
'\n'
);
...
@@ -78,34 +68,28 @@ static int serialize_reflog_entry(
...
@@ -78,34 +68,28 @@ static int serialize_reflog_entry(
return
git_buf_oom
(
buf
);
return
git_buf_oom
(
buf
);
}
}
static
int
reflog_write
(
const
char
*
log_path
,
const
git_oid
*
oid_old
,
static
int
reflog_entry_new
(
git_reflog_entry
**
entry
)
const
git_oid
*
oid_new
,
const
git_signature
*
committer
,
const
char
*
msg
)
{
{
int
error
=
-
1
;
git_reflog_entry
*
e
;
git_buf
log
=
GIT_BUF_INIT
;
git_filebuf
fbuf
=
GIT_FILEBUF_INIT
;
assert
(
log_path
&&
oid_old
&&
oid_new
&&
committer
);
assert
(
entry
);
if
(
serialize_reflog_entry
(
&
log
,
oid_old
,
oid_new
,
committer
,
msg
)
<
0
)
e
=
git__malloc
(
sizeof
(
git_reflog_entry
));
goto
cleanup
;
GITERR_CHECK_ALLOC
(
e
)
;
if
((
error
=
git_filebuf_open
(
&
fbuf
,
log_path
,
GIT_FILEBUF_APPEND
))
<
0
)
memset
(
e
,
0
,
sizeof
(
git_reflog_entry
));
goto
cleanup
;
if
((
error
=
git_filebuf_write
(
&
fbuf
,
log
.
ptr
,
log
.
size
))
<
0
)
*
entry
=
e
;
goto
cleanup
;
error
=
git_filebuf_commit
(
&
fbuf
,
GIT_REFLOG_FILE_MODE
)
;
return
0
;
goto
success
;
}
cleanup:
static
void
reflog_entry_free
(
git_reflog_entry
*
entry
)
git_filebuf_cleanup
(
&
fbuf
);
{
git_signature_free
(
entry
->
committer
);
success:
git__free
(
entry
->
msg
);
git_buf_free
(
&
log
);
git__free
(
entry
);
return
error
;
}
}
static
int
reflog_parse
(
git_reflog
*
log
,
const
char
*
buf
,
size_t
buf_size
)
static
int
reflog_parse
(
git_reflog
*
log
,
const
char
*
buf
,
size_t
buf_size
)
...
@@ -123,8 +107,8 @@ static int reflog_parse(git_reflog *log, const char *buf, size_t buf_size)
...
@@ -123,8 +107,8 @@ static int reflog_parse(git_reflog *log, const char *buf, size_t buf_size)
} while (0)
} while (0)
while
(
buf_size
>
GIT_REFLOG_SIZE_MIN
)
{
while
(
buf_size
>
GIT_REFLOG_SIZE_MIN
)
{
entry
=
git__malloc
(
sizeof
(
git_reflog_entry
));
if
(
reflog_entry_new
(
&
entry
)
<
0
)
GITERR_CHECK_ALLOC
(
entry
)
;
return
-
1
;
entry
->
committer
=
git__malloc
(
sizeof
(
git_signature
));
entry
->
committer
=
git__malloc
(
sizeof
(
git_signature
));
GITERR_CHECK_ALLOC
(
entry
->
committer
);
GITERR_CHECK_ALLOC
(
entry
->
committer
);
...
@@ -171,19 +155,10 @@ static int reflog_parse(git_reflog *log, const char *buf, size_t buf_size)
...
@@ -171,19 +155,10 @@ static int reflog_parse(git_reflog *log, const char *buf, size_t buf_size)
#undef seek_forward
#undef seek_forward
fail:
fail:
if
(
entry
)
{
if
(
entry
)
git__free
(
entry
->
committer
);
reflog_entry_free
(
entry
);
git__free
(
entry
);
}
return
-
1
;
}
static
void
reflog_entry_free
(
git_reflog_entry
*
entry
)
{
git_signature_free
(
entry
->
committer
);
git__free
(
entry
->
msg
);
return
-
1
;
git__free
(
entry
);
}
}
void
git_reflog_free
(
git_reflog
*
reflog
)
void
git_reflog_free
(
git_reflog
*
reflog
)
...
@@ -285,55 +260,58 @@ success:
...
@@ -285,55 +260,58 @@ success:
return
error
;
return
error
;
}
}
int
git_reflog_append
(
git_ref
erence
*
ref
,
const
git_oid
*
oid_ol
d
,
int
git_reflog_append
(
git_ref
log
*
reflog
,
const
git_oid
*
new_oi
d
,
const
git_signature
*
committer
,
const
char
*
msg
)
const
git_signature
*
committer
,
const
char
*
msg
)
{
{
int
error
;
int
count
;
git_reflog_entry
*
entry
;
const
char
*
newline
;
git_buf
log_path
=
GIT_BUF_INIT
;
assert
(
reflog
&&
new_oid
&&
committer
);
git_reference
*
r
;
git_oid
zero_oid
;
if
(
reflog_entry_new
(
&
entry
)
<
0
)
const
git_oid
*
oid
;
if
((
error
=
git_reference_resolve
(
&
r
,
ref
))
<
0
)
return
error
;
oid
=
git_reference_oid
(
r
);
if
(
oid
==
NULL
)
{
giterr_set
(
GITERR_REFERENCE
,
"Failed to write reflog. Cannot resolve reference `%s`"
,
r
->
name
);
git_reference_free
(
r
);
return
-
1
;
return
-
1
;
}
error
=
retrieve_reflog_path
(
&
log_path
,
ref
);
if
((
entry
->
committer
=
git_signature_dup
(
committer
))
==
NULL
)
if
(
error
<
0
)
goto
cleanup
;
goto
cleanup
;
if
(
git_path_exists
(
log_path
.
ptr
)
==
false
)
{
if
(
msg
!=
NULL
)
{
error
=
git_futils_mkpath2file
(
log_path
.
ptr
,
GIT_REFLOG_DIR_MODE
);
if
((
entry
->
msg
=
git__strdup
(
msg
))
==
NULL
)
}
else
if
(
git_path_isfile
(
log_path
.
ptr
)
==
false
)
{
goto
cleanup
;
giterr_set
(
GITERR_REFERENCE
,
"Failed to write reflog. `%s` is directory"
,
log_path
.
ptr
);
newline
=
strchr
(
msg
,
'\n'
);
error
=
-
1
;
}
else
if
(
oid_old
==
NULL
)
{
if
(
newline
)
{
giterr_set
(
GITERR_REFERENCE
,
if
(
newline
[
1
]
!=
'\0'
)
{
"Failed to write reflog. Old OID cannot be NULL for existing reference"
);
giterr_set
(
GITERR_INVALID
,
"Reflog message cannot contain newline"
);
error
=
-
1
;
goto
cleanup
;
}
entry
->
msg
[
newline
-
msg
]
=
'\0'
;
}
}
count
=
git_reflog_entrycount
(
reflog
);
if
(
count
==
0
)
git_oid_fromstr
(
&
entry
->
oid_old
,
GIT_OID_HEX_ZERO
);
else
{
const
git_reflog_entry
*
previous
;
previous
=
git_reflog_entry_byindex
(
reflog
,
count
-
1
);
git_oid_cpy
(
&
entry
->
oid_old
,
&
previous
->
oid_cur
);
}
}
if
(
error
<
0
)
git_oid_cpy
(
&
entry
->
oid_cur
,
new_oid
);
if
(
git_vector_insert
(
&
reflog
->
entries
,
entry
)
<
0
)
goto
cleanup
;
goto
cleanup
;
if
(
!
oid_old
)
{
return
0
;
git_oid_fromstr
(
&
zero_oid
,
GIT_OID_HEX_ZERO
);
error
=
reflog_write
(
log_path
.
ptr
,
&
zero_oid
,
oid
,
committer
,
msg
);
}
else
error
=
reflog_write
(
log_path
.
ptr
,
oid_old
,
oid
,
committer
,
msg
);
cleanup:
cleanup:
git_reference_free
(
r
);
reflog_entry_free
(
entry
);
git_buf_free
(
&
log_path
);
return
-
1
;
return
error
;
}
}
int
git_reflog_rename
(
git_reference
*
ref
,
const
char
*
new_name
)
int
git_reflog_rename
(
git_reference
*
ref
,
const
char
*
new_name
)
...
...
tests-clar/refs/reflog/reflog.c
View file @
40c75652
...
@@ -34,8 +34,6 @@ void test_refs_reflog_reflog__cleanup(void)
...
@@ -34,8 +34,6 @@ void test_refs_reflog_reflog__cleanup(void)
cl_git_sandbox_cleanup
();
cl_git_sandbox_cleanup
();
}
}
void
test_refs_reflog_reflog__append_then_read
(
void
)
void
test_refs_reflog_reflog__append_then_read
(
void
)
{
{
// write a reflog for a given reference and ensure it can be read back
// write a reflog for a given reference and ensure it can be read back
...
@@ -44,21 +42,21 @@ void test_refs_reflog_reflog__append_then_read(void)
...
@@ -44,21 +42,21 @@ void test_refs_reflog_reflog__append_then_read(void)
git_oid
oid
;
git_oid
oid
;
git_signature
*
committer
;
git_signature
*
committer
;
git_reflog
*
reflog
;
git_reflog
*
reflog
;
git_reflog_entry
*
entry
;
const
git_reflog_entry
*
entry
;
char
oid_str
[
GIT_OID_HEXSZ
+
1
];
/* Create a new branch pointing at the HEAD */
/* Create a new branch pointing at the HEAD */
git_oid_fromstr
(
&
oid
,
current_master_tip
);
git_oid_fromstr
(
&
oid
,
current_master_tip
);
cl_git_pass
(
git_reference_create_oid
(
&
ref
,
g_repo
,
new_ref
,
&
oid
,
0
));
cl_git_pass
(
git_reference_create_oid
(
&
ref
,
g_repo
,
new_ref
,
&
oid
,
0
));
git_reference_free
(
ref
);
cl_git_pass
(
git_reference_lookup
(
&
ref
,
g_repo
,
new_ref
));
cl_git_pass
(
git_signature_now
(
&
committer
,
"foo"
,
"foo@bar"
));
cl_git_pass
(
git_signature_now
(
&
committer
,
"foo"
,
"foo@bar"
));
cl_git_pass
(
git_reflog_append
(
ref
,
NULL
,
committer
,
NULL
));
cl_git_pass
(
git_reflog_read
(
&
reflog
,
ref
));
cl_git_fail
(
git_reflog_append
(
ref
,
NULL
,
committer
,
"no ancestor NULL for an existing reflog"
));
cl_git_fail
(
git_reflog_append
(
ref
,
NULL
,
committer
,
"no inner
\n
newline"
));
cl_git_fail
(
git_reflog_append
(
reflog
,
&
oid
,
committer
,
"no inner
\n
newline"
));
cl_git_pass
(
git_reflog_append
(
ref
,
&
oid
,
committer
,
commit_msg
"
\n
"
));
cl_git_pass
(
git_reflog_append
(
reflog
,
&
oid
,
committer
,
NULL
));
cl_git_pass
(
git_reflog_append
(
reflog
,
&
oid
,
committer
,
commit_msg
"
\n
"
));
cl_git_pass
(
git_reflog_write
(
reflog
));
git_reflog_free
(
reflog
);
/* Reopen a new instance of the repository */
/* Reopen a new instance of the repository */
cl_git_pass
(
git_repository_open
(
&
repo2
,
"testrepo.git"
));
cl_git_pass
(
git_repository_open
(
&
repo2
,
"testrepo.git"
));
...
@@ -68,22 +66,18 @@ void test_refs_reflog_reflog__append_then_read(void)
...
@@ -68,22 +66,18 @@ void test_refs_reflog_reflog__append_then_read(void)
/* Read and parse the reflog for this branch */
/* Read and parse the reflog for this branch */
cl_git_pass
(
git_reflog_read
(
&
reflog
,
lookedup_ref
));
cl_git_pass
(
git_reflog_read
(
&
reflog
,
lookedup_ref
));
cl_assert
(
reflog
->
entries
.
length
==
2
);
cl_assert
_equal_i
(
2
,
git_reflog_entrycount
(
reflog
)
);
entry
=
(
git_reflog_entry
*
)
git_vector_get
(
&
reflog
->
entries
,
0
);
entry
=
git_reflog_entry_byindex
(
reflog
,
0
);
assert_signature
(
committer
,
entry
->
committer
);
assert_signature
(
committer
,
entry
->
committer
);
git_oid_tostr
(
oid_str
,
GIT_OID_HEXSZ
+
1
,
&
entry
->
oid_old
);
cl_assert
(
git_oid_streq
(
&
entry
->
oid_old
,
GIT_OID_HEX_ZERO
)
==
0
);
cl_assert_equal_s
(
GIT_OID_HEX_ZERO
,
oid_str
);
cl_assert
(
git_oid_cmp
(
&
oid
,
&
entry
->
oid_cur
)
==
0
);
git_oid_tostr
(
oid_str
,
GIT_OID_HEXSZ
+
1
,
&
entry
->
oid_cur
);
cl_assert_equal_s
(
current_master_tip
,
oid_str
);
cl_assert
(
entry
->
msg
==
NULL
);
cl_assert
(
entry
->
msg
==
NULL
);
entry
=
(
git_reflog_entry
*
)
git_vector_get
(
&
reflog
->
entries
,
1
);
entry
=
git_reflog_entry_byindex
(
reflog
,
1
);
assert_signature
(
committer
,
entry
->
committer
);
assert_signature
(
committer
,
entry
->
committer
);
git_oid_tostr
(
oid_str
,
GIT_OID_HEXSZ
+
1
,
&
entry
->
oid_old
);
cl_assert
(
git_oid_cmp
(
&
oid
,
&
entry
->
oid_old
)
==
0
);
cl_assert_equal_s
(
current_master_tip
,
oid_str
);
cl_assert
(
git_oid_cmp
(
&
oid
,
&
entry
->
oid_cur
)
==
0
);
git_oid_tostr
(
oid_str
,
GIT_OID_HEXSZ
+
1
,
&
entry
->
oid_cur
);
cl_assert_equal_s
(
current_master_tip
,
oid_str
);
cl_assert_equal_s
(
commit_msg
,
entry
->
msg
);
cl_assert_equal_s
(
commit_msg
,
entry
->
msg
);
git_signature_free
(
committer
);
git_signature_free
(
committer
);
...
@@ -94,34 +88,6 @@ void test_refs_reflog_reflog__append_then_read(void)
...
@@ -94,34 +88,6 @@ void test_refs_reflog_reflog__append_then_read(void)
git_reference_free
(
lookedup_ref
);
git_reference_free
(
lookedup_ref
);
}
}
void
test_refs_reflog_reflog__dont_append_bad
(
void
)
{
// avoid writing an obviously wrong reflog
git_reference
*
ref
;
git_oid
oid
;
git_signature
*
committer
;
/* Create a new branch pointing at the HEAD */
git_oid_fromstr
(
&
oid
,
current_master_tip
);
cl_git_pass
(
git_reference_create_oid
(
&
ref
,
g_repo
,
new_ref
,
&
oid
,
0
));
git_reference_free
(
ref
);
cl_git_pass
(
git_reference_lookup
(
&
ref
,
g_repo
,
new_ref
));
cl_git_pass
(
git_signature_now
(
&
committer
,
"foo"
,
"foo@bar"
));
/* Write the reflog for the new branch */
cl_git_pass
(
git_reflog_append
(
ref
,
NULL
,
committer
,
NULL
));
/* Try to update the reflog with wrong information:
* It's no new reference, so the ancestor OID cannot
* be NULL. */
cl_git_fail
(
git_reflog_append
(
ref
,
NULL
,
committer
,
NULL
));
git_signature_free
(
committer
);
git_reference_free
(
ref
);
}
void
test_refs_reflog_reflog__renaming_the_reference_moves_the_reflog
(
void
)
void
test_refs_reflog_reflog__renaming_the_reference_moves_the_reflog
(
void
)
{
{
git_reference
*
master
;
git_reference
*
master
;
...
@@ -163,3 +129,23 @@ void test_refs_reflog_reflog__reference_has_reflog(void)
...
@@ -163,3 +129,23 @@ void test_refs_reflog_reflog__reference_has_reflog(void)
assert_has_reflog
(
true
,
"refs/heads/master"
);
assert_has_reflog
(
true
,
"refs/heads/master"
);
assert_has_reflog
(
false
,
"refs/heads/subtrees"
);
assert_has_reflog
(
false
,
"refs/heads/subtrees"
);
}
}
void
test_refs_reflog_reflog__reading_the_reflog_from_a_reference_with_no_log_returns_an_empty_one
(
void
)
{
git_reference
*
subtrees
;
git_reflog
*
reflog
;
git_buf
subtrees_log_path
=
GIT_BUF_INIT
;
cl_git_pass
(
git_reference_lookup
(
&
subtrees
,
g_repo
,
"refs/heads/subtrees"
));
git_buf_join_n
(
&
subtrees_log_path
,
'/'
,
3
,
git_repository_path
(
g_repo
),
GIT_REFLOG_DIR
,
git_reference_name
(
subtrees
));
cl_assert_equal_i
(
false
,
git_path_isfile
(
git_buf_cstr
(
&
subtrees_log_path
)));
cl_git_pass
(
git_reflog_read
(
&
reflog
,
subtrees
));
cl_assert_equal_i
(
0
,
git_reflog_entrycount
(
reflog
));
git_reflog_free
(
reflog
);
git_reference_free
(
subtrees
);
git_buf_free
(
&
subtrees_log_path
);
}
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