-
Notifications
You must be signed in to change notification settings - Fork 14
/
doGit.gs
185 lines (155 loc) · 5.22 KB
/
doGit.gs
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
"use strict";
/**
* RUN THIRD
* Commits all the data in the extract folders to GIT
*
* run all the dependencies and update the info files
* @return {Array.object} the updated infos
*/
function doGit() {
// this is the extractor handle
var extractor = getExtractor();
// get all the info files
var result = extractor.getAllTheInfos();
if (!result.success) {
throw 'failed to get all the infos ' + JSON.stringify(result);
}
var infos = result.data.content;
// get a git handle
var git = new GasGit(extractor).setAccessToken( getAccessToken('gasgit'));
// get all my repos
var result = git.getMyRepos();
if (!result.success) {
throw 'failed to get all the repos ' + JSON.stringify(result);
}
var repos = result.data;
var starter = false;
// create any non existent repos
infos.forEach(function(d){
// hack for starting at a given name for big reruns
// if (d.repo === "SlidesMerge") starter = true;
// we'll only bother with this if the committed date is earlier than the modified date
if (d.committedDate < d.modifiedDate || !d.committedDate || SETTINGS.GIT.ALL || starter) {
// see if we know it
var repo = findRepo(d,repos);
// if not then create it
if (!repo) {
var result = git.createRepo (d.repo);
if(!result.success) {
Logger.log(JSON.stringify(result));
throw "error creating " + d.repo + ' sure you havet got an upper/lower case confusion? ';
}
else {
repo = result.data;
}
Logger.log('created repo for ' + d.repo);
}
// create a readme if there isnt one
var f = git.getFileByPath(extractor.getEnums().FILES.README,repo);
if(!f.success) {
Logger.log('writing ' + extractor.getEnums().FILES.README + ' for ' + repo.name);
var result = git.getAndCommitAFile (
d.readmeFileId,
extractor.getEnums().FILES.README,
repo
);
}
}
});
// get the updated repos
var result = git.getMyRepos();
if (!result.success) {
throw 'failed to get all the repos ' + JSON.stringify(result);
}
var repos = result.data;
// create/update new dependency files
infos.forEach (function(d) {
if (d.committedDate < d.modifiedDate || !d.committedDate) {
// find the repo - it should exist since we would have created it
var repo = cUseful.Utils.expBackoff ( function () {
return findRepo (d , repos);
}, {
// if we dont have a repo try a few times, because git sometimes comes back before repo can be found
lookahead: function (response,attempt) {
return attempt < 4 && !response;
}
});
if (!repo) {
throw 'should have found repo ' + d.repo;
}
// write dependency contents to git
Logger.log('writing ' + extractor.getEnums().FILES.DEPENDENCIES + ' for ' + repo.name);
var result = git.getAndCommitAFile (
d.dependenciesFileId,
extractor.getEnums().FILES.DEPENDENCIES,
repo
);
// write info file to git
Logger.log('writing ' + extractor.getEnums().FILES.INFO + ' for ' + repo.name);
var result = git.getAndCommitAFile (
d.fileId,
extractor.getEnums().FILES.INFO,
repo
);
// write the all the modules from the main project
Logger.log('writing ' + "modules" + ' for ' + repo.name);
d.modules.forEach(function(m) {
var result = git.getAndCommitAFile (
m.sourceId,
SETTINGS.GIT.SCRIPTS + m.fileName,
repo
);
});
// write the libraries/dependencies
Logger.log('writing ' + "libraries" + ' for ' + repo.name);
d.dependencies.forEach(function(m) {
// if we find this , then we know it and can write the source
var f = findInfo (m , infos) ;
if (f) {
Logger.log ("found library " + m.library);
f.modules.forEach(function(e) {
var result = git.getAndCommitAFile (
e.sourceId,
SETTINGS.GIT.LIBRARIES + f.title + "/" + e.fileName,
repo
);
});
}
if ((!f && m.known) || (f && !m.known)) {
throw 'should have found library ' + m.library + ' in repo ' + repo.name;
}
});
// need to rewrite info file with committed date
d.committedDate = new Date().getTime();
extractor.putContent (d.fileId, d.title, d );
}
else {
Logger.log('No action needed for ' +
d.repo +
':last commit (' +
new Date(d.committedDate).toLocaleString() +
') after last modification (' +
new Date(d.modifiedDate).toLocaleString() +
')' );
}
});
function findInfo (lib,infs) {
var f;
for (var p=0 ; p < infs.length && !f ; p++) {
if(lib.library === infs[p].title) {
f = infs[p];
}
}
return f;
}
function findRepo (inf,rs) {
var f;
for (var p=0 ; p < rs.length && !f ; p++) {
if(inf.repo === rs[p].name) {
f = rs[p];
}
}
return f;
}
return infos;
}