I want to refine a remote update script so it doesn't attempt to re-build the target if no changes were pulled by bzr pull.
Is there any easier way to do it rather than parsing the output from bzr pull?
You can use the bzr missing command:
if ! bzr missing >/dev/null; then
echo some revisions are missing
else
echo we are in sync
fi
You can easily create dummy branches to test this:
mkdir dummy
cd dummy
bzr init trunk
cd trunk
date > date.txt
bzr add
bzr commit -m first
date >> date.txt
bzr commit -m second
bzr branch . -r1 ../behind
bzr branch . ../copy
cd ../copy
! bzr missing >/dev/null && echo behind || echo up to date
cd ../behind
! bzr missing >/dev/null && echo behind || echo up to date
You could also look at the output of bzr status -r branch:PATH_TO_REMOTE_BRANCH to see if there are changes, before pulling.
Related
Error message: "fatal: your current branch 'master' does not have any commits yet"
After Making a file with this code executable
#!/usr/bin/env bash
cd "$(dirname "$(readlink -f "$BASH_SOURCE")")/.."
{
cat <<- 'EOH'
EOH
echo
git log --format='%aN <%aE>' | LC_ALL=C.UTF-8 sort -uf
} > AUTHORS
The problem is that you didn't add anything, and possibly didn't even have a change to commit, so no commit was done. If you really want that first commit without any changes, you can do this:
git commit --allow-empty -m "first commit"
I have this for loop
for repository in ./*/; do
echo $repository && cd $repository && git checkout -b prod && cd - >/dev/null;
done
But if branch prod already exists it prints a message and exit the loop.
How can ignore this error and just go to the next directory ?
Thanks
So the problem is that git checkout -b prod returns failure to the shell if the branch already exists. Since it's connected to the next command (cd -) with the conditional operator &&, that next command only runs if git succeeds. So when git fails, the cd doesn't run, and your shell is left in the wrong directory to continue its loop.
In general, when you want your code to continue even if a command fails, separate the commands with ; or newlines instead of &&.
But a better solution in this case is to just do the cd in a subshell so that it doesn't affect the outer loop's working directory and you don't have to cd - at all:
for repository in ./*/; do
echo "$repository" && (
cd "$repository" && git checkout -b prod
)
done
That will work fine even if the branch creation fails. It will still print out the error message; if you don't want to see those, add the redirect:
for repository in ./*/; do
echo "$repository" && (
cd "$repository" && git checkout -b prod
) 2>/dev/null
done
I've also quoted the expansion of $repository in the commands, which you should almost always do in shell scripts. With the unquoted version, you would get an error if any of the repo directory names had spaces in them, for instance.
Also, that "no side effects in a subshell" thing is great for doing part of your work in a different directory, but it applies more widely. If you had a more complicated loop that set any shell variables or anything while it was in the subdir, those would also be lost. Just something to keep in mind.
Like this
home=$PWD
for repository in "$home"/*/; do
basename "$repository" # to 'echo' $repository
cd "$repository" && git checkout -b prod
done
Better use pushd and popd and additionally it is saver to use find:
while read -r repository; do
pushd "${repository}"
if git checkout -b prod; then
echo "git checkout success"
else
echo "git chechout error"
fi
popd
done < <( find . -mindepth 1 -maxdepth 1 -type d -print )
I am implementing a shell script that will be backing up the database and then push the sql file to Github, I am using centos server the project is located at /opt/server-scripts/backup.sh. How do I automate this?
Here is my implementation so far:
#!/bin/bash/
var=$CURRENT_DATE=date +"%D %T"
docker exec 3856a8e52031 /usr/bin/mysqldump -u root --password=cvxxx django_mysql_docker > backup.sql
# Git Push
GIT=$(which git)
REPO_DIR=/opt/server-scripts/
cd ${REPO_DIR} || exit
${GIT} add --all .
${GIT} commit -m "backup:" + "'$CURRENT_DATE'"
${GIT} https://pmutua:xxxxx#github.com/pmutua/sqlbackup.git master
You can check if a command/executable is installed or it is with in your PATH, one way using type
if type -P git >/dev/null; then
echo 'git is installed.'
fi
If you want to negate the result add the !
if ! type -P git >/dev/null; then
echo 'git is not installed.'
fi
To add that to your script.
#!/usr/bin/env bash
docker exec 3856a8e52031 /usr/bin/mysqldump -u root --password=cvxxx django_mysql_docker > backup.sql
if ! type -P git >/dev/null; then ##: Check if git is not installed
echo 'git is not installed.' >&2 ##: print an error message to stderr
exit 1 ##: Exit with an error
fi
# Git Push
CURRENT_DATE=$(date +"%D %T") ##: Assign the output of date in a variable
REPO_DIR=/opt/server-scripts/
cd "${REPO_DIR}" || exit
git add --all .
git commit -m "backup: '$CURRENT_DATE'"
git push https://pmutua:xxxxx#github.com/pmutua/sqlbackup.git master
You can add the date directly git commit -m "backup: '$(date +"%D %T")'"
that way the date will be same with the output of git log
Other ways to check if a command exists is via command and hash see Howto check if a program exists in my PATH
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 been attempting to use the following script to commit a wordpress plugin to the WP svn repo from my public git repository and keep getting error:
svn: E155013: Can't add '/path/to/file' to a parent directory scheduled for deletion
Whenever there is a new file, not previously known to svn, when adding files, resulting in either an aborted commit, or worse, a broken commit where it commits with missing files.
#! /bin/bash
# A modification of Dean Clatworthy's deploy script as found here: https://github.com/deanc/wordpress-plugin-git-svn
# The difference is that this script lives in the plugin's git repo & doesn't require an existing SVN repo.
# main config
PLUGINSLUG="my-plugin-slug"
CURRENTDIR=`pwd`
MAINFILE="my-plugin.php" # this should be the name of your main php file in the wordpress plugin
# git config
GITPATH="$CURRENTDIR/" # this file should be in the base of your git repository
# svn config
SVNPATH="/tmp/$PLUGINSLUG" # path to a temp SVN repo. No trailing slash required and don't add trunk.
SVNURL="http://plugins.svn.wordpress.org/my-plugin" # Remote SVN repo on wordpress.org, with no trailing slash
SVNUSER="wp_username" # your svn username
# Let's begin...
echo ".........................................."
echo
echo "Preparing to deploy wordpress plugin"
echo
echo ".........................................."
echo
# Check if subversion is installed before getting all worked up
#if [ $(dpkg-query -W -f='${Status}' subversion 2>/dev/null | grep -c "ok installed") != "1" ]
#then
# echo "You'll need to install subversion before proceeding. Exiting....";
# exit 1;
#fi
# Check version in readme.txt is the same as plugin file after translating both to unix line breaks to work around grep's failure to identify mac line breaks
NEWVERSION1=`grep "^Stable tag:" $GITPATH/readme.txt | awk -F' ' '{print $NF}'`
echo "readme.txt version: $NEWVERSION1"
NEWVERSION2=`grep "^ \* Version:" $GITPATH/$MAINFILE | awk -F' ' '{print $NF}'`
echo "$MAINFILE version: $NEWVERSION2"
if [ "$NEWVERSION1" != "$NEWVERSION2" ]; then echo "Version in readme.txt & $MAINFILE don't match. Exiting...."; exit 1; fi
echo "Versions match in readme.txt and $MAINFILE. Let's proceed..."
if git show-ref --tags --quiet --verify -- "refs/tags/$NEWVERSION1"
then
echo "Version $NEWVERSION1 already exists as git tag. Exiting....";
exit 1;
else
echo "Git version does not exist. Let's proceed..."
fi
cd $GITPATH
echo -e "Enter a commit message for this new version: \c"
read COMMITMSG
git commit -am "$COMMITMSG"
echo "Tagging new version in git"
git tag -a "$NEWVERSION1" -m "Tagging version $NEWVERSION1"
echo "Pushing latest commit to origin, with tags"
git push origin master
git push origin master --tags
echo
echo "Creating local copy of SVN repo ..."
svn co $SVNURL $SVNPATH
echo "Clearing svn repo so we can overwrite it"
svn rm $SVNPATH/trunk/*
echo "Exporting the HEAD of master from git to the trunk of SVN"
git checkout-index -a -f --prefix=$SVNPATH/trunk/
echo "Ignoring github specific files and deployment script"
svn propset svn:ignore "deploy.sh
README.md
bower_components
node_modules
.DS_Store
.gitmodules
advanced
assets
gulpfile.js
bower.json
package.json
.git
.gitignore" "$SVNPATH/trunk/"
echo "Changing directory to SVN and committing to trunk"
cd $SVNPATH/trunk/
# Add all new files that are not set to be ignored
echo "Doing the file adding"
# THIS IS WHERE ERROR OCCURS
svn status | grep -v "^.[ \t]*\..*" | grep "^?" | awk '{print $2}' | xargs svn add
echo "Committing"
svn commit --username=$SVNUSER -m "$COMMITMSG"
echo "Creating new SVN tag & committing it"
cd $SVNPATH
svn copy trunk/ tags/$NEWVERSION1/
cd $SVNPATH/tags/$NEWVERSION1
svn commit --username=$SVNUSER -m "Tagging version $NEWVERSION1"
echo "Removing temporary directory $SVNPATH"
rm -fr $SVNPATH/
echo "*** FIN ***"
Could anyone elaborate on what is causing this error and possibly recommend either a different or adapted approach?
Dean's version doesn't contain brainless svn rm $SVNPATH/trunk/*
rm $SVNPATH/trunk/* or checkout with --depth+commit with --set-depth