setup_temporary_shallow: move tempfile struct into function

The setup_temporary_shallow() function creates a temporary file, but we never access the tempfile struct outside of the function. This is OK, since it means we'll just clean up the tempfile on exit. But we can simplify the code a bit by moving the global tempfile struct to the only function in which it's used. Note that it must remain "static" due to tempfile.c's requirement that tempfile storage never goes away until program exit. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Sep 5, 2017 at 08:14 UTC c0e963b77c6892d40d4ca81a71098aaa6c4eaed3
1 file changed +5 -6
shallow.c
+5 -6
@@ -286,22 +286,21 @@ int write_shallow_commits(struct strbuf *out, int use_pack_protocol,
286 return write_shallow_commits_1(out, use_pack_protocol, extra, 0);
287 }
288
289 -static struct tempfile temporary_shallow;
290 -
289 const char *setup_temporary_shallow(const struct oid_array *extra)
290 {
291 + static struct tempfile temp;
292 struct strbuf sb = STRBUF_INIT;
293 int fd;
294
295 if (write_shallow_commits(&sb, 0, extra)) {
297 - fd = xmks_tempfile(&temporary_shallow, git_path("shallow_XXXXXX"));
296 + fd = xmks_tempfile(&temp, git_path("shallow_XXXXXX"));
297
298 if (write_in_full(fd, sb.buf, sb.len) != sb.len)
299 die_errno("failed to write to %s",
301 - get_tempfile_path(&temporary_shallow));
302 - close_tempfile(&temporary_shallow);
300 + get_tempfile_path(&temp));
301 + close_tempfile(&temp);
302 strbuf_release(&sb);
304 - return get_tempfile_path(&temporary_shallow);
303 + return get_tempfile_path(&temp);
304 }
305 /*
306 * is_repository_shallow() sees empty string as "no shallow