Skip to content

Git 集成 API

本文档描述了 Trae Git 集成功能的 API 接口。

概述

Git 集成 API 提供了版本控制操作,包括提交、分支管理、合并、推送等功能,与 Git 仓库无缝集成。

端点

获取仓库状态

http
GET /api/git/status?project_id={project_id}

响应

json
{
  "branch": "main",
  "commit": "abc123def456",
  "status": "clean",
  "ahead": 2,
  "behind": 0,
  "staged_files": [],
  "modified_files": [
    {
      "path": "src/app.js",
      "status": "modified"
    }
  ],
  "untracked_files": [
    {
      "path": "src/new-file.js",
      "status": "untracked"
    }
  ]
}

获取分支列表

http
GET /api/git/branches?project_id={project_id}

响应

json
{
  "current_branch": "main",
  "branches": [
    {
      "name": "main",
      "commit": "abc123def456",
      "is_current": true,
      "last_commit_date": "2024-01-01T00:00:00Z"
    },
    {
      "name": "feature/new-ui",
      "commit": "def456ghi789",
      "is_current": false,
      "last_commit_date": "2024-01-01T00:00:00Z"
    }
  ]
}

创建分支

http
POST /api/git/branch

请求参数

参数类型必需描述
project_idstring项目 ID
branch_namestring分支名称
source_branchstring源分支,默认为当前分支
checkoutboolean是否切换到新分支

响应

json
{
  "branch_name": "feature/new-feature",
  "commit": "abc123def456",
  "created_at": "2024-01-01T00:00:00Z",
  "checked_out": true
}

切换分支

http
POST /api/git/checkout

请求参数

参数类型必需描述
project_idstring项目 ID
branch_namestring分支名称
create_if_not_existsboolean如果分支不存在是否创建

暂存文件

http
POST /api/git/add

请求参数

参数类型必需描述
project_idstring项目 ID
filesarray文件路径列表
allboolean是否暂存所有修改

响应

json
{
  "staged_files": [
    "src/app.js",
    "src/utils.js"
  ],
  "message": "2 个文件已暂存"
}

提交更改

http
POST /api/git/commit

请求参数

参数类型必需描述
project_idstring项目 ID
messagestring提交信息
authorobject作者信息
amendboolean是否修改上一次提交

响应

json
{
  "commit_hash": "def456ghi789",
  "message": "添加新功能",
  "author": {
    "name": "Alice",
    "email": "alice@example.com"
  },
  "timestamp": "2024-01-01T00:00:00Z",
  "files_changed": 2
}

推送到远程

http
POST /api/git/push

请求参数

参数类型必需描述
project_idstring项目 ID
remotestring远程仓库名,默认 origin
branchstring分支名,默认当前分支
forceboolean是否强制推送

响应

json
{
  "remote": "origin",
  "branch": "main",
  "commits_pushed": 2,
  "status": "success"
}

拉取远程更改

http
POST /api/git/pull

请求参数

参数类型必需描述
project_idstring项目 ID
remotestring远程仓库名
branchstring分支名
rebaseboolean是否使用 rebase

响应

json
{
  "commits_pulled": 3,
  "files_changed": 5,
  "conflicts": [],
  "status": "success"
}

合并分支

http
POST /api/git/merge

请求参数

参数类型必需描述
project_idstring项目 ID
source_branchstring源分支
target_branchstring目标分支,默认当前分支
strategystring合并策略

响应

json
{
  "merge_commit": "ghi789jkl012",
  "source_branch": "feature/new-ui",
  "target_branch": "main",
  "conflicts": [
    {
      "file": "src/app.js",
      "type": "content"
    }
  ],
  "status": "conflict"
}

获取提交历史

http
GET /api/git/log?project_id={project_id}&limit={limit}&offset={offset}

响应

json
{
  "commits": [
    {
      "hash": "abc123def456",
      "message": "修复登录问题",
      "author": {
        "name": "Alice",
        "email": "alice@example.com"
      },
      "timestamp": "2024-01-01T00:00:00Z",
      "files_changed": 3
    }
  ],
  "total": 50
}

获取文件差异

http
GET /api/git/diff?project_id={project_id}&file_path={file_path}&commit1={commit1}&commit2={commit2}

响应

json
{
  "file_path": "src/app.js",
  "commit1": "abc123",
  "commit2": "def456",
  "diff": "@@ -1,3 +1,4 @@\n console.log('Hello');\n+console.log('World');\n",
  "additions": 1,
  "deletions": 0
}

解决冲突

http
POST /api/git/resolve-conflict

请求参数

参数类型必需描述
project_idstring项目 ID
file_pathstring冲突文件路径
resolutionstring解决后的内容

创建标签

http
POST /api/git/tag

请求参数

参数类型必需描述
project_idstring项目 ID
tag_namestring标签名称
messagestring标签信息
commitstring提交哈希

示例

完整的开发流程

javascript
// 1. 创建新功能分支
const createBranchResponse = await fetch('/api/git/branch', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer your-token'
  },
  body: JSON.stringify({
    project_id: 'my-project',
    branch_name: 'feature/user-authentication',
    checkout: true
  })
});

const newBranch = await createBranchResponse.json();
console.log('新分支已创建:', newBranch.branch_name);

// 2. 开发功能...
// 修改文件后,暂存更改
const addResponse = await fetch('/api/git/add', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer your-token'
  },
  body: JSON.stringify({
    project_id: 'my-project',
    files: ['src/auth.js', 'src/login.vue']
  })
});

// 3. 提交更改
const commitResponse = await fetch('/api/git/commit', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer your-token'
  },
  body: JSON.stringify({
    project_id: 'my-project',
    message: '添加用户认证功能',
    author: {
      name: 'Alice',
      email: 'alice@example.com'
    }
  })
});

const commit = await commitResponse.json();
console.log('提交成功:', commit.commit_hash);

// 4. 推送到远程
const pushResponse = await fetch('/api/git/push', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer your-token'
  },
  body: JSON.stringify({
    project_id: 'my-project',
    remote: 'origin',
    branch: 'feature/user-authentication'
  })
});

console.log('推送完成');

处理合并冲突

javascript
// 尝试合并分支
const mergeResponse = await fetch('/api/git/merge', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer your-token'
  },
  body: JSON.stringify({
    project_id: 'my-project',
    source_branch: 'feature/user-authentication',
    target_branch: 'main'
  })
});

const mergeResult = await mergeResponse.json();

if (mergeResult.status === 'conflict') {
  console.log('发现冲突,需要手动解决:');
  
  for (const conflict of mergeResult.conflicts) {
    console.log(`- ${conflict.file}`);
    
    // 获取冲突文件的差异
    const diffResponse = await fetch(
      `/api/git/diff?project_id=my-project&file_path=${conflict.file}`
    );
    const diff = await diffResponse.json();
    
    console.log('冲突内容:', diff.diff);
    
    // 用户解决冲突后,提交解决方案
    const resolvedContent = '// 用户解决冲突后的内容';
    
    await fetch('/api/git/resolve-conflict', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer your-token'
      },
      body: JSON.stringify({
        project_id: 'my-project',
        file_path: conflict.file,
        resolution: resolvedContent
      })
    });
  }
  
  // 完成合并
  await fetch('/api/git/commit', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Authorization': 'Bearer your-token'
    },
    body: JSON.stringify({
      project_id: 'my-project',
      message: '解决合并冲突'
    })
  });
  
  console.log('冲突已解决,合并完成');
} else {
  console.log('合并成功,无冲突');
}

查看项目历史

javascript
// 获取最近的提交历史
const logResponse = await fetch('/api/git/log?project_id=my-project&limit=10');
const history = await logResponse.json();

console.log('最近的提交:');
history.commits.forEach(commit => {
  console.log(`${commit.hash.substring(0, 7)} - ${commit.message}`);
  console.log(`  作者: ${commit.author.name} <${commit.author.email}>`);
  console.log(`  时间: ${new Date(commit.timestamp).toLocaleString()}`);
  console.log('');
});

您的终极 AI 驱动 IDE 学习指南