Files
opencode/command/gitea-create-repo.md
Voson 5a05d5ab53 chore: 重构 OpenCode 命令和技能文档体系
- 新增:统一的 git 命令文档(add/commit/push/pull 等)
- 新增:整合的 Gitea 技能文档(API、运行器、工作流等)
- 新增:工作流模板(Android、Go、Node.js 等)
- 移除:已弃用的旧命令脚本和发布脚本
- 改进:.gitignore 添加敏感文件保护规则
- 改进:AGENTS.md 完善了开发规范和示例

此次重组统一了命令和技能的文档结构,便于后续维护和扩展。
2026-01-13 00:27:21 +08:00

6.8 KiB

description
description
Create a new Git repository on Gitea

gitea-create-repo

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:

[<owner>/]<repo> [private|public]
  • 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)

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

Load configuration from ~/.config/gitea/config.env:

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)

Steps

0. Load Configuration

Before executing, load Gitea configuration:

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

1. Parse User Input

Parse from user input:

  • owner: Organization name or username
  • repo: Repository name
  • visibility: private (default) or public

Input parsing logic:

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")

Input validation:

  • Repository name can only contain letters, numbers, underscores, hyphens and dots
if ! [[ "$repo" =~ ^[a-zA-Z0-9_.-]+$ ]]; then
  echo "❌ 仓库名只能包含字母、数字、下划线、连字符和点"
  exit 1
fi

2. Call Gitea API to Create Repository

Use curl to call Gitea API with configuration:

echo "正在创建仓库: $owner/$repo ($visibility)"

# 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

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:

    git rev-parse --is-inside-work-tree
    
  2. If not a Git repository, ask whether to initialize:

    git init
    
  3. Check if origin remote already exists:

    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:

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:

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
仓库创建成功
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

  名称:       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

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