{"thread":{"id":"63228","subject":"--reset-author does not reset author date when used in post-commit hook","startedAt":"2025-03-31T20:19:44Z","lastAt":"2025-04-02T10:06:40Z","messageCount":5,"participants":["Devste Devste","brian m. carlson"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"515398","messageId":"CANM0SV2huiGtWMcP+eEkNBHoieeoXmy_fM7vAOwyoW4=FXw1pg@mail.gmail.com","threadId":"63228","inReplyTo":null,"subject":"--reset-author does not reset author date when used in post-commit hook","fromName":"Devste Devste","fromEmail":"devstemail@gmail.com","sentAt":"2025-03-31T20:19:30Z","receivedAt":"2025-03-31T20:19:44Z","isPatch":false,"sender":{"key":"devstemail@gmail.com","avatar":null},"body":"device time zone is not UTC0\n\npost-commit hook:\nif [[ -n \"${SKIP_POST_COMMIT+x}\" ]] && [[ \"$SKIP_POST_COMMIT\" -eq 1 ]]\nthen\nexit\nfi\n\nTZ=UTC0 SKIP_POST_COMMIT=1 git commit --amend --reset-author --no-edit\n--no-verify --no-post-rewrite --allow-empty --quiet\n\n---\n\nRun:\ngit commit -m \"foo\"\ngit show -s --format=\"Commit: %h%nAuthor: %an%nAuthor Date:\n%ad%nCommitter: %cn%nCommitter Date: %cd%nMessage: %s%n\" HEAD\n\nExpected that both Author Date: and Committer Date: show +0000 as\ntimezone. Only committer date shows +0000, while author date shows\ntime zone of device.\n\nWhen manually running the post-commit code (TZ=UTC0 SKIP_POST_COMMIT=1\n...) it correctly resets the author date to +0000 too\n\nWhen manually setting the date using --date=\"$(date +%s) +0000\"\ninstead of  --reset-author it also works correctly in post-commit\nhook.\n"},{"id":"515403","messageId":"Z-s2p-ak-KpU6uaX@tapette.crustytoothpaste.net","threadId":"63228","inReplyTo":"CANM0SV2huiGtWMcP+eEkNBHoieeoXmy_fM7vAOwyoW4=FXw1pg@mail.gmail.com","subject":"Re: --reset-author does not reset author date when used in post-commit hook","fromName":"brian m. carlson","fromEmail":"sandals@crustytoothpaste.net","sentAt":"2025-04-01T00:43:19Z","receivedAt":"2025-04-01T00:43:22Z","isPatch":false,"sender":{"key":"sandals@crustytoothpaste.net","avatar":"https://avatars.githubusercontent.com/u/497054?v=4"},"body":"On 2025-03-31 at 20:19:30, Devste Devste wrote:\n> device time zone is not UTC0\n> \n> post-commit hook:\n> if [[ -n \"${SKIP_POST_COMMIT+x}\" ]] && [[ \"$SKIP_POST_COMMIT\" -eq 1 ]]\n> then\n> exit\n> fi\n> \n> TZ=UTC0 SKIP_POST_COMMIT=1 git commit --amend --reset-author --no-edit\n> --no-verify --no-post-rewrite --allow-empty --quiet\n> \n> ---\n> \n> Run:\n> git commit -m \"foo\"\n> git show -s --format=\"Commit: %h%nAuthor: %an%nAuthor Date:\n> %ad%nCommitter: %cn%nCommitter Date: %cd%nMessage: %s%n\" HEAD\n> \n> Expected that both Author Date: and Committer Date: show +0000 as\n> timezone. Only committer date shows +0000, while author date shows\n> time zone of device.\n> \n> When manually running the post-commit code (TZ=UTC0 SKIP_POST_COMMIT=1\n> ...) it correctly resets the author date to +0000 too\n> \n> When manually setting the date using --date=\"$(date +%s) +0000\"\n> instead of  --reset-author it also works correctly in post-commit\n> hook.\n\nI can reproduce this, except that I had to use `TZ=America/Toronto`\nsince my system is in UTC.  A quick look at the code led me to a guess\nthat ended up correct.\n\nThe `git commit` code sets `GIT_AUTHOR_NAME`, `GIT_AUTHOR_EMAIL`, and\n`GIT_AUTHOR_DATE`.  The latter contains a timezone identifier.  As a\nconsequence, when the hook is invoked, those values are set in the\nenvironment, and the subsequent `git commit` in the hook uses them.  You\ncan see this by placing something like `env >/tmp/foo` in your hook and\nthen reading the output.\n\nI will say that this is definitely not the intended use of the\npost-commit hook and I am very much not surprised that this doesn't work\nas you intended.  You could certainly fix it by unsetting those\nenvironment variables, but I'd encourage you to adopt a different\napproach, such as by using an alias that sets `TZ=UTC0` to commit.  That\nwill be more likely to work in the future and will definitely be less\nbrittle.\n\nFor instance, your call to `git commit` will still invoke the\n`prepare-commit-msg` (and possibly `commit-msg`) hook, which might\nresult in unexpected changes to your message or even a failure of the\nsecond commit.\n-- \nbrian m. carlson (they/them)\nToronto, Ontario, CA\n"},{"id":"515411","messageId":"CANM0SV0Np+52vjoopvAJ88U3Ue0EF35iarwBpOozrBH9KbH2Uw@mail.gmail.com","threadId":"63228","inReplyTo":"Z-s2p-ak-KpU6uaX@tapette.crustytoothpaste.net","subject":"Re: --reset-author does not reset author date when used in post-commit hook","fromName":"Devste Devste","fromEmail":"devstemail@gmail.com","sentAt":"2025-04-01T03:44:17Z","receivedAt":"2025-04-01T03:44:31Z","isPatch":false,"sender":{"key":"devstemail@gmail.com","avatar":null},"body":">your call to `git commit` will still invoke the\n`prepare-commit-msg` (and possibly `commit-msg`) hook\n\nSince I used --no-edit --no-verify in the example/hook, that shouldn't\nbe the case though?\n\n>such as by using an alias that sets `TZ=UTC0` to commit.\n\nUnfortunately, (at least on Windows) most tools and IDEs don't even\nload a bashrc file (e.g. intellij) for their git UI, so that's not an\noption (and setting the TZ as a global environment variable has\nunwanted side effects, since it will set the timezone in various\napplications randomly, e.g. Electron/Slack will use the TZ for some\nbut not for other things,...)\n\n\nOn Tue, 1 Apr 2025 at 02:43, brian m. carlson\n<sandals@crustytoothpaste.net> wrote:\n>\n> On 2025-03-31 at 20:19:30, Devste Devste wrote:\n> > device time zone is not UTC0\n> >\n> > post-commit hook:\n> > if [[ -n \"${SKIP_POST_COMMIT+x}\" ]] && [[ \"$SKIP_POST_COMMIT\" -eq 1 ]]\n> > then\n> > exit\n> > fi\n> >\n> > TZ=UTC0 SKIP_POST_COMMIT=1 git commit --amend --reset-author --no-edit\n> > --no-verify --no-post-rewrite --allow-empty --quiet\n> >\n> > ---\n> >\n> > Run:\n> > git commit -m \"foo\"\n> > git show -s --format=\"Commit: %h%nAuthor: %an%nAuthor Date:\n> > %ad%nCommitter: %cn%nCommitter Date: %cd%nMessage: %s%n\" HEAD\n> >\n> > Expected that both Author Date: and Committer Date: show +0000 as\n> > timezone. Only committer date shows +0000, while author date shows\n> > time zone of device.\n> >\n> > When manually running the post-commit code (TZ=UTC0 SKIP_POST_COMMIT=1\n> > ...) it correctly resets the author date to +0000 too\n> >\n> > When manually setting the date using --date=\"$(date +%s) +0000\"\n> > instead of  --reset-author it also works correctly in post-commit\n> > hook.\n>\n> I can reproduce this, except that I had to use `TZ=America/Toronto`\n> since my system is in UTC.  A quick look at the code led me to a guess\n> that ended up correct.\n>\n> The `git commit` code sets `GIT_AUTHOR_NAME`, `GIT_AUTHOR_EMAIL`, and\n> `GIT_AUTHOR_DATE`.  The latter contains a timezone identifier.  As a\n> consequence, when the hook is invoked, those values are set in the\n> environment, and the subsequent `git commit` in the hook uses them.  You\n> can see this by placing something like `env >/tmp/foo` in your hook and\n> then reading the output.\n>\n> I will say that this is definitely not the intended use of the\n> post-commit hook and I am very much not surprised that this doesn't work\n> as you intended.  You could certainly fix it by unsetting those\n> environment variables, but I'd encourage you to adopt a different\n> approach, such as by using an alias that sets `TZ=UTC0` to commit.  That\n> will be more likely to work in the future and will definitely be less\n> brittle.\n>\n> For instance, your call to `git commit` will still invoke the\n> `prepare-commit-msg` (and possibly `commit-msg`) hook, which might\n> result in unexpected changes to your message or even a failure of the\n> second commit.\n> --\n> brian m. carlson (they/them)\n> Toronto, Ontario, CA\n"},{"id":"515472","messageId":"Z-xvfqCsQnDIGaXx@tapette.crustytoothpaste.net","threadId":"63228","inReplyTo":"CANM0SV0Np+52vjoopvAJ88U3Ue0EF35iarwBpOozrBH9KbH2Uw@mail.gmail.com","subject":"Re: --reset-author does not reset author date when used in post-commit hook","fromName":"brian m. carlson","fromEmail":"sandals@crustytoothpaste.net","sentAt":"2025-04-01T22:58:06Z","receivedAt":"2025-04-01T22:58:14Z","isPatch":false,"sender":{"key":"sandals@crustytoothpaste.net","avatar":"https://avatars.githubusercontent.com/u/497054?v=4"},"body":"On 2025-04-01 at 03:44:17, Devste Devste wrote:\n> >your call to `git commit` will still invoke the\n> `prepare-commit-msg` (and possibly `commit-msg`) hook\n> \n> Since I used --no-edit --no-verify in the example/hook, that shouldn't\n> be the case though?\n\nMy testing demonstrates that `commit-msg` is skipped with `--no-edit\n--no-verify`, but not `prepare-commit-msg`.\n\n> >such as by using an alias that sets `TZ=UTC0` to commit.\n> \n> Unfortunately, (at least on Windows) most tools and IDEs don't even\n> load a bashrc file (e.g. intellij) for their git UI, so that's not an\n> option (and setting the TZ as a global environment variable has\n> unwanted side effects, since it will set the timezone in various\n> applications randomly, e.g. Electron/Slack will use the TZ for some\n> but not for other things,...)\n\nWhat I could recommend instead is setting a shell script or PowerShell\nfile or such as the default Git binary (instead of git.exe) and setting\n`TZ=UTC0` in that script (possibly only if the command is `commit` or\n`commit-tree`).  I use Neovim with the fugitive extension, and\nit provides functionality to override the Git executable, so I suspect\nother editors will as well.  This is something that will likely be\nrobust and unlikely to break.\n\nThe reason I make that recommendation is that I suspect you're going to\nfind that there are other infelicities or weird edge cases in what\nyou're doing and this would be a use case that I could see getting\nbroken accidentally.  We've accidentally broken people doing `git add`\nin `pre-commit` hooks (which we also don't recommend) in the past, for\ninstance.\n-- \nbrian m. carlson (they/them)\nToronto, Ontario, CA\n"},{"id":"515493","messageId":"CANM0SV27g_37kLfgKu8GVg13mViQ78+DRwdjSf4mGrkNdndPLg@mail.gmail.com","threadId":"63228","inReplyTo":"Z-xvfqCsQnDIGaXx@tapette.crustytoothpaste.net","subject":"Re: --reset-author does not reset author date when used in post-commit hook","fromName":"Devste Devste","fromEmail":"devstemail@gmail.com","sentAt":"2025-04-02T10:06:26Z","receivedAt":"2025-04-02T10:06:40Z","isPatch":false,"sender":{"key":"devstemail@gmail.com","avatar":null},"body":">My testing demonstrates that `commit-msg` is skipped with `--no-edit\n--no-verify`, but not `prepare-commit-msg`.\n\nLooks like a separate bug though?\nhttps://git-scm.com/docs/git-commit#Documentation/git-commit.txt-code--no-editcode\nsays:\n>Use the selected commit message without launching an editor. For example, git commit --amend --no-edit amends a commit without changing its commit message.\n\nWhy would the prepare-commit-msg hook be called then?\n\n>What I could recommend instead is setting a shell script or PowerShell\nfile or such as the default Git binary (instead of git.exe) and setting\n`TZ=UTC0` in that script (possibly only if the command is `commit` or\n`commit-tree`).\n\nThe IDEs only work with git.exe. If it's not there, it won't run at\nall. The only thing possible is setting TZ= as a user environment\nvariable however that will obviously be picked up by other\napplications and cause the weirdest side-effects (e.g. Slack/Electron\nsuddenly start using that time zone for some things but the\nin-built/setting timezone for others and lots of other inconsistency\nissues in tons of applications)\nThere's various tickets for various IDEs for this use case, not just\nfor git, but also for npm and various other tools (e.g. people need to\nchange the JAVA version for a tool or whatever)\n\nSome time ago I opened \"Config timezone to prevent chaos when\nDST/changing timezone\" in the git mailing list and I saw you also\nreplied. But if git had a way to actually set the timezone, these\nworkarounds wouldn't be necessary; alas that's another issue\n\nOn Wed, 2 Apr 2025 at 00:58, brian m. carlson\n<sandals@crustytoothpaste.net> wrote:\n>\n> On 2025-04-01 at 03:44:17, Devste Devste wrote:\n> > >your call to `git commit` will still invoke the\n> > `prepare-commit-msg` (and possibly `commit-msg`) hook\n> >\n> > Since I used --no-edit --no-verify in the example/hook, that shouldn't\n> > be the case though?\n>\n> My testing demonstrates that `commit-msg` is skipped with `--no-edit\n> --no-verify`, but not `prepare-commit-msg`.\n>\n> > >such as by using an alias that sets `TZ=UTC0` to commit.\n> >\n> > Unfortunately, (at least on Windows) most tools and IDEs don't even\n> > load a bashrc file (e.g. intellij) for their git UI, so that's not an\n> > option (and setting the TZ as a global environment variable has\n> > unwanted side effects, since it will set the timezone in various\n> > applications randomly, e.g. Electron/Slack will use the TZ for some\n> > but not for other things,...)\n>\n> What I could recommend instead is setting a shell script or PowerShell\n> file or such as the default Git binary (instead of git.exe) and setting\n> `TZ=UTC0` in that script (possibly only if the command is `commit` or\n> `commit-tree`).  I use Neovim with the fugitive extension, and\n> it provides functionality to override the Git executable, so I suspect\n> other editors will as well.  This is something that will likely be\n> robust and unlikely to break.\n>\n> The reason I make that recommendation is that I suspect you're going to\n> find that there are other infelicities or weird edge cases in what\n> you're doing and this would be a use case that I could see getting\n> broken accidentally.  We've accidentally broken people doing `git add`\n> in `pre-commit` hooks (which we also don't recommend) in the past, for\n> instance.\n> --\n> brian m. carlson (they/them)\n> Toronto, Ontario, CA\n"}]}