Compare diffs from where the two branches last agreed, as a pull request would
1 file+6−50/1 viewed
| 45 | 45 | const base = asked.base ?? found.defaultBranch; | |
| 46 | 46 | const head = asked.head; | |
| 47 | 47 | if (!head || head === base) return { base, head, branches, comparison: null, commits: [], same: head === base }; | |
| 48 | − | const [compared, headLog, baseLog] = await Promise.all([ | |
| 49 | − | repos.compare(found.id, viewer, base, head), | |
| 50 | − | repos.log(path, viewer, head, HEAD_DEPTH), | |
| 51 | − | repos.log(path, viewer, base, BASE_DEPTH), | |
| 52 | − | ]); | |
| 48 | + | const [headLog, baseLog] = await Promise.all([repos.log(path, viewer, head, HEAD_DEPTH), repos.log(path, viewer, base, BASE_DEPTH)]); | |
| 53 | 49 | if (!headLog.ok || headLog.value.length === 0) throw new Response(`There is no branch, tag or commit named ${head}.`, { status: 404 }); | |
| 54 | 50 | // What head has that base does not: its history down to the first commit base also has. | |
| 55 | 51 | const onBase = new Set(baseLog.ok ? baseLog.value.map((commit) => commit.hash) : []); | |
| 60 | 56 | author: commit.author.name, | |
| 61 | 57 | at: commit.authoredAt, | |
| 62 | 58 | })); | |
| 59 | + | // The changes from where the two last agreed, as a pull request would | |
| 60 | + | // bring them: what base gained since is not head taking it away. When | |
| 61 | + | // that point is past what was read, the two tips are compared. | |
| 62 | + | const mergeBase = shared === -1 ? base : headLog.value[shared]!.hash; | |
| 63 | + | const compared = await repos.compare(found.id, viewer, mergeBase, head); | |
| 63 | 64 | const comparison: Comparison | null = compared.ok ? compared.value : null; | |
| 64 | 65 | return { base, head, branches, comparison, commits, same: false }; | |
| 65 | 66 | } |