-
Notifications
You must be signed in to change notification settings - Fork 2
/
git-jam.js
209 lines (189 loc) · 5.3 KB
/
git-jam.js
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
#!/usr/bin/env node
'use strict';
const fs = require('fs');
const path = require('path');
const gitUtils = require('./modules/gitUtils.js');
const filters = require('./modules/filters.js');
const pullpush = require('./modules/pullpush.js');
const constants = require('./modules/constants.json');
const iConfig = require("./modules/interactive-configuration.js");
const spawn = require('child_process').spawn;
const usage = 'Usage : git-jam (init|filter <extension>|push|pull|config [-g] <property> [<value>]|restore|setup-hooks)';
function main(args){
if(args.length === 0){
console.log(usage);
return ;
}
const remainingArgs = args.slice(1);
return gitUtils.getJamPath()
.then(function(jamPath){
if((jamPath == "" || !fs.existsSync(jamPath)) && args[0] != "init"){
console.error("You are not in a jam repository.");
return 1;
}
switch(args[0]){
case 'filter-smudge':
return jamFilterSmudge();
case 'filter-clean':
return jamFilterClean();
case 'init':
return jamInit(remainingArgs);
case 'push':
return jamPush();
case 'pull':
return jamPull();
case 'restore':
return jamRestore();
case 'filter':
return addFilters(remainingArgs);
case 'config':
if(remainingArgs.length > 0){
return jamConfig(remainingArgs);
}
case 'setup-hooks':
return gitUtils.setUpHooks();
default :
console.log(usage);
}
})
.then(function(){
process.stdin.destroy();
})
.catch(function(err){
console.error("The operation failed. Here is the stack trace of the error : ");
console.error(err.stack);
process.stdin.destroy();
});
}
function jamFilterSmudge(){
return filters.jamSmudgeFilter();
}
function jamFilterClean(){
return filters.jamCleanFilter();
}
function jamPush(){
return pullpush.push()
.catch(function(err){
console.log("Pushed failed :", err.message);
});
}
function jamPull(){
return pullpush.pull()
.catch(function(err){
console.log("Pull failed :", err.message);
});
}
function jamRestore(){
return pullpush.restoreFiles()
.catch(function(err){
console.log("Restore failed :", err.message);
});
}
function jamConfig(args){
let g = args.indexOf('-g');
if(g >= 0){
args.splice(g,1);
let result = gitUtils.dotJamConfig(args[0],args[1])
.then(function(result){
if(result){
console.log(result);
}
});
}
else{
let result = gitUtils.gitJamConfig(args[0],args[1])
.then(function(result){
if(result){
console.log(result);
}
})
.catch(function(err){
});
}
}
function addFilters(args){
args.forEach(function(filter){
addFilter(filter);
});
}
function addFilter(filter){
return gitUtils.getJamPath()
.then(function(jamPath){
let attributesPath = path.resolve(jamPath,'../../.gitattributes');
if(!fs.existsSync(attributesPath)){
fs.writeFileSync(attributesPath,'');
}
let attributes = fs.readFileSync(attributesPath,'utf8');
let expression = filter + ' filter=';
let foundAt = attributes.indexOf(filter + ' filter=');
if(foundAt >= 0){
let line = attributes.slice(foundAt);
let endOfLine = line.indexOf('\n') == - 1 ? line.length : line.indexOf('\n');
line = line.slice(0,endOfLine);
attributes = attributes.replace(line,filter + ' filter=jam -crlf');
}
else{
attributes += (attributes[attributes.length] == '\n' || attributes.length == 0 ? '' : '\n') + filter + ' filter=jam -crlf';
}
fs.writeFileSync(attributesPath,attributes);
});
}
function replaceFatFilters(jamPath){
let attributesPath = path.resolve(jamPath,'../../.gitattributes');
if(!fs.existsSync(attributesPath)){
fs.writeFileSync(attributesPath,'');
}
let attributes = fs.readFileSync(attributesPath,'utf8');
let newAttributes = attributes.replace(/fat -crlf/g,"jam -crlf");
fs.writeFileSync(attributesPath,newAttributes);
}
function jamInit(args){
//Make git config entries
return gitUtils.config('filter.jam.clean','git-jam filter-clean')
.then(function(){
return gitUtils.config('filter.jam.smudge','git-jam filter-smudge')
})
.then(function(){
return gitUtils.getJamPath();
})
.then(function(jamPath){
let importFat = args.length > 0 && (args.indexOf('--fat-import') >= 0 || args.indexOf('-f') >=0 );
//if it is a fat repo, move the fat content to jam.
let fatPath = path.resolve(jamPath,'../fat');
if(importFat && fs.existsSync(fatPath)){
fs.renameSync(fatPath,jamPath);
let objectsPath = path.join(jamPath,"objects");
if(fs.existsSync(objectsPath)){
moveAllFiles(objectsPath,jamPath);
}
}
else if(!fs.existsSync(jamPath)){
fs.mkdirSync(jamPath);
}
if(importFat){
replaceFatFilters(jamPath);
}
return Promise.all([pullpush.getCheckedOutJamFiles(jamPath),Promise.resolve(jamPath)]);
})
.then(function(digestsAndPath){
const [digests,jamPath] = digestsAndPath;
fs.writeFileSync(path.join(jamPath,constants.MissingJam),digests.join('\n'));
fs.writeFileSync(path.join(jamPath,constants.ToSyncJam),'');
return Promise.resolve(true);
})
.then(function(){
return iConfig.InteractiveConfiguration().then(function(){return Promise.resolve(true);});
});
}
function moveAllFiles(src,dest){
const files = fs.readdirSync(src);
files.forEach(function(file){
fs.renameSync(path.join(src,file), path.join(dest,file));
});
}
const args = process.argv;
let index = 0;
while(index < args.length && args[index].indexOf('git-jam') < 0){
index++;
}
main(args.slice(index + 1));