-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgit-new
More file actions
executable file
·70 lines (59 loc) · 1.66 KB
/
git-new
File metadata and controls
executable file
·70 lines (59 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/bin/bash
#
# Create a new git repository, initialised "properly"
#-----------------------------------------------------------------------------
usage() {
printf 'Usage: %s [owner/]<repo-name>\n' "${0##*/}"
}
#-----------------------------------------------------------------------------
main() {
set -e
if (($# != 1)); then
printf 'Missing repo name\n' >&2
usage >&2
exit 1
fi
new::read_config
new::create_repo "$1"
new::create_remote_repo "$1"
}
#-----------------------------------------------------------------------------
# Config value taken from git-config(1) if present
use_github=false
github_owner=''
new::read_config() {
new::_get_config use_github use-github
new::_get_config github_owner github-owner
}
new::create_repo() {
local repo="${1##*/}"
git init "${repo}"
cd "${repo}"
fmt -72 <<EOM | git commit --allow-empty -F -
🌱 Initialise Repository
Create an empty commit to initialise the repository so that rebase and
similar commands operate consistently with a parent commit.
EOM
}
new::create_remote_repo() {
local owner="${1%%/*}"
local repo="${1##*/}"
[[ "${use_github}" == true ]] || return 0
if [[ -n "${owner}" ]]; then
github_owner="${owner}"
elif [[ -z "${github_owner}" ]]; then
github_owner="$(hub api https://api.github.com/user | jq -r .login)"
fi
hub create "${github_owner}/${repo}"
git remote set-url origin "https://github.com/${github_owner}/${repo}"
git push -u
}
new::_get_config() {
local var="$1" name="$2"
local val
if val=$(git config --get new."${name}"); then
eval "${var}='${val}'"
fi
}
#-----------------------------------------------------------------------------
[[ "$(caller)" != 0\ * ]] || main "$@"