write_ref_to_lockfile(): new function, extracted from write_ref_sha1()