Files
OpenNoodl/packages/noodl-git/src/actions/history.ts
Michael Cartner b9c60b07dc Initial commit
Co-Authored-By: Eric Tuvesson <eric.tuvesson@gmail.com>
Co-Authored-By: mikaeltellhed <2311083+mikaeltellhed@users.noreply.github.com>
Co-Authored-By: kotte <14197736+mrtamagotchi@users.noreply.github.com>
Co-Authored-By: Anders Larsson <64838990+anders-topp@users.noreply.github.com>
Co-Authored-By: Johan  <4934465+joolsus@users.noreply.github.com>
Co-Authored-By: Tore Knudsen <18231882+torekndsn@users.noreply.github.com>
Co-Authored-By: victoratndl <99176179+victoratndl@users.noreply.github.com>
2024-01-26 11:52:55 +01:00

88 lines
2.3 KiB
TypeScript

import { getCommits } from "../core/logs";
import { Branch } from "../core/models/branch";
import { Commit } from "../core/models/snapshot";
import { getAheadBehind, revSymmetricDifference } from "../core/rev-list";
export class CommitHistoryEntry extends Commit {
public isLocalAhead: boolean;
public isRemoteAhead: boolean;
public constructor(
commit: Commit,
isLocalAhead: boolean,
isRemoteAhead: boolean
) {
super(
commit.repositoryDir,
commit.sha,
commit.shortSha,
commit.summary,
commit.body,
commit.author,
commit.committer,
commit.parentSHAs,
commit.tags
);
this.isLocalAhead = isLocalAhead;
this.isRemoteAhead = isRemoteAhead;
}
}
/**
* Class designed to handle history between multiple branches.
*/
export class CommitHistory {
public constructor(
public readonly repositoryDir: string,
public readonly branch: Branch
) {}
public async fetch(count: number): Promise<readonly CommitHistoryEntry[]> {
const localGitCommits = await getCommits(
this.repositoryDir,
undefined,
count
);
let commits: CommitHistoryEntry[] = localGitCommits.map(
(x) => new CommitHistoryEntry(x, false, false)
);
if (this.branch.remote) {
const remoteAheadGitCommits = await getCommits(
this.repositoryDir,
`${this.branch.nameWithoutRemote}..${this.branch.remote.name}`,
count
);
const remoteOnlyCommits = remoteAheadGitCommits.map(
(x) => new CommitHistoryEntry(x, false, true)
);
//get commits that aren't pushed
const localAheadGitCommits = await getCommits(
this.repositoryDir,
`${this.branch.remote.name}..${this.branch.nameWithoutRemote}`,
count
);
localAheadGitCommits.forEach((aheadCommit) => {
const c = commits.find((c) => c.sha === aheadCommit.sha);
if (c) {
c.isLocalAhead = true;
}
});
commits = remoteOnlyCommits.concat(commits);
} else {
// there is no remote, it's a local branch
// flag all the commits as being "ahead" of
// the remote (even the commit that was the branching points, and commits before it)
commits.forEach((c) => (c.isLocalAhead = true));
}
return commits;
}
}