Skip to content

Commit e5ff701

Browse files
committed
Merge branch 'tb/commit-graph-use-tempfile'
"git update-server-info" and "git commit-graph --write" have been updated to use the tempfile API to avoid leaving cruft after failing. * tb/commit-graph-use-tempfile: server-info.c: remove temporary info files on exit commit-graph.c: remove temporary graph layers on exit
2 parents 2c4aa7a + 8981dca commit e5ff701

File tree

3 files changed

+43
-28
lines changed

3 files changed

+43
-28
lines changed

commit-graph.c

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2002,8 +2002,8 @@ static int write_graph_chunk_base(struct hashfile *f,
20022002
static int write_commit_graph_file(struct write_commit_graph_context *ctx)
20032003
{
20042004
uint32_t i;
2005-
int fd;
20062005
struct hashfile *f;
2006+
struct tempfile *graph_layer; /* when ctx->split is non-zero */
20072007
struct lock_file lk = LOCK_INIT;
20082008
const unsigned hashsz = the_hash_algo->rawsz;
20092009
struct strbuf progress_title = STRBUF_INIT;
@@ -2035,24 +2035,23 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
20352035
LOCK_DIE_ON_ERROR, 0444);
20362036
free(lock_name);
20372037

2038-
fd = git_mkstemp_mode(ctx->graph_name, 0444);
2039-
if (fd < 0) {
2038+
graph_layer = mks_tempfile_m(ctx->graph_name, 0444);
2039+
if (!graph_layer) {
20402040
error(_("unable to create temporary graph layer"));
20412041
return -1;
20422042
}
20432043

2044-
if (adjust_shared_perm(ctx->graph_name)) {
2044+
if (adjust_shared_perm(get_tempfile_path(graph_layer))) {
20452045
error(_("unable to adjust shared permissions for '%s'"),
2046-
ctx->graph_name);
2046+
get_tempfile_path(graph_layer));
20472047
return -1;
20482048
}
20492049

2050-
f = hashfd(fd, ctx->graph_name);
2050+
f = hashfd(get_tempfile_fd(graph_layer), get_tempfile_path(graph_layer));
20512051
} else {
20522052
hold_lock_file_for_update_mode(&lk, ctx->graph_name,
20532053
LOCK_DIE_ON_ERROR, 0444);
2054-
fd = get_lock_file_fd(&lk);
2055-
f = hashfd(fd, get_lock_file_path(&lk));
2054+
f = hashfd(get_lock_file_fd(&lk), get_lock_file_path(&lk));
20562055
}
20572056

20582057
cf = init_chunkfile(f);
@@ -2133,8 +2132,6 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
21332132
char *final_graph_name;
21342133
int result;
21352134

2136-
close(fd);
2137-
21382135
if (!chainf) {
21392136
error(_("unable to open commit-graph chain file"));
21402137
return -1;
@@ -2169,7 +2166,7 @@ static int write_commit_graph_file(struct write_commit_graph_context *ctx)
21692166
free(ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1]);
21702167
ctx->commit_graph_filenames_after[ctx->num_commit_graphs_after - 1] = final_graph_name;
21712168

2172-
result = rename(ctx->graph_name, final_graph_name);
2169+
result = rename_tempfile(&graph_layer, final_graph_name);
21732170

21742171
for (i = 0; i < ctx->num_commit_graphs_after; i++)
21752172
fprintf(get_lock_file_fp(&lk), "%s\n", ctx->commit_graph_hash_after[i]);

server-info.c

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "object-store-ll.h"
1414
#include "server-info.h"
1515
#include "strbuf.h"
16+
#include "tempfile.h"
1617

1718
struct update_info_ctx {
1819
FILE *cur_fp;
@@ -75,9 +76,8 @@ static int update_info_file(char *path,
7576
int force)
7677
{
7778
char *tmp = mkpathdup("%s_XXXXXX", path);
79+
struct tempfile *f = NULL;
7880
int ret = -1;
79-
int fd = -1;
80-
FILE *to_close;
8181
struct update_info_ctx uic = {
8282
.cur_fp = NULL,
8383
.old_fp = NULL,
@@ -86,13 +86,12 @@ static int update_info_file(char *path,
8686
};
8787

8888
safe_create_leading_directories(path);
89-
fd = git_mkstemp_mode(tmp, 0666);
90-
if (fd < 0)
89+
f = mks_tempfile_m(tmp, 0666);
90+
if (!f)
9191
goto out;
92-
to_close = uic.cur_fp = fdopen(fd, "w");
92+
uic.cur_fp = fdopen_tempfile(f, "w");
9393
if (!uic.cur_fp)
9494
goto out;
95-
fd = -1;
9695

9796
/* no problem on ENOENT and old_fp == NULL, it's stale, now */
9897
if (!force)
@@ -121,27 +120,22 @@ static int update_info_file(char *path,
121120
}
122121

123122
uic.cur_fp = NULL;
124-
if (fclose(to_close))
125-
goto out;
126123

127124
if (uic_is_stale(&uic)) {
128-
if (adjust_shared_perm(tmp) < 0)
125+
if (adjust_shared_perm(get_tempfile_path(f)) < 0)
129126
goto out;
130-
if (rename(tmp, path) < 0)
127+
if (rename_tempfile(&f, path) < 0)
131128
goto out;
132129
} else {
133-
unlink(tmp);
130+
delete_tempfile(&f);
134131
}
135132
ret = 0;
136133

137134
out:
138135
if (ret) {
139136
error_errno("unable to update %s", path);
140-
if (uic.cur_fp)
141-
fclose(uic.cur_fp);
142-
else if (fd >= 0)
143-
close(fd);
144-
unlink(tmp);
137+
if (f)
138+
delete_tempfile(&f);
145139
}
146140
free(tmp);
147141
if (uic.old_fp)

t/t5324-split-commit-graph.sh

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ test_expect_success 'setup repo' '
1313
git init &&
1414
git config core.commitGraph true &&
1515
git config gc.writeCommitGraph false &&
16-
infodir=".git/objects/info" &&
16+
objdir=".git/objects" &&
17+
infodir="$objdir/info" &&
1718
graphdir="$infodir/commit-graphs" &&
1819
test_oid_cache <<-EOM
1920
shallow sha1:2132
@@ -718,4 +719,27 @@ test_expect_success 'write generation data chunk when commit-graph chain is repl
718719
)
719720
'
720721

722+
test_expect_success 'temporary graph layer is discarded upon failure' '
723+
git init layer-discard &&
724+
(
725+
cd layer-discard &&
726+
727+
test_commit A &&
728+
test_commit B &&
729+
730+
# Intentionally remove commit "A" from the object store
731+
# so that the commit-graph machinery fails to parse the
732+
# parents of "B".
733+
#
734+
# This takes place after the commit-graph machinery has
735+
# initialized a new temporary file to store the contents
736+
# of the new graph layer, so will allow us to ensure
737+
# that the temporary file is discarded upon failure.
738+
rm $objdir/$(test_oid_to_path $(git rev-parse HEAD^)) &&
739+
740+
test_must_fail git commit-graph write --reachable --split &&
741+
test_dir_is_empty $graphdir
742+
)
743+
'
744+
721745
test_done

0 commit comments

Comments
 (0)