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 <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Michael Haggerty committed
Apr 22, 2013 at 21:52 UTC
fec3137ffcc49d58e6388b1c61da902b031e01ca
1 file changed
+30
-16
refs.c
+30
-16
@@ -1959,12 +1959,36 @@ struct ref_lock *lock_any_ref_for_update(const char *refname,
1959
return lock_ref_sha1_basic(refname, old_sha1, flags, NULL);
1960
}
1961
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)
1968
{
1964
- int *fd = cb_data;
1969
char line[PATH_MAX + 100];
1970
int len;
1971
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
+
1992
if (entry->flag & REF_ISBROKEN) {
1993
/* This shouldn't happen to packed refs. */
1994
error("%s is broken!", entry->name);
@@ -2000,20 +2024,10 @@ static int repack_ref_fn(struct ref_entry *entry, void *cb_data)
2024
return 0;
2025
}
2026
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);
2031
2032
return 0;
2033
}