On Tue, Nov 30, 2021 at 6:18 AM Derrick Stolee <stolee@xxxxxxxxx> wrote: > > On 11/29/2021 3:44 PM, Junio C Hamano wrote: > > Derrick Stolee <stolee@xxxxxxxxx> writes: > > > >> My first reaction is to not write into .git/objects/ directly, but > >> instead make a .git/objects/tmp/ directory and write within that > >> directory. The idea is to prevent leaving stale files in the > >> .git/objects/ directory if the process terminates strangely (say, > >> a power outage or segfault). > > > > Even if we know the name of the object we are writing beforehand, I > > do not think it is a good idea to open-write-close the final object > > file. The approach we already use everywhere is to write into a > > tmpfile/lockfile and rename it to the final name > > > > object-file.c::write_loose_object() uses create_tmpfile() to prepare > > a temporary file whose name begins with "tmp_obj_", so that "gc" can > > recognize stale ones and remove them. > > The only difference is that the tmp_obj_* file would go into the > loose object directory corresponding to the first two hex characters > of the OID, but that no longer happens now. > At the beginning of this patch, I did save the temporary object in a two hex characters directory of "null_oid", but this is also a very strange behavior. "Gc" will indeed clean up these tmp_obj_* files, no matter if they are in .git/objects/ or .git/objects/xx. Thanks, -Han Xin