Chatie / git-scripts

Git Hooks Integration for Chatie Projects
https://npmjs.com/package/@chatie/git-scripts
Apache License 2.0
1 stars 4 forks source link

Re-push produces meaningless version when push fails #19

Closed binsee closed 2 years ago

binsee commented 2 years ago

https://github.com/wechaty/puppet-xp/commits/main?after=983ec43712db872ba6a92307005e119be5262dd4+69&branch=main

image

binsee commented 2 years ago

Unable to successfully push new branch.

Outer git push -u will fail with process.exit(1). The inner git push cannot get the specified branch name, resulting in the inability to git push remote branch

Running git branch --set-upstream-to before git push will not work because the remote does not have this branch.

If the outer process.exit(0) will cause the push not to be the latest commit.

There are two solutions:

  1. Filter out the repeated npm version, that is, the last commit was a modified version, then the version is not modified this time, just process.exit(0). If you run npm version, you need to execute git push again after the current hook returns to ensure that the latest commit is pushed.
  2. Submit pr to git and pass -u and branch parameters to pre-push.
huan commented 2 years ago

The git push hook definitely should be improved.

Let's try to find some solutions to make it better and we can discuss it in our next community meeting.

binsee commented 2 years ago

I think this problem can be solved by the new feature of git-push hook.

refer to:

This hook is called by linkgit:git-push[1] and can be used to prevent a push from taking place. The hook is called with two parameters which provide the name and location of the destination remote, if a named remote is not being used both values will be the same.

Information about what is to be pushed is provided on the hook's standard input with lines of the form:

SP SP SP LF For instance, if the command +git push origin master:foreign+ were run the hook would receive a line like the following: refs/heads/master 67890 refs/heads/foreign 12345 although the full object name would be supplied. If the foreign ref does not yet exist the `` will be the all-zeroes object name. If a ref is to be deleted, the `` will be supplied as `(delete)` and the `` will be the all-zeroes object name. If the local commit was specified by something other than a name which could be expanded (such as `HEAD~`, or an object name) it will be supplied as it was originally given. If this hook exits with a non-zero status, `git push` will abort without pushing anything. Information about why the push is rejected may be sent to the user by writing to standard error. ``` - https://github.com/git/git/blob/4b6846d9dcd391164b72bd70e8a0c0e09776afe3/t/t5571-pre-push-hook.sh I haven't confirmed which version of git this feature is from, and I hope it can be used as soon as possible.
binsee commented 2 years ago

After testing, this feature has been supported in earlier versions. I tested the two versions installed on the mac, and they all work

#!/usr/bin/env node

var path = require('path')
  , scripts = require('../../')
  , name = path.basename(__filename)
  , project = scripts();

const readline = require('readline').createInterface({
  input: process.stdin,
  output: process.stdout
});

readline.question('', data => {
  console.log('input data:', data)
  readline.close()
});

console.log('args:',JSON.stringify(process.argv.slice(2)));
setTimeout(() => {
  process.exit(-1);
}, 1000);
> git push origin test
args: ["origin","git@github.com:binsee/sidecar.git"]
refs/heads/test 65b8bd5bd8e8c889fb8ec6228cfada35164cc423 refs/heads/test 0000000000000000000000000000000000000000
input data: refs/heads/test 65b8bd5bd8e8c889fb8ec6228cfada35164cc423 refs/heads/test 0000000000000000000000000000000000000000

According to the official documentation, this feature has been supported since at least the 2015 version of git 2.2.3.

So, we can use this feature to improve pre-push.