git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Get the commits to be pushed accurately in pre-push hook

From
JCJayce Cao <jaycecao520@gmail.com>
Date
Feb 13, 2025, 02:05 UTC
Message-ID
<CAGwx5_8wNzE51Z7pYA_skimZZ29b8C_Bxk_58kPOqcWAmSQpHw@mail.gmail.com>

My goal is to check the commits to be pushed in pre-push hook to see if they contain sensitive data or not. I have an assumption that those commits which already exist in remote repos have no need to check.

So I read the Git doc and pre-push.sample file, I know that if we push to a new branch that the remote does not have, $remote_oid weil be zero, so we need to examine all commits in this branch. We can run `git rev-list $local_oid` to get all commits to be examined.

But consider this case, if I'm developing a huge project which has millions of commits. I create a new branch (we call it feat/awesome-feat) based on the master branch on my local repo, and create three commits. Then I run the `git push --set-upstream origin feat/awesome-feat` command to push the three commits to the remote. But when the pre-push hook is called, `git rev-list $local_oid` will print millions of commits. The commits except the new three already exist in the remote repo. And the `git push` command will send data only in the new commits to the remote, instead of all history commits.

So I mean we've no idea which commits will be sent to the remote
indeed in the pre-push hook when pushing to a new branch
that the remote doesn't have. I found a workaround:
* Run `git ls-remote -q -h` command to get the commits the remote has.
* Run `git rev-list $local_oid ^$haves` command to get the commits to
be pushed.($haves are the commits obtained from the previous step).

But this workaround seems to be stupid when the remote has many branches. I wonder if there is any better way to get the commits to be pushed accurately in the pre-push hook.

Next: brian m. carlson
Message 1 of 2 in “Get the commits to be pushed accurately in pre-push hook”
  1. Jayce CaoFeb 13, 2025
  2. brian m. carlsonFeb 13, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.