How to prepend partial branch name to git commit with husky - githooks

I have a functional commit-msg git hook that takes the string from beginning up to the first underscore from a git branch's name and prepends it to the commit message.
For better understanding: The branch name is JIRA-123_fix_problem, the commit command goes: git commit -m 'Fix problem' and through the commit-msg hook the commit message will automatically be changed into JIRA-123: Fix problem.
Now we're using Nuxt for our current Vuejs project and this makes use of husky. We want to keep husky in this project. Unfortunately (and, I guess, intentionally), husky works by overwriting the git commit hooks, and thus the file .git/hooks/commit-msg I use will be overwritten by husky and the original commit-msg hook does not work any more.
How can I either prevent husky from overwriting the commit-msg hook or instruct husky to do the same thing?
I can't find any documentation for this problem.

Ok, I finally found (and verified) the soution:
In this husky issue ("Support custom git hooks to be not overridden by husky #323") typicode says:
Husky is overwriting file, because there [is] # husky in it. It checks for this string to know if it's a user script or if it has been generared by husky.
If you delete # husky, it will never overwrite them again.
After I removed the # husky line, re-installing all node modules in our project finally keeps the commit-msg hook I want to use. I originally just copied & pasted the code while maintaining the # husky line from the scaffolding of the project.

Related

NexJS Tailwind not loading styles after cloning a github repository

So I'm following a React NextJS tutorial and everything was fine yesterday, but today I had trouble accessing Sanity backend. After Googling the solution was to comment out tailwindcss: {}, in postcss.config.js.
But then all the styles in the app stopped working, even if I un-commented tailwindcss: {},
Remembering that everything worked last time I pushed to GitHub yesterday I cloned the repo but the issue persists.
Then I started the tutorial from scratch, and both Tailwind and Sanity were working perfectly and they still are. But I was trying to figure out what stops my original app from working.
Been bashing my head for hours now and still can't come up with a solution. I even pushed the now working app, cloned it again, and the clone doesn't work while the original of the clone still works.
I ran both npm i --legacy-peer-deps and Tailwinds installation
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p
The npx command told me that tailwind.config.js and postcss.config.js already exist.
So I tried cloning the repo, deleting tailwind.config.js, postcss.config,js, and package-lock.json. Also ran uninstall command for Tailwind. And then again ran npm i and Tailwinds installation. But it still doesn't work.
Here's the repo - https://github.com/RUSHWAYC/tikdok-test.git
Here's how it looks like:
Here's how it should look like:
Am I missing anything? Any commands that I should be running in the terminal?
Any help would be appreciated.
Steps you can follow to fix this issue:
Uncomment tailwindcss: {} from postcss.config.js, then re-run your development server.
Completely remove everything, start from scratch, then only copy/import files you need from the started repository.
After hours of fewer induced hallucinations I figured it out.
So I found a way on how to fix the Tailwind styles not applying.
Clone the last push to GitHub
Delete tailwind.config.js | postcss.config.js | package.json | package-lock.json
Remake tailwind files and package.json (or in my case copy them from the dude whose project I'm copying)
npm i --legacy-peer-deps
cd backend (to go to Sanity backend folder)
npm i --legacy-peer-deps
cd ..
Don't forget to comment out tailwindcss: {}, in postcss.config.js as it messes up Sanity
npm run dev
So it works now and shows Tailwind CSS applied to the project.
But then, if you git add . there are these errors:
warning: in the working copy of 'package.json', CRLF will be replaced by LF the next time Git touches it
warning: in the working copy of 'postcss.config.js', CRLF will be replaced by LF the next time Git touches it
warning: in the working copy of 'tailwind.config.js', CRLF will be replaced by LF the next time Git touches it
So git reset that.
Found this useful post about the first error. - Git replacing LF with CRLF
Basically, it's something dumb that I'm even dumber to understand.
But at the end of the post, they linked this answered question here about git add --renormalize . that fixes the issue - Git: how to renormalize line endings in all files in all revisions?
So after doing all the steps above and writing some code, one should run:
git add --renormalize . and then git commit -m "Message."
There should be no errors like previously this time, and every time you git commit next there shouldn't be any issues (unless you edit the files in question).
I also changed the config autocrlf settings to input from true after reading the first article I linked. Not sure if I should, but I did and the article I followed on how to do it can be found here - How to change line-ending settings

How to handle git and react project for the first time?

I've installed a react project recently this way:
npx create-react-app projectName
When it successfully installed, apparently the git is also installed on it. Since there is .git directory exist in the root.
From the other side, I created a new project on Gitlab and connected it to the project this way:
git remote add origin http://gitlab.<domain>.com/myName/projectName.git
Now, I want to push a commit on the git named something like "Project Init". But there is no change detected when I run git status. So I cannot add and commit anything.
So, when I run git push origin master I get this error:
hint: 'git pull ...') before pushing again.
When I run git pull origin master, get this error:
fatal: refusing to merge unrelated histories
I stuck in this part ..! What I have to do? In other word, how to synchronise the git and a react project created just now?
Correct way to do it is to create new repo without "initial readme.md commit". Fast way is to git push origin main --allow-unrelated-histories. Or just simply use --force flag in push, but be careful with that
You can use the allow-unrelated-histories flag when pushing.

npm run deploy ignore hooks

I was getting below error while committing the changes.
Simply adding .pre-commit-config.yaml file or deleting .git\hooks folder was not resolving my problem. So I added --no-verify option while committing and it solved my problem.
Now, I am trying to deploy my react app to Github Pages. When I run npm run deploy I get the same error as shown in the below picture.
How I can add --no-verify option while npm run deploy? Simply adding .pre-commit-config.yaml file or deleting .git\hooks folder is not resolving the problem.
I don't want to try pre-commit uninstall command because the command might uninstall the pre-commit globally.
Is there any alternative to disable the hooks locally specific to the current repository?
Edit:
I tried pre-commit uninstall as well. Still throws the same error when I do npm run deploy.
That message should come from c:\dev\react-apps\find-my-event\.git\hooks\pre-commit
For testing, you can rename pre-commit to pre-commit.old and see if the npm run deploy is still affected by the same error message.
That being said, if you are using the pre-commit/pre-commit python frmaework for managing and maintaining multi-language pre-commit hooks., then issues 457 cleary states:
You need one of those to tell pre-commit what hooks you want to run.
To see how to configure one, see http://pre-commit.com
For instance, at a minimum:
Create a file named .pre-commit-config.yaml
you can generate a very basic configuration using pre-commit sample-config

How can I manually run a Git pre-commit hook, without attempting a commit?

I just want to be able to run it to see if the code in my working tree passes it, without actually attempting a commit.
Just run the pre-commit script through the shell:
bash .git/hooks/pre-commit
There's a Python package for this available here. Per the usage documentation:
If you want to manually run all pre-commit hooks on a repository, run pre-commit run --all-files. To run individual hooks use pre-commit run <hook_id>.
So pre-commit run --all-files is what the OP is after.
For a single file:
pre-commit run --files YOUR_FILENAME
Just run git commit. You don't have to add anything before doing this, hence in the end you get the message no changes added to commit.

Why isn't my git hook firing?

I'm trying to write a git hook that fires on git pull, git merge, and git checkout, when my .emacs file changes. For some reason, the hook never seems to fire.
https://github.com/mcandre/dotfiles/blob/master/hooks/post-merge-checkout
osse on #git / #bash helped me identify the issue: I was using the wrong git command to search for file changes. The new one works:
https://github.com/mcandre/dotfiles/blob/594e24d28e599e57b401d3c163d72498b8a3c1f0/hooks/post-merge-checkout

Resources