Commit | Line | Data |
---|---|---|
033c2594 DO |
1 | #!/bin/sh |
2 | ||
3 | # This file is a part of RackTables, a datacenter and server room management | |
4 | # framework. See accompanying file "COPYING" for the full copyright and | |
5 | # licensing information. | |
6 | ||
7 | # This script implements a simple (one file at a time) one-way feed into a git | |
8 | # repository. To make a commit it takes the following PHP code: | |
9 | # | |
10 | # $params = array | |
11 | # ( | |
12 | # 'racktables_pseudo_user', | |
13 | # '/path/to/repository', | |
14 | # 'path/to/file/within/the/repository', | |
15 | # 'commit message text', | |
16 | # ); | |
17 | # $rc = callScript ('git-commit', $params, $file_contents, $stdout, $stderr); | |
18 | # | |
19 | # The meaning of $stdout and $stderr is the same as in queryTerminal(). | |
20 | # The specified repository must exist and the specified pseudo-user must be | |
21 | # able to write to the repository and run "git pull", "git commit" and "git push" | |
22 | # without any user interaction (i.e. the git remote must be on a local | |
23 | # filesystem or be configured to use SSH keys). | |
1bba27d1 | 24 | # |
89e549b7 DO |
25 | # The commit message text is optional; when omitted or empty, a default value |
26 | # will be used. The message may be a multi-line string, in which case it should | |
27 | # follow the format recommended in the "discussion" section of the | |
28 | # git-commit(1) man page. | |
29 | # | |
1bba27d1 DO |
30 | # This script uses sudo to switch between the pseudo-users and requires an |
31 | # entry in sudoers along the following lines: | |
32 | # httpduser ALL=(racktablesuser) NOPASSWD:/path/to/racktables/gateways/git-commit | |
033c2594 | 33 | |
0859a212 | 34 | THISFILE=`basename "$0"` |
89e549b7 | 35 | [ $# -eq 3 -o $# -eq 4 ] || { |
0859a212 | 36 | echo "Usage: $THISFILE <pseudo-user> <repo dir> <path to file> [commit message]" >&2 |
033c2594 DO |
37 | exit 1 |
38 | } | |
39 | ||
40 | SUDOUSER=$1 | |
41 | REPODIR="$2" | |
42 | FILEPATH="$3" | |
89e549b7 | 43 | COMMITMSG="${4:-update $FILEPATH}" |
033c2594 DO |
44 | |
45 | [ `whoami` = "$SUDOUSER" ] || { | |
46 | sudo --non-interactive --set-home --user=$SUDOUSER -- "$0" "$@" | |
47 | exit $? | |
48 | } | |
49 | ||
50 | cd "$REPODIR" | |
51 | git pull --quiet || { | |
0859a212 | 52 | echo "$THISFILE: failed to run 'git pull' (rc=$?)" >&2 |
033c2594 DO |
53 | exit 2 |
54 | } | |
55 | ||
579666a7 DO |
56 | # git processes the path to the file automatically, but the shell |
57 | # redirection obviously does not. | |
58 | DIRNAME=`dirname "$FILEPATH"` | |
59 | [ -d "$DIRNAME" ] || mkdir -p "$DIRNAME" | |
60 | ||
033c2594 DO |
61 | # New file contents is on stdin. |
62 | cat > "$FILEPATH" || { | |
0859a212 | 63 | echo "$THISFILE: failed to write new file contents, trying to roll back." >&2 |
033c2594 | 64 | git checkout --quiet -- "$FILEPATH" || { |
0859a212 | 65 | echo "$THISFILE: failed to run 'git checkout' after a write error." >&2 |
033c2594 DO |
66 | exit 4 |
67 | } | |
68 | exit 3 | |
69 | } | |
70 | ||
579666a7 DO |
71 | # git-diff exits with 0 if the file is not in the repository. |
72 | if ! git cat-file -e HEAD:"$FILEPATH" 2>/dev/null || ! git diff --quiet -- "$FILEPATH"; then | |
033c2594 | 73 | git add -- "$FILEPATH" |
89e549b7 | 74 | git commit --quiet --message="$COMMITMSG" -- "$FILEPATH" |
033c2594 | 75 | git push --quiet || { |
0859a212 | 76 | echo "$THISFILE: failed to run 'git push' (rc=$?)" >&2 |
033c2594 DO |
77 | exit 5 |
78 | } | |
579666a7 | 79 | fi |
033c2594 DO |
80 | |
81 | exit 0 |