@rxap/node-utilities
Version:
Provides a set of utility functions for Node.js development, including file system operations, git integration, and package.json manipulation. It offers functionalities like copying folders, reading JSON files with retry logic, retrieving the current git
42 lines • 1.82 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GetCurrentBranch = GetCurrentBranch;
const child_process_1 = require("child_process");
/**
* Asynchronously retrieves the current branch name of the git repository in the current directory.
*
* This function checks if the current directory is a git repository and then fetches the name of the branch
* that HEAD is pointing to. It uses the `git rev-parse --is-inside-work-tree` command to verify if the directory
* is within a git repository and `git symbolic-ref --short -q HEAD` to obtain the current branch name.
*
* @returns A Promise that resolves to a string representing the current branch name.
*
* @throws {Error} Throws an error if the current directory is not a git repository or if HEAD is not pointing to any branch.
*
* @example
* GetCurrentBranch().then(branchName => {
* console.log(branchName); // Outputs the current branch name
* }).catch(error => {
* console.error(error); // Handles any errors
* });
*/
function GetCurrentBranch() {
return new Promise((resolve, reject) => {
(0, child_process_1.exec)('git rev-parse --is-inside-work-tree', (error, stdout, stderr) => {
if (error || stdout.trim() !== 'true') {
reject(new Error('The current directory is not a git repository'));
}
else {
(0, child_process_1.exec)('git symbolic-ref --short -q HEAD', (error, stdout, stderr) => {
if (error || !stdout) {
reject(new Error('HEAD is not pointing to a branch'));
}
else {
resolve(stdout.trim());
}
});
}
});
});
}
//# sourceMappingURL=get-current-branch.js.map