File: libgit.sh

package info (click to toggle)
tig 2.6.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,188 kB
  • sloc: ansic: 36,941; sh: 10,934; makefile: 394
file content (130 lines) | stat: -rw-r--r-- 2,875 bytes parent folder | download
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
#!/bin/sh
#
# Git test helpers.
#
# Copyright (c) 2006-2025 Jonas Fonseca <jonas.fonseca@gmail.com>
#
# 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 2 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.

set -eu
if [ -n "${BASH_VERSION:-}" ]; then
	set -o pipefail
	IFS=$'\n\t'
fi

author_date=1234567890
author_date_delta=735730

IDENT_A="A. U. Thor <a.u.thor@example.com>"
IDENT_B="René Lévesque <rene.levesque@example.qc.ca>"
IDENT_C="作者 <zuozhea@example.ch>"
IDENT_D="Jørgen Thygesen Brahe <brache@example.dk>"
IDENT_E="Max Power <power123@example.org>"

git_config()
{
	git config --global user.name "Committer"
	git config --global user.email "c.ommitter@example.net"
	git config --global diff.renames false
}

git_init()
{
	dir="${1:-$work_dir}"

	if [ ! -e "$dir/.git" ]; then
		git init -q -- "$dir"
		(cd "$dir" && git_config)
	fi
}

git_add()
{
	[ -e .git ] || die "git_add called outside of git repo"
	path="$1"; shift

	mkdir -p -- "$(dirname -- "$path")"
	file "$path" "$@"
	git add -- "$path"
}

git_commit()
{
	[ -e .git ] || die "git_commit called outside of git repo: $(pwd)"

	GIT_COMMITTER_NAME="$0"
	GIT_COMMITTER_EMAIL="$0"

	export GIT_AUTHOR_DATE="$author_date"
	author_date="$(expr "$author_date" + "$author_date_delta")"
	[ -z "${GIT_COMMITTER_DATE:-}" ] &&
		export GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"

	git commit -q --allow-empty "$@"
	unset GIT_COMMITTER_DATE
}

create_dirty_workdir()
{
	git init -q .
	git_config

	printf '*.o\n' > .gitignore
	printf '*~\n' > .git/info/exclude

	for file in a b.c "d~" e/f "g h" i.o .j "h~/k"; do
		dir="$(dirname -- "$file")"
		[ -n "$dir" ] && mkdir -p -- "$dir"
		printf "%s\n%s" "$file" "$(seq 1 10)" > "$file"
	done

	git add .
	git_commit --author="$IDENT_A" --message="Initial commit"

	for file in a b.c "d~" e/f "g h" i.o .j "h~/k"; do
		printf "%s\n%s" "$file CHANGED" "$(seq 1 8)" > "$file"
	done
}

create_worktree()
{
	worktree_base_dir="$HOME/worktree-base"
	work_dir_abs="$(pwd)"

	mkdir -p "$worktree_base_dir"
	(cd "$worktree_base_dir" && {
		create_repo_from_tgz "$base_dir/files/repo-one.tgz" &&
		git branch dev
		git worktree add -- "$work_dir_abs" dev
	})
}

create_repo_from_tgz()
{
	git_init .
	tar zxof "$1"
	git reset -q --hard
}

git_clone()
{
	repo_tgz="$base_dir/files/$1.tgz"
	if [ -e "$repo_tgz" ]; then
		clone_dir="${2:-$work_dir}"
		(cd "$clone_dir" && {
			git_init .
			tar zxof "$repo_tgz"
			git reset -q --hard
		})
	else
		die "No generator for $(basename "$1")"
	fi
}