From 3e5b7ed79b64bdbb0756cdd7169848528364bfca Mon Sep 17 00:00:00 2001 From: Kevin Saul Date: Thu, 7 May 2026 04:37:41 +1200 Subject: [PATCH] remote: don't create empty FETCH_HEAD file when update suppressed (#7244) --- src/libgit2/remote.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/libgit2/remote.c b/src/libgit2/remote.c index bd90dd50b..b67f75ce1 100644 --- a/src/libgit2/remote.c +++ b/src/libgit2/remote.c @@ -2092,7 +2092,9 @@ cleanup: return error; } -static int truncate_fetch_head(const char *gitdir) +static int truncate_fetch_head( + const char *gitdir, + unsigned int update_flags) { git_str path = GIT_STR_INIT; int error; @@ -2100,9 +2102,15 @@ static int truncate_fetch_head(const char *gitdir) if ((error = git_str_joinpath(&path, gitdir, GIT_FETCH_HEAD_FILE)) < 0) return error; - error = git_futils_truncate(path.ptr, GIT_REFS_FILE_MODE); - git_str_dispose(&path); + /* When update suppressed, skip truncate when file doesn't exist to prevent creating empty file */ + if (!(update_flags & GIT_REMOTE_UPDATE_FETCHHEAD) && + !git_fs_path_exists(path.ptr)) + goto out; + error = git_futils_truncate(path.ptr, GIT_REFS_FILE_MODE); + +out: + git_str_dispose(&path); return error; } @@ -2136,7 +2144,7 @@ int git_remote_update_tips( else tagopt = download_tags; - if ((error = truncate_fetch_head(git_repository_path(remote->repo))) < 0) + if ((error = truncate_fetch_head(git_repository_path(remote->repo), update_flags)) < 0) goto out; if (tagopt == GIT_REMOTE_DOWNLOAD_TAGS_ALL) {