Skip to content

Commit 1d45523

Browse files
mhaggergitster
authored andcommitted
write_ref_to_lockfile(): new function, extracted from write_ref_sha1()
This is the first step towards separating the checking and writing of the new reference value to committing the change. Signed-off-by: Michael Haggerty <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 71ad050 commit 1d45523

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

refs.c

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3048,23 +3048,15 @@ int is_branch(const char *refname)
30483048
}
30493049

30503050
/*
3051-
* Write sha1 into the ref specified by the lock. Make sure that errno
3052-
* is sane on error.
3051+
* Write sha1 into the open lockfile, then close the lockfile. On
3052+
* errors, rollback the lockfile and set errno to reflect the problem.
30533053
*/
3054-
static int write_ref_sha1(struct ref_lock *lock,
3055-
const unsigned char *sha1, const char *logmsg)
3054+
static int write_ref_to_lockfile(struct ref_lock *lock,
3055+
const unsigned char *sha1)
30563056
{
30573057
static char term = '\n';
30583058
struct object *o;
30593059

3060-
if (!lock) {
3061-
errno = EINVAL;
3062-
return -1;
3063-
}
3064-
if (!lock->force_write && !hashcmp(lock->old_sha1, sha1)) {
3065-
unlock_ref(lock);
3066-
return 0;
3067-
}
30683060
o = parse_object(sha1);
30693061
if (!o) {
30703062
error("Trying to write ref %s with nonexistent object %s",
@@ -3089,6 +3081,28 @@ static int write_ref_sha1(struct ref_lock *lock,
30893081
errno = save_errno;
30903082
return -1;
30913083
}
3084+
return 0;
3085+
}
3086+
3087+
/*
3088+
* Write sha1 into the ref specified by the lock. Make sure that errno
3089+
* is sane on error.
3090+
*/
3091+
static int write_ref_sha1(struct ref_lock *lock,
3092+
const unsigned char *sha1, const char *logmsg)
3093+
{
3094+
if (!lock) {
3095+
errno = EINVAL;
3096+
return -1;
3097+
}
3098+
if (!lock->force_write && !hashcmp(lock->old_sha1, sha1)) {
3099+
unlock_ref(lock);
3100+
return 0;
3101+
}
3102+
3103+
if (write_ref_to_lockfile(lock, sha1))
3104+
return -1;
3105+
30923106
clear_loose_ref_cache(&ref_cache);
30933107
if (log_ref_write(lock->ref_name, lock->old_sha1, sha1, logmsg) < 0 ||
30943108
(strcmp(lock->ref_name, lock->orig_ref_name) &&

0 commit comments

Comments
 (0)