|
8 | 8 | #include "diff.h" |
9 | 9 | #include "revision.h" |
10 | 10 | #include "commit-slab.h" |
| 11 | +#include "sigchain.h" |
11 | 12 |
|
12 | 13 | static int is_shallow = -1; |
13 | 14 | static struct stat_validity shallow_stat; |
@@ -206,27 +207,53 @@ int write_shallow_commits(struct strbuf *out, int use_pack_protocol, |
206 | 207 | return write_shallow_commits_1(out, use_pack_protocol, extra, 0); |
207 | 208 | } |
208 | 209 |
|
209 | | -char *setup_temporary_shallow(const struct sha1_array *extra) |
| 210 | +static struct strbuf temporary_shallow = STRBUF_INIT; |
| 211 | + |
| 212 | +static void remove_temporary_shallow(void) |
| 213 | +{ |
| 214 | + if (temporary_shallow.len) { |
| 215 | + unlink_or_warn(temporary_shallow.buf); |
| 216 | + strbuf_reset(&temporary_shallow); |
| 217 | + } |
| 218 | +} |
| 219 | + |
| 220 | +static void remove_temporary_shallow_on_signal(int signo) |
| 221 | +{ |
| 222 | + remove_temporary_shallow(); |
| 223 | + sigchain_pop(signo); |
| 224 | + raise(signo); |
| 225 | +} |
| 226 | + |
| 227 | +const char *setup_temporary_shallow(const struct sha1_array *extra) |
210 | 228 | { |
| 229 | + static int installed_handler; |
211 | 230 | struct strbuf sb = STRBUF_INIT; |
212 | 231 | int fd; |
213 | 232 |
|
| 233 | + if (temporary_shallow.len) |
| 234 | + die("BUG: attempt to create two temporary shallow files"); |
| 235 | + |
214 | 236 | if (write_shallow_commits(&sb, 0, extra)) { |
215 | | - struct strbuf path = STRBUF_INIT; |
216 | | - strbuf_addstr(&path, git_path("shallow_XXXXXX")); |
217 | | - fd = xmkstemp(path.buf); |
| 237 | + strbuf_addstr(&temporary_shallow, git_path("shallow_XXXXXX")); |
| 238 | + fd = xmkstemp(temporary_shallow.buf); |
| 239 | + |
| 240 | + if (!installed_handler) { |
| 241 | + atexit(remove_temporary_shallow); |
| 242 | + sigchain_push_common(remove_temporary_shallow_on_signal); |
| 243 | + } |
| 244 | + |
218 | 245 | if (write_in_full(fd, sb.buf, sb.len) != sb.len) |
219 | 246 | die_errno("failed to write to %s", |
220 | | - path.buf); |
| 247 | + temporary_shallow.buf); |
221 | 248 | close(fd); |
222 | 249 | strbuf_release(&sb); |
223 | | - return strbuf_detach(&path, NULL); |
| 250 | + return temporary_shallow.buf; |
224 | 251 | } |
225 | 252 | /* |
226 | 253 | * is_repository_shallow() sees empty string as "no shallow |
227 | 254 | * file". |
228 | 255 | */ |
229 | | - return xstrdup(""); |
| 256 | + return temporary_shallow.buf; |
230 | 257 | } |
231 | 258 |
|
232 | 259 | void setup_alternate_shallow(struct lock_file *shallow_lock, |
|
0 commit comments