I have been researching how to change this behavior all day with no luck, so here goes.
Is there a way in iterm2, when viewing git logs, to change the way the cmd+click functions on the git log hash? Ideally, I am hoping that cmd+click would would open a browser window with the correct github url where the change set could be viewed.
If this is not possible, please let me know. I believe this would be very helpful to others, I wish I had the magic wand to figure out how to configure this.
Thoughts?
While this is not ideal, here is how I was able to work around this issue. I built a commit hook! Not perfect, I know. Ideas?
#!/bin/sh
#
# Automatically adds branch name and branch description to every commit message.
# Edit .git/hooks/commit-msg & make sure it is excutable chmod +x
# Requires git config --add remote.github.url {value}
#
NAME=$(git branch | grep '*' | sed 's/* //')
DESCRIPTION=$(git config branch."$NAME".description)
TEXT=$(cat "$1" | sed '/^#.*/d')
GIT_COMMIT_SHORT_ID=$(git rev-parse --short HEAD)
GIT_COMMIT_ID=$(git rev-parse HEAD)
GIT_GITHUB_URL=$(git config --get remote.github.url)
if [ -n "$TEXT" ]
then
echo "$NAME"': '$(cat "$1" | sed '/^#.*/d') > "$1"
if [ -n "$DESCRIPTION" ]
then
echo "" >> "$1"
echo $DESCRIPTION >> "$1"
fi
echo $GIT_GITHUB_URL$GIT_COMMIT_ID >> "$1"
else
echo "Aborting commit due to empty commit message."
exit 1
fi
Related
I have almost 3 days thinking about it and I can't find a way to make me add a topic without having to do a git push.
Is there any way to modify your push after doing a git push?
Let me explain:
I'm trying to make a pre-push extract a specific value from the message and put it as a topic so I can do this:
git commit -m "CELL:ANT-1234 Testing pre-push"
git push master
Be equal to:
git commit -m "CELL:ANT-1234 Testing pre-push"
git push master -o topic=ANT-1234
I have managed to prepare everything and have the topic and message prepared without problems but I can't find a way to put it in the push without having to do another git push inside the pre-push, this makes me do the push 2 times and... although it works, in the terminal I get an error because when it is executed 2 times it tells me that there are no changes.
I have managed to get this far:
#!/bin/sh
while read local_ref local_sha remote_ref remote_sha
do
FULL_COMMIT_MESSAGE=$(git log -1 --pretty=%B $local_sha)
MESSAGE=$(echo "$FULL_COMMIT_MESSAGE" | awk '{print $1}')
TOPIC=$(echo "$remote_ref" | awk -F 'topic=' '{print $2}')
# If the value of the topic is not specified in the flag, we obtain it from the CELL identifier
echo "Validating if topic are set"
if [ -z "$TOPIC" ]; then
if echo "$MESSAGE" | grep -iE '^CELL:(ANT|JDRL)' > /dev/null; then
TOPIC=$(echo "$MESSAGE" | awk -F ':' '{print $2}')
echo "The topic are set with value -> ${TOPIC}"
# PROBLEM HERE
git push --push-option=topic=$TOPIC origin "$local_sha:$remote_ref$TOPIC"
exit 0
fi
fi
echo "Validating that the commit set message is correct"
echo $MESSAGE
if ! echo "$MESSAGE" | grep -iE '^CELL:(ANT|JDRL)' > /dev/null; then
echo "ERROR: Commit message must start with 'CELL:ANT' or 'CELL:JDRL'"
exit 1
fi
echo "Validate if exist a topic in this commit"
if [ -z "$TOPIC" ]; then
echo "ERROR: Flag not found -o topic=XXX or commit message does not start with 'CELL:ANT' or 'CELL:JDRL'"
exit 1
fi
done
my problem is in:
git push --push-option=topic=$TOPIC origin "$local_sha:$remote_ref$TOPIC"
isn't there another way to add the topic just to the first push I did in the terminal instead of the pre-push?
Pre-push is a validator, if you want to control the push options you're going to have to script it, probably a git alias is best because it allows easy repo-local overrides.
I am trying to write a bash script which checks if there are any commits of a repository and returns a message. Here is the script so far.
MY_PATH="C:/test"
cd "$MY_PATH"
git clone https://github.com/test-repo/docker-react.git
cd "docker-react"
git checkout master
if [[ -n "`git log --pretty=format: --name-only --since="200 days ago" | sort | uniq`" ]]
then
echo -e No Commits last 2 days
else
echo -e Commits available
fi
When I execute this code it always show commits available even when I change the days.
Where am I going wrong?
That looks like a bash script, which would be interpreted by the Git for Windows bash shell.
It means it should be checked with ShellCheck, which would then recommend:
#!/bin/bash
MY_PATH="C:/test"
cd "$MY_PATH" || exit
git clone https://github.com/test-repo/docker-react.git
cd "docker-react" || exit
git checkout master
if [[ -n "$(git log --pretty=format: --name-only --since="200 days ago" | sort | uniq)" ]]
then
echo -e No Commits last 2 days
else
echo -e Commits available
fi
Not yet ideal: ideally, it would check if the repository is not already cloned. But it should work better.
I'm trying to create a script to do this:
git add "file"
git commit -m "Comment"
My idea is to run:
gac "file" "Comment"
I know I can do something similar but for all files, with:
echo 'alias gac="/path/to/gitaddcommit.sh"' >> ~/.bash_profile
And the .sh would be:
!/bin/bash
git add .
echo “Enter commit message: “
git commit -am “$commitMessage”
Well you need two things :
A bin folder where you can put every sh script you want to use everywhere.
More knowledge about shell scripting and how you can get argv (in your ex: 'file' 'Comment')
So first go to your /home/<username> then mkdir bin && cd bin && pwd
then copy the pwd and add it into your PATH env variable inside your .bashrc
path example: PATH='/bin/:/sbin/:/home//bin
Then source ~/.bashrc you can now use every sh script inside you bin folder everywhere.
Cool so first problem done !
you don't have to do echo alias gac="/path/to/gitaddcommit.sh"' >> ~/.bash_profile anymore.
Now second problem here a post that can help you post
And let me show you for your example :
cd ~/bin && vi gac.sh
Now the script :
#!/bin/sh
if [ "$#" -ne 2 ]; then
echo "Usage: ./gac FILENAME COMMIT_MESSAGE" >&2
exit 1
fi
git add "$1"
git commit -am "$2"
First we check the number or arg then git add and commit.
Simple and fast maybe checking if arg one is a file might be a good idea too.
PS: i'm going to re write my post ahah
Here's what I have in my .bashrc:
ga ()
{
if test "$1" != "-f" && git rev-parse HEAD > /dev/null 2>&1 && ! git diff-index --quiet HEAD; then
echo 'Repo is dirty. -f to force' 1>&2;
return 1;
fi;
git add "$#";
list=$(git diff --name-only --cached | tr \\n \ );
git commit -m "Add $list"
}
The commit message is autogenerated, but you could easily modify it to prompt the user or take it from somewhere else.
I have a bash script that gets updated fairly often that I would like to have self-update only itself using git, but not affect anything else.
I found an Example Script that updates itself, but it uses git pull --force which updates everything. Most of the time this should be fine, but I hesitate to automatically do something with the potential to have unintended consequences, safer to just affect only itself.
My attempts to modify that script to use checkout or cherry-pick have not been successful.
Does anyone have a function that updates only $0 or can write one?
Edit:
This is the messy code I wrote for my script.
#!/bin/bash
BRANCH="master"
SCRIPTNAME=$1
REPOSITORY="https://stash.xxx/projects/IT/repos/xxx/browse/$SCRIPTNAME"
self_update() {
git fetch
if [[ -n $(git diff --name-only origin/$BRANCH | grep $SCRIPTNAME) ]]
then
echo The version you have and the version on stash are different
echo
echo Do you want to:
echo
echo s. Show messy differences
echo c. Open repository in Chrome
echo
echo d. Download the stash version, overwrite your current version, and exit script
echo
echo q. return to the previous menu
read choice
case $choice in
s)
git diff origin/$BRANCH
echo
read -p "Enter to Return " enterkey
;;
c)
open -a "/Applications/Google Chrome.app" "$REPOSITORY"
;;
d)
git checkout -f origin/$BRANCH -- $SCRIPTNAME
#head -5 $SCRIPTNAME
exit
;;
q)
break
;;
*)
echo Please enter one of the choices.
;;
esac
else
echo You are using the current version of $SCRIPTNAME
break
fi
}
#testing code
head -5 $SCRIPTNAME
while :
do
self_update
done
head -5 $SCRIPTNAME
Checkout should work
git fetch the-remote
git checkout the-remote/the-branch -- the-file.sh
This better not run on windows because it will reject rewrite the script while it's running.
I have one pre-commit script/hook working just fine to search for specific string pattern in the files and reject the commit. I'm not sure how to read the incoming files in the pre-receive script to search for string pattern.
My pre-commit scripts looks like this:
#!/usr/bin/env bash
if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
# Initial commit: diff against an empty tree object
EMPTY_TREE=$(git hash-object -t tree /dev/null)
against=$EMPTY_TREE
fi
FILES=$(git diff --cached --name-only $against)
if [ -n "$FILES" ]; then
string1 = $(grep -rE --line-number 'access_key' $FILES)
if [ -n "$string1" ] then
echo "string1 there so reject it"
while true; do
exit 1;
done
fi
fi
I'm not sure how to convert this to a pre-receive hook script on git server side.
I've been trying this for hours with no luck. Can someone please help me out here?