File: 50vcs-commit

package info (click to toggle)
etckeeper 1.18.23-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,640 kB
  • sloc: sh: 1,267; python: 111; makefile: 109; ansic: 81
file content (151 lines) | stat: -rwxr-xr-x 3,988 bytes parent folder | download | duplicates (2)
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
#!/bin/sh
set -e

cleanup () {
	if [ -n "$logfile" ]; then
		rm -f "$logfile"
	fi
}
if [ -n "$1" ]; then
	if command -v mktemp >/dev/null; then
		tempfile="mktemp -t etckeeper-$VCS.XXXXXXXXXX"
	elif command -v tempfile >/dev/null; then
		tempfile="tempfile"
	else
		echo "etckeeper warning: can't find tempfile or mktemp" >&2
		exit 1
	fi

	trap cleanup EXIT
	logfile="$($tempfile)"
	if [ "x$1" = "x--stdin" ]; then
		cat > "$logfile"
	else
		sed '1s/^-m \{0,1\}//' >"$logfile" <<-EOF
			$*
		EOF
	fi
else
	logfile=""
fi

hostname=`hostname 2>/dev/null || cat /etc/hostname`
hostname="${hostname%%.*}"
dnsdomainname=`dnsdomainname 2>/dev/null || true`
if [ -n "$dnsdomainname" ]; then
	hostname="$hostname.$dnsdomainname"
fi

ORIG_USER=$USER
USER=
if [ -n "$SUDO_USER" ]; then
	USER="$SUDO_USER"
else
	# try to check tty ownership, in case user su'd to root
	TTY="$(tty 2>/dev/null || true)"
	if [ -n "$TTY" ] && [ -c "$TTY" ]; then
		USER="$(find "$TTY" -printf "%u")"
	fi
fi

if [ "$VCS" = git ] && [ -d .git ]; then
	# When not su'd to root, still set environment variables, 
	# since git's own code to determine the author and committer
	# has several edge cases where it fails and would prevent the
	# commit.
	if [ -z "$USER" ]; then
		USER="$(whoami)"
	fi
	if [ -n "$USER" ]; then
		# Use user.name and user.email from the gitconfig belonging
		# to USER.
		USER_HOME="$(getent passwd "$USER" | cut -d: -f6)"
		if [ -n "$USER_HOME" ] && [ -e "$USER_HOME/.gitconfig" ]; then
			if [ -z "$GIT_AUTHOR_NAME" ]; then
				GIT_AUTHOR_NAME="$(git config -f "$USER_HOME/.gitconfig" user.name)" || true
				export GIT_AUTHOR_NAME
			fi
			if [ -z "$GIT_AUTHOR_EMAIL" ]; then
				GIT_AUTHOR_EMAIL="$(git config -f "$USER_HOME/.gitconfig" user.email)" || true
				export GIT_AUTHOR_EMAIL
			fi
		fi
		if [ -z "$GIT_AUTHOR_NAME" ] || [ -z "$GIT_AUTHOR_EMAIL" ]; then
			if [ -n "$USER_HOME" ] && [ -e "$USER_HOME/.config/git/config" ]; then
				if [ -z "$GIT_AUTHOR_NAME" ]; then
					GIT_AUTHOR_NAME="$(git config -f "$USER_HOME/.config/git/config" user.name)" || true
					export GIT_AUTHOR_NAME
				fi
				if [ -z "$GIT_AUTHOR_EMAIL" ]; then
					GIT_AUTHOR_EMAIL="$(git config -f "$USER_HOME/.config/git/config" user.email)" || true
					export GIT_AUTHOR_EMAIL
				fi
			fi
		fi

		if [ -z "$GIT_COMMITTER_EMAIL" ]; then
			GIT_COMMITTER_EMAIL="$(git config --global user.email)" || true
			export GIT_COMMITTER_EMAIL
		fi
		if [ -z "$GIT_AUTHOR_NAME" ]; then
			GIT_AUTHOR_NAME="$USER"
			export GIT_AUTHOR_NAME
		fi
		if [ -z "$GIT_AUTHOR_EMAIL" ]; then
			GIT_AUTHOR_EMAIL="$USER@$hostname"
			export GIT_AUTHOR_EMAIL
		fi
		if [ -z "$GIT_COMMITTER_EMAIL" ]; then
			GIT_COMMITTER_EMAIL=`whoami`"@$hostname"
			export GIT_COMMITTER_EMAIL
		fi
	fi

	# gc ten times more frequently than the default
	# (unless some other config is set)
	GIT_GC_OPTIONS=
	if ! git config gc.auto >/dev/null; then
		GIT_GC_OPTIONS="-c gc.auto=670"
	fi

	if [ -n "$logfile" ]; then
		git $GIT_GC_OPTIONS commit $GIT_COMMIT_OPTIONS -F "$logfile"
	else
		git $GIT_GC_OPTIONS commit $GIT_COMMIT_OPTIONS
	fi
elif [ "$VCS" = hg ] && [ -d .hg ]; then
	if [ -n "$USER" ]; then
		LOGNAME="$USER"
		export LOGNAME
	fi
	if [ -z "$HGUSER" ]; then
		HGUSER="$USER@$hostname"
		export HGUSER
	fi
	if [ -n "$logfile" ]; then
		hg commit $HG_COMMIT_OPTIONS -l "$logfile"
	else
		hg commit $HG_COMMIT_OPTIONS
	fi
elif [ "$VCS" = bzr ] && [ -d .bzr ]; then
	if [ -z "$EMAIL" ] && [ -n "$USER" ]; then
		EMAIL="$USER <$USER@$hostname>"
		export EMAIL
	else
		bzr whoami >/dev/null 2>&1 || export EMAIL="$ORIG_USER <$ORIG_USER@$hostname>"
	fi
	if [ -n "$logfile" ]; then
		bzr commit $BZR_COMMIT_OPTIONS -F "$logfile"
	else
		bzr commit --quiet $BZR_COMMIT_OPTIONS
	fi
elif [ "$VCS" = darcs ] && [ -d _darcs ]; then
	if [ -z "$USER" ]; then
		USER=root
	fi
	if [ -n "$logfile" ]; then
		darcs record --author="$USER" $DARCS_COMMIT_OPTIONS --logfile="$logfile"
	else
		darcs record --author="$USER" $DARCS_COMMIT_OPTIONS
	fi
fi