summaryrefslogtreecommitdiff
path: root/bin/gitify
blob: 7282dc3f5a3cc83b392a6c4f4f720f892bd8b6c8 (plain)
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
#!/usr/bin/env bash

empty() {
	[[ $(stat -c %s "$1") -eq 0 ]]
}

url2murl() {
	local x
	x=$1
	x=${x//'^'/'^5E'}
	x=${x//':'/'^3A'}
	x=${x//'%'/'^25'}
	printf '%s' "$x"
}

murl2url() {
	local x
	x=$1
	x=${x//'^25'/'%'}
	x=${x//'^3A'/':'}
	x=${x//'^5E'/'^'}
	printf '%s' "$x"
}

main() {
	set -euE -o pipefail
	shopt -s nullglob
	shopt -s globstar

	top=$PWD
	mkdir -p "$1"
	cd "$1"
	git init
	echo 'ref: refs/heads/PROGRAMS/CVTUTF' > .git/HEAD
	lastbranch=''

	for snapshot in "$top"/dat/pools/snaps/*; do
		snapname="${snapshot##*/}"
		time="${snapname%%-*}"
		dirpart="${snapname#*-}"
		dirpart="${dirpart//_/\/}"

		branch=$dirpart
		newbranch=true
		if [[ -n "$lastbranch" ]]; then
			git checkout "$lastbranch"
			if [[ "$branch" == *.OLD ]]; then
				git checkout HEAD^
			fi
			git checkout -b "$branch" || newbranch=false
			git checkout "$branch"
		fi

		rm -f -- * .metadata.txt
		cp -- "$snapshot"/* .

		if [[ "$time" != *99 ]]; then
			listingdir=("$top/dat/content-dir/$time/"**"/Public/$dirpart")
			if [[ ${#listingdir[@]} != 1 ]]; then
				>&2 printf 'Cannot find wayback listing for %s' "$snapname"
				false
			fi
			if ! [[ -f readme.txt ]] && ! empty "$listingdir/readme.txt"; then
			   cp "$listingdir/readme.txt" .
			fi
			cp "$listingdir/metadata.txt" .metadata.txt
		fi

		if $newbranch || [[ -n "$(git status -s .)" ]]; then
			if [[ "$time" = *99 ]]; then
				msg="Synthesized listing: ${time} ${dirpart}"
				time="${time%99}00"
			else
				waurl="http://web.archive.org/web/$(murl2url "${listingdir##*/content-dir/}/")"
				msg="$waurl"
			fi

			HACK_TZ=-0500
			if [[ "$branch" == BETA/CVTUTF-1-4 || "$branch" == ALPHA/CVTUTF-1-5-draft ]]; then
				HACK_TZ=-0400
			fi
			HACK_NAME='Luke Shumaker'
			HACK_EMAIL='lukeshu@lukeshu.com'

			gitdate="$(sed -E 's/(....)(..)(..)(..)(..)(..)/\1-\2-\3T\4:\5:\6 '"$HACK_TZ"'/' <<<"$time")"

			git add .

			export GIT_AUTHOR_DATE=$gitdate
			export GIT_AUTHOR_NAME=$HACK_NAME
			export GIT_AUTHOR_EMAIL=$HACK_EMAIL

			export GIT_COMMITTER_DATE=$gitdate
			export GIT_COMMITTER_NAME=$HACK_NAME
			export GIT_COMMITTER_EMAIL=$HACK_EMAIL

			git commit --allow-empty -m "$msg"
			if [[ "$branch" != *.OLD ]]; then
				lastbranch="$branch"
			fi
			if [[ "$branch" == PROGRAMS/CVTUTF ]] && git log -n1 --stat|grep -qF 'ExpectedOutput.txt'; then
				git filter-branch -f --parent-filter 'cat; echo " -p BETA/CVTUTF-1-3"' HEAD^..HEAD
				git update-ref -d refs/original/refs/heads/"$branch"
			fi
			if [[ "$branch" == PROGRAMS/CVTUTF.OLD ]] && git log -n1 --stat|grep -qi '.*\.c\s'; then
				git filter-branch -f --parent-filter 'cat; echo " -p PROGRAMS/CVTUTF^"' HEAD^..HEAD
				git update-ref -d refs/original/refs/heads/"$branch"
			fi
		fi
	done
}

main "$@"