summaryrefslogtreecommitdiffstats
path: root/git-getall
blob: b622551d95ce0e20017bbf9fde5f9e42c7e1f7c8 (plain) (blame)
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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/bin/sh
#
# Retrieve public web-interface for git repositories,
# then ensure that the same repository tree exists and
# is up to date in the current directory.
# Currently ONLY CGIT is supported for web scrapping.

# user and repository informations
http_url=""
git_remote=""
git_user="git"
git_dir="."
git_branches="trunk master main"
# regexp patterns
pattern_before="<a class='button' href='"
pattern_after="'>"

log() {
	echo "[${0}] $@" 2>/dev/null
}

usage() {
	[[ ! -z ${1} ]] && log "${1}"
	cat <<-EOF
	usage: git-dlall [-u user] [-d root directory] [-p git_repositories] -l http_url -r git_remote
	EOF
}

_git_setup() {
	valid_branch=""
	for branch in ${git_branches}; do
		if git branch | grep "${branch}" >/dev/null; then
			valid_branch="${branch}"
			break
		fi
	done
	# only continue if a valid principal branch is found
	[ -z "${valid_branch}" ] && return 1

	git pull origin "${valid_branch}" >/dev/null
	git branch --set-upstream-to="origin/${valid_branch}" "${valid_branch}" >/dev/null
}

main() {
	# retrieve command line arguments
	while getopts "d:l:r:u:" arg; do
		case "${arg}" in
			l)
				http_url=${OPTARG}
				;;
			d)
				git_dir=${OPTARG}
				;;
			u)
				git_user=${OPTARG}
				;;
			p)
				git_repositories=${OPTARG}
				;;
			r)
				git_remote=${OPTARG}
				;;
			*)
				usage
				exit 1
				;;
		esac
	done

	# ensure required variables are not empty
	if [ -z "${git_remote}" ] || [ -z "${http_url}" ]; then
		usage "missing remote informations"
		exit 1
	fi

	# ensure git root directory exists
	if [ ! -d "${git_dir}" ]; then 
		mkdir "${git_dir}"
		[ "${?}" -ne 0 ] && usage && exit 1
	fi

	# retrieve available repositories
	http_content=$(curl --silent --insecure "${http_url}")
	repo_list=$(echo "${http_content}" | grep -o "$pattern_before.*$pattern_after" | cut -d "'" -f 4)

	# sanitize repositories
	for repository in ${repo_list}; do
		repository="${repository#/}"
		repositories="${repositories} ${repository%/}"
	done

	# sync repository
	for repository in ${repositories}; do
		repository_fullpath="${git_dir}/${repository}"
		if [ -d "${repository_fullpath}" ]; then
			log "repository : ${repository}. check"
		else
			log "repository : ${repository}. clone"
			git clone "${git_user}@${git_remote}:${repository}" "${repository_fullpath}"
		fi
		(cd "${repository_fullpath}" ; _git_setup)
	done
}

main $@
remember that computers suck.