3 # git-import-pk3: Import a PK3 into a branch of a bare Git repo.
11 echo "$(basename "$0"): error: $*" >&2
21 export GIT_DIR=/path/to/xonotic-maps.pk3dir.git # absolute path
22 export GIT_WORK_TREE=/tmp/worktree # pk3 is extracted here
24 export GIT_AUTHOR_NAME="Some Guy"
25 export GIT_AUTHOR_EMAIL="someguy@example.com"
27 $(basename $0) nick branch map.pk3 [changes.txt]
34 [ $# -ge 3 -a $# -le 4 ] || {
36 die "wrong number of arguments"
39 [ -z "$GIT_DIR" ] && {
44 [ -z "$GIT_WORK_TREE" ] && {
46 die "GIT_WORK_TREE not set"
49 [ -z "$(ls -A "$GIT_WORK_TREE")" ] ||
50 die "GIT_WORK_TREE is not empty"
53 branch=$(git check-ref-format --print "$nick/$2") ||
54 die "malformed branch name '$nick/$2'"
56 pk3name="$(basename "$pk3path")"
61 [ -z "$GIT_AUTHOR_NAME" ] && export GIT_AUTHOR_NAME="$nick"
62 [ -z "$GIT_AUTHOR_EMAIL" ] && export GIT_AUTHOR_EMAIL="${nick}@example.com"
66 echo -e "Import $pk3name\n" | cat - "$changes" 2> /dev/null
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
76 master=refs/heads/master
77 ref=refs/heads/$branch
79 # Figure out the parent commit
81 $(git rev-parse --verify -q $ref || git rev-parse --verify -q $master) ||
82 die "couldn't determine parent commit"
84 # Read the tree at master into index
85 git read-tree $master ||
86 die "couldn't initialize index"
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"
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
95 git ls-files --exclude-standard -o -z |
96 git update-index --add -z --stdin ||
97 die "couldn't add files to index"
99 # Check if the index contains changes against parent
100 git diff-index --cached --quiet $parent &&
101 die "found no changes to commit"
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"