Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit 0179c94

Browse files
peffgitster
authored andcommitted
shallow: automatically clean up shallow tempfiles
We sometimes write tempfiles of the form "shallow_XXXXXX" during fetch/push operations with shallow repositories. Under normal circumstances, we clean up the result when we are done. However, we do no take steps to clean up after ourselves when we exit due to die() or signal death. This patch teaches the tempfile creation code to register handlers to clean up after ourselves. To handle this, we change the ownership semantics of the filename returned by setup_temporary_shallow. It now keeps a copy of the filename itself, and returns only a const pointer to it. We can also do away with explicit tempfile removal in the callers. They all exit not long after finishing with the file, so they can rely on the auto-cleanup, simplifying the code. Note that we keep things simple and maintain only a single filename to be cleaned. This is sufficient for the current caller, but we future-proof it with a die("BUG"). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0cc77c3 commit 0179c94

File tree

5 files changed

+40
-37
lines changed

5 files changed

+40
-37
lines changed

builtin/receive-pack.c

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -828,14 +828,10 @@ static void execute_commands(struct command *commands,
828828
}
829829
}
830830

831-
if (shallow_update) {
832-
if (!checked_connectivity)
833-
error("BUG: run 'git fsck' for safety.\n"
834-
"If there are errors, try to remove "
835-
"the reported refs above");
836-
if (alt_shallow_file && *alt_shallow_file)
837-
unlink(alt_shallow_file);
838-
}
831+
if (shallow_update && !checked_connectivity)
832+
error("BUG: run 'git fsck' for safety.\n"
833+
"If there are errors, try to remove "
834+
"the reported refs above");
839835
}
840836

841837
static struct command *read_head_info(struct sha1_array *shallow)
@@ -1087,10 +1083,6 @@ static void update_shallow_info(struct command *commands,
10871083
cmd->skip_update = 1;
10881084
}
10891085
}
1090-
if (alt_shallow_file && *alt_shallow_file) {
1091-
unlink(alt_shallow_file);
1092-
alt_shallow_file = NULL;
1093-
}
10941086
free(ref_status);
10951087
}
10961088

commit.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ extern int write_shallow_commits(struct strbuf *out, int use_pack_protocol,
209209
extern void setup_alternate_shallow(struct lock_file *shallow_lock,
210210
const char **alternate_shallow_file,
211211
const struct sha1_array *extra);
212-
extern char *setup_temporary_shallow(const struct sha1_array *extra);
212+
extern const char *setup_temporary_shallow(const struct sha1_array *extra);
213213
extern void advertise_shallow_grafts(int);
214214

215215
struct shallow_info {

fetch-pack.c

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -947,17 +947,6 @@ static void update_shallow(struct fetch_pack_args *args,
947947
if (!si->shallow || !si->shallow->nr)
948948
return;
949949

950-
if (alternate_shallow_file) {
951-
/*
952-
* The temporary shallow file is only useful for
953-
* index-pack and unpack-objects because it may
954-
* contain more roots than we want. Delete it.
955-
*/
956-
if (*alternate_shallow_file)
957-
unlink(alternate_shallow_file);
958-
free((char *)alternate_shallow_file);
959-
}
960-
961950
if (args->cloning) {
962951
/*
963952
* remote is shallow, but this is a clone, there are

shallow.c

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "diff.h"
99
#include "revision.h"
1010
#include "commit-slab.h"
11+
#include "sigchain.h"
1112

1213
static int is_shallow = -1;
1314
static struct stat_validity shallow_stat;
@@ -206,27 +207,53 @@ int write_shallow_commits(struct strbuf *out, int use_pack_protocol,
206207
return write_shallow_commits_1(out, use_pack_protocol, extra, 0);
207208
}
208209

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)
210228
{
229+
static int installed_handler;
211230
struct strbuf sb = STRBUF_INIT;
212231
int fd;
213232

233+
if (temporary_shallow.len)
234+
die("BUG: attempt to create two temporary shallow files");
235+
214236
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+
218245
if (write_in_full(fd, sb.buf, sb.len) != sb.len)
219246
die_errno("failed to write to %s",
220-
path.buf);
247+
temporary_shallow.buf);
221248
close(fd);
222249
strbuf_release(&sb);
223-
return strbuf_detach(&path, NULL);
250+
return temporary_shallow.buf;
224251
}
225252
/*
226253
* is_repository_shallow() sees empty string as "no shallow
227254
* file".
228255
*/
229-
return xstrdup("");
256+
return temporary_shallow.buf;
230257
}
231258

232259
void setup_alternate_shallow(struct lock_file *shallow_lock,

upload-pack.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ static void create_pack_file(void)
8181
const char *argv[12];
8282
int i, arg = 0;
8383
FILE *pipe_fd;
84-
char *shallow_file = NULL;
84+
const char *shallow_file = NULL;
8585

8686
if (shallow_nr) {
8787
shallow_file = setup_temporary_shallow(NULL);
@@ -242,11 +242,6 @@ static void create_pack_file(void)
242242
error("git upload-pack: git-pack-objects died with error.");
243243
goto fail;
244244
}
245-
if (shallow_file) {
246-
if (*shallow_file)
247-
unlink(shallow_file);
248-
free(shallow_file);
249-
}
250245

251246
/* flush the data */
252247
if (0 <= buffered) {

0 commit comments

Comments
 (0)