Skip to content

Commit

Permalink
feat(git): allow to install git-hook functions to local repositories (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
NiasSt90 committed Jan 6, 2023
1 parent c5f7924 commit 5b4b646
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
26 changes: 26 additions & 0 deletions lib/util/git/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1056,4 +1056,30 @@ describe('util/git/index', () => {
expect(sha).toBe(git.getBranchCommit(defaultBranch));
});
});

describe('installHook()', () => {
it('installHook()', async () => {
//git.getCommitMessages() only returns the first line (i.e. subject) of each msg
await git.installHook(
'commit-msg',
'#!/bin/sh\necho "APPENDED FROM COMMIT-MSG HOOK" >> $1;'
);
const files: FileChange[] = [
{
type: 'addition',
path: 'some-new-file',
contents: 'some new-contents',
},
];
setNoVerify(['push']);
await git.commitFiles({
branchName: 'renovate/something',
files,
message: 'Orig-commit-msg',
});

const messages = await git.getCommitMessages();
expect(messages[0]).toBe('Orig-commit-msg APPENDED FROM COMMIT-MSG HOOK');
});
});
});
11 changes: 11 additions & 0 deletions lib/util/git/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,17 @@ export async function initRepo(args: StorageConfig): Promise<void> {
await fetchBranchCommits();
}

export async function installHook(
name: string,
hookSource: string
): Promise<void> {
await syncGit();
const localDir = GlobalConfig.get('localDir')!;
const gitHooks = upath.join(localDir, '.git/hooks');
await fs.writeFile(`${gitHooks}/${name}`, hookSource);
await fs.chmod(`${gitHooks}/${name}`, 0o500);
}

async function resetToBranch(branchName: string): Promise<void> {
logger.debug(`resetToBranch(${branchName})`);
await git.raw(['reset', '--hard']);
Expand Down

0 comments on commit 5b4b646

Please sign in to comment.