Explorer

definy.mcp-merge-branch

description
The definy_merge_branch MCP tool: merge the latest snapshot of branch into the branch into (default main) of the project with a three-way merge of the parts (definy.merge-parts) from their merge base, and take a snapshot with merged_commit_hash. Only the creator of into can take snapshots; the snapshot is type-checked
type
Fn(arguments: @definy.json, request: @http.request) -> @definy.service-io
expression
fn(arguments$0, request$1) =>
  let project$2 = match @definy.json-get-string(value: $0, key: "project") {
      #some(text$90) =>
        $90,
      #none(_) =>
        ""
    };
  let branch$3 = match @definy.json-get-string(value: $0, key: "branch") {
      #some(text$91) =>
        $91,
      #none(_) =>
        ""
    };
  let into$4 = match @definy.json-get-string(value: $0, key: "into") {
      #some(text$92) =>
        $92,
      #none(_) =>
        "main"
    };
  let message$5 = match @definy.json-get-string(value: $0, key: "message") {
      #some(text$93) =>
        $93,
      #none(_) =>
        @core.string-join(items: ["Merge ", $3, " into ", $4], separator: "")
    };
  match @core.or(left: @core.equal(left: $2, right: ""), right: @core.equal(left: $3, right: "")) {
    true =>
      @definy.mcp-text-error(text: "project and branch are required"),
    false =>
      @definy.mcp-with-account(
        then: fn(account$6) =>
          @definy.read-branch-snapshot(
            project: $2,
            branch: $4,
            then: fn(commit$7, commits$8) =>
              @definy.read-branch-snapshot(
                project: $2,
                branch: $3,
                then: fn(commit$9, commits$10) =>
                  match $7 {
                    #none(_) =>
                      @definy.mcp-text-error(
                        text: @core.string-join(items: ["Branch ", $4, " of project ", $2, " was not found"], separator: "")
                      ),
                    #some(ours$11) =>
                      match $9 {
                        #none(_) =>
                          @definy.mcp-text-error(
                            text: @core.string-join(items: ["Branch ", $3, " of project ", $2, " was not found"], separator: "")
                          ),
                        #some(theirs$12) =>
                          @definy.merge-ancestors(
                            queue: [$11.event_hash],
                            index: 0,
                            result: [],
                            then: fn(ancestors$13) =>
                              @definy.merge-ancestors(
                                queue: [$12.event_hash],
                                index: 0,
                                result: [],
                                then: fn(ancestors$14) =>
                                  match @definy.merge-contains(items: $13, item: $12.event_hash) {
                                    true =>
                                      @definy.mcp-text(
                                        text: @core.string-join(items: ["Nothing to merge: ", $4, " already contains the latest snapshot of ", $3], separator: "")
                                      ),
                                    false =>
                                      match @definy.merge-first-common(ours: $13, theirs: $14) {
                                        #none(_) =>
                                          @definy.mcp-text-error(text: "The branches have no common snapshot"),
                                        #some(base_hash$15) =>
                                          @definy.read-snapshot-content(
                                            event_hash: $15,
                                            then: fn(commit$16) =>
                                              @definy.read-snapshot-content(
                                                event_hash: $11.event_hash,
                                                then: fn(commit$17) =>
                                                  @definy.read-snapshot-content(
                                                    event_hash: $12.event_hash,
                                                    then: fn(commit$18) =>
                                                      match $16 {
                                                        #none(_) =>
                                                          @definy.mcp-text-error(text: "The merge base could not be read"),
                                                        #some(base$19) =>
                                                          match $17 {
                                                            #none(_) =>
                                                              @definy.mcp-text-error(text: "The latest snapshot of the branch could not be read"),
                                                            #some(ours$20) =>
                                                              match $18 {
                                                                #none(_) =>
                                                                  @definy.mcp-text-error(text: "The latest snapshot of the merged branch could not be read"),
                                                                #some(theirs$21) =>
                                                                  let merged$22 = @definy.merge-parts(
                                                                      base: @definy.merge-flatten(modules: $19.modules),
                                                                      ours: @definy.merge-flatten(modules: $20.modules),
                                                                      theirs: @definy.merge-flatten(modules: $21.modules)
                                                                    );
                                                                  @definy.mcp-submit-content(
                                                                    account: $6,
                                                                    after_time_ms: @std.max(a: $11.commit.time_ms, b: $12.commit.time_ms),
                                                                    content: #project_commit<@core.event-content>({
                                                                      project_name: $20.project_name,
                                                                      branch: $20.branch,
                                                                      project_description: $20.project_description,
                                                                      message: $5,
                                                                      parent_commit_hash: #some($11.event_hash),
                                                                      merged_commit_hash: #some($12.event_hash),
                                                                      modules: @definy.merge-modules(base: $19.modules, ours: $20.modules, theirs: $21.modules, parts: $22.parts)
                                                                    }),
                                                                    then: fn(accepted$23, text$24) =>
                                                                      match $23 {
                                                                        true =>
                                                                          @definy.mcp-text(
                                                                            text: @core.string-join(
                                                                              items: [
                                                                                "Merged ",
                                                                                $3,
                                                                                " into ",
                                                                                $4,
                                                                                " of project ",
                                                                                $2,
                                                                                match @core.equal(left: @core.list-length(value: $22.conflicts), right: 0) {
                                                                                  true =>
                                                                                    "",
                                                                                  false =>
                                                                                    @core.string-join(
                                                                                      items: [
                                                                                        "\nBoth branches changed these parts differently; the merged branch's version is used: ",
                                                                                        @core.string-join(items: $22.conflicts, separator: ", ")
                                                                                      ],
                                                                                      separator: ""
                                                                                    )
                                                                                },
                                                                                "\n",
                                                                                $24
                                                                              ],
                                                                              separator: ""
                                                                            )
                                                                          ),
                                                                        false =>
                                                                          @definy.mcp-text-error(text: $24)
                                                                      }
                                                                  )
                                                              }
                                                          }
                                                      }
                                                  )
                                              )
                                          )
                                      }
                                  }
                              )
                          )
                      }
                  }
              )
          )
      )
  }
visibility
public
definy.mcp-merge-branch (project definy, branch main, snapshot l0VFjJZP174d8CbZWFoujAGTYjgsQu4uTq5uWCZaXgo)
id: o0BjT42WxgV9pBd-p0tDgtufBB1fb7YtXxMTQSmafxQ
content hash: bvcBn4U3Pn3Kle7qsETLHXBCsHWiKfCaCJAUv9L3xbQ