]> git.nbdom.net Git - nb.git/commitdiff
etc/profile.d/git.sh
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Fri, 12 Jan 2018 05:11:36 +0000 (05:11 +0000)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Fri, 12 Jan 2018 05:11:36 +0000 (05:11 +0000)
etc/profile.d/git.sh

index 8e6bd782cfa6455c44604e33ace0d0e138a263d9..a289f78c209127e16de8aa68729c085f506ff41f 100644 (file)
@@ -11,20 +11,20 @@ alias git_ls_tree="git ls-tree --full-tree -r HEAD"
 alias git_ls_files="git ls-files"
 
 git_new_branch() {
-  git checkout -b "$1"
-  git push -u origin "$1"
+       git checkout -b "$1"
+       git push -u origin "$1"
 }
 
 git_pull_reset() {
-  local branch=$(git branch --list|awk '/\*/{print $2; exit}')
-  [ -n "$1" ] && branch="$1"
-  [ -z "$branch" ] && return
-  git fetch --all 
-  git reset --hard "$branch"
+       local branch=$(git branch --list|awk '/\*/{print $2; exit}')
+       [ -n "$1" ] && branch="$1"
+       [ -z "$branch" ] && return
+       git fetch --all 
+       git reset --hard "$branch"
 }
 
 git_log_files() {
-  git log --pretty=format: --name-status
+       git log --pretty=format: --name-status
 }
 
 git_history_clean() {
@@ -32,178 +32,185 @@ git_history_clean() {
 }
 
 git_cache_rm() {
-  echo "See: http://dalibornasevic.com/posts/2-permanently-remove-files-and-folders-from-a-git-repository"
-  echo git filter-branch --index-filter "git rm --cached --ignore-unmatch $@" --force --prune-empty -- --all
+       echo "See: http://dalibornasevic.com/posts/2-permanently-remove-files-and-folders-from-a-git-repository"
+       echo git filter-branch --index-filter "git rm --cached --ignore-unmatch $@" --force --prune-empty -- --all
 }
 
 gitcd() {
 
-  shell_help_noarg "Change directory to run git commands\nUsage: $FUNCNAME FILE|DIR GIT_ARGUMENTS" "$@" && return 1
-  local i d dir args
-  local pwd; pwd=$PWD
-  #for i in $@; do
+       shell_help_noarg "Change directory to run git commands\nUsage: $FUNCNAME FILE|DIR GIT_ARGUMENTS" "$@" && return 1
+       local i d dir args
+       local pwd; pwd=$PWD
+       #for i in $@; do
 
-  while [ $# -gt 0 ]; do
+       while [ $# -gt 0 ]; do
 
-    i="$1"
+               i="$1"
 
-    # Replace file with realpath
-    if [ -e "$i" ]; then
-      i=$(realpath "$i")
+               # Replace file with realpath
+               if [ -e "$i" ]; then
+                       i=$(realpath "$i")
 
-      # if dir stay there
-      #[ -d "$i" ] && i="$i/.git"
-      [ -d "$i" ] && i="$i/README.md"
+                       # if dir stay there
+                       #[ -d "$i" ] && i="$i/.git"
+                       [ -d "$i" ] && i="$i/README.md"
 
-      d=$(dirname "$i")
+                       d=$(dirname "$i")
 
-      dir="$d"
-    fi
+                       dir="$d"
+               fi
 
-    shell_push_arg "$i" args
-    shift
+               shell_push_arg "$i" args
+               shift
 
-  done
+       done
 
-  [ -z "$dir" ] && echo "Can't find any file in $@" && return 1
+       [ -z "$dir" ] && echo "Can't find any file in $@" && return 1
 
-  cd "$dir" || return
-  #echo PWD=$(pwd)
+       cd "$dir" || return
+       #echo PWD=$(pwd)
 
-  eval git ${args}
+       eval git ${args}
 
-  cd "$pwd"
+       cd "$pwd"
 
 } # < gitcd()
 
 git_commit_push() {
-  git commit -a -m "$*"; git push
+       git commit -a -m "$*"; git push
 }
 
 git_commit_push_add() {
-  git add . && git_commit_push "$*"
+       git add . && git_commit_push "$*"
 }
 
 git_create() {
-  shell_help "Usage: $FUNCNAME create git project from current dir" "$@" && return 1
+       shell_help "Usage: $FUNCNAME create git project from current dir" "$@" && return 1
 
-  local dir="$(basename $(pwd))"
-  local repo=$(echo "$GIT_REPO" | perl -pe 's,^\w+://,,; s,/.*$,,;')
-  [ -n "$1" ] && repo="$1" && shift
+       local dir="$(basename $(pwd))"
+       local repo=$(echo "$GIT_REPO" | perl -pe 's,^\w+://,,; s,/.*$,,;')
+       [ -n "$1" ] && repo="$1" && shift
 
-  #
-  # Check, create repo on server
-  #
+       #
+       # Check, create repo on server
+       #
 
-  echo ">git init --bare ${dir}.git"
-  local user=${repo%@*}
-  ssh root@${repo#*@} "cd ~git || exit; test -e ${dir}.git && echo "${dir}.git already exists on $repo" 1>&2 && exit 1; git init --bare ${dir}.git && chown -R $user.$user ${dir}.git" || return
+       echo ">git init --bare ${dir}.git"
+       local user=${repo%@*}
+       ssh root@${repo#*@} "cd ~git || exit; test -e ${dir}.git && echo "${dir}.git already exists on $repo" 1>&2 && exit 1; git init --bare ${dir}.git && chown -R $user.$user ${dir}.git" || return
 
-  echo ">git init"
-  git init
+       echo ">git init"
+       git init
 
-  # Create files
-  [ -e README.md ] || printf "# ${dir}\n\n## Synopsis\n\nDESCRIPTION\n" > README.md
+       # Create files
+       [ -e README.md ] || printf "# ${dir}\n\n## Synopsis\n\nDESCRIPTION\n" > README.md
 
-  echo ">git add README.md"
-  git add README.md
+       echo ">git add README.md"
+       git add README.md
 
-  echo ">git commit -m \"Created by $FUNCNAME\""
-  git commit -m "Created by $FUNCNAME"
+       echo ">git commit -m \"Created by $FUNCNAME\""
+       git commit -m "Created by $FUNCNAME"
 
-  echo ">git remote add origin $repo:${dir}.git"
-  git remote add origin $repo:${dir}.git
+       echo ">git remote add origin $repo:${dir}.git"
+       git remote add origin $repo:${dir}.git
 
-  echo ">git push --set-upstream origin master"
-  git push --set-upstream origin master
+       echo ">git push --set-upstream origin master"
+       git push --set-upstream origin master
 }
 
 git_clone() {
-  local repo=$GIT_REPO
-  case "$repo" in
-    *"//"*)  ;;
-    *) repo="$repo:"
-  esac
-
-  local git=${1:?Usage: git_clone PROJECT_PATH}
-  shift
-  git clone ${repo}${git%.git}.git $@
+       local repo=$GIT_REPO
+       case "$repo" in
+               *"//"*)  ;;
+               *) repo="$repo:"
+       esac
+
+       local git=${1:?Usage: git_clone PROJECT_PATH}
+       shift
+       git clone ${repo}${git%.git}.git $@
 }
 
 git_list() {
-  local repo=$(echo "$GIT_REPO" | perl -pe 's,^\w+://,,; s,/.*$,,;')
-  ssh $repo list | perl -ne '/(\S+).git/ and print "$1\n"'
+       local repo=$(echo "$GIT_REPO" | perl -pe 's,^\w+://,,; s,/.*$,,;')
+       ssh $repo list | perl -ne '/(\S+).git/ and print "$1\n"'
 }
 
 _git_conn_check=""
 git_conn_check() {
-  #local dest=$(git config --local --get remote.origin.url | sed -e 's/:.*$//' -e 's/^.*@//')
-  if [ "$1" = "-reset" ]; then
-    _git_conn_check=""
-    return
-  fi
-
-  local dest=$(git config --local --get remote.origin.url)
-  local port=''
-
-  if [ -z "$dest" ]; then
-    logger -t $NAME -s "Can't get url from \`git config --het remote.origin.url\` in `pwd`"
-    return 1
-  fi
-
-  echo "$dest" | grep -qm1 '^[a-z]\+:' || dest="ssh://$dest"
-  dest=$(url2nc "$dest")
-
-  local ok
-  case "$_git_conn_check" in
-    *" $dest=0 "*) ok=0 ;;
-    *" $dest=1 "*) ok=1 ;;
-    *)
-      ok=0
-      case "$dest" in
-        *"@"*) timeout 5 ssh -o BatchMode=yes -o ConnectTimeout=5 $dest true && ok=1 ;;
-        *) nc -z -w 5 $dest >/dev/null 2>&1 && ok=1 ;;
-      esac
-      [ -z "$_git_conn_check" ] && _git_conn_check=" "
-      _git_conn_check="${_git_conn_check}$dest=$ok "
-
-      if [ "$ok" != "1" ]; then
-        logger -t $NAME -s "Can't connect to $dest"
-        return 1
-      fi
-
-    ;;
-  esac
-
-  return 0
+       #local dest=$(git config --local --get remote.origin.url | sed -e 's/:.*$//' -e 's/^.*@//')
+       if [ "$1" = "-reset" ]; then
+               _git_conn_check=""
+               return
+       fi
+
+       local dest=$(git config --local --get remote.origin.url)
+       local port=''
+
+       if [ -z "$dest" ]; then
+               logger -t $NAME -s "Can't get url from \`git config --het remote.origin.url\` in `pwd`"
+               return 1
+       fi
+
+       echo "$dest" | grep -qm1 '^[a-z]\+:' || dest="ssh://$dest"
+       dest=$(url2nc "$dest")
+
+       local ok
+       case "$_git_conn_check" in
+               *" $dest=0 "*) ok=0 ;;
+               *" $dest=1 "*) ok=1 ;;
+               *)
+                       ok=0
+                       case "$dest" in
+                               *"@"*) timeout 5 ssh -o BatchMode=yes -o ConnectTimeout=5 $dest true && ok=1 ;;
+                               *) nc -z -w 5 $dest >/dev/null 2>&1 && ok=1 ;;
+                       esac
+                       [ -z "$_git_conn_check" ] && _git_conn_check=" "
+                       _git_conn_check="${_git_conn_check}$dest=$ok "
+
+                       if [ "$ok" != "1" ]; then
+                               logger -t $NAME -s "Can't connect to $dest"
+                               return 1
+                       fi
+
+               ;;
+       esac
+
+       return 0
 }
 
 git_merge_from() {
-  local usage="Usage: git_merge_from BRANCH, but does not push !"
-  local from=${1:?$usage}
-  local branch=$(git branch 2>/dev/null|awk '/^\*/{print $2}')
-  [ -z "$branch" ] && echo "$usage" 1>&2 && return 1
-  eval "git checkout $from && git pull && git checkout $branch && git merge $from"
+       local usage="Usage: git_merge_from BRANCH, but does not push !"
+       local from=${1:?$usage}
+       local branch=$(git branch 2>/dev/null|awk '/^\*/{print $2}')
+       [ -z "$branch" ] && echo "$usage" 1>&2 && return 1
+       eval "git checkout $from && git pull && git checkout $branch && git merge $from"
 }
 
 git_merge_to() {
-  local usage="Usage: git_merge_to BRANCH, and push branch !"
-  local to=${1:?$usage}
-  local branch=$(git branch 2>/dev/null|awk '/^\*/{print $2}')
-  [ -z "$branch" ] && echo "$usage" 1>&2 && return 1
-  eval "git checkout $to && git pull && git merge $branch && git push && git checkout $branch"
+       local usage="Usage: git_merge_to BRANCH, and push branch !"
+       local to=${1:?$usage}
+       local branch=$(git branch 2>/dev/null|awk '/^\*/{print $2}')
+       [ -z "$branch" ] && echo "$usage" 1>&2 && return 1
+       eval "git checkout $to && git pull && git merge $branch && git push && git checkout $branch"
 }
 
 git_branch_create_push() {
-  git checkout -b "$1" && git push --set-upstream origin "$1"
+       git checkout -b "$1" && git push --set-upstream origin "$1"
 }
 
 git_name() {
-  git config --local remote.origin.url|sed 's,^.*[:/]\([^:/\.]\+\)\(\.git\)\?$,\1,'
+       git config --local remote.origin.url|sed 's,^.*[:/]\([^:/\.]\+\)\(\.git\)\?$,\1,'
 }
 
 git_branch() {
-  git branch | awk '/^\*/{print $2}'
+       git branch | awk '/^\*/{print $2}'
+}
+
+git_track_branches() {
+# From http://stevelorek.com/how-to-shrink-a-git-repository.html
+       for branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master`; do
+               git branch --track ${branch##*/} $branch
+       done
 }
 
 git_size_pack() {
@@ -219,18 +226,40 @@ echo "All sizes are in kB. The pack column is the size of the object, compressed
 output="size,pack,SHA,location"
 for y in $objects
 do
-  # extract the size in bytes
-  size=$((`echo $y | cut -f 5 -d ' '`/1024))
-  # extract the compressed size in bytes
-  compressedSize=$((`echo $y | cut -f 6 -d ' '`/1024))
-  # extract the SHA
-  sha=`echo $y | cut -f 1 -d ' '`
-  # find the objects location in the repository tree
-  other=`git rev-list --all --objects | grep $sha`
-  #lineBreak=`echo -e "\n"`
-  output="${output}\n${size},${compressedSize},${other}"
+       # extract the size in bytes
+       size=$((`echo $y | cut -f 5 -d ' '`/1024))
+       # extract the compressed size in bytes
+       compressedSize=$((`echo $y | cut -f 6 -d ' '`/1024))
+       # extract the SHA
+       sha=`echo $y | cut -f 1 -d ' '`
+       # find the objects location in the repository tree
+       other=`git rev-list --all --objects | grep $sha`
+       #lineBreak=`echo -e "\n"`
+       output="${output}\n${size},${compressedSize},${other}"
 done
 
 echo -e $output | column -t -s ', '
 )
 }
+
+git_clean_repo() {
+  local usage="git_clean_repo REPO"
+  local repo=${1:-?$usage}
+  cat <<EOF
+  git_clone $repo || return
+  git_track_branches || return
+
+  echo "Cleaning the files"
+  git filter-branch --tag-name-filter cat --index-filter 'git rm -r --cached --ignore-unmatch filename' --prune-empty -f -- --all || return
+
+  echo "Reclaim space"
+  rm -rf .git/refs/original/ || return
+  git reflog expire --expire=now --all || return
+  git gc --prune=now || return
+  git gc --aggressive --prune=now || return
+
+  echo "Push the cleaned repository"
+  git push origin --force --all || return
+  git push origin --force --tags || return
+EOF
+}