/
edit_ignore.html
416 lines (309 loc) · 15.5 KB
/
edit_ignore.html
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
<html>
<head>
<title>Git-ignore Editor</title> <!-- NOTE : Don't edit title, without updating listChanged.js which uses this title to close this editor -->
<script src="settings.js"></script>
<link rel="stylesheet" type="text/css" href="color_styles.css" />
<link rel="stylesheet" type="text/css" href="settings.css" />
<link rel="stylesheet" type="text/css" href="node_modules/css-spinners/dist/round/round.css" />
</head>
<style>
body{
font-family: Arial;
font-size: 11pt;
background: var(--body-background);
color: var(--body-text);
}
#outer-content{
margin: 20px;
}
textarea{
/* width: 400; */
line-height: 1;
color: var(--body-text);
border-width: thin;
background-color: var(--input-background);
}
input{
color: var(--body-text);
border-width: thin;
background-color: var(--input-background);
}
input, label {
font-size: 9pt;
display:block;
}
a{
color: var(--link-color);
}
.green{
color: var(--green-text);
}
.red{
color: var(--red-text);
}
img[src="images/warning.png"]
{
filter: invert( 0% );
}
</style>
<body id='body' class='light'>
<!-- Dark mode on/off , zoom -->
<script>
if (localState.dark){
document.body.classList.add('dark');
}
window.document.body.style.zoom = global.state.zoom;
</script>
<!-- HTML -->
<div id="outer-content" style="width: auto;">
<div id="inner-content">
<h2 id="header_Settings">.gitignore Editor
<!-- HELP HERE -->
<img id="about-icon" style='vertical-align:middle;float: right;position:relative; right: 20px' height="17" width="17"
onclick="let evt = {}; evt.name='Git-ignore Editor';opener.opener._callback('help',evt);"
onmouseover="document.getElementById('about-icon').src='images/questionmark_hover.png' "
onmouseout="document.getElementById('about-icon').src='images/questionmark_black.png' "
src="images/questionmark_black.png" >
</h2>
<hr>
<!-- busy spinner -->
<div id="spinner" class="spin-round warning" style="left:45%; top:150px; visibility:collapse;"></div>
<!-- .ignore file editor -->
<div>
<label for="ignoreFile"> .gitignore file:</label>
<textarea id="ignoreFile" placeholder="Write ignore rules here" style="height:300px; font-family:monospace" oninput="displayIgnoreList()"></textarea><br><br>
<!-- Button row -->
<button style="float: right; margin-left: 20px" onclick="retrackGitIndex()">
Retrack <img height="12" width="12" src="images/warning.png">
</button>
<button style="float: left; " onclick="resetGitIgnore()">Reset</button> <br><br>
<br>
<!-- Warning text -->
<div style="
border: thin;
border-style: dashed;
padding: 12px;
">
<img height="15" width="15" src="images/warning.png">
Ignore rules do not apply to tracked files.
"Retrack" removes tracked files, and retrack them according to ignore rules.
"Retrack" may take a long time if there are large folders not being ignored.
</div>
</div>
<BR>
<a href="https://github.com/github/gitignore" onclick="require('nw.gui').Shell.openExternal( this.href);return false;">
Click for .gitignore inspiration
</a>
</div>
<br><br>
<!-- Preview of ignore result -->
<h2 id="header_Settings">Preview</h2>
<hr>
<div id="ignoredList" placeholder="Paste Github-account access token here" style="font-size: 13px;" >
</div>
</div>
</div>
<!-- Scripting -->
</body>
<script>
// File paths
const ignoreFileName = global.state.repos[global.state.repoNumber].localFolder + pathsep + '.gitignore';
const settingsDir = require('os').homedir() + pathsep + '.Pragma-git';
const ignoreFileBackup = settingsDir + pathsep + '.gitignore.pragma-backup';
<!-- On Load event-->
window.addEventListener( 'DOMContentLoaded', async (event) => {
// Always on top
win = gui.Window.get();
win.setAlwaysOnTop( state.alwaysOnTop );
console.log('Create_github_repository.html :DOM fully loaded and parsed');
// Import modules
simpleGit = require('simple-git'); // I am not sure why it did not work to use opener.simpleGit
const os = require('os');
const util = require('./util_module.js'); // Pragma-git common functions
const fs = require('fs');
const pathsep = require('path').sep; // Os-dependent path separator
// Load .ignore file
document.getElementById('ignoreFile').value = fs.readFileSync(ignoreFileName);
// Store backup of .ignore file
await fs.copyFileSync(ignoreFileName, ignoreFileBackup, fs.constants.COPYFILE_FICLONE, (err) => {
if (err) {
console.log("Error copying to file : " + ignoreFileBackup + '\n', err);
}else {
console.log("Success copying to file : " + ignoreFileBackup + '\n', result);
}
});
// Reset staged files (make gitignore handle files that were staged)
// Note: 'Changed files window' selects files that should NOT be commited in the array : localState.unstaged
// That is the mechanism for Main Window Store-operation to add all modified files as default.
// The files deselected in Changed files window' are thus not staged.
await simpleGit( global.state.repos[global.state.repoNumber].localFolder).reset( [ ], onStatus);
function onStatus(err, result ){
console.log('Reset staged files')
console.log(result);
console.log(err);
}
// NOTE 1: Question is if I should restore staged files after closing this window? Now I don't, and rely on above mentioned mechanism
// NOTE 2: The index of tracked files can be updated according to .gitignore, by :
// git rm -r --cached . # Clear index (will show lots of files modified)
// git add . # Update index of tracked files, thus also removing the ones that were previously tracked, but should be untracked by new .gitignore
//
// TODO : Maybe I should have a button to "Reset tracking" ? Or a dialog when saving, if git status --ignored has changed when leaving this window
// A simple "git reset" (means git reset --mixed) would suffice. Maybe add that to Reset button ?
// update html
//await retrackGitIndex() // This is very slow if bad .gitignore. For instance forgetting node_modules/ in .gitignore
await displayIgnoreList();
// Set window size to match content
let padding = Number(document.getElementById('outer-content').style.padding.replace('px','') ) ;
let dx = document.getElementById('outer-content').scrollWidth * global.state.zoom;
let dy = 0.7 * document.getElementById('body').scrollHeight * global.state.zoom;
// Minimum width
if (dx < 500)
dx = 500;
// Minimum height
if (dy < 600)
dy = 600;
let hx = window.outerWidth - window.innerWidth;
let hy = window.outerHeight - window.innerHeight;
}
);
<!-- On Load event-->
window.addEventListener('unload', (event) => {
console.log('edit_ignore.html :DOM fully unloaded -- maybe window closed ');
close();
}
);
<!-- Functions-->
async function resetGitIgnore(){ // Reset button -- Reset .gitignore to state when started editor
console.log('RESET gitignore file');
// Retore .ignore file from backup
await fs.copyFileSync( ignoreFileBackup, ignoreFileName, fs.constants.COPYFILE_FICLONE, (err) => {
if (err) {
console.log("Error copying from file : " + ignoreFileBackup + '\n', err);
}else {
console.log("Success copying from file : " + ignoreFileBackup + '\n', result);
}
});
// Load .ignore backup
document.getElementById('ignoreFile').value = fs.readFileSync(ignoreFileBackup);
// Reset index
try{
await simpleGit( global.state.repos[global.state.repoNumber].localFolder)
.reset( onReset);
function onReset(err, result ){ console.log(result) }
}catch(err){
console.log('Error in git reset()');
console.log(err);
}
await displayIgnoreList();
}
async function close(){ // Save .gitignore and remove backup
console.log('SAVE gitignore file');
// Save, and remove backup if succesful
try{
await fs.writeFileSync(ignoreFileName, document.getElementById('ignoreFile').value);
await fs.unlinkSync(ignoreFileBackup);
opener.document.getElementById('gitignoreText').innerText = fs.readFileSync(ignoreFileName);
}catch(err){
}
}
function save(){
// Save .gitignore
fs.writeFileSync(ignoreFileName, document.getElementById('ignoreFile').value , (err, result) => {
if (err) {
console.log("Error writing to file : " + ignoreFileName + '\n', err);
}else {
console.log("Success writing to file : " + ignoreFileName + '\n', result);
}
} );
}
async function retrackGitIndex() { // Apply button -- Retracks indexed files and preview
// Save .gitignore
save();
// Display .ignore file
document.getElementById('ignoreFile').value = fs.readFileSync(ignoreFileName);
// Clean index : git rm -rf --cached .
try{
await simpleGit( global.state.repos[global.state.repoNumber].localFolder).raw( [ 'rm', '-r', '-f','--cached', '.'], onRm);
function onRm(err, result ){
if (err) {
console.log("Error : git rm -rf --cached . \n" , err);
}else {
console.log("Success : git rm -rf --cached . \n", result);
}
}
}catch(err){
}
document.getElementById('spinner').style.visibility = 'visible';
// Add all files to index : git add .
try{
await simpleGit( state.repos[state.repoNumber].localFolder ).add( '.', onAdd );
function onAdd(err, result) {
if (err) {
document.getElementById('spinner').style.visibility = 'collapse';
console.log("Error : git add . \n" , err);
}else {
document.getElementById('spinner').style.visibility = 'collapse';
console.log("Success : git add . \n", result);
}
}
}catch(err){
document.getElementById('spinner').style.visibility = 'collapse';
}
await displayIgnoreList();
}
async function displayIgnoreList(){ // Preview ignored files
opener.document.getElementById('gitignoreText').innerText = document.getElementById('ignoreFile').value;
// Save .gitignore
save();
let status = "";
await simpleGit( global.state.repos[global.state.repoNumber].localFolder).raw( [ 'status', '--porcelain=v1', '--ignored'], onStatus);
function onStatus(err, result ){
status = result;
}
let splitted = status.split( '\n');
// Find modified files
let modified = '';
let html = '';
for(let row = 0; row < splitted.length; row++) {
if ( !splitted[row].startsWith('!!') ){
// Differentiate between 'deleted'. and 'deleted from tracking'
if ( splitted[row].startsWith('D') ){
// If file does not exist => file is deleted
try {
let fileName = splitted[row].substring(3);
let filePath = global.state.repos[global.state.repoNumber].localFolder + pathsep + fileName;
if (fs.existsSync(filePath)) {
//file exists, must be deleted from tracking
modified += splitted[row].substring(3) + '(untracked) \n';
}else{
modified += splitted[row].substring(3) + '(deleted) \n';
}
} catch(err) {
console.error(err)
}
}else{
modified += splitted[row].substring(3) + '\n';
}
}
}
html += '<h3> Currently modified files : </h3>';
html += '<pre>' + modified + '</pre>';
// Find ignored
let ignored = '';
for(let row = 0; row < splitted.length; row++) {
if ( splitted[row].startsWith('!!') ){
ignored += splitted[row].substring(3) + '\n';;
}
}
html += '<h3> Ignored list : </h3>';
html += '<pre>' + ignored + '</pre>';
document.getElementById('ignoredList').innerHTML = html;
}
// TODO:
// - close-window function (call save and then close window)
// - remove save button
// - help
// - add tab in settings.html to call this window
// - scroll bar for preview (as in textarea above -- see long example in Dicom2usb repo)
</script>
</html>