setup_temporary_shallow(): use tempfile module

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Aug 10, 2015 at 11:47 UTC 6e122b449b41d83ccfa459819688f7092aa1da19
1 file changed +7 -28
shallow.c
+7 -28
@@ -1,4 +1,5 @@
1 #include "cache.h"
2 +#include "tempfile.h"
3 #include "lockfile.h"
4 #include "commit.h"
5 #include "tag.h"
@@ -208,50 +209,28 @@ int write_shallow_commits(struct strbuf *out, int use_pack_protocol,
209 return write_shallow_commits_1(out, use_pack_protocol, extra, 0);
210 }
211
211 -static struct strbuf temporary_shallow = STRBUF_INIT;
212 -
213 -static void remove_temporary_shallow(void)
214 -{
215 - if (temporary_shallow.len) {
216 - unlink_or_warn(temporary_shallow.buf);
217 - strbuf_reset(&temporary_shallow);
218 - }
219 -}
220 -
221 -static void remove_temporary_shallow_on_signal(int signo)
222 -{
223 - remove_temporary_shallow();
224 - sigchain_pop(signo);
225 - raise(signo);
226 -}
212 +static struct tempfile temporary_shallow;
213
214 const char *setup_temporary_shallow(const struct sha1_array *extra)
215 {
216 struct strbuf sb = STRBUF_INIT;
217 int fd;
218
233 - if (temporary_shallow.len)
234 - die("BUG: attempt to create two temporary shallow files");
235 -
219 if (write_shallow_commits(&sb, 0, extra)) {
237 - strbuf_addstr(&temporary_shallow, git_path("shallow_XXXXXX"));
238 - fd = xmkstemp(temporary_shallow.buf);
239 -
240 - atexit(remove_temporary_shallow);
241 - sigchain_push_common(remove_temporary_shallow_on_signal);
220 + fd = xmks_tempfile(&temporary_shallow, git_path("shallow_XXXXXX"));
221
222 if (write_in_full(fd, sb.buf, sb.len) != sb.len)
223 die_errno("failed to write to %s",
245 - temporary_shallow.buf);
246 - close(fd);
224 + get_tempfile_path(&temporary_shallow));
225 + close_tempfile(&temporary_shallow);
226 strbuf_release(&sb);
248 - return temporary_shallow.buf;
227 + return get_tempfile_path(&temporary_shallow);
228 }
229 /*
230 * is_repository_shallow() sees empty string as "no shallow
231 * file".
232 */
254 - return temporary_shallow.buf;
233 + return get_tempfile_path(&temporary_shallow);
234 }
235
236 void setup_alternate_shallow(struct lock_file *shallow_lock,