Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect the upstream branch name when base is not specified #139

Merged

Conversation

beaucollins
Copy link
Contributor

@beaucollins beaucollins commented Jul 9, 2020

Currently the lint-prepush command assumes master as a base branch in the project.

While the configuration does allow this to be explicitly overridden we can potentially provide a default behavior that works even when a project doesn't use master as a base branch.

Changes

When the base branch is not explicitly specified, instead of defaulting to master, git rev-parse can be used to determine the named git ref for the remote branch being pushed to.

For example, I'm on a local branch named fix/hard-coded-master, and it is set to use origin/fix/hard-coded-master as the upstream branch.

git branch --set-upstream-to=origin/fix/hard-coded-master

Using git rev-parse when can see what the current branch's upsream ref is:

git rev-parse --abbrev-ref $branch@{upstream}
origin/fix/hard-coded-master

Using this information, when the base branch is not explicitly specified and the local branch has an upstream branch, we can now use these two refs with git diff to get the list of relevant files.

Here's the command as utils/fetchGitDiff.js would run it:

git diff `git rev-parse --abbrev-ref $branch@{upstream}`...HEAD
index.js

How To Test

Make sure the local branch has an upstream branch and the base is not specified in package.json:

git branch --set-upstream-to=origin/fix/hard-coded-master
Branch 'fix/hard-coded-master' set up to track remote branch 'fix/hard-coded-master' from 'origin'.

cat package.json| jq '.["lint-prepush"].base'
null

Clear out the .lint-prepush cache:

rm -r ~/.lint-prepush

Reset the remote branch to remove the local branch's commits if they've already been pushed:

git push --force origin master:fix/hard-coded-master

Run git push with DEBUG=*

DEBUG=* git push 
  // snip
  lint-prepush:index Base not specified, checking for upstream ref +1ms
  lint-prepush:index Upstream branch name origin/fix/hard-coded-master +7ms
  // stip
  lint-prepush:index Committed GIT files:  [ 'index.js' ] +9ms
  ✔ Linting *.js files
    ✔ node_modules/.bin/eslint (401ms)
  lint-prepush:index Cached Current Commit Hash +410ms

Voila! 🎉  Code is ready to be Shipped.

@theenadayalank
Copy link
Owner

Thank you @beaucollins for your contribution to this project. Your change has been scheduled to be related with the next major release(v3.0.0).

@theenadayalank theenadayalank changed the base branch from master to develop April 29, 2024 18:38
@theenadayalank theenadayalank self-requested a review April 29, 2024 18:38
@theenadayalank theenadayalank added the enhancement New feature or request label Apr 29, 2024
@theenadayalank theenadayalank merged commit e527f27 into theenadayalank:develop Apr 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants