use-multiple-gits
Version:
CLI tool to manage multiple git configurations (user.name, user.email, SSH keys) with easy switching between identities
93 lines (89 loc) • 3.68 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.uninstallPreCommitHook = exports.installPreCommitHook = void 0;
const path = __importStar(require("path"));
const fs = __importStar(require("fs"));
const fs_1 = require("./fs");
const configStorage_1 = require("./configStorage");
const errors_1 = require("./errors");
const GIT_HOOKS_DIR = '.git/hooks';
const PRE_COMMIT_HOOK = 'pre-commit';
const installPreCommitHook = async (repoPath = process.cwd()) => {
const hooksDir = path.join(repoPath, GIT_HOOKS_DIR);
const hookPath = path.join(hooksDir, PRE_COMMIT_HOOK);
// Check if it's a git repository
if (!(0, fs_1.fileExists)(path.join(repoPath, '.git'))) {
throw new Error('Not a git repository. Run this command from a git repository root.');
}
// Create hooks directory if it doesn't exist
if (!(0, fs_1.fileExists)(hooksDir)) {
fs.mkdirSync(hooksDir, { recursive: true });
}
const configs = await (0, configStorage_1.getAllConfigs)();
const configNames = configs.map(c => c.name).join(', ');
const hookContent = `#!/bin/bash
# Multi-Git Pre-commit Hook
# Verifies that the correct git identity is being used
CURRENT_NAME=$(git config user.name)
CURRENT_EMAIL=$(git config user.email)
echo "Current Git identity: $CURRENT_NAME <$CURRENT_EMAIL>"
echo "Available configs: ${configNames}"
# Note: This hook can be enhanced to check directory mappings
# For now, it's a simple identity verification hook
exit 0
`;
try {
await (0, fs_1.writeFile)(hookPath, hookContent);
await (0, fs_1.makeExecutable)(hookPath);
}
catch (error) {
throw new errors_1.FileSystemError(`Failed to install pre-commit hook: ${error.message}`, error);
}
};
exports.installPreCommitHook = installPreCommitHook;
const uninstallPreCommitHook = async (repoPath = process.cwd()) => {
const hookPath = path.join(repoPath, GIT_HOOKS_DIR, PRE_COMMIT_HOOK);
if ((0, fs_1.fileExists)(hookPath)) {
try {
fs.unlinkSync(hookPath);
}
catch (error) {
throw new errors_1.FileSystemError(`Failed to uninstall pre-commit hook: ${error.message}`, error);
}
}
};
exports.uninstallPreCommitHook = uninstallPreCommitHook;
//# sourceMappingURL=gitHooks.js.map