-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgremote.js
More file actions
executable file
·325 lines (268 loc) · 13.4 KB
/
Copy pathgremote.js
File metadata and controls
executable file
·325 lines (268 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#!/usr/bin/env node
import { execSync } from 'child_process';
import { existsSync } from 'fs';
import chalk from 'chalk';
function validateRepository() {
if (!existsSync('.git')) {
console.error(chalk.red('❌ Error: Not a git repository (or any of the parent directories): .git'));
process.exit(1);
}
}
function showHelp() {
console.log(chalk.magenta.bold('\n🌐 gremote - Remote Management\n'));
console.log(chalk.cyan('Purpose:'), 'Manage remote repositories with comprehensive operations for adding, removing, and configuring remotes.\n');
console.log(chalk.cyan('Command:'), chalk.white('gremote [subcommand] [options]\n'));
console.log(chalk.cyan('Subcommands:'));
console.log(' ' + chalk.green('add <name> <url>') + ' - Add a new remote');
console.log(' ' + chalk.green('remove <name>') + ' - Remove a remote');
console.log(' ' + chalk.green('rm <name>') + ' - Same as remove');
console.log(' ' + chalk.green('rename <old> <new>') + ' - Rename a remote');
console.log(' ' + chalk.green('set-url <name> <url>') + ' - Change remote URL');
console.log(' ' + chalk.green('get-url <name>') + ' - Get remote URL');
console.log(' ' + chalk.green('show <name>') + ' - Show remote details');
console.log(' ' + chalk.green('prune <name>') + ' - Prune remote tracking branches\n');
console.log(chalk.cyan('List Options:'));
console.log(' ' + chalk.green('-v, --verbose') + ' - Show URLs along with names');
console.log(' ' + chalk.green('--push') + ' - Show push URLs (with set-url)');
console.log(' ' + chalk.green('--add') + ' - Add URL instead of replace (with set-url)');
console.log(' ' + chalk.green('--delete') + ' - Delete URL (with set-url)');
console.log(' ' + chalk.green('-h, --help') + ' - Show detailed help information\n');
console.log(chalk.cyan('Common Use Cases:'));
console.log(chalk.white(' gremote') + ' # List all remotes');
console.log(chalk.white(' gremote -v') + ' # List with URLs');
console.log(chalk.white(' gremote add origin <url>') + ' # Add origin remote');
console.log(chalk.white(' gremote add upstream <url>') + ' # Add upstream for forks');
console.log(chalk.white(' gremote set-url origin <url>') + ' # Change origin URL');
console.log(chalk.white(' gremote remove upstream') + ' # Remove upstream');
console.log(chalk.white(' gremote show origin') + ' # Show origin details');
console.log(chalk.white(' gremote prune origin') + ' # Clean up remote branches\n');
console.log(chalk.cyan('🔗 URL Formats:'));
console.log(' • HTTPS: https://github.com/user/repo.git');
console.log(' • SSH: git@github.com:user/repo.git');
console.log(' • Git: git://github.com/user/repo.git');
console.log('\n' + chalk.gray('═'.repeat(60)));
}
function isValidRemoteName(name) {
// Basic validation for remote names
return /^[a-zA-Z0-9._-]+$/.test(name) && name.length > 0;
}
async function main() {
const args = process.argv.slice(2);
if (args.includes('-h') || args.includes('--help')) {
showHelp();
return;
}
validateRepository();
console.log(chalk.magenta.bold('🌐 Remote Repository Management'));
console.log(chalk.gray('━'.repeat(40)));
// Build git remote command
let remoteCmd = 'git remote';
let action = 'list';
let remoteName = '';
let remoteUrl = '';
if (args.length === 0) {
action = 'list';
} else {
const subcommand = args[0];
if (subcommand === 'add') {
action = 'add';
remoteName = args[1];
remoteUrl = args[2];
remoteCmd = `git remote add`;
if (!remoteName || !remoteUrl) {
console.error(chalk.red('❌ Error: Both remote name and URL are required'));
console.log(chalk.yellow('💡 Usage: gremote add <name> <url>'));
process.exit(1);
}
if (!isValidRemoteName(remoteName)) {
console.error(chalk.red('❌ Error: Invalid remote name'));
console.log(chalk.yellow('💡 Remote names should contain only letters, numbers, dots, hyphens, and underscores'));
process.exit(1);
}
remoteCmd += ` ${remoteName} "${remoteUrl}"`;
} else if (subcommand === 'remove' || subcommand === 'rm') {
action = 'remove';
remoteName = args[1];
remoteCmd = `git remote remove`;
if (!remoteName) {
console.error(chalk.red('❌ Error: Remote name is required'));
console.log(chalk.yellow('💡 Usage: gremote remove <name>'));
process.exit(1);
}
remoteCmd += ` ${remoteName}`;
} else if (subcommand === 'rename') {
action = 'rename';
const oldName = args[1];
const newName = args[2];
remoteCmd = `git remote rename`;
if (!oldName || !newName) {
console.error(chalk.red('❌ Error: Both old and new names are required'));
console.log(chalk.yellow('💡 Usage: gremote rename <old-name> <new-name>'));
process.exit(1);
}
if (!isValidRemoteName(newName)) {
console.error(chalk.red('❌ Error: Invalid new remote name'));
process.exit(1);
}
remoteCmd += ` ${oldName} ${newName}`;
} else if (subcommand === 'set-url') {
action = 'set-url';
remoteName = args[1];
remoteUrl = args[2];
remoteCmd = `git remote set-url`;
// Handle options
if (args.includes('--push')) {
remoteCmd += ' --push';
}
if (args.includes('--add')) {
remoteCmd += ' --add';
}
if (args.includes('--delete')) {
remoteCmd += ' --delete';
}
if (!remoteName || (!remoteUrl && !args.includes('--delete'))) {
console.error(chalk.red('❌ Error: Remote name and URL are required'));
console.log(chalk.yellow('💡 Usage: gremote set-url <name> <url>'));
process.exit(1);
}
remoteCmd += ` ${remoteName}`;
if (remoteUrl) {
remoteCmd += ` "${remoteUrl}"`;
}
} else if (subcommand === 'get-url') {
action = 'get-url';
remoteName = args[1];
remoteCmd = `git remote get-url`;
if (!remoteName) {
console.error(chalk.red('❌ Error: Remote name is required'));
console.log(chalk.yellow('💡 Usage: gremote get-url <name>'));
process.exit(1);
}
if (args.includes('--push')) {
remoteCmd += ' --push';
}
if (args.includes('--all')) {
remoteCmd += ' --all';
}
remoteCmd += ` ${remoteName}`;
} else if (subcommand === 'show') {
action = 'show';
remoteName = args[1];
remoteCmd = `git remote show`;
if (!remoteName) {
console.error(chalk.red('❌ Error: Remote name is required'));
console.log(chalk.yellow('💡 Usage: gremote show <name>'));
process.exit(1);
}
remoteCmd += ` ${remoteName}`;
} else if (subcommand === 'prune') {
action = 'prune';
remoteName = args[1];
remoteCmd = `git remote prune`;
if (!remoteName) {
console.error(chalk.red('❌ Error: Remote name is required'));
console.log(chalk.yellow('💡 Usage: gremote prune <name>'));
process.exit(1);
}
if (args.includes('--dry-run')) {
remoteCmd += ' --dry-run';
}
remoteCmd += ` ${remoteName}`;
} else if (subcommand === '-v' || subcommand === '--verbose') {
action = 'list';
remoteCmd = 'git remote -v';
} else {
// Treat as listing with options
action = 'list';
if (args.includes('-v') || args.includes('--verbose')) {
remoteCmd = 'git remote -v';
}
}
}
console.log(chalk.cyan(`🔍 Running: ${remoteCmd}`));
try {
const result = execSync(remoteCmd, { encoding: 'utf-8' });
if (action === 'list') {
if (result.trim()) {
console.log(chalk.cyan('🌐 Configured Remotes:'));
const remotes = result.trim().split('\n');
if (remoteCmd.includes('-v')) {
// Verbose listing with URLs
const remoteMap = new Map();
remotes.forEach(line => {
const [name, url, type] = line.split(/\s+/);
if (!remoteMap.has(name)) {
remoteMap.set(name, { fetch: '', push: '' });
}
if (type === '(fetch)') {
remoteMap.get(name).fetch = url;
} else if (type === '(push)') {
remoteMap.get(name).push = url;
}
});
for (const [name, urls] of remoteMap) {
console.log(` ${chalk.green('📡')} ${chalk.white.bold(name)}`);
console.log(` ${chalk.gray('Fetch:')} ${chalk.white(urls.fetch)}`);
if (urls.push !== urls.fetch) {
console.log(` ${chalk.gray('Push:')} ${chalk.white(urls.push)}`);
}
}
} else {
// Simple listing
remotes.forEach(remote => {
console.log(` ${chalk.green('📡')} ${chalk.white(remote)}`);
});
}
} else {
console.log(chalk.yellow('ℹ️ No remotes configured'));
console.log(chalk.gray('💡 Add a remote with: gremote add origin <url>'));
}
} else {
// Other actions
if (result.trim()) {
console.log(result);
}
const actionMessages = {
'add': `✅ Remote '${remoteName}' added successfully`,
'remove': `✅ Remote '${remoteName}' removed successfully`,
'rename': `✅ Remote renamed successfully`,
'set-url': `✅ Remote URL updated successfully`,
'get-url': `📋 Remote URL for '${remoteName}':`,
'show': `📊 Remote details for '${remoteName}':`,
'prune': `✅ Remote branches pruned successfully`
};
if (actionMessages[action]) {
console.log(chalk.green(actionMessages[action]));
}
}
// Show next steps based on action
console.log(chalk.gray('━'.repeat(40)));
console.log(chalk.cyan('💡 Remote Operations:'));
if (action === 'add') {
console.log(chalk.white(' gpush -u ' + remoteName + ' main') + ' # Push and set upstream');
console.log(chalk.white(' gpull') + ' # Pull from remote');
} else if (action === 'list') {
console.log(chalk.white(' gremote add <name> <url>') + ' # Add new remote');
console.log(chalk.white(' gremote show <name>') + ' # Show remote details');
}
console.log(chalk.white(' gremote -v') + ' # List remotes with URLs');
console.log(chalk.white(' gstatus') + ' # Check current status');
console.log(chalk.green('\n✅ Command completed successfully'));
} catch (error) {
console.error(chalk.red(`❌ Error: ${error.message}`));
// Common error suggestions
if (error.message.includes('already exists')) {
console.log(chalk.yellow(`💡 Remote '${remoteName}' already exists. Use set-url to change it.`));
} else if (error.message.includes('No such remote')) {
console.log(chalk.yellow(`💡 Remote '${remoteName}' not found. Use 'gremote' to list existing remotes.`));
} else if (error.message.includes('Invalid URL')) {
console.log(chalk.yellow('💡 Check URL format. Examples:'));
console.log(chalk.yellow(' • https://github.com/user/repo.git'));
console.log(chalk.yellow(' • git@github.com:user/repo.git'));
}
process.exit(1);
}
}
if (import.meta.url === `file://${process.argv[1]}`) {
main().catch(console.error);
}