Initial commit: OpenClaw workspace setup
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
# Git 自动提交脚本使用说明 (Python 版本)
|
||||
|
||||
## 文件说明
|
||||
|
||||
- `git_auto_commit.py` - Python 自动提交脚本
|
||||
- `git_config.env` - **需要你填写**的认证配置
|
||||
|
||||
## 配置步骤
|
||||
|
||||
1. **编辑配置文件**
|
||||
```
|
||||
# 编辑 scripts/git_config.env
|
||||
GIT_USERNAME=你的用户名
|
||||
GIT_TOKEN=你的Access Token
|
||||
```
|
||||
|
||||
2. **测试脚本**
|
||||
```bash
|
||||
# 直接运行(使用默认目录 C:/ai/openclaw)
|
||||
python scripts/git_auto_commit.py
|
||||
|
||||
# 或指定其他目录
|
||||
python scripts/git_auto_commit.py D:/其他/目录
|
||||
```
|
||||
|
||||
3. **设置定时任务(每晚12点)**
|
||||
|
||||
**Windows (PowerShell 管理员):**
|
||||
```powershell
|
||||
$Action = New-ScheduledTaskAction -Execute "python.exe" -Argument "C:\ai\openclaw\scripts\git_auto_commit.py"
|
||||
$Trigger = New-ScheduledTaskTrigger -Daily -At "00:00"
|
||||
Register-ScheduledTask -TaskName "GitAutoCommit" -Action $Action -Trigger $Trigger
|
||||
```
|
||||
|
||||
**或者使用任务计划程序 GUI:**
|
||||
1. 打开"任务计划程序"
|
||||
2. 创建基本任务
|
||||
3. 触发器: 每天 00:00
|
||||
4. 操作: 启动程序
|
||||
5. 程序: `python` 或 `python.exe`
|
||||
6. 参数: `C:\ai\openclaw\scripts\git_auto_commit.py`
|
||||
|
||||
## 安全提示
|
||||
|
||||
- `git_config.env` 文件包含敏感信息,已添加到 `.gitignore`
|
||||
- **强烈建议**使用 Gitea Access Token 而不是密码
|
||||
- 在 Gitea 中生成 Token: 用户设置 -> 应用 -> 生成令牌
|
||||
|
||||
## 手动运行
|
||||
|
||||
```bash
|
||||
# 使用默认目录 (C:/ai/openclaw)
|
||||
python scripts/git_auto_commit.py
|
||||
|
||||
# 指定目录
|
||||
python scripts/git_auto_commit.py C:/其他/路径
|
||||
```
|
||||
|
||||
## 依赖
|
||||
|
||||
只需要 Python 3.6+,无需额外依赖。
|
||||
@@ -0,0 +1,85 @@
|
||||
#!/bin/bash
|
||||
# git-auto-commit.sh - 自动提交指定目录到 Gitea
|
||||
# 用法: ./git-auto-commit.sh [目录路径]
|
||||
|
||||
set -e
|
||||
|
||||
# 配置
|
||||
REPO_URL="https://gittea.dev/popiskill/skills.git"
|
||||
BRANCH="master"
|
||||
COMMIT_MSG="Auto commit: $(date '+%Y-%m-%d %H:%M:%S')"
|
||||
DEFAULT_SOURCE_DIR="/c/ai/openclaw" # Git Bash 路径格式 (对应 C:\ai\openclaw)
|
||||
|
||||
# 从配置文件读取凭证
|
||||
CONFIG_FILE="$(dirname "$0")/git-config.env"
|
||||
|
||||
if [ ! -f "$CONFIG_FILE" ]; then
|
||||
echo "错误: 配置文件不存在: $CONFIG_FILE"
|
||||
echo "请创建配置文件并设置 GIT_USERNAME 和 GIT_PASSWORD"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
source "$CONFIG_FILE"
|
||||
|
||||
if [ -z "$GIT_USERNAME" ] || [ -z "$GIT_PASSWORD" ]; then
|
||||
echo "错误: 请在配置文件中设置 GIT_USERNAME 和 GIT_PASSWORD"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# 检查参数,使用默认值
|
||||
if [ $# -lt 1 ]; then
|
||||
SOURCE_DIR="$DEFAULT_SOURCE_DIR"
|
||||
echo "未指定目录,使用默认: $SOURCE_DIR"
|
||||
else
|
||||
SOURCE_DIR="$1"
|
||||
fi
|
||||
|
||||
if [ ! -d "$SOURCE_DIR" ]; then
|
||||
echo "错误: 目录不存在: $SOURCE_DIR"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# 创建工作目录
|
||||
WORK_DIR=$(mktemp -d)
|
||||
trap "rm -rf $WORK_DIR" EXIT
|
||||
|
||||
echo "工作目录: $WORK_DIR"
|
||||
echo "源目录: $SOURCE_DIR"
|
||||
|
||||
# 克隆仓库或初始化
|
||||
cd "$WORK_DIR"
|
||||
|
||||
# 构建带凭证的 URL
|
||||
AUTH_URL="https://${GIT_USERNAME}:${GIT_PASSWORD}@${REPO_URL#https://}"
|
||||
|
||||
# 尝试克隆现有仓库
|
||||
if git clone "$AUTH_URL" . 2>/dev/null; then
|
||||
echo "已克隆现有仓库"
|
||||
else
|
||||
echo "初始化新仓库..."
|
||||
git init
|
||||
git remote add origin "$AUTH_URL"
|
||||
fi
|
||||
|
||||
# 复制文件到仓库
|
||||
echo "复制文件..."
|
||||
rsync -av --delete --exclude='.git' --exclude='.openclaw/' "$SOURCE_DIR/" .
|
||||
|
||||
# 配置 git
|
||||
git config user.email "auto@commit.local"
|
||||
git config user.name "Auto Commit"
|
||||
|
||||
# 添加所有更改
|
||||
git add -A
|
||||
|
||||
# 检查是否有更改要提交
|
||||
if git diff --cached --quiet; then
|
||||
echo "没有更改需要提交"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# 提交并推送
|
||||
git commit -m "$COMMIT_MSG"
|
||||
git push origin "$BRANCH"
|
||||
|
||||
echo "✓ 成功提交到 $REPO_URL"
|
||||
@@ -0,0 +1,147 @@
|
||||
#!/usr/bin/env python3
|
||||
# git_auto_commit.py - 自动提交指定目录到 Gitea
|
||||
# 用法: python git_auto_commit.py [目录路径]
|
||||
|
||||
import os
|
||||
import sys
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from urllib.parse import urlparse
|
||||
|
||||
# 配置
|
||||
REPO_URL = "https://gittea.dev/popiskill/skills.git"
|
||||
BRANCH = "master"
|
||||
DEFAULT_SOURCE_DIR = "C:/ai/openclaw" # Windows 路径格式
|
||||
|
||||
# 排除的文件/目录
|
||||
EXCLUDE_PATTERNS = ['.git', '.openclaw']
|
||||
|
||||
def load_config():
|
||||
"""从配置文件读取凭证"""
|
||||
script_dir = Path(__file__).parent
|
||||
config_file = script_dir / "git_config.env"
|
||||
|
||||
if not config_file.exists():
|
||||
print(f"错误: 配置文件不存在: {config_file}")
|
||||
print("请创建配置文件并设置 GIT_USERNAME 和 GIT_TOKEN")
|
||||
sys.exit(1)
|
||||
|
||||
config = {}
|
||||
with open(config_file, 'r', encoding='utf-8') as f:
|
||||
for line in f:
|
||||
line = line.strip()
|
||||
if line and not line.startswith('#') and '=' in line:
|
||||
key, value = line.split('=', 1)
|
||||
config[key.strip()] = value.strip().strip('"\'')
|
||||
|
||||
if not config.get('GIT_USERNAME') or not config.get('GIT_TOKEN'):
|
||||
print("错误: 请在配置文件中设置 GIT_USERNAME 和 GIT_TOKEN")
|
||||
sys.exit(1)
|
||||
|
||||
return config['GIT_USERNAME'], config['GIT_TOKEN']
|
||||
|
||||
def build_auth_url(repo_url, username, token):
|
||||
"""构建带凭证的 URL"""
|
||||
parsed = urlparse(repo_url)
|
||||
return f"{parsed.scheme}://{username}:{token}@{parsed.netloc}{parsed.path}"
|
||||
|
||||
def copy_directory(src, dst, exclude=None):
|
||||
"""复制目录,排除指定文件"""
|
||||
if exclude is None:
|
||||
exclude = []
|
||||
|
||||
src_path = Path(src)
|
||||
dst_path = Path(dst)
|
||||
|
||||
for item in src_path.rglob('*'):
|
||||
# 检查是否在排除列表中
|
||||
rel_path = item.relative_to(src_path)
|
||||
if any(part in exclude for part in rel_path.parts):
|
||||
continue
|
||||
|
||||
if item.is_file():
|
||||
target = dst_path / rel_path
|
||||
target.parent.mkdir(parents=True, exist_ok=True)
|
||||
shutil.copy2(item, target)
|
||||
|
||||
def run_git_command(args, cwd=None, check=True):
|
||||
"""运行 git 命令"""
|
||||
result = subprocess.run(
|
||||
['git'] + args,
|
||||
cwd=cwd,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
encoding='utf-8'
|
||||
)
|
||||
if check and result.returncode != 0:
|
||||
print(f"Git 错误: {result.stderr}")
|
||||
raise subprocess.CalledProcessError(result.returncode, ['git'] + args)
|
||||
return result
|
||||
|
||||
def main():
|
||||
# 获取源目录
|
||||
if len(sys.argv) < 2:
|
||||
source_dir = DEFAULT_SOURCE_DIR
|
||||
print(f"未指定目录,使用默认: {source_dir}")
|
||||
else:
|
||||
source_dir = sys.argv[1]
|
||||
|
||||
source_path = Path(source_dir)
|
||||
if not source_path.exists():
|
||||
print(f"错误: 目录不存在: {source_dir}")
|
||||
sys.exit(1)
|
||||
|
||||
# 加载配置
|
||||
username, token = load_config()
|
||||
|
||||
# 创建临时工作目录
|
||||
work_dir = tempfile.mkdtemp(prefix='git_auto_commit_')
|
||||
print(f"工作目录: {work_dir}")
|
||||
print(f"源目录: {source_dir}")
|
||||
|
||||
try:
|
||||
# 构建带凭证的 URL
|
||||
auth_url = build_auth_url(REPO_URL, username, token)
|
||||
|
||||
# 尝试克隆现有仓库
|
||||
try:
|
||||
run_git_command(['clone', auth_url, '.'], cwd=work_dir)
|
||||
print("已克隆现有仓库")
|
||||
except subprocess.CalledProcessError:
|
||||
print("初始化新仓库...")
|
||||
run_git_command(['init'], cwd=work_dir)
|
||||
run_git_command(['remote', 'add', 'origin', auth_url], cwd=work_dir)
|
||||
|
||||
# 复制文件到仓库
|
||||
print("复制文件...")
|
||||
copy_directory(source_dir, work_dir, exclude=EXCLUDE_PATTERNS)
|
||||
|
||||
# 配置 git
|
||||
run_git_command(['config', 'user.email', 'auto@commit.local'], cwd=work_dir)
|
||||
run_git_command(['config', 'user.name', 'Auto Commit'], cwd=work_dir)
|
||||
|
||||
# 添加所有更改
|
||||
run_git_command(['add', '-A'], cwd=work_dir)
|
||||
|
||||
# 检查是否有更改要提交
|
||||
status_result = run_git_command(['diff', '--cached', '--quiet'], cwd=work_dir, check=False)
|
||||
if status_result.returncode == 0:
|
||||
print("没有更改需要提交")
|
||||
return
|
||||
|
||||
# 提交并推送
|
||||
commit_msg = f"Auto commit: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}"
|
||||
run_git_command(['commit', '-m', commit_msg], cwd=work_dir)
|
||||
run_git_command(['push', 'origin', BRANCH], cwd=work_dir)
|
||||
|
||||
print(f"✓ 成功提交到 {REPO_URL}")
|
||||
|
||||
finally:
|
||||
# 清理临时目录
|
||||
shutil.rmtree(work_dir, ignore_errors=True)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,9 @@
|
||||
# Gitea 认证配置
|
||||
# 请填写你的 Gitea 凭据
|
||||
|
||||
# Gitea 用户名
|
||||
GIT_USERNAME=your_username
|
||||
|
||||
# Gitea Access Token (推荐) 或密码
|
||||
# 在 Gitea 生成 Token: 用户设置 -> 应用 -> 生成令牌
|
||||
GIT_TOKEN=your_token_or_password
|
||||
Reference in New Issue
Block a user