docs: 重构命令和技能文档体系,规范化文档格式和内容组织

This commit is contained in:
2026-01-13 10:25:18 +08:00
parent 5a05d5ab53
commit f31f198407
19 changed files with 1055 additions and 2342 deletions

View File

@@ -2,255 +2,103 @@
description: Create a new Git repository on Gitea
---
# gitea-create-repo
Create a new Git repository on Gitea via API.
Create a new Git repository on Gitea.
## Features
- Create new repository under specified organization or user via Gitea API
- Support creating private or public repositories
- Automatically add remote repository address after successful creation
## User Input Format
User can specify parameters in the following format:
## 工作目录
**macOS / Linux:**
```
[<owner>/]<repo> [private|public]
~/.config/gitea/
```
- `owner`: Organization name or username (optional, uses `GITEA_DEFAULT_ORG` if set, otherwise uses current user)
- `repo`: Repository name (required)
- `private|public`: Visibility (optional, default `private`)
**Windows:**
```
%USERPROFILE%\.config\gitea\
```
**Examples**:
- `my-project` - Create private repository under default organization (or current user if not set)
- `ai/my-project` - Create private repository my-project under ai organization
- `ai/my-project public` - Create public repository my-project under ai organization
- `username/test private` - Create private repository test under username user
配置文件从该目录加载。
## Configuration
**User input format:**
```
$ARGUMENTS = [<owner>/]<repo> [private|public]
```
Load configuration from `~/.config/gitea/config.env`:
**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
| Config Item | Source |
| --- | --- |
| Gitea Server URL | `GITEA_URL` from config.env |
| API Token | `GITEA_TOKEN` from config.env |
| Default Organization | `GITEA_DEFAULT_ORG` from config.env (optional) |
Please perform the following:
## Steps
### 0. Load Configuration
Before executing, load Gitea configuration:
1. **Load configuration:**
**macOS / Linux:**
```bash
config_file="$HOME/.config/gitea/config.env"
if [ ! -f "$config_file" ]; then
echo "❌ Gitea 未配置,请运行 /gitea-reset 进行初始化"
exit 1
fi
source "$config_file"
# Validate required variables
if [ -z "$GITEA_URL" ] || [ -z "$GITEA_TOKEN" ]; then
echo "❌ 配置文件不完整,请运行 /gitea-reset 重新配置"
exit 1
fi
source ~/.config/gitea/config.env
```
### 1. Parse User Input
Parse from user input:
- `owner`: Organization name or username
- `repo`: Repository name
- `visibility`: `private` (default) or `public`
**Input parsing logic**:
```bash
input="$1"
visibility="${2:-private}"
# Parse owner/repo
if [[ "$input" =~ / ]]; then
owner=$(echo "$input" | cut -d'/' -f1)
repo=$(echo "$input" | cut -d'/' -f2)
else
# Use default organization if available, otherwise use current user
if [ -z "$GITEA_DEFAULT_ORG" ]; then
# Get current user from Gitea API
owner=$(curl -s -H "Authorization: token $GITEA_TOKEN" \
"${GITEA_URL}/api/v1/user" | jq -r '.login')
if [ -z "$owner" ] || [ "$owner" = "null" ]; then
echo "❌ 无法获取当前用户信息,请使用 owner/repo 格式"
exit 1
fi
echo "使用当前用户: $owner"
else
owner="$GITEA_DEFAULT_ORG"
echo "使用默认组织: $owner"
fi
repo="$input"
fi
# Convert visibility to boolean
private_bool=$([ "$visibility" = "private" ] && echo "true" || echo "false")
**Windows PowerShell:**
```powershell
Get-Content "$env:USERPROFILE\.config\gitea\config.env" | ForEach-Object {
if ($_ -match '^([^=]+)=(.*)$') {
[Environment]::SetEnvironmentVariable($matches[1], $matches[2], 'Process')
}
}
```
**Input validation**:
- Repository name can only contain letters, numbers, underscores, hyphens and dots
- If not exists: prompt to run `/gitea-reset`
```bash
if ! [[ "$repo" =~ ^[a-zA-Z0-9_.-]+$ ]]; then
echo "❌ 仓库名只能包含字母、数字、下划线、连字符和点"
exit 1
fi
```
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
### 2. Call Gitea API to Create Repository
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
Use curl to call Gitea API with configuration:
4. **Extract repository info from response:**
- `html_url` - Web URL
- `clone_url` - HTTPS URL
- `ssh_url` - SSH URL
```bash
echo "正在创建仓库: $owner/$repo ($visibility)"
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>`
# Try organization repository first
response=$(curl -s -w "\n%{http_code}" -X POST \
-H "Authorization: token $GITEA_TOKEN" \
-H "Content-Type: application/json" \
-d "{
\"name\": \"${repo}\",
\"private\": ${private_bool},
\"auto_init\": false,
\"default_branch\": \"main\"
}" \
"${GITEA_URL}/api/v1/orgs/${owner}/repos")
http_code=$(echo "$response" | tail -n1)
body=$(echo "$response" | sed '$d')
# If 404, try user repository
if [ "$http_code" = "404" ]; then
echo "⚠️ 组织不存在,尝试创建用户仓库..."
response=$(curl -s -w "\n%{http_code}" -X POST \
-H "Authorization: token $GITEA_TOKEN" \
-H "Content-Type: application/json" \
-d "{
\"name\": \"${repo}\",
\"private\": ${private_bool},
\"auto_init\": false,
\"default_branch\": \"main\"
}" \
"${GITEA_URL}/api/v1/user/repos")
http_code=$(echo "$response" | tail -n1)
body=$(echo "$response" | sed '$d')
fi
```
> Note: First try organization API, if 404 then try user API.
### 3. Handle Response
```bash
case "$http_code" in
201)
echo "✓ 仓库创建成功"
;;
409)
echo "❌ 仓库已存在: $owner/$repo"
echo "查看仓库: ${GITEA_URL}/$owner/$repo"
exit 1
;;
404)
echo "❌ Owner 不存在或无权限: $owner"
exit 1
;;
*)
echo "❌ 创建失败 (HTTP $http_code)"
echo "$body" | jq -r '.message // empty'
exit 1
;;
esac
# Extract repository info
html_url=$(echo "$body" | jq -r '.html_url')
clone_url=$(echo "$body" | jq -r '.clone_url')
ssh_url=$(echo "$body" | jq -r '.ssh_url')
```
**Response Handling**:
- **HTTP 201**: Repository created successfully
- **HTTP 409**: Repository already exists
- **HTTP 404**: Owner does not exist or no permission
- **Other**: API error, display error message
### 4. Ask Whether to Add Remote Repository
Ask user whether to add the newly created repository as current project's remote repository.
**If user confirms**:
1. Check if current directory is a Git repository:
```bash
git rev-parse --is-inside-work-tree
```
2. If not a Git repository, ask whether to initialize:
```bash
git init
```
3. Check if origin remote already exists:
```bash
git remote get-url origin
```
4. Add or update remote repository:
- If no origin: `git remote add origin <clone_url>`
- If origin exists: Ask whether to overwrite, after confirmation execute `git remote set-url origin <clone_url>`
### 5. Output Result Summary
Output creation result summary:
```bash
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "仓库创建成功"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
echo " 名称: $owner/$repo"
echo " 可见性: $visibility"
echo " Web URL: $html_url"
echo " HTTPS URL: $clone_url"
echo " SSH URL: $ssh_url"
echo ""
```
**Example Output**:
6. **Display result in Chinese:**
```
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
仓库创建成功
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
名称: ai/my-project
可见性: private
Web URL: https://git.digitevents.com/ai/my-project
HTTPS URL: https://git.digitevents.com/ai/my-project.git
SSH URL: git@git.digitevents.com:ai/my-project.git
名称: [owner]/[repo]
可见性: [private/public]
Web URL: [html_url]
HTTPS URL: [clone_url]
SSH URL: [ssh_url]
```
## Notes
- **Permission check**: Ensure Token has permission to create repository
- **Organization vs User**: Creating organization repository and user repository use different API endpoints
- **Repository naming**: Follow Gitea naming rules, avoid special characters
**Notes:**
- Requires repo creation permission in token
- Organization and user repositories use different API endpoints
- Default branch is `main` (not `master`)