I'm trying to write a shell script that should fail out if the current git branch doesn't match a pre-defined value.
if $gitBranch != 'my-branch'; then
echo 'fail'
exit 1
fi
Unfortunately, my shell scripting skills are not up to scratch: How do I get the current git branch in my variable?
To get the name of the current branch: git rev-parse --abbrev-ref HEAD
So, to check:
if test "$(git rev-parse --abbrev-ref HEAD)" != my-branch; then
echo Current branch is not my-branch >&2
exit 1
fi
You can get the branch using git branch and a regex:
$gitBranch=$(git branch | sed -n -e 's/^\* \(.*\)/\1/p')
After that you just have to make your test.
Related
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 bash script that iterates over 4 different folders (git repositories) and updates/pushes the changes for each one of them. I have the script mostly complete, except the authentication part.
Here's my current script:
#!/bin/bash
# Fetch username and password
echo "Please insert your git credentials!"
read -p 'Username: ' username
read -sp 'Password: ' password
# Check if you svn and git installed in your machine
if which svn &> /dev/null && which git &> /dev/null; then
# Store the current dir
CUR_DIR=$(pwd)
# Let the person running the script know what's going on.
echo "Pulling in latest changes for all repositories..."
for D in $CUR_DIR/*; do
if [ -d "${D}" ]; then
echo "${D}"
cd $(basename $D);
# Make sure SVN is on trunk branch and git branch is on master
SVN_BRANCH=$(svn info | grep '^URL:' | egrep -o '(tags|branches)/[^/]+|trunk' | egrep -o '[^/]+$')
GIT_BRANCH=$(git symbolic-ref --short HEAD)
if [ "$SVN_BRANCH"!="trunk" ] || [ "$GIT_BRANCH"!="master" ]; then
echo $CUR_DIR
echo "Make sure you're on SVN trunk branch and git master branch."
exit -1
fi
echo "Update SVN repository";
svn up
# Update git
git add .;
git commit -m "Update with the changes from svn trunk branch."
echo "Start pushing changes to main repository."
git push origin master
echo "Update git repository";
git pull origin master
# Update SVN
svn add .
svn commit -m "Update with changes from git master branch."
cd ..
fi
echo "Complete!"
done
else
echo "You need both svn and git CLI's installed to run this script!";
fi
As you can see, I'm already fetching the credentials in the beginning of the script, my problem is how can I use them to do the actions that need authentication, namely
git push origin master
git pull origin master
How can I build the git commands to make the authenticated request?
I am trying to execute a migration from SVN to GIT using git svn clone .. and everything works smoothly.
Now i need to transform the tags in SVN to real tags in git using this command
for t in $(git for-each-ref --format='%(refname:short)' refs/remotes/origin/tags)
do
git tag ${t/origin\/tags\//} $t - #BAD substitution error - need to fix
git branch -D -r $t
done
If i run this script in my commandline it works, but if i run this script in a shell script it will fail with a "Bad substitution error" . Any advice here?
The full script is here:
#!/bin/bash
## Modificed script - Fork from https://github.com/MPDFT/svn-to-git
####### Project name
PROJECT_NAME="" #Example - Digisharp
SVN_USERNAME="" #Example - adys
GIT_USERNAME="" #Example - adys
GIT_CREDENTIAL="" #Example - pz2fekhjcsq5io5xbslcuss5lspo4lcgh4cwjswge265uzxrnzxv
####### SVN
# SVN repository to be migrated
SVN_URL="" #Example -
####### GIT
# Git repository to migrate - IMPORTANT! YOU MUST INCLUDE YOUR USERNAME AND PASSWORD(PAT TOKEN FOR AZURE)
# We need this to automate the git push without having it asking you for password
GIT_URL="" #Example -
###########################
#### Don't need to change from here
###########################
#STYLE_COLOR
RED='\033[0;31m'
LIGHT_GREEN='\e[1;32m'
NC='\033[0m' # No Color
echo -e "${LIGHT_GREEN} [LOG] Starting migration of ${NC}" $SVN_TRUNK
echo -e "${LIGHT_GREEN} [LOG] Using: ${NC}" $(git --version)
echo -e "${LIGHT_GREEN} [LOG] Using: ${NC}" $(svn --version | grep svn,)
echo -e "${LIGHT_GREEN} [LOG] Step 01/05 Create Directories ${NC}"
echo -e "${LIGHT_GREEN} [RUN] Step 02/05 ${NC} £ git svn clone --stdlayout --no-minimize-url $BASE_SVN $PROJECT_NAME --user=$SVN_USERNAME"
git svn clone --stdlayout --no-minimize-url $SVN_URL $PROJECT_NAME --user=$SVN_USERNAME --authors-file=authors.txt
cd $PROJECT_NAME
echo -e "${LIGHT_GREEN} [RUN] Step 03/05 ${NC} $ git remote add origin"
git remote add origin $GIT_URL
echo -e "${LIGHT_GREEN} [RUN] Step 04/05 ${NC} - Preparing the git branches and tags"
for t in $(git for-each-ref --format='%(refname:short)' refs/remotes/origin/tags)
do
#git tag ${t/origin\/tags\//} $t - BAD substitution error - need to fix
git branch -D -r $t
done
for b in $(git for-each-ref --format='%(refname:short)' refs/remotes)
do
git branch $b refs/remotes/$b
git branch -D -r $b
done
echo -e "${LIGHT_GREEN} [RUN] Step 05/05 [RUN] git push ${NC}"
git push origin --all
git push origin --tags
echo "Successful - The git repository is now available in" $GIT_URL
I run the command with sh migration.sh
Are you executing your script with something like /bin/sh /path/to/script.sh?
${t/origin\/tags\//} is available in bash but isn't POSIX shell compatible, you might have success chancing it to something like:
echo "$t" | sed 's~origin/tags/~~'
I would highly recommend linting your script with shellcheck, as you got a few potential errors.
I run the command with sh migration.sh.
${var/search/replacement} is a bash feature. Running sh explicitly means the #!/bin/bash shebang line is ignored. Never run scripts that way.
There's no need to edit the script to remove the bashisms. Just run it with ./migration.sh so the shebang is respected.
$ ./migration.sh
I am trying to navigate through all existing all branches and lookup if files with a certain extension such as (.zip or .exe exist)
I tried to write a bash script to achieve this task.
for branch in $(git branch);
do
echo "I am in: $branch"
git ls-files *.exe
done
I would like to see the file path when it is detected.
You are not changing to the branch so you are always checking the last branch you checked out. Try this:
# In the repo's working directory
for branch in $(git branch -a|grep -v remotes|sed 's/\*//g'); do
echo "I am in branch: ${branch}"
git checkout ${branch}
find . -type f -name '*.md'
done
Following is how I solved my problem:
read -p "Extension to lookup [example: .zip]: " extensionType
for branch in $(git branch);
do
if [[ $branch == *"Release"* ]]; then
echo "----------------------------------"
echo ">>Navigating to: $branch"
echo ">>$branch..."
git checkout $branch
git ls-files "*$extensionType"
echo "----------------------------------"
fi
done
I hope this helps.
i've made the following bash script to commit the parent repo after some change in submodule.
it's all about that the script want to cd .. to check the parent repo current branch but the problem is that the cd .. is not affecting the upcoming commands because i guess the subshell
i've tried to run
1- cd ../ && before each command
2- make alias but didn't succeed
3- run exec but the script didn't continued
#!/bin/sh
#
# An example hook script to verify what is about to be committed.
# Called by "git commit" with no arguments. The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
#
# To enable this hook, rename this file to "post-commit".
commit_msg= git log -1 --pretty=%B
if [[ $(git branch | grep \* | cut -d ' ' -f2) == "int1177/next" ]]; then
cd ..
if [[ $(git branch | grep \* | cut -d ' ' -f2) == "B0/next" ]]; then
git add 6_Tests
git commit -m "bs esss"
echo "development branch B0/next has now new commit"
else
echo "development branch isn't B0/next"
fi
else
echo "current branch isn't int1177/next"
fi
Actually, this particular problem is not a bash issue, but rather a Git issue.
Why doesn't "cd" work in a shell script? is valid in general, and is a suitable answer to many other questions. But this particular post-commit hook is trying to chdir out of a submodule into its parent superproject, then make a commit within the parent superproject. That is possible. It may be a bad idea for other reasons—in general it's unwise to have Git commit hooks create commits, even in other repositories1—but in this particular case you're running into the fact that Git finds its directories through environment variables.
In particular, there's an environment variable GIT_DIR that tells Git: The .git directory containing the repository is at this path. When Git runs a Git hook, Git typically sets $GIT_DIR to . or .git. If $GIT_DIR is not set, Git will find the .git directory by means of a directory-tree search, but if $GIT_DIR is set, Git assumes that $GIT_DIR is set correctly.
The solution is to unset GIT_DIR:
unset GIT_DIR
cd ..
The rest of the sub-shell commands will run in the one-step-up directory, and now that $GIT_DIR is no longer set, Git will search the superproject's work-tree for the .git directory for the superproject.
As an aside, this:
$(git branch | grep \* | cut -d ' ' -f2)
is a clumsy way to get the name of the current branch. Use:
git rev-parse --abbrev-ref HEAD
instead, here. (The other option is git symbolic-ref --short HEAD but that fails noisily with a detached HEAD, while you probably want the quiet result to be just the word HEAD, which the rev-parse method will produce.)
1The main danger in this case is that the superproject repository is not necessarily in any shape to handle a commit right now. Edit: or, as discovered in this comment, is not even set up to be a superproject for that submodule, yet, much less to have a submodule-updating commit added.