Skip to content

Commit fec3137

Browse files
mhaggergitster
authored andcommitted
refs: extract a function write_packed_entry()
Extract the I/O code from the "business logic" in repack_ref_fn(). Later there will be another caller for this function. Signed-off-by: Michael Haggerty <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 694b7a1 commit fec3137

File tree

1 file changed

+30
-16
lines changed

1 file changed

+30
-16
lines changed

refs.c

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1959,12 +1959,36 @@ struct ref_lock *lock_any_ref_for_update(const char *refname,
19591959
return lock_ref_sha1_basic(refname, old_sha1, flags, NULL);
19601960
}
19611961

1962-
static int repack_ref_fn(struct ref_entry *entry, void *cb_data)
1962+
/*
1963+
* Write an entry to the packed-refs file for the specified refname.
1964+
* If peeled is non-NULL, write it as the entry's peeled value.
1965+
*/
1966+
static void write_packed_entry(int fd, char *refname, unsigned char *sha1,
1967+
unsigned char *peeled)
19631968
{
1964-
int *fd = cb_data;
19651969
char line[PATH_MAX + 100];
19661970
int len;
19671971

1972+
len = snprintf(line, sizeof(line), "%s %s\n",
1973+
sha1_to_hex(sha1), refname);
1974+
/* this should not happen but just being defensive */
1975+
if (len > sizeof(line))
1976+
die("too long a refname '%s'", refname);
1977+
write_or_die(fd, line, len);
1978+
1979+
if (peeled) {
1980+
if (snprintf(line, sizeof(line), "^%s\n",
1981+
sha1_to_hex(peeled)) != PEELED_LINE_LENGTH)
1982+
die("internal error");
1983+
write_or_die(fd, line, PEELED_LINE_LENGTH);
1984+
}
1985+
}
1986+
1987+
static int repack_ref_fn(struct ref_entry *entry, void *cb_data)
1988+
{
1989+
int *fd = cb_data;
1990+
enum peel_status peel_status;
1991+
19681992
if (entry->flag & REF_ISBROKEN) {
19691993
/* This shouldn't happen to packed refs. */
19701994
error("%s is broken!", entry->name);
@@ -2000,20 +2024,10 @@ static int repack_ref_fn(struct ref_entry *entry, void *cb_data)
20002024
return 0;
20012025
}
20022026

2003-
len = snprintf(line, sizeof(line), "%s %s\n",
2004-
sha1_to_hex(entry->u.value.sha1), entry->name);
2005-
/* this should not happen but just being defensive */
2006-
if (len > sizeof(line))
2007-
die("too long a refname '%s'", entry->name);
2008-
write_or_die(*fd, line, len);
2009-
if (!peel_entry(entry)) {
2010-
/* This reference could be peeled; write the peeled value: */
2011-
if (snprintf(line, sizeof(line), "^%s\n",
2012-
sha1_to_hex(entry->u.value.peeled)) !=
2013-
PEELED_LINE_LENGTH)
2014-
die("internal error");
2015-
write_or_die(*fd, line, PEELED_LINE_LENGTH);
2016-
}
2027+
peel_status = peel_entry(entry);
2028+
write_packed_entry(*fd, entry->name, entry->u.value.sha1,
2029+
peel_status == PEEL_PEELED ?
2030+
entry->u.value.peeled : NULL);
20172031

20182032
return 0;
20192033
}

0 commit comments

Comments
 (0)