Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve short sha #93

Merged
merged 4 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ outputs:
sha:
description: >
The object name for the commit itself.
ex) 2414721
ex) 2414721230345897234093645897124245324623
short-sha:
description: >
The object name for the commit itself.
Expand Down
4 changes: 2 additions & 2 deletions core/fetch_total_commit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface FetchTotalCommitArgs {

export async function fetchTotalCommit({ owner, repo, host, sha }: FetchTotalCommitArgs) {
const stdout = await gh.graphql({ host })`
{
query {
repository(owner: "${owner}", name: "${repo}") {
object(expression: "${sha}") {
... on Commit {
Expand All @@ -21,5 +21,5 @@ export async function fetchTotalCommit({ owner, repo, host, sha }: FetchTotalCom
}
}`;
const repository = JSON.parse(stdout);
return repository.object.history.totalCount;
return repository.data.repository.object.history.totalCount;
}
38 changes: 32 additions & 6 deletions core/gh_describe.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { fetchHistory } from "./fetch_history.ts";
import { fetchSha } from "./fetch_sha.ts";
import { fetchTags } from "./fetch_tags.ts";
import { fetchTotalCommit } from "./fetch_total_commit.ts";
import { GhDescribeError } from "./gh_describe_error.ts";
import { resolveRepo } from "./resolve_repo.ts";
import { searchTag } from "./search_tags.ts";
Expand Down Expand Up @@ -72,11 +73,26 @@ export interface GhDescribeOutput {
shortSha: string;
}

export function createDescribe(tag: string, distance: number, sha: string) {
// Return the slot of the most-significant bit set in x.
export function MSB(x: number) {
let r = 0;
while (x > 1) {
x >>= 1;
r++;
}
return r;
}

export function createDescribe(
tag: string,
distance: number,
sha: string,
shortShaChars: number,
): string {
if (distance === 0) {
return tag;
} else {
return `${tag}-${distance}-g${sha.substring(0, 7)}`;
return `${tag}-${distance}-g${sha.substring(0, shortShaChars)}`;
}
}

Expand All @@ -92,12 +108,22 @@ export async function ghDescribe(options?: GhDescribeOptions): Promise<GhDescrib
} = options ?? {};
const { owner, repo, host } = await resolveRepo(options?.repo);

const [tags, { sha, histories }] = await Promise.all([
const [tags, { sha, histories, shortShaChars }] = await Promise.all([
fetchTags({ owner, repo, host, match, exclude }),
(async () => {
const sha = await fetchSha({ owner, repo, host, sha: commitish });
const histories = fetchHistory({ owner, repo, host, sha });
return { sha, histories };

// Emulate https://github.com/git/git/blob/e9356ba3ea2a6754281ff7697b3e5a1697b21e24/object-name.c#L829-L847
const commitCount = await fetchTotalCommit({ owner, repo, host, sha });
// fetch highest set bit in commitCount
const distance = MSB(commitCount) + 1;
// calculate how many chars to use for short sha
// 7 is the default for git describe
// https://git-scm.com/docs/git-describe#_examples
const shortShaChars = Math.max(7, Math.round((distance + 1) / 2));

return { sha, histories, shortShaChars };
})(),
]);

Expand All @@ -110,12 +136,12 @@ export async function ghDescribe(options?: GhDescribeOptions): Promise<GhDescrib
throw new GhDescribeError("No names found, cannot describe anything.");
}

const describe = createDescribe(tag, distance, sha);
const describe = createDescribe(tag, distance, sha, shortShaChars);
return {
describe,
tag,
distance,
sha,
shortSha: sha.substring(0, 7),
shortSha: sha.substring(0, shortShaChars),
};
}
Loading