7 Commits

Author SHA1 Message Date
Ivan Malopinsky f6898b7056 1.4.0 2016-01-21 23:16:00 -05:00
Ivan Malopinsky ae2fa19df6 add [git-fresh] to logs 2016-01-20 00:00:00 -05:00
Ivan Malopinsky 2798299f11 add comments 2016-01-19 00:00:00 -05:00
Ivan Malopinsky f3fb0fc3f9 check that remote branch exists before rebasing against it 2016-01-17 00:00:00 -05:00
Ivan Malopinsky c7d9abc59e fix HEAD ref check 2016-01-16 00:00:00 -05:00
Ivan Malopinsky 80593359ca fix git directory check 2016-01-15 00:00:00 -05:00
Ivan Malopinsky 337c9679bc homebrew now supported 2015-12-08 20:13:26 -05:00
3 changed files with 42 additions and 13 deletions
+1
View File
@@ -33,6 +33,7 @@ root: root branch, master by default
### Package
* [Homebrew](http://brew.sh/): `brew install git-fresh`
* [bpkg](http://www.bpkg.io/): `bpkg install imsky/git-fresh`
## License
+40 -12
View File
@@ -28,16 +28,19 @@ EOD
exit 0
}
error () {
echo "[git-fresh] error on line $1"
exit 1
say () {
echo "[git-fresh] $@" 1>&2
}
die () {
echo "[git-fresh] $@" 1>&2
say $@
exit 1
}
error () {
die "error on line $1"
}
trap 'error $LINENO' ERR
while getopts ":fmrslF" opt; do
@@ -69,23 +72,46 @@ done
shift $((OPTIND-1))
[[ ! -e .git ]] && die "Not a git repository"
[[ $(ls -l .git/refs/heads | wc -l) -eq "1" ]] && die "No HEAD ref available"
# Are we inside a git repository?
INSIDE_GIT_REPO=$(git rev-parse --is-inside-work-tree 2> /dev/null)
if [[ -z "$INSIDE_GIT_REPO" ]]; then
die "Not a git repository"
fi
# Are we in a non-empty git repository?
TOP_LEVEL_DIRECTORY=$(git rev-parse --show-toplevel)
[[ $(ls -l "$TOP_LEVEL_DIRECTORY/.git/refs/heads" | wc -l) -eq "1" ]] && die "No HEAD ref available"
CURRENT=$(git rev-parse --abbrev-ref HEAD)
REMOTE=${1:-origin}
ROOT=${2:-master}
# Update remotes and prune stale remotes
git remote update
git remote prune $REMOTE
STASH_STAMP=git-fresh-$(date +%s)
# Stash changed files
if ! git diff-files --quiet; then
git stash save $STASH_STAMP
fi
git rebase $REMOTE $CURRENT
# If the current branch exists on the remote, rebase against it
REMOTE_CURRENT=$(git ls-remote $REMOTE --heads 2> /dev/null | grep "heads/$CURRENT$" | cat)
if [[ ! -z "$REMOTE_CURRENT" ]]; then
git rebase $REMOTE $CURRENT
fi
# Switch to root branch (master)
git checkout $ROOT > /dev/null 2>&1
@@ -96,6 +122,8 @@ else
git rebase -q $REMOTE/$ROOT
fi
# Compute stale branches
SMART_STALE=$(git branch -a --merged | tr -d "\* " | grep -Ev ">|$ROOT" | cat)
LOCAL_STALE=$(grep -Ev "^remotes/" <<< "$SMART_STALE" | cat)
@@ -110,7 +138,7 @@ if [[ ! -z "${SMART_STALE// }" ]]; then
if [[ "$FORCE_DELETE_STALE" = true ]]; then
echo -n $LOCAL_STALE | xargs git branch -d 2> /dev/null
else
echo "Local stale branches found:" $(echo -n $LOCAL_STALE | tr "\n" " ")
say "Local stale branches found:" $(echo -n $LOCAL_STALE | tr "\n" " ")
fi
fi
@@ -121,12 +149,12 @@ if [[ ! -z "${SMART_STALE// }" ]]; then
echo -n $REMOTE_STALE | xargs git push $REMOTE --delete
fi
else
echo "Remote stale branches found:" $(echo -n $REMOTE_STALE | tr "\n" " ")
say "Remote stale branches found:" $(echo -n $REMOTE_STALE | tr "\n" " ")
fi
fi
if [[ "$FORCE_DELETE_STALE" != true && "$STALE_BRANCHES" = true ]]; then
echo "Delete stale branches with: git fresh -f"
say "Delete stale branches with: git fresh -f"
fi
fi
@@ -134,7 +162,7 @@ if [[ ! -z $(git rev-parse --verify --quiet "$CURRENT") ]]; then
git checkout $CURRENT 2> /dev/null
if [ "$REBASE" = true ] && [ "$MERGE" = true ]; then
echo "Rebase and merge enabled, skipping both"
say "Rebase and merge enabled, skipping both"
else
if [[ "$REBASE" = true ]]; then
git rebase $REMOTE/$ROOT
@@ -152,7 +180,7 @@ if [[ ! -z $(git stash list | grep $STASH_STAMP | cat) ]]; then
if [[ "$APPLY_STASH" = true ]]; then
git stash pop
else
echo "Stashed changes present, apply with: git stash pop"
say "Stashed changes present, apply with: git stash pop"
fi
fi
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "git-fresh",
"version": "1.3.0",
"version": "1.4.0",
"description": "Utility to keep Git repositories fresh",
"global": true,
"repo": "imsky/git-fresh",