0

Get the common ancestor between two commits

by
Published Oct 24, 2023

Returns the best common ancestor between two commits, specified in a revspec of 2 commits (e.g. 3a8b42..9ff173). If more than one best common ancestor exists, only one will be returned. It is unspecified which will be returned.

Script bitbucket Verified

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 398 days ago
1
type Bitbucket = {
2
  username: string;
3
  password: string;
4
};
5
/**
6
 * Get the common ancestor between two commits
7
 * Returns the best common ancestor between two commits, specified in a revspec
8
of 2 commits (e.g. 3a8b42..9ff173).
9

10
If more than one best common ancestor exists, only one will be returned. It is
11
unspecified which will be returned.
12
 */
13
export async function main(
14
  auth: Bitbucket,
15
  repo_slug: string,
16
  revspec: string,
17
  workspace: string
18
) {
19
  const url = new URL(
20
    `https://api.bitbucket.org/2.0/repositories/${workspace}/${repo_slug}/merge-base/${revspec}`
21
  );
22

23
  const response = await fetch(url, {
24
    method: "GET",
25
    headers: {
26
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
27
    },
28
    body: undefined,
29
  });
30
  if (!response.ok) {
31
    const text = await response.text();
32
    throw new Error(`${response.status} ${text}`);
33
  }
34
  return await response.json();
35
}
36