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
f44fd59e
Commit
f44fd59e
authored
Feb 05, 2014
by
Carlos Martín Nieto
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refs: check the ref's old value when deleting
Recognize when the reference has changed since we loaded it.
parent
7ee8c7e6
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
46 additions
and
2 deletions
+46
-2
include/git2/refs.h
+4
-1
src/refs.c
+9
-1
tests/refs/races.c
+33
-0
No files found.
include/git2/refs.h
View file @
f44fd59e
...
@@ -398,8 +398,11 @@ GIT_EXTERN(int) git_reference_rename(
...
@@ -398,8 +398,11 @@ GIT_EXTERN(int) git_reference_rename(
* will be immediately removed on disk but the memory will not be freed.
* will be immediately removed on disk but the memory will not be freed.
* Callers must call `git_reference_free`.
* Callers must call `git_reference_free`.
*
*
* This function will return an error if the reference has changed
* from the time it was looked up.
*
* @param ref The reference to remove
* @param ref The reference to remove
* @return 0 or an error code
* @return 0
, GIT_EMODIFIED
or an error code
*/
*/
GIT_EXTERN
(
int
)
git_reference_delete
(
git_reference
*
ref
);
GIT_EXTERN
(
int
)
git_reference_delete
(
git_reference
*
ref
);
...
...
src/refs.c
View file @
f44fd59e
...
@@ -116,7 +116,15 @@ void git_reference_free(git_reference *reference)
...
@@ -116,7 +116,15 @@ void git_reference_free(git_reference *reference)
int
git_reference_delete
(
git_reference
*
ref
)
int
git_reference_delete
(
git_reference
*
ref
)
{
{
return
git_refdb_delete
(
ref
->
db
,
ref
->
name
,
NULL
,
NULL
);
const
git_oid
*
old_id
=
NULL
;
const
char
*
old_target
=
NULL
;
if
(
ref
->
type
==
GIT_REF_OID
)
old_id
=
&
ref
->
target
.
oid
;
else
old_target
=
ref
->
target
.
symbolic
;
return
git_refdb_delete
(
ref
->
db
,
ref
->
name
,
old_id
,
old_target
);
}
}
int
git_reference_lookup
(
git_reference
**
ref_out
,
int
git_reference_lookup
(
git_reference
**
ref_out
,
...
...
tests/refs/races.c
View file @
f44fd59e
...
@@ -59,3 +59,36 @@ void test_refs_races__symbolic_create_matching(void)
...
@@ -59,3 +59,36 @@ void test_refs_races__symbolic_create_matching(void)
git_reference_free
(
ref2
);
git_reference_free
(
ref2
);
git_reference_free
(
ref3
);
git_reference_free
(
ref3
);
}
}
void
test_refs_races__delete
(
void
)
{
git_reference
*
ref
,
*
ref2
;
git_oid
id
,
other_id
;
git_oid_fromstr
(
&
id
,
commit_id
);
git_oid_fromstr
(
&
other_id
,
other_commit_id
);
/* We can delete a value that matches */
cl_git_pass
(
git_reference_lookup
(
&
ref
,
g_repo
,
refname
));
cl_git_pass
(
git_reference_delete
(
ref
));
git_reference_free
(
ref
);
/* We cannot delete a symbolic value that doesn't match */
cl_git_pass
(
git_reference_lookup
(
&
ref
,
g_repo
,
"HEAD"
));
cl_git_pass
(
git_reference_symbolic_create_matching
(
&
ref2
,
g_repo
,
"HEAD"
,
other_refname
,
1
,
NULL
,
NULL
,
refname
));
cl_git_fail_with
(
GIT_EMODIFIED
,
git_reference_delete
(
ref
));
git_reference_free
(
ref
);
git_reference_free
(
ref2
);
cl_git_pass
(
git_reference_create
(
&
ref
,
g_repo
,
refname
,
&
id
,
1
,
NULL
,
NULL
));
git_reference_free
(
ref
);
/* We cannot delete an oid value that doesn't match */
cl_git_pass
(
git_reference_lookup
(
&
ref
,
g_repo
,
refname
));
cl_git_pass
(
git_reference_create_matching
(
&
ref2
,
g_repo
,
refname
,
&
other_id
,
1
,
NULL
,
NULL
,
&
id
));
cl_git_fail_with
(
GIT_EMODIFIED
,
git_reference_delete
(
ref
));
git_reference_free
(
ref
);
git_reference_free
(
ref2
);
}
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