]> git.xonotic.org Git - xonotic/xonotic.git/blob - misc/tools/git-import-pk3
54c8593e48030a8bb0c40f73334bdd06596cc725
[xonotic/xonotic.git] / misc / tools / git-import-pk3
1 #!/bin/sh
2
3 # git-import-pk3: Import a PK3 into a branch of a bare Git repo.
4
5 set -x
6
7 # Helpers
8
9 die()
10 {
11     echo "$(basename "$0"): error: $*" >&2
12     exit 1
13 }
14
15 usage()
16 {
17     cat <<EOF
18 Usage:
19
20   # mandatory
21   export GIT_DIR=/path/to/xonotic-maps.pk3dir.git # absolute path
22   export GIT_WORK_TREE=/tmp/worktree # pk3 is extracted here
23   # optional
24   export GIT_AUTHOR_NAME="Some Guy"
25   export GIT_AUTHOR_EMAIL="someguy@example.com"
26
27   $(basename $0) nick branch map.pk3 [changes.txt]
28
29 EOF
30 }
31
32 # Check usage
33
34 [ $# -ge 3 -a $# -le 4 ] || {
35     usage
36     die "wrong number of arguments"
37 }
38
39 [ -z "$GIT_DIR" ] && {
40     usage
41     die "GIT_DIR not set"
42 }
43
44 [ -z "$GIT_WORK_TREE" ] && {
45     usage
46     die "GIT_WORK_TREE not set"
47 }
48
49 [ -z "$(ls -A "$GIT_WORK_TREE")" ] ||
50 die "GIT_WORK_TREE is not empty"
51
52 nick="$1"
53 branch=$(git check-ref-format --print "$nick/$2") ||
54 die "malformed branch name '$nick/$2'"
55 pk3path="$3"
56 pk3name="$(basename "$pk3path")"
57 changes="$4"
58
59 # Set up commit info
60
61 [ -z "$GIT_AUTHOR_NAME" ] && export GIT_AUTHOR_NAME="$nick"
62 [ -z "$GIT_AUTHOR_EMAIL" ] && export GIT_AUTHOR_EMAIL="${nick}@example.com"
63
64 message()
65 {
66     echo -e "Import $pk3name\n" | cat - "$changes" 2> /dev/null
67 }
68
69 # Extract the PK3 (the -n suppresses prompting in an edge case)
70 # FIXME: perhaps the caller should handle extraction and clean up
71 unzip -n -qq "$pk3path" -d "$GIT_WORK_TREE" ||
72 die "couldn't extract PK3 contents"
73 trap 'find "$GIT_WORK_TREE" -mindepth 1 -delete' EXIT
74
75 # Note the refs
76 master=refs/heads/master
77 ref=refs/heads/$branch
78
79 # Figure out the parent commit
80 parent=\
81 $(git rev-parse --verify -q $ref || git rev-parse --verify -q $master) ||
82 die "couldn't determine parent commit"
83
84 # Read the tree at master into index
85 git read-tree $master ||
86 die "couldn't initialize index"
87
88 # Reject any modified files, the mapper should create a branch instead
89 [ -z "$(git diff-files --diff-filter=M)" ] ||
90 die "found changes to files in master"
91
92 # Add untracked files; to filter out generated files such as BSP, add
93 # them to .gitignore and push, --exclude-standard should take care of
94 # the rest
95 git ls-files --exclude-standard -o -z |
96 git update-index --add -z --stdin ||
97 die "couldn't add files to index"
98
99 # Check if the index contains changes against parent
100 git diff-index --cached --quiet $parent &&
101 die "found no changes to commit"
102
103 # Commit the index and point the ref to the new commit
104 tree=$(git write-tree) &&
105 commit=$(message | git commit-tree $tree -p $parent) &&
106 git update-ref -m "import-pk3: $pk3name" $ref $commit ||
107 die "couldn't commit changes"