UNPKG

2.62 kBapplication/x-shView Raw
1#!/usr/bin/env bash
2set -e # Stop on the first failure that occurs
3
4DOCS_PATH=.git/groc-tmp
5TARGET_BRANCH=gh-pages
6TARGET_REMOTE=origin
7
8# Git spits out status information on $stderr, and we don't want to relay that as an error to the
9# user. So we wrap git and do error handling ourselves...
10exec_git() {
11 args=''
12 for (( i = 1; i <= $#; i++ )); do
13 eval arg=\$$i
14 if [[ $arg == *\ * ]]; then
15 #} We assume that double quotes will not be used as part of argument values.
16 args="$args \"$arg\""
17 else
18 args="$args $arg"
19 fi
20 done
21
22 set +e
23 #} Even though we wrap the arguments in quotes, bash is splitting on whitespace within. Why?
24 result=`eval git $args 2>&1`
25 status=$?
26 set -e
27
28 if [[ $status -ne 0 ]]; then
29 echo "$result" >&2
30 exit $status
31 fi
32
33 echo "$result"
34 return 0
35}
36
37if [[ `git status -s` != "" ]]; then
38 echo "Please commit or stash your changes before publishing documentation to github!" >&2
39 exit 1
40fi
41
42CURRENT_BRANCH=`git branch 2>/dev/null| sed -n '/^\*/s/^\* //p'`
43CURRENT_COMMIT=`git rev-parse HEAD`
44
45# Preserve the project's .gitignore so that we don't check in or otherwise screw up hidden files
46if [[ -e .gitignore ]]; then
47 cp .gitignore $DOCS_PATH/
48fi
49
50if [[ `git branch --no-color | grep " $TARGET_BRANCH"` == "" ]]; then
51 # Do a fetch from origin to see if it was created remotely
52 exec_git fetch $TARGET_REMOTE
53
54 # Does it exist remotely?
55 if [[ `git branch -a --no-color | grep " remotes/$TARGET_REMOTE/$TARGET_BRANCH"` == "" ]]; then
56 echo "No '$TARGET_BRANCH' branch exists. Creating one"
57 exec_git symbolic-ref HEAD refs/heads/$TARGET_BRANCH
58 rm .git/index
59
60 # Preserve ignored files, but make sure they're actually ignored!
61 if [[ -e $DOCS_PATH/.gitignore ]]; then
62 cp $DOCS_PATH/.gitignore .gitignore
63 exec_git add .gitignore
64 fi
65
66 exec_git clean -fdq
67 else
68 echo "No local branch '$TARGET_BRANCH', checking out 'origin/$TARGET_BRANCH' and tracking that"
69 exec_git checkout -b $TARGET_BRANCH $TARGET_REMOTE/$TARGET_BRANCH
70 fi
71
72else
73 exec_git checkout $TARGET_BRANCH
74fi
75
76# We want to keep in complete sync (deleting old docs, or cruft from previous documentation output)
77exec_git ls-files | xargs rm
78
79cp -Rf $DOCS_PATH/* .
80if [[ -e $DOCS_PATH/.gitignore ]]; then
81 cp $DOCS_PATH/.gitignore .gitignore
82fi
83
84# Do nothing unless we actually have changes
85if [[ `git status -s` != "" ]]; then
86 exec_git add -A
87 exec_git commit -m "Generated documentation for $CURRENT_COMMIT"
88 exec_git push origin $TARGET_BRANCH
89fi
90
91# Clean up after ourselves
92rm -rf $DOCS_PATH
93
94exec_git checkout $CURRENT_BRANCH