When a commit message fails validation, rather than throwing out the commit message entirely the commit message is saved into a draft file and restored on the next commit attempt. PR Close #38304
		
			
				
	
	
		
			49 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			49 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
/**
 | 
						|
 * @license
 | 
						|
 * Copyright Google LLC All Rights Reserved.
 | 
						|
 *
 | 
						|
 * Use of this source code is governed by an MIT-style license that can be
 | 
						|
 * found in the LICENSE file at https://angular.io/license
 | 
						|
 */
 | 
						|
 | 
						|
import {info} from 'console';
 | 
						|
import {writeFileSync} from 'fs';
 | 
						|
 | 
						|
import {loadCommitMessageDraft} from './commit-message-draft';
 | 
						|
 | 
						|
/**
 | 
						|
 * Restore the commit message draft to the git to be used as the default commit message.
 | 
						|
 *
 | 
						|
 * The source provided may be one of the sources described in
 | 
						|
 *   https://git-scm.com/docs/githooks#_prepare_commit_msg
 | 
						|
 */
 | 
						|
export function restoreCommitMessage(
 | 
						|
    filePath: string, source?: 'message'|'template'|'squash'|'commit') {
 | 
						|
  if (!!source) {
 | 
						|
    info('Skipping commit message restoration attempt');
 | 
						|
    if (source === 'message') {
 | 
						|
      info('A commit message was already provided via the command with a -m or -F flag');
 | 
						|
    }
 | 
						|
    if (source === 'template') {
 | 
						|
      info('A commit message was already provided via the -t flag or config.template setting');
 | 
						|
    }
 | 
						|
    if (source === 'squash') {
 | 
						|
      info('A commit message was already provided as a merge action or via .git/MERGE_MSG');
 | 
						|
    }
 | 
						|
    if (source === 'commit') {
 | 
						|
      info('A commit message was already provided through a revision specified via --fixup, -c,');
 | 
						|
      info('-C or --amend flag');
 | 
						|
    }
 | 
						|
    process.exit(0);
 | 
						|
  }
 | 
						|
  /** A draft of a commit message. */
 | 
						|
  const commitMessage = loadCommitMessageDraft(filePath);
 | 
						|
 | 
						|
  // If the commit message draft has content, restore it into the provided filepath.
 | 
						|
  if (commitMessage) {
 | 
						|
    writeFileSync(filePath, commitMessage);
 | 
						|
  }
 | 
						|
  // Exit the process
 | 
						|
  process.exit(0);
 | 
						|
}
 |