public inbox for git@vger.kernel.org
 help / color / mirror / Atom feed
From: "brian m. carlson" <sandals@crustytoothpaste.net>
To: Devste Devste <devstemail@gmail.com>
Cc: git@vger.kernel.org
Subject: Re: --reset-author does not reset author date when used in post-commit hook
Date: Tue, 1 Apr 2025 00:43:19 +0000	[thread overview]
Message-ID: <Z-s2p-ak-KpU6uaX@tapette.crustytoothpaste.net> (raw)
In-Reply-To: <CANM0SV2huiGtWMcP+eEkNBHoieeoXmy_fM7vAOwyoW4=FXw1pg@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 2215 bytes --]

On 2025-03-31 at 20:19:30, Devste Devste wrote:
> device time zone is not UTC0
> 
> post-commit hook:
> if [[ -n "${SKIP_POST_COMMIT+x}" ]] && [[ "$SKIP_POST_COMMIT" -eq 1 ]]
> then
> exit
> fi
> 
> TZ=UTC0 SKIP_POST_COMMIT=1 git commit --amend --reset-author --no-edit
> --no-verify --no-post-rewrite --allow-empty --quiet
> 
> ---
> 
> Run:
> git commit -m "foo"
> git show -s --format="Commit: %h%nAuthor: %an%nAuthor Date:
> %ad%nCommitter: %cn%nCommitter Date: %cd%nMessage: %s%n" HEAD
> 
> Expected that both Author Date: and Committer Date: show +0000 as
> timezone. Only committer date shows +0000, while author date shows
> time zone of device.
> 
> When manually running the post-commit code (TZ=UTC0 SKIP_POST_COMMIT=1
> ...) it correctly resets the author date to +0000 too
> 
> When manually setting the date using --date="$(date +%s) +0000"
> instead of  --reset-author it also works correctly in post-commit
> hook.

I can reproduce this, except that I had to use `TZ=America/Toronto`
since my system is in UTC.  A quick look at the code led me to a guess
that ended up correct.

The `git commit` code sets `GIT_AUTHOR_NAME`, `GIT_AUTHOR_EMAIL`, and
`GIT_AUTHOR_DATE`.  The latter contains a timezone identifier.  As a
consequence, when the hook is invoked, those values are set in the
environment, and the subsequent `git commit` in the hook uses them.  You
can see this by placing something like `env >/tmp/foo` in your hook and
then reading the output.

I will say that this is definitely not the intended use of the
post-commit hook and I am very much not surprised that this doesn't work
as you intended.  You could certainly fix it by unsetting those
environment variables, but I'd encourage you to adopt a different
approach, such as by using an alias that sets `TZ=UTC0` to commit.  That
will be more likely to work in the future and will definitely be less
brittle.

For instance, your call to `git commit` will still invoke the
`prepare-commit-msg` (and possibly `commit-msg`) hook, which might
result in unexpected changes to your message or even a failure of the
second commit.
-- 
brian m. carlson (they/them)
Toronto, Ontario, CA

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 263 bytes --]

  reply	other threads:[~2025-04-01  0:43 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-31 20:19 --reset-author does not reset author date when used in post-commit hook Devste Devste
2025-04-01  0:43 ` brian m. carlson [this message]
2025-04-01  3:44   ` Devste Devste
2025-04-01 22:58     ` brian m. carlson
2025-04-02 10:06       ` Devste Devste

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=Z-s2p-ak-KpU6uaX@tapette.crustytoothpaste.net \
    --to=sandals@crustytoothpaste.net \
    --cc=devstemail@gmail.com \
    --cc=git@vger.kernel.org \
    /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