diff --git a/.changeset/few-plums-enjoy.md b/.changeset/few-plums-enjoy.md new file mode 100644 index 0000000000..4e2ee0f719 --- /dev/null +++ b/.changeset/few-plums-enjoy.md @@ -0,0 +1,8 @@ +--- +'@backstage/backend-common': patch +--- + +Updated the Git class with the following: + +- Added `depth` and `noCheckout` options to Git clone, using these you can create a bare clone that includes just the git history +- New `log` function which you can use to view the commit history of a git repo diff --git a/packages/backend-common/api-report.md b/packages/backend-common/api-report.md index 061931832d..a62a280dc9 100644 --- a/packages/backend-common/api-report.md +++ b/packages/backend-common/api-report.md @@ -234,8 +234,13 @@ export class Git { remote: string; url: string; }): Promise; - // (undocumented) - clone(options: { url: string; dir: string; ref?: string }): Promise; + clone(options: { + url: string; + dir: string; + ref?: string; + depth?: number; + noCheckout?: boolean; + }): Promise; // (undocumented) commit(options: { dir: string; @@ -249,12 +254,10 @@ export class Git { email: string; }; }): Promise; - // (undocumented) currentBranch(options: { dir: string; fullName?: boolean; }): Promise; - // (undocumented) fetch(options: { dir: string; remote?: string }): Promise; // (undocumented) static fromAuth: (options: { @@ -264,7 +267,7 @@ export class Git { }) => Git; // (undocumented) init(options: { dir: string; defaultBranch?: string }): Promise; - // (undocumented) + log(options: { dir: string; ref?: string }): Promise; merge(options: { dir: string; theirs: string; @@ -280,9 +283,7 @@ export class Git { }): Promise; // (undocumented) push(options: { dir: string; remote: string }): Promise; - // (undocumented) readCommit(options: { dir: string; sha: string }): Promise; - // (undocumented) resolveRef(options: { dir: string; ref: string }): Promise; } diff --git a/packages/backend-common/src/scm/git.test.ts b/packages/backend-common/src/scm/git.test.ts index abbafc375d..46aa0804b6 100644 --- a/packages/backend-common/src/scm/git.test.ts +++ b/packages/backend-common/src/scm/git.test.ts @@ -320,4 +320,21 @@ describe('Git', () => { }); }); }); + + describe('log', () => { + it('should call isomorphic-git with the correct arguments', async () => { + const dir = '/some/mock/dir'; + const ref = 'as43bd7'; + + const git = Git.fromAuth({}); + + await git.log({ dir, ref }); + + expect(isomorphic.log).toHaveBeenCalledWith({ + fs, + dir, + ref, + }); + }); + }); }); diff --git a/packages/backend-common/src/scm/git.ts b/packages/backend-common/src/scm/git.ts index 372ecb0260..0446f08767 100644 --- a/packages/backend-common/src/scm/git.ts +++ b/packages/backend-common/src/scm/git.ts @@ -76,12 +76,15 @@ export class Git { return git.commit({ fs, dir, message, author, committer }); } + /** https://isomorphic-git.org/docs/en/clone */ async clone(options: { url: string; dir: string; ref?: string; + depth?: number; + noCheckout?: boolean; }): Promise { - const { url, dir, ref } = options; + const { url, dir, ref, depth, noCheckout } = options; this.config.logger?.info(`Cloning repo {dir=${dir},url=${url}}`); return git.clone({ fs, @@ -90,7 +93,8 @@ export class Git { dir, ref, singleBranch: true, - depth: 1, + depth: depth ?? 1, + noCheckout, onProgress: this.onProgressHandler(), headers: { 'user-agent': 'git/@isomorphic-git', @@ -99,7 +103,7 @@ export class Git { }); } - // https://isomorphic-git.org/docs/en/currentBranch + /** https://isomorphic-git.org/docs/en/currentBranch */ async currentBranch(options: { dir: string; fullName?: boolean; @@ -110,7 +114,7 @@ export class Git { >; } - // https://isomorphic-git.org/docs/en/fetch + /** https://isomorphic-git.org/docs/en/fetch */ async fetch(options: { dir: string; remote?: string }): Promise { const { dir, remote = 'origin' } = options; this.config.logger?.info( @@ -138,7 +142,7 @@ export class Git { }); } - // https://isomorphic-git.org/docs/en/merge + /** https://isomorphic-git.org/docs/en/merge */ async merge(options: { dir: string; theirs: string; @@ -180,7 +184,7 @@ export class Git { }); } - // https://isomorphic-git.org/docs/en/readCommit + /** https://isomorphic-git.org/docs/en/readCommit */ async readCommit(options: { dir: string; sha: string; @@ -189,12 +193,25 @@ export class Git { return git.readCommit({ fs, dir, oid: sha }); } - // https://isomorphic-git.org/docs/en/resolveRef + /** https://isomorphic-git.org/docs/en/resolveRef */ async resolveRef(options: { dir: string; ref: string }): Promise { const { dir, ref } = options; return git.resolveRef({ fs, dir, ref }); } + /** https://isomorphic-git.org/docs/en/log */ + async log(options: { + dir: string; + ref?: string; + }): Promise { + const { dir, ref } = options; + return git.log({ + fs, + dir, + ref: ref ?? 'HEAD', + }); + } + private onAuth = () => ({ username: this.config.username, password: this.config.password,