aboutsummaryrefslogtreecommitdiff
path: root/libs/git
blob: 2cde3dd358d363c34000f19b37c6e01754239840 (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
#!/usr/bin/env bash

# Copyright (C) 2016 Paul Kocialkowski <contact@paulk.fr>
# Copyright (C) 2019 Andrew Robbins <contact@andrewrobbins.info>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

BRANCH_PREFIX="libreboot-"
DOTGIT=".git"
HEAD="HEAD"
ORIGIN_HEAD="origin/HEAD"
WILDDOTPATCH="*.patch"
GIT_NAME="Libreboot"
GIT_EMAIL="libreboot@libreboot.org"

git_check() {
	local repository_path=$1

	directory_filled_check "$repository_path/$DOTGIT"
}

git_clone() {
	local repository_path=$1
	local url=$2

	git clone "$url" "$repository_path"
}

git_submodule_update() {
	local repository_path=$1

	(
		cd "$repository_path" 2>/dev/null || exit 1

		git submodule update --init
	)
}

git_merge() {
	local repository_path=$1
	local revision=$2

	(
		cd "$repository_path" 2>/dev/null || exit 1

		git merge "$revision"
	)
}

git_branch_create() {
	local repository_path=$1
	local branch=$2
	local revision=$3

	(
		cd "$repository_path" 2>/dev/null || exit 1

		git checkout -B "$branch"

		if [[ -n "$revision" ]]
		then
			git reset --hard "$revision"
		fi
	)
}

git_branch_delete() {
	local repository_path=$1
	local branch=$2

	(
		cd "$repository_path" 2>/dev/null || exit 1

		git branch -D "$branch"
	)
}

git_branch_checkout() {
	local repository_path=$1
	local branch=$2

	(
		cd "$repository_path" 2>/dev/null || exit 1

		git checkout "$branch" > /dev/null
	)
}

git_branch_check() {
	local repository_path=$1
	local branch=$2

	(
		cd "$repository_path" 2>/dev/null || exit 1

		git rev-parse --verify "$branch" >/dev/null 2>&1
	)
}

git_fetch() {
	local repository_path=$1

	(
		cd "$repository_path" 2>/dev/null || exit 1

		git fetch origin
	)
}

git_fetch_check() {
	local repository_path=$1

	(
		cd "$repository_path" 2>/dev/null || exit 1

		git fetch --dry-run origin >/dev/null 2>&1
	)
}

git_clean() {
	local repository_path=$1

	(
		cd "$repository_path" 2>/dev/null || exit 1

		git clean -df
	)
}

git_remove() {
	local repository_path=$1
	local path=$2

	(
		cd "$repository_path" 2>/dev/null || exit 1

		git rm -rf "$path"
	)
}

git_diff_staged_check() {
	local repository_path=$1

	(
		cd "$repository_path" 2>/dev/null || exit 1

		git diff --staged --quiet
	)
}

git_diff_check() {
	local repository_path=$1

	(
		cd "$repository_path" 2>/dev/null || exit 1

		git diff --quiet
	)
}

git_commit() {
	local repository_path=$1
	local message=$2

	(
		export GIT_COMMITTER_NAME=$GIT_NAME
		export GIT_COMMITTER_EMAIL=$GIT_EMAIL

		cd "$repository_path" 2>/dev/null || exit 1

		git commit --author="$GIT_NAME <$GIT_EMAIL>" -m "$message"
	)
}

git_am() {
	local repository_path=$1
	local branch=$2
	local patch=$3

	(
		export GIT_COMMITTER_NAME=$GIT_NAME
		export GIT_COMMITTER_EMAIL=$GIT_EMAIL

		cd "$repository_path" 2>/dev/null || exit 1

		git checkout "$branch" >/dev/null 2>&1

		if ! git am "$patch"; then
			git am --abort

			exit 1
		fi
	)
}

git_apply() {
	local repository_path=$1
	local branch=$2
	local patch=$3

	(
		cd "$repository_path" 2>/dev/null || exit 1

		git checkout "$branch" >/dev/null 2>&1
		git apply --index "$patch"
	)
}

git_apply_check() {
	local repository_path=$1
	local branch=$2
	local patch=$3

	(
		cd "$repository_path" 2>/dev/null || exit 1

		git checkout "$branch" >/dev/null 2>&1
		git apply --check "$patch"
	)
}

git_patch() {
	local repository_path=$1
	local branch=$2
	local patch=$3

	git_apply_check "$repository_path" "$branch" "$patch" || return 1

	case $patch in
		*.patch)
			git_am "$repository_path" "$branch" "$patch"
			;;
		*.diff)
			git_apply "$repository_path" "$branch" "$patch"
			git_commit "$repository_path" "Applied ${patch##*/}"
			;;
		*)
			;;
	esac
}

git_revision() {
	local repository_path=$1

	(
		cd "$repository_path" 2>/dev/null || exit 1

		git rev-parse "$HEAD"
	)
}

git_describe() {
	local repository_path=$1

	(
		cd "$repository_path" 2>/dev/null || exit 1

		git describe --tags
	)
}

git_files() {
	local repository_path="$1"

	(
		cd "$repository_path" 2>/dev/null || exit 1

		git ls-files -z | sort -z
	)
}

git_project_repository_path() {
	local repository=$1

	printf '%s\n' "$root/$SOURCES/$repository"
}

git_project_check() {
	local repository=$1

	local repository_path=$(git_project_repository_path "$repository")

	git_check "$repository_path"
}

git_project_patch_recursive() {
	local project=$1
	local repository=$2
	local branch=$3
	local path=$4

	local repository_path=$(git_project_repository_path "$repository")
	local project_path=$(project_path "$project")
	local patches_path=$project_path/$PATCHES/$path

	if ! [[ -d $project_path/$PATCHES ]]; then
		return 0
	fi

	for patch in "$patches_path"/[!.]*.@(patch|diff); do
		git_patch "$repository_path" "$branch" "$patch" || return 1
	done

	if [[ -n $path && $path != . ]]; then
		git_project_patch_recursive "$project" "$repository" "$branch" "$(dirname "$path")"
	fi
}

git_project_clone() {
	local repository=$1
	shift
	local urls=$@

	local repository_path=$(git_project_repository_path "$repository")
	local directory_path=$(dirname "$repository_path")
	local url

	mkdir -p "$directory_path"

	(
		set +e

		for url in $urls
		do
			if git_clone "$repository_path" "$url"
			then
				return 0
			fi
		done

		return 1
	)
}

git_project_prepare() {
	local project=$1
	shift
	local repository=$1
	shift

	git_project_prepare_revision "$project" "$repository" "$@"
	git_project_prepare_blobs "$project" "$repository" "$@"
	git_project_prepare_patch "$project" "$repository" "$@"
}

git_project_prepare_blobs() {
	local project=$1
	shift
	local repository=$1
	shift

	local repository_path=$(git_project_repository_path "$repository")
	local blob

	while read -r blob
	do
		git_remove "$repository_path" "$blob"
	done < <(project_blobs "$project" "$@")

	if ! git_diff_staged_check "$repository_path"
	then
		git_commit "$repository_path" "Removed blobs"
	fi
}

git_project_prepare_patch() {
	local project=$1
	shift
	local repository=$1
	shift

	local branch=$project
	local argument
	local path

	for argument in "$@"
	do
		if [[ -z "$path" ]]
		then
			path="$argument"
		else
			path="$path/$argument"
		fi

		branch="$branch-$argument"
	done

	if [[ -n $branch ]]
	then
		local prepare_branch=$BRANCH_PREFIX$branch
		local prepare_path=$path

		git_project_patch_recursive "$project" "$repository" "$prepare_branch" "$prepare_path"
	fi
}

git_project_prepare_revision() {
	local project=$1
	shift
	local repository=$1
	shift

	local repository_path=$(git_project_repository_path "$repository")
	local project_path=$(project_path "$project")
	local configs_path="$project_path/$CONFIGS"
	local branch=$project
	local prepare_revision
	local argument
	local path

	for argument in "" "$@"
	do
		if [[ -n $argument ]]
		then
			if [[ -z $path ]]
			then
				path="$argument"
			else
				path="$path/$argument"
			fi

			branch="$branch-$argument"
		fi

		local revision_path="$configs_path/$path/$REVISION"

		if [[ -f $revision_path ]]; then
			prepare_revision=$(< "$revision_path")
		fi
	done

	if [[ -n $branch ]]
	then
		local prepare_branch=$BRANCH_PREFIX$branch

		git_branch_create "$repository_path" "$prepare_branch" "$prepare_revision"
	fi
}

git_project_prepare_check() {
	local project=$1
	shift
	local repository=$1
	shift

	local repository_path=$(git_project_repository_path "$repository")
	local branch=$project
	local argument

	for argument in "$@"
	do
		branch="$branch-$argument"
	done

	if [[ -n $branch ]]
	then
		local prepare_branch=$BRANCH_PREFIX$branch

		git_branch_check "$repository_path" "$prepare_branch"
	fi
}

git_project_prepare_clean() {
	local project=$1
	shift
	local repository=$1
	shift

	local repository_path=$(git_project_repository_path "$repository")
	local branch=$project
	local argument

	for argument in "$@"
	do
		branch="$branch-$argument"
	done

	if [[ -n $branch ]]
	then
		local prepare_branch=$BRANCH_PREFIX$branch

		if git_branch_check "$repository_path" "$prepare_branch"
		then
			git_branch_delete "$repository_path" "$prepare_branch"
		fi
	fi
}

git_project_checkout() {
	local project=$1
	shift
	local repository=$1
	shift

	local repository_path=$(git_project_repository_path "$repository")
	local branch=$project
	local argument

	for argument in "$@"
	do
		branch="$branch-$argument"
	done

	if [[ -n $branch ]]
	then
		local checkout_branch=$BRANCH_PREFIX$branch

		if git_branch_check "$repository_path" "$checkout_branch"
		then
			git_branch_checkout "$repository_path" "$checkout_branch"
			git_submodule_update "$repository_path"
		fi
	fi
}

git_project_update() {
	local project=$1
	shift
	local repository=$1
	shift

	local repository_path=$(git_project_repository_path "$repository")

	git_fetch "$repository_path"
	git_branch_checkout "$repository_path" "$ORIGIN_HEAD"

	git_project_prepare_clean "$project" "$repository" "$@"
	git_project_prepare "$project" "$repository" "$@"
}

git_project_update_check() {
	local project=$1
	shift
	local repository=$1
	shift

	git_project_prepare_check "$project" "$repository" "$@"

	git_fetch_check "$repository_path"
}

git_project_release() {
	local project=$1
	shift
	local repository=$1
	shift

	local repository_path=$(git_project_repository_path "$repository")
	local branch=$project
	local argument

	for argument in "$@"
	do
		branch="$branch-$argument"
	done

	if [[ -n $branch ]]
	then
		local release_branch=$BRANCH_PREFIX$branch

		if git_branch_check "$repository_path" "$release_branch"
		then
			local archive_path="$root/$RELEASE/$SOURCES/$project/$release_branch.$ARCHIVE"
			local sources_path="$root/$SOURCES/$repository"

			printf '%s\n' "Releasing sources archive for $project (with ${arguments:-no argument}) from "$repository" git"

			git_branch_checkout "$repository_path" "$release_branch"
			git_submodule_update "$repository_path"
			git_clean "$repository_path"
			archive_create "$archive_path" "$sources_path" "$release_branch"
			file_verification_create "$archive_path"
		fi
	fi
}

git_project_release_check() {
	local project=$1
	shift
	local repository=$1
	shift

	local repository_path=$(git_project_repository_path "$repository")
	local branch=$project
	local argument

	for argument in "$@"
	do
		branch="$branch-$argument"
	done

	if [[ -n $branch ]]
	then
		local release_branch=$BRANCH_PREFIX$branch

		if git_branch_check "$repository_path" "$release_branch"
		then
			local archive_path="$root/$RELEASE/$SOURCES/$project/$release_branch.$ARCHIVE"

			file_exists_check "$archive_path"
		fi
	fi
}