{"thread":{"id":"59453","subject":"[PATCH] sequencer: rewrite save_head() in terms of write_message()","startedAt":"2023-03-23T16:47:46Z","lastAt":"2023-03-24T15:01:17Z","messageCount":2,"participants":["Oswald Buddenhagen","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"474008","messageId":"20230323162235.995544-1-oswald.buddenhagen@gmx.de","threadId":"59453","inReplyTo":null,"subject":"[PATCH] sequencer: rewrite save_head() in terms of write_message()","fromName":"Oswald Buddenhagen","fromEmail":"oswald.buddenhagen@gmx.de","sentAt":"2023-03-23T16:22:35Z","receivedAt":"2023-03-23T16:47:46Z","isPatch":true,"sender":{"key":"oswald.buddenhagen@gmx.de","avatar":"https://avatars.githubusercontent.com/u/812380?v=4"},"body":"Saves some code duplication.\n\nSigned-off-by: Oswald Buddenhagen <oswald.buddenhagen@gmx.de>\n---\n sequencer.c | 20 +-------------------\n 1 file changed, 1 insertion(+), 19 deletions(-)\n\ndiff --git a/sequencer.c b/sequencer.c\nindex 3be23d7ca2..ff985fb2e9 100644\n--- a/sequencer.c\n+++ b/sequencer.c\n@@ -3187,25 +3187,7 @@ static int create_seq_dir(struct repository *r)\n \n static int save_head(const char *head)\n {\n-\tstruct lock_file head_lock = LOCK_INIT;\n-\tstruct strbuf buf = STRBUF_INIT;\n-\tint fd;\n-\tssize_t written;\n-\n-\tfd = hold_lock_file_for_update(&head_lock, git_path_head_file(), 0);\n-\tif (fd < 0)\n-\t\treturn error_errno(_(\"could not lock HEAD\"));\n-\tstrbuf_addf(&buf, \"%s\\n\", head);\n-\twritten = write_in_full(fd, buf.buf, buf.len);\n-\tstrbuf_release(&buf);\n-\tif (written < 0) {\n-\t\terror_errno(_(\"could not write to '%s'\"), git_path_head_file());\n-\t\trollback_lock_file(&head_lock);\n-\t\treturn -1;\n-\t}\n-\tif (commit_lock_file(&head_lock) < 0)\n-\t\treturn error(_(\"failed to finalize '%s'\"), git_path_head_file());\n-\treturn 0;\n+\treturn write_message(head, strlen(head), git_path_head_file(), 1);\n }\n \n static int rollback_is_safe(void)\n-- \n2.40.0.152.g15d061e6df\n\n"},{"id":"474093","messageId":"xmqq1qle5h3b.fsf@gitster.g","threadId":"59453","inReplyTo":"20230323162235.995544-1-oswald.buddenhagen@gmx.de","subject":"Re: [PATCH] sequencer: rewrite save_head() in terms of write_message()","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2023-03-24T15:01:12Z","receivedAt":"2023-03-24T15:01:17Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Oswald Buddenhagen <oswald.buddenhagen@gmx.de> writes:\n\n> Saves some code duplication.\n\nThere are two and a half small behaviour changes, but I think it is\na good change overall.\n\n - We used to fail with \"could not lock HEAD\", but now we give the\n   real filename for \"sequencer/head\", when the locking fails.\n\n - We used to write the file in a single write_in_full() call, which\n   in practice would have been done in a single write() system call.\n   Now we do it in two steps, the body of the line in one, and a\n   single terminating LF in another.  Also, there is a new error\n   mode (object name gets written successfully but writing the\n   terminating LF fails) with a new error message.\n\nThe former is definite improvement that may help debugging when\nthings go wrong.  The latter should not be measurable. The new error\nmode probably is not noticeable in practice.\n\nLooking good.  Will queue.  Thanks.\n\n> Signed-off-by: Oswald Buddenhagen <oswald.buddenhagen@gmx.de>\n> ---\n>  sequencer.c | 20 +-------------------\n>  1 file changed, 1 insertion(+), 19 deletions(-)\n>\n> diff --git a/sequencer.c b/sequencer.c\n> index 3be23d7ca2..ff985fb2e9 100644\n> --- a/sequencer.c\n> +++ b/sequencer.c\n> @@ -3187,25 +3187,7 @@ static int create_seq_dir(struct repository *r)\n>  \n>  static int save_head(const char *head)\n>  {\n> -\tstruct lock_file head_lock = LOCK_INIT;\n> -\tstruct strbuf buf = STRBUF_INIT;\n> -\tint fd;\n> -\tssize_t written;\n> -\n> -\tfd = hold_lock_file_for_update(&head_lock, git_path_head_file(), 0);\n> -\tif (fd < 0)\n> -\t\treturn error_errno(_(\"could not lock HEAD\"));\n> -\tstrbuf_addf(&buf, \"%s\\n\", head);\n> -\twritten = write_in_full(fd, buf.buf, buf.len);\n> -\tstrbuf_release(&buf);\n> -\tif (written < 0) {\n> -\t\terror_errno(_(\"could not write to '%s'\"), git_path_head_file());\n> -\t\trollback_lock_file(&head_lock);\n> -\t\treturn -1;\n> -\t}\n> -\tif (commit_lock_file(&head_lock) < 0)\n> -\t\treturn error(_(\"failed to finalize '%s'\"), git_path_head_file());\n> -\treturn 0;\n> +\treturn write_message(head, strlen(head), git_path_head_file(), 1);\n>  }\n>  \n>  static int rollback_is_safe(void)\n"}]}