Send patches - preferably formatted by git format-patch - to patches at archlinux32 dot org.
summaryrefslogtreecommitdiff
path: root/src/lib/repo/create.sh
blob: 31b46e12f2e569b2627544a6b39cc4d2c26a952b (plain)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/bin/bash
#
# SPDX-License-Identifier: GPL-3.0-or-later

[[ -z ${DEVTOOLS_INCLUDE_REPO_CREATE_SH:-} ]] || return 0
DEVTOOLS_INCLUDE_REPO_CREATE_SH=1

_DEVTOOLS_LIBRARY_DIR=${_DEVTOOLS_LIBRARY_DIR:-@pkgdatadir@}
# shellcheck source=src/lib/common.sh
source "${_DEVTOOLS_LIBRARY_DIR}"/lib/common.sh
# shellcheck source=src/lib/api/gitlab.sh
source "${_DEVTOOLS_LIBRARY_DIR}"/lib/api/gitlab.sh
# shellcheck source=src/lib/repo/clone.sh
source "${_DEVTOOLS_LIBRARY_DIR}"/lib/repo/clone.sh
# shellcheck source=src/lib/repo/configure.sh
source "${_DEVTOOLS_LIBRARY_DIR}"/lib/repo/configure.sh

set -e


pkgctl_repo_create_usage() {
	local -r COMMAND=${_DEVTOOLS_COMMAND:-${BASH_SOURCE[0]##*/}}
	cat <<- _EOF_
		Usage: ${COMMAND} [OPTIONS] [PKGBASE]...

		Create a new Git packaging repository in the canonical GitLab namespace.

		This command requires a valid GitLab API authentication. To setup a new
		GitLab token or check the currently configured one please consult the
		'auth' subcommand for further instructions.

		If invoked without a parameter, try to create a packaging repository
		based on the PKGBUILD from the current working directory and configure
		the local repository afterwards.

		OPTIONS
		    -c, --clone   Clone the Git repository after creation
		    -h, --help    Show this help text

		EXAMPLES
		    $ ${COMMAND} libfoo
_EOF_
}

pkgctl_repo_create() {
	# options
	local pkgbases=()
	local pkgbase
	local clone=0
	local configure=0

	# variables
	local path

	while (( $# )); do
		case $1 in
			-h|--help)
				pkgctl_repo_create_usage
				exit 0
				;;
			-c|--clone)
				clone=1
				shift
				;;
			-*)
				die "invalid argument: %s" "$1"
				;;
			*)
				pkgbases=("$@")
				break
				;;
		esac
	done

	# check if invoked without any path from within a packaging repo
	if (( ${#pkgbases[@]} == 0 )); then
		if [[ -f PKGBUILD ]]; then
			if ! path=$(realpath -e .); then
				die "failed to read path from current directory"
			fi
			pkgbases=("$(basename "${path}")")
			clone=0
			configure=1
		else
			pkgctl_repo_create_usage
			exit 1
		fi
	fi

	# create projects
	for pkgbase in "${pkgbases[@]}"; do
		if ! gitlab_api_create_project "${pkgbase}" >/dev/null; then
			die "failed to create project: ${pkgbase}"
		fi
		msg_success "Successfully created ${pkgbase}"
		if (( clone )); then
			pkgctl_repo_clone "${pkgbase}"
		elif (( configure )); then
			pkgctl_repo_configure
		fi
	done

	# some convenience hints if not in auto clone/configure mode
	if (( ! clone )) && (( ! configure )); then
		cat <<- _EOF_

		For new clones:
			$(msg2 "pkgctl repo clone ${pkgbases[*]}")
		For existing clones:
			$(msg2 "pkgctl repo configure ${pkgbases[*]}")
		_EOF_
	fi
}