@harryisfish/gitt
Version:
A command-line tool to help you manage Git repositories and remote repositories, such as keeping in sync, pushing, pulling, etc.
146 lines (145 loc) • 6.18 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 });
const commander_1 = require("commander");
const simple_git_1 = require("simple-git");
const errors_1 = require("./errors");
const clean_1 = require("./commands/clean");
const git = (0, simple_git_1.simpleGit)();
const packageJson = require('../package.json');
// Handle Ctrl+C and other termination signals
process.on('SIGINT', () => {
throw new errors_1.UserCancelError('\nOperation cancelled');
});
process.on('SIGTERM', () => {
throw new errors_1.UserCancelError('\nProgram terminated');
});
// Check if current directory is a Git repository
async function checkGitRepo() {
const isRepo = await git.checkIsRepo();
if (!isRepo) {
throw new errors_1.GitError('Current directory is not a Git repository');
}
// Check if remote repository is configured
const remotes = await git.getRemotes();
if (remotes.length === 0) {
throw new errors_1.GitError('Current Git repository has no remote configured');
}
// Check if remote repository is accessible
try {
await git.fetch(['--dry-run']);
}
catch (error) {
throw new errors_1.GitError('Cannot access remote repository, please check network connection or repository permissions');
}
}
async function main() {
try {
// Check for updates before parsing commands
try {
// Use dynamic import to load update-notifier
const updateNotifier = await Promise.resolve().then(() => __importStar(require('update-notifier'))).then(m => m.default);
updateNotifier({ pkg: packageJson }).notify();
}
catch (e) {
// Ignore update check errors
}
const program = new commander_1.Command();
program
.name('gitt')
.description('A CLI tool for Git branch management')
.version(packageJson.version, '-v, --version', 'Show version number');
// Default command: clean deleted branches
program
.option('-i, --interactive', 'Interactive mode: Select branches to delete')
.option('-d, --dry-run', 'Dry run: Show what would be deleted without deleting')
.option('--stale [days]', 'Find stale branches (default: 90 days)')
.action(async (options) => {
await checkGitRepo();
// options.stale is undefined when not passed, true when passed without value, or a string when passed with value
const isStale = options.stale !== undefined;
const staleDays = options.stale === true || options.stale === undefined ? 90 : parseInt(options.stale, 10);
await (0, clean_1.cleanDeletedBranches)({
interactive: options.interactive || false,
dryRun: options.dryRun || false,
stale: isStale,
staleDays: staleDays
});
});
// set-main command
program
.command('set-main <branch>')
.description('Set the main branch for the current project')
.action(async (branch) => {
await checkGitRepo();
await Promise.resolve().then(() => __importStar(require('./commands/config'))).then(m => m.configMainBranch(branch));
});
// ignore command
program
.command('ignore <pattern>')
.description('Add a branch pattern to the ignore list (e.g., "release/*")')
.action(async (pattern) => {
await checkGitRepo();
await Promise.resolve().then(() => __importStar(require('./commands/config'))).then(m => m.configIgnoreBranch(pattern));
});
// upgrade command
program
.command('upgrade')
.description('Check for updates and upgrade to the latest version')
.action(async () => {
// No need to check Git repo for upgrade command
await Promise.resolve().then(() => __importStar(require('./commands/upgrade'))).then(m => m.upgradeCommand());
});
// Add examples to help
program.addHelpText('after', `
Examples:
$ gitt # Auto-clean deleted branches
$ gitt -i # Select branches to delete interactively
$ gitt -d # Preview deletion
$ gitt --stale # Find branches inactive for 90+ days
$ gitt --stale 30 # Find branches inactive for 30+ days
$ gitt ignore "temp/*" # Ignore branches matching "temp/*"
$ gitt set-main master # Set main branch to 'master'
$ gitt upgrade # Check for updates and upgrade
`);
await program.parseAsync(process.argv);
}
catch (error) {
(0, errors_1.handleError)(error);
}
}
// Start the program
main();