@startuml git-branch-finish-logic
!theme plain
title 程序逻辑流图：aide flow finish 安全合并

start

:FlowTracker.next_part("finish", summary);

partition "分支合并准备" {
  :从 FlowStatus 获取分支信息;
  note right
    - source_branch
    - start_commit
    - task_branch (当前分支)
  end note

  :记录 end_commit = rev_parse_head();
}

partition "检测源分支变更" {
  :GitIntegration.has_commits_since(start_commit, source_branch);

  if (源分支有新提交?) then (是)
    partition "创建临时分支处理" {
      :从 start_commit 检出临时分支;
      :temp_branch = task_branch + "-merge";
      :git checkout -b temp_branch start_commit;

      :在临时分支执行 squash 合并;
      :git merge --squash task_branch;
      :git commit -m "任务压缩提交";

      :BranchManager.record_branch_finish();
      note right
        status: "merged-to-temp"
        temp_branch: temp_branch
      end note

      :输出警告;
      note right
        "⚠ 源分支有新提交"
        "已在临时分支完成合并"
        "请手动处理后续操作"
      end note
    }
  else (否)
    partition "正常合并流程" {
      :切回源分支;
      :git checkout source_branch;

      :软重置到起始提交;
      :git reset --soft start_commit;

      :创建压缩提交;
      :git add .;
      :git commit -m "[aide] 任务: {summary}";

      :BranchManager.record_branch_finish();
      note right
        status: "finished"
        end_commit: 新提交哈希
      end note
    }
  endif
}

partition "更新分支概况" {
  :BranchManager.save_branches();
  note right: 同时生成 JSON 和 MD
}

:继续原有 finish 逻辑;

stop

@enduml
