Skip to content

Commit 743da3b

Browse files
authored
api-diff can take two commits (#2080)
api-diff can take two commits
1 parent 7596d63 commit 743da3b

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

tools/deno/api-diff.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
*
88
* Copyright Oxide Computer Company
99
*/
10-
import * as flags from 'https://deno.land/std@0.208.0/flags/mod.ts'
1110
import { exists } from 'https://deno.land/std@0.208.0/fs/mod.ts'
11+
import { parseArgs } from 'https://deno.land/std@0.220.1/cli/mod.ts'
1212
import { $ } from 'https://deno.land/x/dax@0.39.1/mod.ts'
1313

1414
const HELP = `
@@ -21,17 +21,19 @@ Requirements:
2121
2222
Usage:
2323
./tools/deno/api-diff.ts [-f] [PR number or commit SHA]
24+
./tools/deno/api-diff.ts [-f] [commit SHA] [commit SHA]
2425
./tools/deno/api-diff.ts -h
2526
2627
Flags:
2728
-f, --force Download spec and regen client even if dir already exists
2829
-h, --help Show this help message
2930
3031
Parameters:
31-
PR number or commit SHA: If left out, interactive picker is shown
32+
PR number or commit SHA: If left out, interactive picker is shown.
33+
If two positional arguments are passed, we assume they are commits.
3234
`.trim()
3335

34-
function printHelpAndExit() {
36+
function printHelpAndExit(): never {
3537
console.log(HELP)
3638
Deno.exit()
3739
}
@@ -54,10 +56,16 @@ async function pickPr() {
5456
}
5557

5658
async function getCommitRange(
57-
arg: string | number | undefined
59+
args: Array<string | number>
5860
): Promise<{ base: string; head: string }> {
59-
if (!arg || typeof arg === 'number') {
60-
const prNum = arg || (await pickPr())
61+
// if there are two or more args, assume two commits
62+
if (args.length >= 2) {
63+
return { base: args[0].toString(), head: args[1].toString() }
64+
}
65+
66+
// if there are no args or the arg is a number, we're talking about a PR
67+
if (args.length === 0 || typeof args[0] === 'number') {
68+
const prNum = args[0] || (await pickPr())
6169
const query = `{
6270
repository(owner: "oxidecomputer", name: "omicron") {
6371
pullRequest(number: ${prNum}) {
@@ -72,10 +80,11 @@ async function getCommitRange(
7280
}
7381

7482
// otherwise assume it's a commit
83+
const head = args[0]
7584
const parents =
76-
await $`gh api repos/oxidecomputer/omicron/commits/${arg} --jq '.parents'`.json()
85+
await $`gh api repos/oxidecomputer/omicron/commits/${head} --jq '.parents'`.json()
7786
if (parents.length > 1) throw new Error(`Commit has multiple parents:`)
78-
return { base: parents[0].sha, head: arg }
87+
return { base: parents[0].sha, head }
7988
}
8089

8190
async function genForCommit(commit: string, force: boolean) {
@@ -102,14 +111,14 @@ if (!$.commandExistsSync('gh')) throw Error('Need gh (GitHub CLI)')
102111
// prefer difftastic if it exists. https://difftastic.wilfred.me.uk/
103112
const diffTool = $.commandExistsSync('difft') ? 'difft' : 'diff'
104113

105-
const args = flags.parse(Deno.args, {
114+
const args = parseArgs(Deno.args, {
106115
alias: { force: 'f', help: 'h' },
107116
boolean: ['force', 'help'],
108117
})
109118

110119
if (args.help) printHelpAndExit()
111120

112-
const { base, head } = await getCommitRange(args._[0])
121+
const { base, head } = await getCommitRange(args._)
113122

114123
const tmpDirBase = await genForCommit(base, args.force)
115124
const tmpDirHead = await genForCommit(head, args.force)

0 commit comments

Comments
 (0)