Compare commits

...

4 Commits

Author SHA1 Message Date
Dave Musicant / thinkpad e8fb8521b6 Merge branch 'pull-before-push' 2 months ago
Guillaume Berche 0397e468d8
Add new -R option triggering a git pull --rebase (#121)
Fix #88
2 months ago
Dave Musicant / thinkpad 500305f591 bats test for new rebasing 2 months ago
Guillaume Berche 67ad3061a2 Add new -R option triggering a git pull --rebase
Fix #88
4 months ago

@ -36,6 +36,7 @@
#
REMOTE=""
PULL_BEFORE_PUSH=0
BRANCH=""
SLEEP_TIME=2
DATE_FMT="+%Y-%m-%d %H:%M:%S"
@ -65,6 +66,7 @@ shelp() {
echo ' "+%Y-%m-%d %H:%M:%S"'
echo " -r <remote> If given and non-empty, a 'git push' to the given <remote>"
echo " is done after every commit; default is empty, i.e. no push"
echo " -R If given along with -r, a 'git pull --rebase <remote>' is done before any push"
echo " -b <branch> The branch which should be pushed automatically;"
echo " - if not given, the push command used is 'git push <remote>',"
echo " thus doing a default push (see git man pages for details)"
@ -133,7 +135,7 @@ is_merging () {
###############################################################################
while getopts b:d:h:g:L:l:m:p:r:s:e:x:M option; do # Process command line options
while getopts b:d:h:g:L:l:m:p:r:s:e:x:MR option; do # Process command line options
case "${option}" in
b) BRANCH=${OPTARG} ;;
d) DATE_FMT=${OPTARG} ;;
@ -150,6 +152,7 @@ while getopts b:d:h:g:L:l:m:p:r:s:e:x:M option; do # Process command line option
m) COMMITMSG=${OPTARG} ;;
M) SKIP_IF_MERGING=1 ;;
p | r) REMOTE=${OPTARG} ;;
R) PULL_BEFORE_PUSH=1 ;;
s) SLEEP_TIME=${OPTARG} ;;
x) EXCLUDE_PATTERN=${OPTARG} ;;
e) EVENTS=${OPTARG} ;;
@ -293,8 +296,13 @@ if [ -n "$REMOTE" ]; then # are we pushing to a remote?
PUSH_CMD="$GIT push $REMOTE $BRANCH"
fi
fi
if [[ $PULL_BEFORE_PUSH -eq 1 ]]; then
PULL_CMD="$GIT pull --rebase $REMOTE" # Branch not set, pull to remote without a branch
fi
else
PUSH_CMD="" # if not remote is selected, make sure push command is empty
PULL_CMD="" # if not remote is selected, make sure pull command is empty
fi
# A function to reduce git diff output to the actual changed content, and insert file line numbers.
@ -389,6 +397,11 @@ eval "$INW" "${INW_ARGS[@]}" | while read -r line; do
# shellcheck disable=SC2086
$GIT commit $GIT_COMMIT_ARGS -m"$FORMATTED_COMMITMSG" # construct commit message and commit
if [ -n "$PULL_CMD" ]; then
echo "Pull command is $PULL_CMD"
eval "$PULL_CMD"
fi
if [ -n "$PUSH_CMD" ]; then
echo "Push command is $PUSH_CMD"
eval "$PUSH_CMD"

@ -0,0 +1,71 @@
#!/usr/bin/env bats
# This is a testscript using the bats testing framework:
# https://github.com/sstephenson/bats
# To run it, at a command prompt:
# bats testscript.bats
load startup-shutdown
function pulling_and_rebasing_correctly { #@test
# Create a file, verify that it hasn't been added yet,
# then commit and push
cd remote
# Start up gitwatch and see if commit and push happen automatically
# after waiting two seconds
${BATS_TEST_DIRNAME}/../gitwatch.sh -r origin -R "$testdir/local/remote" 3>- &
GITWATCH_PID=$!
# Keeps kill message from printing to screen
disown
# According to inotify documentation, a race condition results if you write
# to directory too soon after it has been created; hence, a short wait.
sleep 1
echo "line1" >> file1.txt
# Wait a bit for inotify to figure out the file has changed, and do its add,
# commit, and push.
sleep $WAITTIME
# Verify that push happened
currentcommit=$(git rev-parse master)
remotecommit=$(git rev-parse origin/master)
[ "$currentcommit" = "$remotecommit" ]
# Create a second local
cd ../..
mkdir local2
cd local2
git clone -q ../remote
cd remote
# Add a file to new repo
sleep 1
echo "line2" >> file2.txt
git add file2.txt
git commit -am "file 2 added"
git push
# Change back to original repo, make a third change, then verify that
# second one got here
cd ../../local/remote
sleep 1
echo "line3" >> file3.txt
# Verify that push happened
currentcommit=$(git rev-parse master)
remotecommit=$(git rev-parse origin/master)
[ "$currentcommit" = "$remotecommit" ]
# Verify that new file is here
sleep $WAITTIME
[ -f file2.txt ]
# Remove testing directories
cd /tmp
rm -rf $testdir
}
Loading…
Cancel
Save