Browse code

Add implementation

Robert Cranston authored on 26/05/2022 19:31:46
Showing 1 changed files

... ...
@@ -105,3 +105,109 @@ esac
105 105
 user="${GIT_GITOLITE_USER:-"$USER"}"
106 106
 host="${GIT_GITOLITE_HOST:-"git.$(dnsdomainname)"}"
107 107
 remote="${GIT_GITOLITE_REMOTE:-"origin"}"
108
+
109
+## ssh
110
+ssh()
111
+{
112
+  command ssh -T "git@$host" "$@"
113
+}
114
+
115
+## Repository-independent commands
116
+case "$command" in
117
+  'info'|'help') ssh "$command" "$@"; exit; ;;
118
+esac
119
+
120
+## Helpers
121
+toplevel="$(git rev-parse --show-toplevel)"
122
+project="$(basename "$toplevel")"
123
+repo="$user/$project"
124
+url="https://$host/$repo"
125
+remote_url="$(git remote get-url "$remote" 2> /dev/null || true)"
126
+remote_repo="$(printf '%s\n' "$remote_url" | sed 's#^\(.\+://\)\?[^/]\+/##')"
127
+head="$(git rev-parse --abbrev-ref HEAD 2> /dev/null || true)"
128
+licenses='https://github.com/github/choosealicense.com/raw/gh-pages/_licenses'
129
+
130
+remote()
131
+{
132
+  git remote remove "$remote" 2> /dev/null || true
133
+  git remote add    "$remote" "$url"
134
+}
135
+
136
+commit()
137
+{
138
+  message="$1"; shift;
139
+  file="$1"; shift;
140
+  printf '%s\n' "$@" >> "$file"
141
+  eval "$(git var GIT_EDITOR)" '"$file"'
142
+  git add "$file"
143
+  git commit -m "$message"
144
+}
145
+
146
+fmt()
147
+{
148
+  printf '%s\n' "$@" \
149
+  | command fmt --width=79 --goal=79
150
+}
151
+
152
+## Repository initialization commands
153
+[ "$remote_url" = "" ] || [ "$remote_url" = "$url" ] || \
154
+  warning "remote '$remote_url' does not match '$url'"
155
+case "$command" in
156
+  'init')
157
+    # Readme.
158
+    commit 'Add readme' "$toplevel/README.md" \
159
+      "# [\`$project\`][]" \
160
+      '' \
161
+      ${description:+"$description"} \
162
+      ${description:+""} \
163
+      "[\`$project\`]: $url" \
164
+      ${references:+"$references"}
165
+    # License.
166
+    license_raw="$(curl -sL "$licenses/$license.txt")"
167
+    license_title="$(
168
+      printf '%s' "$license_raw" \
169
+      | awk -F': ' '/^title:/{print $2; exit; }'
170
+    )"
171
+    license_body="$(
172
+      printf '%s' "$license_raw" \
173
+      | awk 's>=2&&!/^$/{b++};b;/^---$/{s++}' \
174
+      | sed \
175
+        -e 's/\[year\]/'"$(date +%Y)"'/' \
176
+        -e 's/\[fullname\]/'"$(git config user.name)"'/' \
177
+    )"
178
+    printf '%s\n' "$license_body" >| "$toplevel/LICENSE"
179
+    git add "$toplevel/LICENSE"
180
+    commit 'Add license' "$toplevel/README.md" \
181
+      '' \
182
+      '## License' \
183
+      '' \
184
+      "$(fmt \
185
+        "Licensed under the [$license_title][] unless otherwise noted, see" \
186
+        'the [`LICENSE`][] file.' \
187
+      )" \
188
+      '' \
189
+      "[$license_title]: https://choosealicense.com/licenses/$license" \
190
+      '[`LICENSE`]: LICENSE'
191
+    # Remote.
192
+    remote
193
+    ssh create "$repo"
194
+    git push
195
+    exit
196
+    ;;
197
+  'remote')
198
+    remote
199
+    exit
200
+    ;;
201
+esac
202
+
203
+## Repository-dependent commands
204
+[ "$remote_repo" ] || \
205
+  error "remote repo for '$remote' is not defined (run 'git-gitolite init'?)"
206
+case "$command" in
207
+  'head')      ssh symbolic-ref "$remote_repo" HEAD "refs/heads/$head"; ;;
208
+  'publish')   ssh perms        "$remote_repo" + READERS 'gitweb';      ;;
209
+  'unpublish') ssh perms        "$remote_repo" - READERS 'gitweb';      ;;
210
+  'rm')        ssh D unlock     "$remote_repo";
211
+               ssh D rm         "$remote_repo";                         ;;
212
+  *)           ssh "$command"   "$remote_repo" "$@";                    ;;
213
+esac