From 67ad3061a258ee0ad7e0cbd6335d5d3b7fa39e8a Mon Sep 17 00:00:00 2001 From: Guillaume Berche Date: Fri, 19 Jan 2024 10:00:43 +0100 Subject: [PATCH 1/2] Add new -R option triggering a git pull --rebase Fix #88 --- gitwatch.sh | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/gitwatch.sh b/gitwatch.sh index a8eb89a..5c90aa2 100755 --- a/gitwatch.sh +++ b/gitwatch.sh @@ -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 If given and non-empty, a 'git push' to the given " echo " is done after every commit; default is empty, i.e. no push" + echo " -R If given along with -r, a 'git pull --rebase ' is done before any push" echo " -b The branch which should be pushed automatically;" echo " - if not given, the push command used is 'git push '," 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" From 500305f591a30a575b04b272684f7b457af141ff Mon Sep 17 00:00:00 2001 From: Dave Musicant / thinkpad Date: Wed, 13 Mar 2024 16:54:47 -0500 Subject: [PATCH 2/2] bats test for new rebasing --- tests/pull-rebase.bats | 71 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 tests/pull-rebase.bats diff --git a/tests/pull-rebase.bats b/tests/pull-rebase.bats new file mode 100644 index 0000000..f08217a --- /dev/null +++ b/tests/pull-rebase.bats @@ -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 +} +