summaryrefslogtreecommitdiffstats
path: root/.bin/oak
blob: 4ea560ef8b99ec94b3f7ff83021584430318d777 (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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/bin/sh
#             _
#            | |
#  __   __,  | |
# /  \_/  |  |/_)
# \__/ \_/|_/| \_/
#
# ~/.bin/oak
#
# manage multiple dotfiles repo in same work tree
#
# Example:
#  dotfiles.d/
#  |_ config
#  |_ secret
#
# ~ rgoncalves.se

usage() {
	cat <<- EOF
	USAGE ${0} : git_dirname git_action [arguments...]
	    -b: enable bootstraping for absent repository
	    -d: oak directory to work on
	        works in batch mode if no directory is provided
	    -f: force all operations
	        use with caution
	    -m: commit message when using -s option
	        commits_msg_have_to_be_written_like_this
	    -r: git remote with full path to parent directory
	    -h: show usage of ${0}
	EOF
}

log() {
	echo "[OAK  ] ${@}"
}

git_chroot() {
	log ">> ${1}"
	git --git-dir="${OAK_ROOT}/${1}" --work-tree="${HOME}" ${2}
}

git_bootstrap() {
	# directory url remote
	git_chroot "${1}" "clone --bare git@${2}/${1} ${OAK_ROOT}/${1}"
	[ "${?}" -ne 0 ] && log "Failed to bootstrap ${1}" && exit 1
	git_chroot "${1}" "checkout trunk" >/dev/null 2>&1
	git_chroot "${1}" "branch --set-upstream-to trunk origin/trunk" >/dev/null 2>&1
	git_chroot "${1}" "push -u origin trunk" >/dev/null 2>&1
}

git_sync() {
	# actions
	git_sane_all
	pull_out=$(git_chroot "${1}" "pull")
	if [ "${?}" -ne 0 ]; then
		log "error while doing git pull"
		exit 1
	fi
	echo "${pull_out}" 2>&1 | grep " ."
	git_chroot "${1}" "add -u" >/dev/null
	git_chroot "${1}" "commit -m ${OAK_MSG}"
	git_chroot "${1}" "push --set-upstream origin trunk"
	# cleanup
	unset pull_out push_out
}

git_list() {
	log "Available repositories : "
	for dir in ${OAK_DIRS}; do
		log " - ${dir}"
	done
}

git_sane() {
	if [ "${1}" = "${2}" ]; then
		return 1
	fi
	git_chroot "${1}" "ls-files" > ~/.cache/oak_a
	git_chroot "${2}" "ls-files" > ~/.cache/oak_b
	out=$(grep -F -x -f ~/.cache/oak_a ~/.cache/oak_b)
	if [ -n "${out}" ]; then
		log "detected similar files for directories: ${repo_a}, ${repo_b}"
		echo "${out}"
		return 1
	fi
	return 0
}

git_sane_all() {
	for repo_a in ${DOT_DIRS}; do
		for repo_b in ${DOT_DIRS}; do
			if [ "${repo_a}" = "${repo_b}" ]; then
				continue
			fi
			git_sane "${DOT_ROOT}/${repo_a}" "${DOT_ROOT}/${repo_b}"
			if [ "${?}" -ne 0 ]; then
				log "aborting"
				exit 1
			fi
		done
	done
}

main() {

	OAK_ROOT="${HOME}/.dotfiles.d"
	OAK_DIRS=$(ls "${OAK_ROOT}")
	OAK_DIR=""
	OAK_REMOTE=""
	OAK_CMD=""
	OAK_MSG=""

	# Retrieve oak params
	while getopts "bd:hlm:r:s" arg; do
		case "${arg}" in
			b)
				OAK_BOOTSTRAP=1
				;;
			d)
				OAK_DIR="${OPTARG}"
				;;
			f)
				OAK_FORCE=1
				;;
			h)
				usage && exit 0
				;;
			m)
				OAK_MSG="$(echo ${OPTARG} | tr -s '_' ' ')"
				;;
			l)
				git_list
				exit 0
				;;
			r)
				OAK_REMOTE="${OPTARG}"
				;;
			s)
				OAK_SYNC=1
				;;
		esac
	done

	[ -z "${OAK_MSG}" ] && OAK_MSG=$(date +%j_%H_%M_%S)

	# Bootstrap non-present repositories
	if [ -n "${OAK_DIR}" ] && [ ! -d "${OAK_ROOT}/${OAK_DIR}" ]; then
		git_bootstrap "${OAK_DIR}" "${OAK_REMOTE}"
		if [ "${?}" -ne 0 ]; then
			log "Git repository does not exist. Error"
			exit 1
		fi
		log "Successfully bootstraped git repository"
	fi

	# Retrieve oak command for git
	shift $(expr "${OPTIND}" - 1)
	for cmd in "${@}"; do
		OAK_CMD="${OAK_CMD}${cmd} "
	done

	# Git synchronization in each repository
	if [ -n "${OAK_SYNC}" ]; then
		for dir in ${OAK_DIR:-$OAK_DIRS}; do
			log "synchronize ${dir}"
			git_sync "${dir}"
		done
	fi

	# Raw command in each git repository
	if [ -n "${OAK_CMD}" ]; then
		for dir in ${OAK_DIR:-$OAK_DIRS}; do
			git_chroot "${dir}" "${OAK_CMD}"
		done
	fi

}

main $@
remember that computers suck.