Subject: Re: [PATCH v4] gc: save log from daemonized gc --auto and print it next time

From: Junio C Hamano <[email-masked]>
Date: Mon, 14 Sep 2015 10:37:08 -0700

Junio C Hamano <[email-masked]> writes:

> Thanks, will queue.

Ehh, I spoke a bit too early.

>> diff --git a/builtin/gc.c b/builtin/gc.c
>> index bcc75d9..2c3aaeb 100644
>> --- a/builtin/gc.c
>> +++ b/builtin/gc.c
>> @@ -43,9 +43,20 @@ static struct argv_array prune_worktrees = ARGV_ARRAY_INIT;
>>  static struct argv_array rerere = ARGV_ARRAY_INIT;
>>  
>>  static char *pidfile;
>> +static struct strbuf log_filename = STRBUF_INIT;
>> +static int daemonized;
>>  
>>  static void remove_pidfile(void)
>>  {
>> +	if (daemonized && log_filename.len) {
>> +		struct stat st;
>> +
>> +		close(2);
>> +		if (stat(log_filename.buf, &st) ||
>> +		    !st.st_size ||
>> +		    rename(log_filename.buf, git_path("gc.log")))
>> +			unlink(log_filename.buf);
>> +	}

Unfortuantely we cannot queue this as-is, as we let the tempfile API
to automatically manage the pidfile since ebebeaea (gc: use tempfile
module to handle gc.pid file, 2015-08-10), and you cannot piggy-back
the log file finalization to this function that no longer exists.

Besides, it is obviously wrong to remove this log file in a function
whose name is remove_pidfile() ;-)

Adding a new function to tempfile API that puts the file to a final
place if it is non-empty and otherwise remove it, and using that to
create this "gc.log" file, would be the cleanest from the point of
view of _this_ codepath.  I however do not know if that is too
specific for the need of this codepath or "leave it if non-empty,
but otherwise remove as it is uninteresting" is fairly common thing
we would want and it is a good addition to the API set.

Michael, what do you think?

>> @@ -330,13 +341,21 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
>>  			fprintf(stderr, _("See \"git help gc\" for manual housekeeping.\n"));
>>  		}
>>  		if (detach_auto) {
>> +			struct strbuf sb = STRBUF_INIT;
>> +			if (strbuf_read_file(&sb, git_path("gc.log"), 0) > 0)
>> +				return error(_("last gc run reported:\n"
>> +					       "%s\n"
>> +					       "not running until %s is removed"),
>> +					     sb.buf, git_path("gc.log"));
>> +			strbuf_release(&sb);
>> +
>>  			if (gc_before_repack())
>>  				return -1;
>>  			/*
>>  			 * failure to daemonize is ok, we'll continue
>>  			 * in foreground
>>  			 */
>> -			daemonize();
>> +			daemonized = !daemonize();
>>  		}
>>  	} else
>>  		add_repack_all_option();
>> @@ -349,6 +368,18 @@ int cmd_gc(int argc, const char **argv, const char *prefix)
>>  		    name, (uintmax_t)pid);
>>  	}
>>  
>> +	if (daemonized) {
>> +		int fd;
>> +
>> +		strbuf_addstr(&log_filename, git_path("gc.log_XXXXXX"));
>> +		fd = xmkstemp(log_filename.buf);
>> +		if (fd >= 0) {
>> +			dup2(fd, 2);
>> +			close(fd);
>> +		} else
>> +			strbuf_release(&log_filename);
>> +	}
>> +
>>  	if (gc_before_repack())
>>  		return -1;