Git hook on Ubuntu broken - bash

I recently got a git hook from someone that aims to add the issue number, which is in a specific location of the branch name, to the beginning of all commits. The goal is to take the #number from feature/#number-issue. Here is some info:
➜ .githooks pwd
/home/luctia/.githooks
➜ .githooks git config --global --list
user.name=luctia
user.email=myemail
core.hookspath=/home/luctia/.githooks
➜ .githooks cat commit-msg
#!/bin/sh
WI=$(git status --branch | grep -iPo "(feature|bug)\/#\d+" | head -1)
WI=$(echo "($WI)" | grep -Po "\d+")
if [[ ! -z "$WI" ]]; then
WI="#$WI"
CM=$(cat "$1")
if [[ ! $CM == *"$WI "* ]]; then
echo "$WI $CM" > "$1"
fi
fi
This doesn't seem to work, though. The script is executable for every user, so that's not the issue. I have tried switching from sh to bash, and with that edit I've executed the script on a file in a repo, which added the number to the beginning of the file, so I know it works. I'm not sure if git hooks can execute bash files, but it doesn't make a difference whether I use sh or bash, though I would like to know if it can run bash scripts.
I'm using Webstorm for my IDE right now, and it doesn't work in there, and it also doesn't work on CLI git. I have no idea how to proceed.
Edit: I am pretty sure the script is not executed. When I add data > /tmp/hook to the script, no file appears. I do have to change from sh to bash though.

The problem was that I was trying to make this work on a pre-existing project, with an existing .git directory. I thought changing the config with the --global flag would just work, but apparently the config inside the .git directory of the project did not change, and the old hookspath was still there. When I changed it, the script started working.

Related

Execute Bash Script After Git Post-Receive Hook executes

I have a bash script on my test server that will export my wordpress db, rsync the db to the prod server, and git push all of my files to prod sever.
Within the prod server's git repo I have a git post-receive hook correctly configured.
#!/bin/bash
#Receive Git Push from Test
git --work-tree=/home/username/public_html --git-dir=/home/username/public_html/git/production-site.git checkout -f
Within the working tree directory (WordPress directory) on the prod server I also have a bash script that will import the newly uploaded db. /home/username/public_html/db-import-script.sh
#!/bin/bash
#bunch of commands
...
...
...
Question:
How can I automatically execute the db import script immediately following a git push?
troubleshooting:
inside of post-receive, I have tried using an absolute paths to execute the script, no luck
#!/bin/bash
#Receive Git Push from Test
git --work-tree=/home/username/public_html --git-dir=/home/username/public_html/git/production-site.git checkout -f
#execute script with absolute path
/home/username/public_html/db-import-script.sh
db-import-script.sh does not execute. NOTE: this script must remain located in the Wordpress directory b/c it uses wp-cli commands for various actions.
any tips?
I use e.g. gitea and on server one has to simply copy a script in post-receive.d/ folder. The post-receive hook (see below and you may use it as a template) will scan this folder and execute scripts in it.
#!/usr/bin/env bash
# AUTO GENERATED BY GITEA, DO NOT MODIFY
data=$(cat)
exitcodes=""
hookname=$(basename $0)
GIT_DIR=${GIT_DIR:-$(dirname $0)/..}
for hook in ${GIT_DIR}/hooks/${hookname}.d/*; do
test -x "${hook}" && test -f "${hook}" || continue
echo "${data}" | "${hook}"
exitcodes="${exitcodes} $?"
done
for i in ${exitcodes}; do
[ ${i} -eq 0 ] || exit ${i}
done
kiss rule... (keep it simple stupid)
rather than spending days trying to learn sysdig well enough to trace a process that I have never previously heard of and it subprocesses. (no offence intended Charles, just need to actually get tasks done. Your bash debug-log snippet highly useful)
and rather than creating some git / gitea hybrid (no offence #m19v, I did try your solution, but didn't work)
knowing that production server db-import.sh worked properly and that my test server git push / db upload push.sh worked properly.
My final solution was to leave the production server's post-receive properly configured and to.... remotely execute my db-import.sh script via ssh directly within the directory in which it needs to be executed.
In a nutshell, I added this to the end of push.sh script on my test server:
#Remotely execute db import
ssh -p22 -i /home/username/.ssh/id_rsa username#1233.456.789.12 'cd public_html && bash' << EOF
./db-import.sh
EOF
Bang problem solved...

How do I show up the current branch in WSL ( I use Vscode)

I use Vscode, and I want to know Where my HEAD is pointing branch,
How Can I show up the current branch name like Bash?
I use WSL(ubuntu)termimal in my Vscode and OS is Windows 10
Thank you
Note that, from microsoft/vscode issue 67670, the current branch name is already visible in the status bar of VSCode.
Or, with Git 2.22+ (Q2 2019)
git branch --show-current
It is true the prompt in a git bash in VSCode does not display the Git branch.
You need to configure the $SHELL
For example, to enable running bash as a login shell (which runs .bash_profile), pass in the -l argument (with double quotes):
// Linux
"terminal.integrated.shellArgs.linux": ["-l"]
Then in your ~/.bashrc can include a special prompt.
I got it configured by modifying the .bashrc file in the /home/ on the WSL session. You can do vim ~/.bashrc to edit the file.
Find and replace the code block in the .bashrc with this;
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u#\h\[\033[00m\]:\[\033[01;34m\]\$(`git branch --show-current 2>/dev/null`)\[\033[00m\]\$ '
else
PS1='${debian_chroot:+($debian_chroot)}\u#\h:\w\$(`git branch --show-current 2>/dev/null`)\$ '
fi
I found #Diganto Paul's answer does not show the current directory (as it was by default). I used this instead:
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u#\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\[\033[33m\]$(parse_git_branch)\[\033[00m\]\$ '
Copied from: https://hinty.io/ivictbor/show-git-branch-in-bash-on-linux-windows-wsl-2-cygwin-prompt/

How to run a script on every commit

I want to run a script locally whenever someone in RepoA, makes a commit on a branch
currentBranch=$(git rev-parse --abbrev-ref HEAD)
if [[ $currentBranch = *"my-"* ]]; then
echo "contains my"
else
echo "Hold on there! you need to rename your branch before you commit!
"
fi
I have this running so far which works, whenever I run npm run test:script it runs > "test:script": "./branchname.sh"
however, I have some issues. one how can I run this every time someone commits?
I have tried putting it in my package.json
"pre-commit": [
"lint",
"test:script"
],
but it doesn't run on every commit
also, how can I get the commit itself to abort if the script fails, i.e. jump into the else block
You can take advantage of git hooks. There is a bunch of files you can find in your .git/hooks folder whitin your project folder.
Here the full documentation: https://git-scm.com/book/gr/v2/Customizing-Git-Git-Hooks
Note that you need to give exec permissions to hook files.
Hooks can be basically bash files, so you can abort the commit exiting with a value != 0.
Here an example: https://github.com/xyzale/my-stuff/blob/master/git/hooks/pre-commit
In order to share the hooks with your collaborators you can add a hooks/ folder to your repository and either symlinking it to .git/hooks or editing the git config about the hooks location through the command
git config core.hooksPath hooks

Docker unable to start an interactive shell if the image has an entry script

My custom-made image ends with
ENTRYPOINT [ "/bin/bash", "-c", "/home/tool/entry_script.sh" ]
This is absolutely needed because at runtime, the first thing the user must do is to update an already cloned github project, and users will often forget to do it.
But then, when i try to launch using
docker run -it --rm my_image /bin/bash
i can see that the ENTRYPOINT script is being executed, but then the container exit.
I expect to have /bin/bash being executed and the shell to remain in interactive mode, due to -it flags.
What am I doing wrong?
UPDATE: I add my entry script
#!/bin/bash
echo "UPDATING GIT REPO";
cd /home/tool/cloned_github_tools_root
git pull
git submodule init
git submodule update
echo "Entrypoint ended";
Actually I've not kind of errors at runtime
When you set and entry point in a docker container. It is the only thing it will run. It's the one and only process that matters (PID 1). Once your entry_point.sh script finishes running and returns and exit code, docker thinks the container has done what it needed to do and exits, since the only process inside it exits.
If you want to launch a shell inside the container, you can modify your entry point script like so:
#!/bin/bash
echo "UPDATING GIT REPO";
cd /home/tool/cloned_github_tools_root
git pull
git submodule init
git submodule update
echo "Entrypoint ended";
/bin/bash "$#"
This starts a shell after the repo update has been done. The container will now exit when the user quits the shell.
The -i and -t flags will make sure the session gives you an stdin/stdout and will allocate a psuedo-tty for you, but they will not automatically run bash for you. Some containers don't even have bash in them.
I think the original question and answer are pretty good (thank you!). However I had the same exact problem but the provided solution did not work for me. I ended up wasting a lot of time figuring out what I was doing wrong. Hence I came up with a solution that should work all the time, if this could save time for others. In my docker entry point I'm sourcing a shell script file from Intel compiler and the received parameters $# are somewhat changed by the 'source' command. Then when ending the script with /bin/bash "$#" the original parameters are gone. Here is my updated version that would be safer for all use cases:
#!/bin/bash
# Save original parameters
allparams=("$#")
echo "UPDATING GIT REPO";
cd /home/tool/cloned_github_tools_root
git pull
git submodule init
git submodule update
echo "Entrypoint ended";
# Forward initial parameters
/bin/bash "${allparams[#]}"

Bash Shell Script Process Each Directory in Home

I am using Git Bash, and I would like to write a script that processes the same set of commands for each directory (local repo) in my home directory. This would be easy enough in DOS, which most consider as handicapped at best, so I'm sure there's a way to do it in bash.
For example, some pseudo-code:
ls --directories-in-this-folder -> $repo_list
for each $folder in $repo_list do {
...my commmand set for each repo...
}
Does anyone know how to do this in Bash?
You can do that in bash (even on Windows, if you name your script git-xxx anywhere in your %PATH%)
#! /bin/bash
cd /your/git/repos/dir
for folder in $(ls -1); do
cd /your/git/repos/dir/$folder
# your git command
done
As mentioned in "Git Status Across Multiple Repositories on a Mac", you don't even have to cd into the git repo folder in order to execute a git command:
#! /bin/bash
cd /your/git/repos/dir
for folder in $(ls -1); do
worktree=/your/git/repos/dir/$folder
gitdir=$worktree/.git # for non-bare repos
# your git command
git --git-dir=$gitdir --work-tree=$worktree ...
done

Resources