|
| 1 | +/** |
| 2 | + * Commit template module. |
| 3 | + * |
| 4 | + * TODO: Move to docs and link there. |
| 5 | + * |
| 6 | + * Read the message from the a configured commit template file and use it as a prefix in the message. |
| 7 | + * |
| 8 | + * A commit template is built-in Git behavior to see a value for the start of each commit message. This is useful if you have a ticket number, project name, or similar to add at the start of each commit. |
| 9 | + * |
| 10 | + * Note that VS Code and Git CLI both automatically read from this file when generating a commit. However, the value is hard to use. There is behavior in this extension to move the old message to the end and enter a commit type prefix and commit message before it, but there is no way to know from the content of the message for sure whether the old message is a commit template value or just a hand-typed message. |
| 11 | + * |
| 12 | + * To avoid making an extra config value for the extension that one has to manage say in a Settings file or internal data, the approach is rather to use the existing commit template pattern in Git. |
| 13 | + */ |
| 14 | +import * as fs from 'fs'; |
| 15 | +import * as path from 'path'; |
| 16 | +import { getWorkspaceFolder } from "../workspace"; |
| 17 | +import { execute } from "./cli"; |
| 18 | + |
| 19 | +const CONFIG_SUBCOMMAND = "config" |
| 20 | +const COMMIT_TEMPLATE_IDENTIFIER = 'commit.template' |
| 21 | + |
| 22 | +/** |
| 23 | + * Get a value from the Git config. |
| 24 | + * |
| 25 | + * The CLI will assume local (project) project by default. |
| 26 | + */ |
| 27 | +export async function _getConfigValue(options: string[]) { |
| 28 | + const workspace = getWorkspaceFolder() |
| 29 | + const { stdout, stderr } = await execute( |
| 30 | + workspace, |
| 31 | + CONFIG_SUBCOMMAND, |
| 32 | + options |
| 33 | + ); |
| 34 | + |
| 35 | + if (stderr) { |
| 36 | + console.debug(`stderr for 'git ${CONFIG_SUBCOMMAND}' command:`, stderr); |
| 37 | + } |
| 38 | + |
| 39 | + return stdout |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * Get commit template path. |
| 44 | + * |
| 45 | + * Get the configured value for a commit template path if set. |
| 46 | + */ |
| 47 | +async function _getCommitTemplatePath() { |
| 48 | + try { |
| 49 | + const options = [COMMIT_TEMPLATE_IDENTIFIER] |
| 50 | + return await _getConfigValue(options) |
| 51 | + } catch (_e) { |
| 52 | + return null |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * Read a file. |
| 58 | + * |
| 59 | + * NB. Use current workspace as the base path. |
| 60 | + */ |
| 61 | +function _readFile(filePath: string) { |
| 62 | + const workspace = getWorkspaceFolder() |
| 63 | + const p = path.join(workspace, filePath) |
| 64 | + |
| 65 | + let value |
| 66 | + |
| 67 | + try { |
| 68 | + value = fs.readFileSync(p, "utf-8") |
| 69 | + } catch (err) { |
| 70 | + console.error(`Could not find template file: ${p}. ${err.toString()}`) |
| 71 | + |
| 72 | + return null |
| 73 | + } |
| 74 | + |
| 75 | + if (!value) { |
| 76 | + return null |
| 77 | + } |
| 78 | + |
| 79 | + console.debug(`Read ${p} and found: ${value}`) |
| 80 | + |
| 81 | + return value |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * Get value of commit template file. |
| 86 | + * |
| 87 | + * Return null if file is not configured or file is missing, without aborting. |
| 88 | + */ |
| 89 | +export async function getCommitTemplateValue() { |
| 90 | + const filePath = await _getCommitTemplatePath() |
| 91 | + |
| 92 | + if (!filePath) { |
| 93 | + console.error(`Could not read missing file: ${filePath}`) |
| 94 | + return null |
| 95 | + } |
| 96 | + |
| 97 | + return _readFile(filePath) |
| 98 | +} |
0 commit comments