-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.js
More file actions
424 lines (356 loc) · 10.9 KB
/
code.js
File metadata and controls
424 lines (356 loc) · 10.9 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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
const state = {
initialized: false,
files: {},
staged: {},
commits: [],
branches: {
main: null
},
currentBranch: 'main',
HEAD: null,
stash: [],
remotes: {},
config: {
user: {
name: '',
email: ''
}
}
};
// Utility functions
function generateCommitHash() {
return Math.random().toString(36).substring(2, 15);
}
function formatDate(date) {
return new Date(date).toLocaleString();
}
// Main command processor
function processCommand(command) {
addToHistory(command);
const parts = command.trim().split(' ');
if (parts[0] !== 'git') {
showOutput('Error: This is a Git practice environment. Please use git commands.');
return;
}
const subCommand = parts[1];
const args = parts.slice(2);
switch(subCommand) {
// Setup and Configuration
case 'config':
handleConfig(args);
break;
// Basic Commands
case 'init':
handleInit();
break;
case 'add':
handleAdd(args);
break;
case 'commit':
handleCommit(args);
break;
case 'status':
handleStatus();
break;
// Branch Management
case 'branch':
handleBranch(args);
break;
case 'checkout':
handleCheckout(args);
break;
case 'merge':
handleMerge(args);
break;
// Remote Operations
case 'remote':
handleRemote(args);
break;
case 'push':
handlePush(args);
break;
case 'pull':
handlePull(args);
break;
// History and Diffs
case 'log':
handleLog(args);
break;
case 'diff':
handleDiff(args);
break;
// Stashing
case 'stash':
handleStash(args);
break;
default:
showOutput(`Error: Unknown git command "${subCommand}"`);
}
}
// Command Handlers
function handleInit() {
if (!state.initialized) {
state.initialized = true;
state.HEAD = null;
state.branches.main = null;
showOutput('Initialized empty Git repository');
} else {
showOutput('Git repository already initialized');
}
}
function handleRemote(args) {
if (!checkInitialized()) return;
const [action, name, url] = args;
switch(action) {
case 'add':
if (name && url) {
state.remotes[name] = url;
showOutput(`Added remote ${name} with URL ${url}`);
} else {
showOutput('Error: Please provide a name and URL for the remote');
}
break;
case 'remove':
if (name && state.remotes[name]) {
delete state.remotes[name];
showOutput(`Removed remote ${name}`);
} else {
showOutput('Error: Remote not found');
}
break;
case '-v':
case '--verbose':
let output = '';
for (let remote in state.remotes) {
output += `${remote}\t${state.remotes[remote]}\n`;
}
showOutput(output || 'No remotes configured');
break;
default:
showOutput('Error: Unknown remote command');
}
}
function handleAdd(args) {
if (!checkInitialized()) return;
if (args[0] === '.') {
// Add all files
Object.keys(state.files).forEach(file => {
state.staged[file] = true;
});
showOutput('Added all files to staging area');
} else if (args[0]) {
state.files[args[0]] = true;
state.staged[args[0]] = true;
showOutput(`Added ${args[0]} to staging area`);
} else {
showOutput('Error: Please specify a file to add');
}
}
function handleCommit(args) {
if (!checkInitialized()) return;
if (Object.keys(state.staged).length === 0) {
showOutput('Error: Nothing to commit');
return;
}
if (args[0] === '-m' && args[1]) {
const message = args.slice(1).join(' ').replace(/"/g, '');
const commitHash = generateCommitHash();
const commit = {
hash: commitHash,
message,
files: {...state.staged},
parent: state.HEAD,
timestamp: new Date().toISOString(),
author: `${state.config.user.name} <${state.config.user.email}>`
};
state.commits.push(commit);
state.HEAD = commitHash;
state.branches[state.currentBranch] = commitHash;
state.staged = {};
showOutput(`[${state.currentBranch} ${commitHash}] ${message}`);
} else {
showOutput('Error: Please provide a commit message (-m "your message")');
}
}
function handleStatus() {
if (!checkInitialized()) return;
let status = `On branch ${state.currentBranch}\n\n`;
if (Object.keys(state.staged).length > 0) {
status += 'Changes to be committed:\n';
for (let file in state.staged) {
status += ` new file: ${file}\n`;
}
}
const unstaged = Object.keys(state.files).filter(file => !state.staged[file]);
if (unstaged.length > 0) {
status += '\nChanges not staged for commit:\n';
unstaged.forEach(file => {
status += ` modified: ${file}\n`;
});
}
if (Object.keys(state.staged).length === 0 && unstaged.length === 0) {
status += 'nothing to commit, working tree clean\n';
}
showOutput(status);
}
function handleBranch(args) {
if (!checkInitialized()) return;
if (args.length === 0) {
// List branches
let output = '';
Object.keys(state.branches).forEach(branch => {
output += (branch === state.currentBranch ? '* ' : ' ') + branch + '\n';
});
showOutput(output);
return;
}
const [branchName] = args;
if (args[0] === '-d') {
// Delete branch
if (args[1] === state.currentBranch) {
showOutput('Error: Cannot delete the current branch');
return;
}
delete state.branches[args[1]];
showOutput(`Deleted branch ${args[1]}`);
} else {
// Create new branch
state.branches[branchName] = state.HEAD;
showOutput(`Created branch ${branchName}`);
}
}
function handleCheckout(args) {
if (!checkInitialized()) return;
const [target] = args;
if (target === '-b') {
// Create and checkout new branch
const newBranch = args[1];
state.branches[newBranch] = state.HEAD;
state.currentBranch = newBranch;
showOutput(`Switched to a new branch '${newBranch}'`);
} else if (state.branches.hasOwnProperty(target)) {
// Checkout existing branch
state.currentBranch = target;
state.HEAD = state.branches[target];
showOutput(`Switched to branch '${target}'`);
} else {
showOutput(`Error: branch '${target}' does not exist`);
}
}
function handleLog(args) {
if (!checkInitialized()) return;
if (state.commits.length === 0) {
showOutput('No commits yet');
return;
}
let log = '';
[...state.commits].reverse().forEach(commit => {
log += `commit ${commit.hash}\n`;
log += `Author: ${commit.author}\n`;
log += `Date: ${formatDate(commit.timestamp)}\n\n`;
log += ` ${commit.message}\n\n`;
});
showOutput(log);
}
function handleConfig(args) {
if (args[0] === '--global') {
const [, setting, value] = args;
const [category, key] = setting.split('.');
if (state.config[category]) {
state.config[category][key] = value.replace(/"/g, '');
showOutput(`Set ${category}.${key} to ${value}`);
} else {
showOutput(`Error: Invalid configuration category '${category}'`);
}
} else {
showOutput('Error: Only --global configuration is supported');
}
}
// Helper Functions
function checkInitialized() {
if (!state.initialized) {
showOutput('Error: Not a git repository (use "git init" first)');
return false;
}
return true;
}
function showOutput(text) {
const output = document.getElementById('output');
output.innerHTML = text.replace(/\n/g, '<br>');
}
function addToHistory(command) {
const history = document.getElementById('history');
const entry = document.createElement('div');
entry.textContent = `$ ${command}`;
history.insertBefore(entry, history.firstChild);
}
function handlePush(args) {
if (!checkInitialized()) return;
const [remote, branch] = args;
if (remote && branch) {
if (state.remotes[remote]) {
showOutput(`Pushed to ${remote}/${branch}`);
} else {
showOutput(`Error: Remote ${remote} not found`);
}
} else {
showOutput('Error: Please specify a remote and branch to push to');
}
}
function handlePull(args) {
if (!checkInitialized()) return;
const [remote, branch] = args;
if (remote && branch) {
if (state.remotes[remote]) {
showOutput(`Pulled from ${remote}/${branch}`);
} else {
showOutput(`Error: Remote ${remote} not found`);
}
} else {
showOutput('Error: Please specify a remote and branch to pull from');
}
}
function handleDiff(args) {
if (!checkInitialized()) return;
let diff = '';
const unstaged = Object.keys(state.files).filter(file => !state.staged[file]);
unstaged.forEach(file => {
diff += `diff --git a/${file} b/${file}\n`;
diff += `new file mode 100644\n`;
diff += `--- /dev/null\n`;
diff += `+++ b/${file}\n`;
diff += `@@ -0,0 +1 @@\n`;
diff += `+${file} content\n`;
});
showOutput(diff || 'No differences');
}
function handleStash(args) {
if (!checkInitialized()) return;
const unstaged = Object.keys(state.files).filter(file => !state.staged[file]);
if (unstaged.length > 0) {
state.stash.push({...state.files});
state.files = {};
showOutput('Saved working directory and index state');
} else {
showOutput('No local changes to save');
}
}
function handleMerge(args) {
if (!checkInitialized()) return;
const [branch] = args;
if (branch && state.branches[branch]) {
state.HEAD = state.branches[branch];
showOutput(`Merged branch ${branch} into ${state.currentBranch}`);
} else {
showOutput(`Error: Branch ${branch} not found`);
}
}
// Event Listener
const commandInput = document.getElementById('commandInput');
commandInput.addEventListener('keypress', function(e) {
if (e.key === 'Enter') {
const command = this.value;
processCommand(command);
this.value = '';
}
});