git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Paul Tan <pyokagan@gmail.com>
To: Junio C Hamano <gitster@pobox.com>
Cc: Git List <git@vger.kernel.org>,
	Johannes Schindelin <johannes.schindelin@gmx.de>,
	Stefan Beller <sbeller@google.com>
Subject: Re: [PATCH/WIP v3 04/31] am: implement patch queue mechanism
Date: Fri, 19 Jun 2015 20:49:29 +0800	[thread overview]
Message-ID: <CACRoPnSVWNOPPJAhJS1S-HbHUEVJ1p_jz-dnFmuk8Hh1NaMLwg@mail.gmail.com> (raw)
In-Reply-To: <xmqq7fr0eoho.fsf@gitster.dls.corp.google.com>

On Fri, Jun 19, 2015 at 4:43 AM, Junio C Hamano <gitster@pobox.com> wrote:
> Paul Tan <pyokagan@gmail.com> writes:
>
>> diff --git a/builtin/am.c b/builtin/am.c
>> index dbc8836..af68c51 100644
>> --- a/builtin/am.c
>> +++ b/builtin/am.c
>> @@ -6,6 +6,158 @@
>>  #include "cache.h"
>>  #include "builtin.h"
>>  #include "exec_cmd.h"
>> +#include "parse-options.h"
>> +#include "dir.h"
>> +
>> +struct am_state {
>> +     /* state directory path */
>> +     struct strbuf dir;
>
> Is this a temporary variable you will append "/patch", etc. to form
> a different string to use for fopen() etc., or do you use separate
> strbufs for things like that and this is only used to initialize
> them?
>
>  - If the former then "dir" is a misnomer.
>
>  - If the latter, then it probably does not have to be a strbuf;
>    rather, it should probably be a "const char *".  Unless you pass
>    this directly to functions that take a strbuf, such as
>    remove_dir_recursively(), that is.

It's the latter, and yes it does not need to be an strbuf since it
will not usually change.

However, I don't think it should be a const char*, as it means that
the API user has to keep track of the lifetime of the "dir" string.
Note that we will have to git_pathdup() as git_path() returns a static
buffer:

char *dir = git_pathdup("rebase-apply");
struct am_state state;
am_state_init(&state);
state.dir = dir;
...stuff...
if (foo) {
     ... separate code path ...
     am_state_release(&state);
     free(dir);
     return 0;
}
... separate code path ...
am_state_release(&state);
free(dir);
return 0;

Note the additional memory bookkeeping.

Instead, I would rather the am_state struct take ownership of the
"dir" string and free it in am_state_release(). So dir will be a
char*:

struct am_state state;
am_state_init(&state, git_path("rebase-apply"));
... stuff ...
if (foo) {
     ... separate code path ...
     am_state_release(&state);
     return 0;
}
... separate code path ...
am_state_release(&state);
return 0;

>> +/**
>> + * Release memory allocated by an am_state.
>> + */
>
> Everybody else in this file seems to say "Initializes", "Returns",
> "Reads", etc.  While I personally prefer to use imperative
> (i.e. give command to this function to "release memory allocated"),
> you would want to be consistent throughout the file; "Releases
> memory" would make it so.

OK

>> +/**
>> + * Setup a new am session for applying patches
>> + */
>> +static void am_setup(struct am_state *state)
>> +{
>> +     if (mkdir(state->dir.buf, 0777) < 0 && errno != EEXIST)
>> +             die_errno(_("failed to create directory '%s'"), state->dir.buf);
>> +
>> +     write_file(am_path(state, "next"), 1, "%d", state->cur);
>> +
>> +     write_file(am_path(state, "last"), 1, "%d", state->last);
>
> These two lines are closely related pair; drop the blank in between.
>
> I am tno sure if write_file() is an appropriate thing to use,
> though.  What happens when you get interrupted after opening the
> file but before you manage to write and close?  Shouldn't we be
> doing the usual "write to temp, close and then rename to final"
> dance?  This comment applies to all the other use of write_file().

We could, although I don't think it would help us much. The state
directory is made up of many different files, so even if we made
modifications to single files atomic, there's no point if we terminate
unexpectedly in the middle of writing multiple files to the state
directory as the state, as a whole, is corrupted anyway.

So, we must be able to make updates to the entire state directory as a
single transaction, which is a more difficult problem...

Furthermore, while applying patches, we may face abnormal termination
between e.g. the patch application and commit, so I think it is safe
to say that if abnormal termination occurs, we will not be able to
reliably --resume or --skip anyway, and the user should just run
--abort to go back to the last known state.

However, it would be an improvement if we wrote the "next" and "last"
files last, as we use their presence to determine if we have an am
session in progress or not, so if we terminate in am_setup() we will
just have a stray directory. I will make this change in the next
iteration.

Regards,
Paul

  reply	other threads:[~2015-06-19 12:49 UTC|newest]

Thread overview: 64+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-06-18 11:25 [PATCH/WIP v3 00/31] Make git-am a builtin Paul Tan
2015-06-18 11:25 ` [PATCH/WIP v3 01/31] wrapper: implement xopen() Paul Tan
2015-06-24 16:28   ` Johannes Schindelin
2015-06-24 16:59     ` Stefan Beller
2015-06-24 18:39       ` Johannes Schindelin
2015-07-01  9:41         ` Paul Tan
2015-07-01  9:53           ` Paul Tan
2015-06-18 11:25 ` [PATCH/WIP v3 02/31] wrapper: implement xfopen() Paul Tan
2015-06-18 11:25 ` [PATCH/WIP v3 03/31] am: implement skeletal builtin am Paul Tan
2015-06-18 20:26   ` Junio C Hamano
2015-06-19  9:56     ` Paul Tan
2015-06-18 11:25 ` [PATCH/WIP v3 04/31] am: implement patch queue mechanism Paul Tan
2015-06-18 17:47   ` Stefan Beller
2015-06-18 20:43   ` Junio C Hamano
2015-06-19 12:49     ` Paul Tan [this message]
2015-06-24 14:59   ` Johannes Schindelin
2015-06-25 15:16     ` Paul Tan
2015-06-18 11:25 ` [PATCH/WIP v3 05/31] am: split out mbox/maildir patches with git-mailsplit Paul Tan
2015-06-18 20:52   ` Junio C Hamano
2015-06-18 11:25 ` [PATCH/WIP v3 06/31] am: detect mbox patches Paul Tan
2015-06-18 21:02   ` Junio C Hamano
2015-06-24  8:41     ` Paul Tan
2015-06-24 15:10   ` Johannes Schindelin
2015-06-25 13:40     ` Paul Tan
2015-06-26  7:42       ` Paul Tan
2015-06-18 11:25 ` [PATCH/WIP v3 07/31] am: extract patch, message and authorship with git-mailinfo Paul Tan
2015-06-18 21:10   ` Junio C Hamano
2015-06-19  9:22     ` Paul Tan
2015-06-19 16:13       ` Junio C Hamano
2015-06-24  7:54         ` Paul Tan
2015-06-24 15:59           ` Junio C Hamano
2015-06-25 11:54             ` Paul Tan
     [not found]       ` <CAPc5daVbpB_T4cY1xvLrBKPUZw0JNMXqNAOsKE-R7NPO2nrnZA@mail.gmail.com>
2015-06-19 16:15         ` Paul Tan
2015-06-19 20:12           ` Johannes Schindelin
2015-06-24 16:36   ` Johannes Schindelin
2015-06-26  8:11     ` Paul Tan
2015-06-26 20:41       ` Junio C Hamano
2015-06-18 11:25 ` [PATCH/WIP v3 08/31] am: apply patch with git-apply Paul Tan
2015-06-18 21:23   ` Junio C Hamano
2015-06-18 11:25 ` [PATCH/WIP v3 09/31] am: commit applied patch Paul Tan
2015-06-18 11:25 ` [PATCH/WIP v3 10/31] am: refresh the index at start Paul Tan
2015-06-18 21:28   ` Junio C Hamano
2015-06-19  8:07     ` Paul Tan
2015-06-18 11:25 ` [PATCH/WIP v3 11/31] am: refuse to apply patches if index is dirty Paul Tan
2015-06-18 11:25 ` [PATCH/WIP v3 12/31] am: implement --resolved/--continue Paul Tan
2015-06-18 11:25 ` [PATCH/WIP v3 13/31] am: implement --skip Paul Tan
2015-06-18 11:25 ` [PATCH/WIP v3 14/31] am: implement --abort Paul Tan
2015-06-18 11:25 ` [PATCH/WIP v3 15/31] am: don't accept patches when there's a session in progress Paul Tan
2015-06-18 11:25 ` [PATCH/WIP v3 16/31] am: implement quiet option Paul Tan
2015-06-18 11:25 ` [PATCH/WIP v3 17/31] am: exit with user friendly message on patch failure Paul Tan
2015-06-18 11:25 ` [PATCH/WIP v3 18/31] am: implement am --signoff Paul Tan
2015-06-18 11:25 ` [PATCH/WIP v3 19/31] cache-tree: introduce write_index_as_tree() Paul Tan
2015-06-18 11:25 ` [PATCH/WIP v3 20/31] am: implement 3-way merge Paul Tan
2015-06-18 11:25 ` [PATCH/WIP v3 21/31] am: --rebasing Paul Tan
2015-06-18 11:25 ` [PATCH/WIP v3 22/31] am: don't use git-mailinfo if --rebasing Paul Tan
2015-06-18 11:25 ` [PATCH/WIP v3 23/31] am: handle stray state directory Paul Tan
2015-06-18 11:25 ` [PATCH/WIP v3 24/31] am: implement -k/--keep, --keep-non-patch Paul Tan
2015-06-18 11:25 ` [PATCH/WIP v3 25/31] am: implement --[no-]message-id, am.messageid Paul Tan
2015-06-18 11:25 ` [PATCH/WIP v3 26/31] am: support --keep-cr, am.keepcr Paul Tan
2015-06-18 11:25 ` [PATCH/WIP v3 27/31] am: implement --[no-]scissors Paul Tan
2015-06-18 11:25 ` [PATCH/WIP v3 28/31] am: pass git-apply's options to git-apply Paul Tan
2015-06-18 11:25 ` [PATCH/WIP v3 29/31] am: implement --ignore-date Paul Tan
2015-06-18 11:25 ` [PATCH/WIP v3 30/31] am: implement --committer-date-is-author-date Paul Tan
2015-06-18 11:25 ` [PATCH/WIP v3 31/31] am: implement -S/--gpg-sign, commit.gpgsign Paul Tan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CACRoPnSVWNOPPJAhJS1S-HbHUEVJ1p_jz-dnFmuk8Hh1NaMLwg@mail.gmail.com \
    --to=pyokagan@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=johannes.schindelin@gmx.de \
    --cc=sbeller@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).