Skip to content

Commit 48f729f

Browse files
committed
🐛 Fix remote detection with branch names containing slashes
Close #9
1 parent 2adda2b commit 48f729f

File tree

2 files changed

+105
-8
lines changed

2 files changed

+105
-8
lines changed

util.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
import { common } from "https://deno.land/[email protected]/path/mod.ts";
21
import { execute, ExecuteOptions } from "./process.ts";
32

43
export async function getRemoteContains(
54
commitish: string,
65
options: ExecuteOptions = {},
76
): Promise<string | undefined> {
8-
const stdout = await execute(
7+
const branches = (await execute(
98
["branch", "-r", "--contains", commitish, "--format=%(refname:short)"],
109
options,
11-
);
12-
const result = common(stdout.trim().split("\n"));
13-
if (!result) {
14-
return undefined;
15-
}
16-
return result?.substring(0, result.length - 1);
10+
)).trim().split("\n");
11+
const remotes = (await execute(["remote"], options)).trim().split("\n");
12+
const remoteContains = remotes.filter((remote) => {
13+
return branches.some((branch) => branch.startsWith(`${remote}/`));
14+
});
15+
// Prefer "origin" if it exists
16+
return remoteContains.includes("origin") ? "origin" : remoteContains.at(0);
1717
}
1818

1919
export async function getRemoteFetchURL(

util_test.ts

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ Deno.test("getRemoteContains", async (t) => {
1616
"returns undefined if no remote contains the commitish",
1717
async () => {
1818
const executeStub = stub(_internals, "execute", (args, _options) => {
19+
if (args.at(0) === "remote") {
20+
return Promise.resolve("origin\nfork\n");
21+
}
1922
if (args.at(0) === "branch") {
2023
return Promise.resolve("\n");
2124
}
@@ -34,6 +37,9 @@ Deno.test("getRemoteContains", async (t) => {
3437
"returns 'origin' if 'origin/my-awesome-branch' contains the commitish",
3538
async () => {
3639
const executeStub = stub(_internals, "execute", (args, _options) => {
40+
if (args.at(0) === "remote") {
41+
return Promise.resolve("origin\nfork\n");
42+
}
3743
if (args.at(0) === "branch") {
3844
return Promise.resolve("origin/my-awesome-branch\n");
3945
}
@@ -52,11 +58,35 @@ Deno.test("getRemoteContains", async (t) => {
5258
"returns 'origin' if 'origin/HEAD' and 'origin/my-awesome-branch' contains the commitish",
5359
async () => {
5460
const executeStub = stub(_internals, "execute", (args, _options) => {
61+
if (args.at(0) === "remote") {
62+
return Promise.resolve("origin\nfork\n");
63+
}
5564
if (args.at(0) === "branch") {
5665
return Promise.resolve("origin/HEAD\norigin/my-awesome-branch\n");
5766
}
5867
unreachable();
5968
});
69+
try {
70+
const remote = await getRemoteContains("<<commitish>>");
71+
assertEquals(remote, "origin");
72+
} finally {
73+
executeStub.restore();
74+
}
75+
},
76+
);
77+
78+
await t.step(
79+
"returns 'fork' if 'fork/my-awesome-branch' contains the commitish",
80+
async () => {
81+
const executeStub = stub(_internals, "execute", (args, _options) => {
82+
if (args.at(0) === "remote") {
83+
return Promise.resolve("origin\nfork\n");
84+
}
85+
if (args.at(0) === "branch") {
86+
return Promise.resolve("fork/my-awesome-branch\n");
87+
}
88+
unreachable();
89+
});
6090
try {
6191
const remote = await getRemoteContains("<<commitish>>");
6292
assertEquals(remote, "fork");
@@ -65,6 +95,73 @@ Deno.test("getRemoteContains", async (t) => {
6595
}
6696
},
6797
);
98+
99+
await t.step(
100+
"returns 'origin' if 'origin/feature/my-awesome-branch' contains the commitish (#9)",
101+
async () => {
102+
const executeStub = stub(_internals, "execute", (args, _options) => {
103+
if (args.at(0) === "remote") {
104+
return Promise.resolve("origin\nfork\n");
105+
}
106+
if (args.at(0) === "branch") {
107+
return Promise.resolve("origin/feature/my-awesome-branch\n");
108+
}
109+
unreachable();
110+
});
111+
try {
112+
const remote = await getRemoteContains("<<commitish>>");
113+
assertEquals(remote, "origin");
114+
} finally {
115+
executeStub.restore();
116+
}
117+
},
118+
);
119+
120+
await t.step(
121+
"returns 'fork/my-awesome-fork' if 'fork/my-awesome-fork/feature/my-awesome-branch' contains the commitish",
122+
async () => {
123+
const executeStub = stub(_internals, "execute", (args, _options) => {
124+
if (args.at(0) === "remote") {
125+
return Promise.resolve("origin\nfork/my-awesome-fork\n");
126+
}
127+
if (args.at(0) === "branch") {
128+
return Promise.resolve(
129+
"fork/my-awesome-fork/feature/my-awesome-branch\n",
130+
);
131+
}
132+
unreachable();
133+
});
134+
try {
135+
const remote = await getRemoteContains("<<commitish>>");
136+
assertEquals(remote, "fork/my-awesome-fork");
137+
} finally {
138+
executeStub.restore();
139+
}
140+
},
141+
);
142+
143+
await t.step(
144+
"returns 'origin' if 'origin/my-awesome-branch' and 'fork/my-awesome-branch' contains the commitish",
145+
async () => {
146+
const executeStub = stub(_internals, "execute", (args, _options) => {
147+
if (args.at(0) === "remote") {
148+
return Promise.resolve("origin\nfork\n");
149+
}
150+
if (args.at(0) === "branch") {
151+
return Promise.resolve(
152+
"fork/my-awesome-branch\norigin/my-awesome-branch\n",
153+
);
154+
}
155+
unreachable();
156+
});
157+
try {
158+
const remote = await getRemoteContains("<<commitish>>");
159+
assertEquals(remote, "origin");
160+
} finally {
161+
executeStub.restore();
162+
}
163+
},
164+
);
68165
});
69166

70167
Deno.test("getRemoteFetchURL", async (t) => {

0 commit comments

Comments
 (0)