Compare commits
27 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f6898b7056 | |||
| ae2fa19df6 | |||
| 2798299f11 | |||
| f3fb0fc3f9 | |||
| c7d9abc59e | |||
| 80593359ca | |||
| 337c9679bc | |||
| f18a2dc6ba | |||
| 97c7dfb4b5 | |||
| 46a74c1b08 | |||
| b9336549c1 | |||
| 30ee8139f7 | |||
| 4482885845 | |||
| 615988dc01 | |||
| c9b1f5c2e0 | |||
| 85ee0683eb | |||
| c48f1d592a | |||
| 15e62530d9 | |||
| 8e81d7276e | |||
| b6d35128c4 | |||
| 6dc3f6870d | |||
| 28f347b89d | |||
| a94af51b11 | |||
| b11805e886 | |||
| 5327cb53c9 | |||
| c5db0306de | |||
| a16bc0b0c8 |
@@ -2,23 +2,23 @@
|
||||
|
||||
Keep your repo fresh with one command.
|
||||
|
||||
* Stashes your unstaged changes
|
||||
* Updates local master to match remote, prunes stale branches
|
||||
* Deletes stale local and remote branches with `-f` flag
|
||||
* Merges remote master into current branch with `-m` flag
|
||||
* Rebases current branch against remote master with `-r` flag
|
||||
* Restores your stashed changes with `-s` flag
|
||||
* Wipes the slate clean with `-F` flag
|
||||
|
||||
## Usage
|
||||
|
||||
```
|
||||
Usage: git fresh [-fmrsF] [remote] [root]
|
||||
Usage: git fresh [-fmrF] [-sl] [remote] [root]
|
||||
By default, git-fresh will:
|
||||
- rebase against remote current branch
|
||||
- stash changes
|
||||
- prune remote branches
|
||||
|
||||
-f: Delete stale local and remote branches
|
||||
-m: Merge remote root into current branch
|
||||
-r: Rebase current branch against remote root
|
||||
-s: Apply stashed changes after run
|
||||
-F: Reset local root to remote root, wipe workspace
|
||||
|
||||
-s: Apply stashed changes after run
|
||||
-l: Only delete local stale branches
|
||||
|
||||
remote: remote name, origin by default
|
||||
root: root branch, master by default
|
||||
```
|
||||
@@ -29,10 +29,11 @@ root: root branch, master by default
|
||||
|
||||
1. Clone or download
|
||||
2. `cd git-fresh`
|
||||
3. `sudo make install`
|
||||
3. `sudo ./install`
|
||||
|
||||
### Package
|
||||
|
||||
* [Homebrew](http://brew.sh/): `brew install git-fresh`
|
||||
* [bpkg](http://www.bpkg.io/): `bpkg install imsky/git-fresh`
|
||||
|
||||
## License
|
||||
|
||||
@@ -1,24 +1,49 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
# git-fresh
|
||||
# https://github.com/imsky/git-fresh
|
||||
# By Ivan Malopinsky - http://imsky.co
|
||||
# MIT License
|
||||
|
||||
usage () {
|
||||
echo "Usage: git fresh [-fmrsF] [remote] [root]"
|
||||
echo "-f: Delete stale local and remote branches"
|
||||
echo "-m: Merge remote root into current branch"
|
||||
echo "-r: Rebase current branch against remote root"
|
||||
echo "-s: Apply stashed changes after run"
|
||||
echo "-F: Reset local root to remote root, wipe workspace"
|
||||
echo "remote: remote name, origin by default"
|
||||
echo "root: root branch, master by default"
|
||||
exit 1;
|
||||
cat << EOD
|
||||
Usage: git fresh [-fmrF] [-sl] [remote] [root]
|
||||
By default, git-fresh will:
|
||||
- rebase against remote current branch
|
||||
- stash changes
|
||||
- prune remote branches
|
||||
|
||||
-f: Delete stale local and remote branches
|
||||
-m: Merge remote root into current branch
|
||||
-r: Rebase current branch against remote root
|
||||
-F: Reset local root to remote root, wipe workspace
|
||||
|
||||
-s: Apply stashed changes after run
|
||||
-l: Only delete local stale branches
|
||||
|
||||
remote: remote name, origin by default
|
||||
root: root branch, master by default
|
||||
EOD
|
||||
|
||||
exit 0
|
||||
}
|
||||
|
||||
say () {
|
||||
echo "[git-fresh] $@" 1>&2
|
||||
}
|
||||
|
||||
die () {
|
||||
say $@
|
||||
exit 1
|
||||
}
|
||||
|
||||
error () {
|
||||
echo -n "[git-fresh] error on line $1"
|
||||
die "error on line $1"
|
||||
}
|
||||
|
||||
trap 'error $LINENO' ERR
|
||||
|
||||
while getopts "fmrsF" opt; do
|
||||
while getopts ":fmrslF" opt; do
|
||||
case $opt in
|
||||
f)
|
||||
FORCE_DELETE_STALE=true
|
||||
@@ -32,30 +57,62 @@ while getopts "fmrsF" opt; do
|
||||
s)
|
||||
APPLY_STASH=true
|
||||
;;
|
||||
l)
|
||||
DELETE_ONLY_LOCAL=true
|
||||
;;
|
||||
F)
|
||||
FORCE_LOCAL_RESET=true
|
||||
;;
|
||||
*)
|
||||
usage
|
||||
break
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
shift $((OPTIND-1))
|
||||
|
||||
# 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
|
||||
|
||||
# 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
|
||||
|
||||
if [[ "$FORCE_LOCAL_RESET" = true ]]; then
|
||||
@@ -65,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)
|
||||
@@ -79,21 +138,23 @@ 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
|
||||
|
||||
if [[ ! -z "${REMOTE_STALE// }" ]]; then
|
||||
STALE_BRANCHES=true
|
||||
if [[ "$FORCE_DELETE_STALE" = true ]]; then
|
||||
echo -n $REMOTE_STALE | xargs git push $REMOTE --delete
|
||||
if [[ "$DELETE_ONLY_LOCAL" != true ]]; 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"
|
||||
if [[ "$FORCE_DELETE_STALE" != true && "$STALE_BRANCHES" = true ]]; then
|
||||
say "Delete stale branches with: git fresh -f"
|
||||
fi
|
||||
fi
|
||||
|
||||
@@ -101,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
|
||||
@@ -119,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
|
||||
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
#!/usr/bin/env bash
|
||||
INSTALL_DIR=${1:-/usr/local/bin}
|
||||
|
||||
mkdir -p $INSTALL_DIR
|
||||
cp git-fresh $INSTALL_DIR && ([ -e $INSTALL_DIR/git-fresh ] && echo git-fresh installed in $INSTALL_DIR)
|
||||
+2
-2
@@ -1,8 +1,8 @@
|
||||
{
|
||||
"name": "git-fresh",
|
||||
"version": "1.0.0",
|
||||
"version": "1.4.0",
|
||||
"description": "Utility to keep Git repositories fresh",
|
||||
"global": true,
|
||||
"repo": "imsky/git-fresh",
|
||||
"install": "sudo make install"
|
||||
"install": "sudo ./install"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user