auto-coder-web 0.1.84__py3-none-any.whl → 0.1.85__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -328,7 +328,8 @@ async def get_file_diff(
328
328
  return {
329
329
  "before_content": "", # 初始提交前没有内容
330
330
  "after_content": file_content,
331
- "diff_content": repo.git.show(f"{commit.hexsha} -- {file_path}")
331
+ "diff_content": repo.git.show(f"{commit.hexsha} -- {file_path}"),
332
+ "file_status": "added"
332
333
  }
333
334
  else:
334
335
  raise HTTPException(status_code=404, detail=f"File {file_path} not found in commit {commit_hash}")
@@ -336,51 +337,61 @@ async def get_file_diff(
336
337
  # 获取父提交
337
338
  parent = commit.parents[0]
338
339
 
339
- # 检查文件是否存在于当前提交
340
- file_in_commit = False
341
- try:
342
- after_content = repo.git.show(f"{commit.hexsha}:{file_path}")
343
- file_in_commit = True
344
- except git.GitCommandError:
345
- after_content = "" # 文件在当前提交中不存在(可能被删除)
346
-
347
- # 检查文件是否存在于父提交
348
- file_in_parent = False
349
- try:
350
- before_content = repo.git.show(f"{parent.hexsha}:{file_path}")
351
- file_in_parent = True
352
- except git.GitCommandError:
353
- before_content = "" # 文件在父提交中不存在(新增文件)
354
-
355
- # 获取文件差异
356
- try:
357
- diff_content = repo.git.diff(f"{parent.hexsha}..{commit.hexsha}", "--", file_path)
358
- except git.GitCommandError:
359
- # 如果无法直接获取差异,可能是重命名或其他特殊情况
360
- diff_content = ""
361
-
362
- # 尝试查找可能的重命名
363
- diff_index = parent.diff(commit)
364
- for diff_item in diff_index:
365
- if diff_item.renamed:
366
- if diff_item.b_path == file_path: # 重命名后的路径匹配
340
+ # 获取提交的差异索引
341
+ diff_index = parent.diff(commit)
342
+
343
+ # 初始化变量
344
+ before_content = ""
345
+ after_content = ""
346
+ diff_content = ""
347
+ file_status = "unknown"
348
+ found_file = False
349
+
350
+ # 查找匹配的文件差异
351
+ for diff_item in diff_index:
352
+ # 检查文件路径是否匹配当前或重命名后的文件
353
+ if diff_item.a_path == file_path or diff_item.b_path == file_path:
354
+ found_file = True
355
+ # 根据diff_item确定文件状态
356
+ file_status = get_file_status_from_diff(diff_item)
357
+
358
+ # 根据文件状态获取内容
359
+ if file_status == "added":
360
+ # 新增文件
361
+ after_content = repo.git.show(f"{commit.hexsha}:{file_path}")
362
+ diff_content = repo.git.diff(f"{parent.hexsha}..{commit.hexsha}", "--", file_path)
363
+ elif file_status == "deleted":
364
+ # 删除文件
365
+ before_content = repo.git.show(f"{parent.hexsha}:{file_path}")
366
+ diff_content = repo.git.diff(f"{parent.hexsha}..{commit.hexsha}", "--", file_path)
367
+ elif file_status == "renamed":
368
+ # 重命名文件
369
+ if diff_item.a_path == file_path:
370
+ # 查询的是原文件名
367
371
  before_content = repo.git.show(f"{parent.hexsha}:{diff_item.a_path}")
368
- diff_content = repo.git.diff(f"{parent.hexsha}..{commit.hexsha}", "--", diff_item.a_path, file_path)
369
- break
370
- elif diff_item.a_path == file_path: # 重命名前的路径匹配
371
372
  after_content = repo.git.show(f"{commit.hexsha}:{diff_item.b_path}")
372
- diff_content = repo.git.diff(f"{parent.hexsha}..{commit.hexsha}", "--", file_path, diff_item.b_path)
373
- break
373
+ else:
374
+ # 查询的是新文件名
375
+ before_content = repo.git.show(f"{parent.hexsha}:{diff_item.a_path}")
376
+ after_content = repo.git.show(f"{commit.hexsha}:{diff_item.b_path}")
377
+ diff_content = repo.git.diff(f"{parent.hexsha}..{commit.hexsha}", "--", diff_item.a_path, diff_item.b_path)
378
+ else:
379
+ # 修改文件
380
+ before_content = repo.git.show(f"{parent.hexsha}:{file_path}")
381
+ after_content = repo.git.show(f"{commit.hexsha}:{file_path}")
382
+ diff_content = repo.git.diff(f"{parent.hexsha}..{commit.hexsha}", "--", file_path)
383
+
384
+ break
374
385
 
375
- # 检查我们是否找到了内容
376
- if not file_in_commit and not file_in_parent:
386
+ # 如果没有找到匹配的文件
387
+ if not found_file:
377
388
  raise HTTPException(status_code=404, detail=f"File {file_path} not found in commit {commit_hash} or its parent")
378
389
 
379
390
  return {
380
391
  "before_content": before_content,
381
392
  "after_content": after_content,
382
393
  "diff_content": diff_content,
383
- "file_status": "added" if not file_in_parent else "deleted" if not file_in_commit else "modified"
394
+ "file_status": file_status
384
395
  }
385
396
 
386
397
  except HTTPException:
@@ -393,6 +404,28 @@ async def get_file_diff(
393
404
  )
394
405
 
395
406
 
407
+ def get_file_status_from_diff(diff_item) -> str:
408
+ """
409
+ 根据Git diff对象确定文件变更类型
410
+
411
+ Args:
412
+ diff_item: Git diff对象
413
+
414
+ Returns:
415
+ 文件状态: added(新增), deleted(删除), renamed(重命名), copied(复制) 或 modified(修改)
416
+ """
417
+ if diff_item.new_file:
418
+ return "added"
419
+ elif diff_item.deleted_file:
420
+ return "deleted"
421
+ elif diff_item.renamed:
422
+ return "renamed"
423
+ elif diff_item.copied:
424
+ return "copied"
425
+ else:
426
+ return "modified"
427
+
428
+
396
429
  @router.get("/api/current-changes")
397
430
  async def get_current_changes(
398
431
  limit: int = 3,
auto_coder_web/version.py CHANGED
@@ -1 +1 @@
1
- __version__ = "0.1.84"
1
+ __version__ = "0.1.85"