git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Chris Torek <chris.torek@gmail.com>
To: "Jan Rüegg" <rggjan@gmail.com>
Cc: git@vger.kernel.org
Subject: Re: Destructive pre-commit behaviour and "--all"
Date: Wed, 22 Mar 2023 13:00:40 -0700	[thread overview]
Message-ID: <CAPx1GvftGM2kpVL9JWWXrjyTRWs+6OxteoJDq9dgeOjuidUGwg@mail.gmail.com> (raw)
In-Reply-To: <CAMbUV8F8y62n5ud3AoWQak8J==Zrn7kzwTE7jCveVJEpfcqncg@mail.gmail.com>

On Wed, Mar 22, 2023 at 3:35 AM Jan Rüegg <rggjan@gmail.com> wrote:
> Why doesn't "git commit --all" keep the files added to the staging
> area, so "git commit --all" would have the same behaviour as "git add
> --all && git commit"?

It's not completely clear to me what behavior you *want* here,
but I can answer the "why doesn't" question.

Remember that `git commit` can be aborted (in several ways).  If
it *is* aborted, whatever it did must be rolled back.  This means
that `git commit -a`, which is otherwise a lot like `git add -u &&
git commit`, is fundamentally different from a successful `git add
-u` followed by a `git commit`.  In particular, if the `git
commit` *is* aborted, the `git commit -a` command *must not* leave
the updated files in the index.  The `git commit` without `-a` did
not update any files in the index, so it does not have to "roll
back" the index update.

To achieve this, `git commit -a` doesn't actually update *the*
index at all.  Instead, it prepares a *new* index and updates
that new index.  It then proceeds to do the committing, using
the new index, rather than the main index, as if it were *the*
index.  If all goes well so that the commit is done, the code
then swaps in the *new* index for *the* index.  If the commit
is aborted, the code simply *deletes* the new index, leaving
*the* index unchanged.

This is fundamentally different from "update *the* index, then
start committing, then either roll back *the* index or leave it in
place": there's a secondary temporary index involved here.

Things get even more complicated if you use `git commit --only`
(vs `git commit --include`) as in this case there are *three*
indices simultaneously active ("the" index, "proposed index for
commit", and "proposed index if commit succeeds").  All three
must be managed carefully and correctly.

It is unwise to invoke `git add` in a pre-commit hook precisely
because there may be two or three indices, and `git add` cannot
affect all of them correctly.

Note that Git could be designed differently (with real databases
that have roll-forward and roll-back options), but that's a much
bigger change.  The process described here is merely how Git works
now, which explains the constraints on using `git add` in pre-
commit hooks.

Chris

  reply	other threads:[~2023-03-22 20:06 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-22 10:34 Destructive pre-commit behaviour and "--all" Jan Rüegg
2023-03-22 20:00 ` Chris Torek [this message]
2023-03-22 21:26   ` Chris Torek
2023-03-22 21:58     ` Junio C Hamano
2023-03-28  9:33       ` Jan Rüegg

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

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

  git send-email \
    --in-reply-to=CAPx1GvftGM2kpVL9JWWXrjyTRWs+6OxteoJDq9dgeOjuidUGwg@mail.gmail.com \
    --to=chris.torek@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=rggjan@gmail.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.
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 NNTP newsgroup(s).