105 lines
2.7 KiB
Markdown
105 lines
2.7 KiB
Markdown
---
|
|
description: Create a new Git repository on Gitea
|
|
---
|
|
|
|
Create a new Git repository on Gitea via API.
|
|
|
|
## 工作目录
|
|
|
|
**macOS / Linux:**
|
|
```
|
|
~/.config/gitea/
|
|
```
|
|
|
|
**Windows:**
|
|
```
|
|
%USERPROFILE%\.config\gitea\
|
|
```
|
|
|
|
配置文件从该目录加载。
|
|
|
|
**User input format:**
|
|
```
|
|
$ARGUMENTS = [<owner>/]<repo> [private|public]
|
|
```
|
|
|
|
**Examples:**
|
|
- `my-project` - Private repo under default org or current user
|
|
- `ai/my-project` - Private repo under ai organization
|
|
- `ai/my-project public` - Public repo under ai organization
|
|
- `username/test private` - Private repo under username
|
|
|
|
Please perform the following:
|
|
|
|
1. **Load configuration:**
|
|
|
|
**macOS / Linux:**
|
|
```bash
|
|
source ~/.config/gitea/config.env
|
|
```
|
|
|
|
**Windows PowerShell:**
|
|
```powershell
|
|
Get-Content "$env:USERPROFILE\.config\gitea\config.env" | ForEach-Object {
|
|
if ($_ -match '^([^=]+)=(.*)$') {
|
|
[Environment]::SetEnvironmentVariable($matches[1], $matches[2], 'Process')
|
|
}
|
|
}
|
|
```
|
|
|
|
- If not exists: prompt to run `/gitea-reset`
|
|
|
|
2. **Parse user input from `$ARGUMENTS`:**
|
|
- Extract: owner (optional), repo (required), visibility (optional, default: private)
|
|
- If no owner specified:
|
|
- Use `GITEA_DEFAULT_ORG` if set
|
|
- Otherwise get current user from API: `GET /api/v1/user`
|
|
- Validate repo name: only letters, numbers, underscores, hyphens, dots
|
|
|
|
3. **Create repository via API:**
|
|
- Try organization API first:
|
|
```bash
|
|
POST ${GITEA_URL}/api/v1/orgs/${owner}/repos
|
|
Body: {
|
|
"name": "${repo}",
|
|
"private": true/false,
|
|
"auto_init": false,
|
|
"default_branch": "main"
|
|
}
|
|
```
|
|
- If 404, try user API: `POST /api/v1/user/repos`
|
|
- Handle response codes:
|
|
- 201: Success
|
|
- 409: Repository already exists
|
|
- 404: Owner not found or no permission
|
|
- Other: API error
|
|
|
|
4. **Extract repository info from response:**
|
|
- `html_url` - Web URL
|
|
- `clone_url` - HTTPS URL
|
|
- `ssh_url` - SSH URL
|
|
|
|
5. **Ask user if they want to add remote:**
|
|
- Check if current directory is a git repo
|
|
- If not, ask to initialize: `git init`
|
|
- Check if `origin` remote exists
|
|
- Add or update remote: `git remote add/set-url origin <clone_url>`
|
|
|
|
6. **Display result in Chinese:**
|
|
```
|
|
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
仓库创建成功
|
|
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
|
|
|
名称: [owner]/[repo]
|
|
可见性: [private/public]
|
|
Web URL: [html_url]
|
|
HTTPS URL: [clone_url]
|
|
SSH URL: [ssh_url]
|
|
```
|
|
|
|
**Notes:**
|
|
- Requires repo creation permission in token
|
|
- Organization and user repositories use different API endpoints
|
|
- Default branch is `main` (not `master`)
|