Skip to content

Instantly share code, notes, and snippets.

@SpiritCroc
Created November 22, 2017 19:37
Show Gist options
  • Save SpiritCroc/f991549265e4b5aff19166bccef917cd to your computer and use it in GitHub Desktop.
Save SpiritCroc/f991549265e4b5aff19166bccef917cd to your computer and use it in GitHub Desktop.
#!/bin/sh
#
# Author:
# SpiritCroc <spiritcroc@gmail.com>
#
workdir="$HOME/.watchrepo"
repodir="$workdir/repo"
rememberfile="$workdir/remember"
tmprememberfile="$workdir/tmpremember"
#configfile="$workdir/watchrepo.conf"
config_sep1=";"
config_sep2="\n"
remote_sep="--"
branch_sep="__"
m_add=0
config_add=""
m_diff=0
CL_CYAN="$(tput setaf 6)"
CL_RED="$(tput setaf 1)"
CL_YELLOW="$(tput setaf 3)"
CL_BOLD="$(tput bold)"
CL_CLR="$(tput sgr0)"
CL_OUT="$CL_CYAN"
CL_BRANCH="$CL_BOLD"
CL_ADD="$CL_YELLOW"
CL_REMOVE="$CL_YELLOW"
CL_SAME="$CL_CYAN"
CL_WRN="$CL_RED$CL_BOLD"
show_usage() {
echo -e "Usage: $0 [(add ADDRESS) | list | (diff BRANCH) | remember]"
echo -e
echo -e "Example add usage:"
echo -e "\t$0 add https://github.com/sonyxperiadev/local_manifests/commits/master"
}
list_diff() {
cd "$repodir"
branches=$(git branch | sed "s;.* ;;")
for branch in $branches; do
branch="$(echo "$branch")"
if [ "$branch" != "master" ]; then
diff_branch "$branch" 0
fi
done
cd "$m_start_dir"
}
# Only call if $PWD = $repodir !
acknowledge_branch() {
remote="$1"
remote_branch="$2"
local_branch="$remote$branch_sep$remote_branch"
# Check if branch already exists
if git rev-parse --verify "$local_branch" > /dev/null 2> /dev/null; then
echo -e $CL_OUT"Updating $local_branch"$CL_CLR
git checkout master
git branch -D "$local_branch"
else
echo -e $CL_OUT"Creating $local_branch"$CL_CLR
fi
git branch "$local_branch" "$remote/$remote_branch"
}
# Only call if $PWD = $repodir !
diff_branch() {
local_branch="$1"
fetch="$2"
remote="$(echo "$local_branch" | sed "s;$branch_sep.*;;")"
remote_branch="$(echo "$local_branch" | sed "s;.*$branch_sep;;")"
if [ "$fetch" = 1 ]; then
git fetch "$remote"
fi
local_rev="$(git rev-parse "$local_branch")"
remote_rev="$(git rev-parse "$remote/$remote_branch")"
pretty_branch="$(echo "$remote/$remote_branch" | sed "s;$remote_sep;/;")"
if [ "$local_rev" = "$remote_rev" ]; then
echo -e $CL_SAME$CL_BRANCH"$pretty_branch"$CL_CLR$CL_SAME" is up-to-date."$CL_CLR
else
removecount="$(git log "$remote_rev".."$local_rev" --format=oneline | wc -l)"
if [ "$removecount" != "0" ]; then
echo -e $CL_REMOVE$CL_BRANCH"$pretty_branch"$CL_CLR$CL_REMOVE" has $removecount removed commits"$CL_CLR
fi
addcount="$(git log "$local_rev".."$remote_rev" --format=oneline | wc -l)"
if [ "$addcount" != 0 ]; then
echo -e $CL_ADD$CL_BRANCH"$pretty_branch"$CL_CLR$CL_ADD" has $addcount new commits"$CL_CLR
fi
if [ "$removecount" != "0" ] || [ "$addcount" != "0" ]; then
# View command to view diff
echo -e $CL_OUT"\t$0 diff $local_branch"$CL_CLR
fi
fi
}
# Only call if $PWD = $repodir !
diff_full_branch() {
local_branch="$1" # TODO check if exists
remote="$(echo "$local_branch" | sed "s;$branch_sep.*;;")"
remote_branch="$(echo "$local_branch" | sed "s;.*$branch_sep;;")"
local_rev="$(git rev-parse "$local_branch")"
remote_rev="$(git rev-parse "$remote/$remote_branch")"
pretty_branch="$(echo "$remote/$remote_branch" | sed "s;$remote_sep;/;")"
if [ "$local_rev" = "$remote_rev" ]; then
echo -e $CL_SAME$CL_BRANCH"$pretty_branch"$CL_CLR$CL_SAME" is up-to-date."$CL_CLR
else
removed="$(git log --reverse "$remote_rev".."$local_rev" --format=oneline | sed 's/ .*//')"
added="$(git log --reverse "$local_rev".."$remote_rev" --format=oneline | sed 's/ .*//')"
if [ ! -z "$removed" ]; then
echo $CL_OUT"Removed commits:"$CL_CLR
while read commit; do
git log "$commit" -n 1 --format=oneline
done <<< "$removed"
fi
if [ ! -z "$added" ]; then
echo $CL_OUT"Added commits:"$CL_CLR
while read commit; do
git log "$commit" -n 1 --format=oneline
done <<< "$added"
fi
if [ ! -z "$removed" ]; then
echo $CL_OUT"Removed commits: $(diff_full_commit_help 0)"$CL_CLR
while read commit; do
diff_full_commit "$commit" 0
done <<< "$removed"
fi
if [ ! -z "$added" ]; then
echo $CL_OUT"Added commits: $(diff_full_commit_help 0)"$CL_CLR
while read commit; do
diff_full_commit "$commit" 0
done <<< "$added"
fi
unset REPLY
read -p "Acknowledge diffs? [y/N]: "
if [ "$REPLY" = "y" ] || [ "$REPLY" = "z" ] || [ "$REPLY" = "j" ] || [ "$REPLY" = "Y" ] || [ "$REPLY" = "Z" ] || [ "$REPLY" = "J" ]; then
acknowledge_branch "$remote" "$remote_branch"
fi
fi
}
diff_full_commit_help() {
remembering=$1
echo -n "Enter s to show full commit, m to show full message, "
if [ "$remembering" = 1 ] ; then
echo -n "f to forget commit,"
else
echo -n "r to remember commit,"
fi
echo " enter to continue"
}
# Only call if $PWD = $repodir !
diff_full_commit() {
commit="$1"
remembering=$2
git log "$commit" --format=oneline -n 1
REPLY="nothing"
while [ ! -z "$REPLY" ]; do
read -p "> " </dev/tty
if [ "$REPLY" = "s" ]; then
git show "$commit"
elif [ "$REPLY" = "m" ]; then
git log "$commit" -n 1
elif [ "$REPLY" = "r" ] && [ "$remembering" != 1 ]; then
echo "Remembering $commit"
echo "$commit" >> "$rememberfile"
unset REPLY
elif [ "$REPLY" = "f" ] && [ "$remembering" = 1 ]; then
echo "Forgetting $commit"
grep -v "$commit" "$rememberfile" > "$tmprememberfile"
unset REPLY
elif [ ! -z "$REPLY" ]; then
echo "Don't know command $REPLY"
fi
done
}
list_remember() {
if [ ! -f "$rememberfile" ]; then
echo $CL_OUT"Nothing remembered!"$CL_CLR
return
fi
cd "$repodir"
echo $CL_OUT"$(diff_full_commit_help 1)"$CL_CLR
while read commit; do
diff_full_commit "$commit" 1
done < "$rememberfile"
cd "$m_start_dir"
if [ -f "$tmprememberfile" ]; then
mv "$tmprememberfile" "$rememberfile"
fi
}
# Only call if $PWD = $repodir !
diff_branches() {
git checkout master
branches=$(git branch | sed "s;.* ;;")
for branch in $branches; do
branch="$(echo "$branch")"
if [ "$branch" != "master" ]; then
diff_branch "$branch" 1
fi
done
}
add_repo_guess() {
full_address="$1"
cd "$repodir"
info="$(echo "$full_address" | sed "s;.*.com/;;")"
IFS='/' read -r -a infos <<< "$info"
owner="${infos[0]}"
repo="${infos[1]}"
branch="${infos[3]}"
address="$(echo "$full_address" | sed "s;$info;;")$owner/$repo"
echo "Address: $address"
echo "Branch: $branch"
unset REPLY
read -p "Is this correct? [Y/n]: "
if [ "$REPLY" = "n" ] || [ "$REPLY" = "N" ]; then
add_repo_prompt $owner $repo $branch $address
else
add_repo $owner $repo $branch $address
fi
}
add_repo_prompt() {
owner="$1"
repo="$2"
branch="$3"
address="$4"
unset REPLY
read -p "Enter repo owner name ($owner): "
if [ ! -z "$REPLY" ]; then
owner="$REPLY"
fi
unset REPLY
read -p "Enter repo name ($repo): "
if [ ! -z "$REPLY" ]; then
repo="$REPLY"
fi
unset REPLY
read -p "Enter remote branch ($branch): "
if [ ! -z "$REPLY" ]; then
branch="$REPLY"
fi
unset REPLY
read -p "Enter git address ($address): "
if [ ! -z "$REPLY" ]; then
address="$REPLY"
fi
add_repo $owner $repo $branch $address
}
add_repo() {
owner="$1"
repo="$2"
branch="$3"
address="$4"
remote="$owner$remote_sep$repo"
existing_address="$(git remote get-url "$remote" 2>/dev/null)"
if [ "$?" = "0" ]; then
if [ "$existing_address" != "$address" ]; then
echo -e $CL_WRN"Remote $remote already exists, but with different address ($existing_address). Exiting..."$CL_CLR
exit 1
else
echo -e $CL_OUT"Remote $remote already exists"$CL_CLR
fi
else
echo -e $CL_OUT"Adding remote $remote..."$CL_CLR
git remote add "$remote" "$address"
fi
echo -e $CL_OUT"Updating remote..."$CL_CLR
git fetch "$remote"
echo -e $CL_OUT"Acknowledging current state..."$CL_CLR
acknowledge_branch "$remote" "$branch"
git checkout master
cd "$m_start_dir"
}
m_start_dir="$PWD"
mkdir -p "$workdir" || exit 1
#touch "$configfile" || exit 1
if [ ! -d "$repodir" ]; then
mkdir -p "$repodir" || exit 1
cd "$repodir"
# Create git repo
git init
# Create empty master branch to checkout
git commit --allow-empty -m "Initial empty"
cd "$m_start_dir"
fi
for arg; do
if [ "$m_add" = 0 ] && [ "$m_diff" = 0 ] && [ "$arg" = "list" ]; then
list_diff
exit 0
elif [ "$m_add" = 0 ] && [ "$m_diff" = 0 ] && [ "$arg" = "remember" ]; then
list_remember
exit 0
elif [ "$m_add" = 0 ] && [ "$m_diff" = 0 ] && [ "$arg" = "add" ]; then
m_add=1
elif [ "$m_add" = 0 ] && [ "$m_diff" = 0 ] && [ "$arg" = "diff" ]; then
m_diff=1
elif [ "$m_add" = 1 ]; then
config_add="$arg"
m_add="c"
elif [ "$m_diff" = 1 ]; then
m_diff="$arg"
elif [ "$arg" = "-h" ] || [ "$arg" = "--help" ]; then
show_usage
exit 0
else
show_usage
exit 1
fi
done
if [ "$m_add" = 0 ] && [ "$m_diff" = 0 ]; then
cd "$repodir"
diff_branches
cd "$m_start_dir"
elif [ "$m_add" = "c" ]; then
add_repo_guess "$config_add"
elif [ "$m_diff" != 0 ] && [ "$m_diff" != 1 ]; then
cd "$repodir"
diff_full_branch "$m_diff"
cd "$m_start_dir"
else
show_usage
exit 1
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment