gitkit 3.72 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
#!/bin/bash
#########################################################
# 							#
# This is BashStyle-NG					#
#							#
# Licensed under GNU GENERAL PUBLIC LICENSE v3    	#
#							#
# Copyright 2007 - 2015 Christopher Bratusek		#
#							#
#########################################################

case ${1} in

	action)
		if [[ -d .git ]]; then
			if [[ -f .git/dotest/rebasing ]]; then
				ACTION="rebase"
			elif [[ -f .git/dotest/applying ]]; then
				ACTION="apply"
			elif [[ -f .git/dotest-merge/interactive ]]; then
				ACTION="rebase -i"
			elif [[ -d .git/dotest-merge ]]; then
				ACTION="rebase -m"
			elif [[ -f .git/MERGE_HEAD ]]; then
				ACTION="merge"
			elif [[ -f .git/index.lock ]]; then
				ACTION="locked"
			elif [[ -f .git/BISECT_LOG ]]; then
				ACTION="bisect"
			else	ACTION="nothing"
			fi
			echo $ACTION
		else	echo --
		fi
	;;

	branch)
		if [[ -d .git ]]; then
			BRANCH=$(git symbolic-ref HEAD 2>/dev/null)
			echo ${BRANCH#refs/heads/}
		else	echo --
		fi
Christopher Roy Bratusek's avatar
Christopher Roy Bratusek committed
43
	;;
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88

	bz2)
		if [[ -d .git ]]; then
			git archive master | bzip2 -9 > "$(basename $PWD)".tar.bz2
		else	echo "current directory is not the root of a git repository"
		fi
	;;

	xz)
		if [[ -d .git ]]; then
			git archive master | xz -9 > "$(basename $PWD)".tar.xz
		else	echo "current directory is not the root of a git repository"
		fi
	;;

	zip)
		if [[ -d .git ]]; then
			git archive master --format=zip > "$(basename $PWD)".zip
		else	echo "current directory is not the root of a git repository"
		fi
	;;

	cloneuser)
		curl -s https://api.github.com/users/"${2}"/repos?per_page=200 | \
			ruby -rubygems -e 'require "json"; JSON.load(STDIN.read).each { |repo| %x[git clone #{repo["ssh_url"]} ]}'
	;;
	
	cloneorg)
		curl -s https://api.github.com/orgs/"${2}"/repos?per_page=200 | \
			ruby -rubygems -e 'require "json"; JSON.load(STDIN.read).each { |repo| %x[git clone #{repo["ssh_url"]} ]}'
	;;

	export)
		if [[ "$2" && -d .git ]]; then
			git checkout-index --prefix="$2"/ -a
		fi
	;;

	funmsg)	wget -qO - http://whatthecommit.com/index.txt ;;

	openhub)
		if [[ -d .git ]]; then
			git remote -v | grep fetch | \
				sed 's/\(.*github.com\)[:|/]\(.*\).git (fetch)/\2/' | \
				awk {'print "https://github.com/" $1'} | \
89
				(xargs x-www-browser &) || echo "not a github repository"
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
		else	echo "current directory is not the root of a git repository"
		fi
	;;

	revision)
		if [[ -d .git ]]; then
			REVISION=$(git rev-parse HEAD 2>/dev/null)
			REVISION=${REVISION/HEAD/}
			echo ${REVISION:0:6}
		else	echo --
		fi
	;;

	revno)
		if [[ -d .git ]]; then
			git rev-list --reverse HEAD | \
				awk "/$(git log -n 1 --pretty="format:%h")/ {print NR}"
		else	echo --
		fi
	;;

	undelete)
		if [[ -d .git ]]; then
			git checkout $(git ls-files --deleted)
		else	echo "current directory is not the root of a git repository"
		fi
	;;

	*)
		bsng-help -a "Christopher Roy Bratusek" -e "nano@jpberlin.de" -h "http://www.nanolx.org/"\
			-l "GNU GPL v3" -n "gitkit" -s "various companion functions for GIT" -v "${BSNG_VERSION}" -y "${BSNG_YEAR}"\
			-o "action:|print the current action in a GIT repo"\
			-o "branch:|print the current branch in a GIT repo"\
			-o "bz2:|create bz2 archive from a GIT repo"\
			-o "xz:|create xz archive from a GIT repo"\
			-o "zip:|create zip archive from a GIT repo"\
			-o "cloneuser:username|clone all repos from a GitHub user (SSH)"\
			-o "cloneorg:organization|clone all repos from a GitHub organization (SSH)"\
			-o "export:directory|export GIT repo for release tarball"\
			-o "funmsg:|create funny GIT commit message"\
			-o "openhub:|open GitHub project page using x-www-browser"\
			-o "revision:|get 6 digit revision from a GIT repo"\
			-o "revno:|get traditional revision number from a GIT repo"\
133
			-o "undelete:|undelete files accidentally deleted locally"
134 135 136
	;;

esac