2 ///<script type="text/javascript">
6 * inotify hooks for ~/gitlive
7 * that commit and push any changes made.
8 * Bit like a revision controled backed up file system!?
13 GI = imports.gi.GIRepository
14 GLib = imports.gi.GLib;
16 // we add this in, as it appears to get lost sometimes if we set it using the ENV. variable in builder.sh
17 GI.IRepository.prepend_search_path(GLib.get_home_dir() + '/.Builder/girepository-1.1');
21 Notify = imports.gi.Notify;
23 Spawn = imports.Spawn;
25 StatusIcon = imports.StatusIcon.StatusIcon;
26 Monitor = imports.Monitor.Monitor;
29 //File = imports[__script_path__+'/../introspection-doc-generator/File.js'].File
30 Gtk.init (null, null);
32 var gitlive = GLib.get_home_dir() + "/gitlive";
34 if (!GLib.file_test(gitlive, GLib.FileTest.IS_DIR)) {
35 var msg = new Gtk.MessageDialog({message_type:
36 Gtk.MessageType.INFO, buttons : Gtk.ButtonsType.OK, text: "GIT Live - ~/gitlive does not exist."});
44 var monitor = new Monitor({
48 * action: 'add' | rm | update
61 this.lastAdd = new Date();
63 GLib.timeout_add(GLib.PRIORITY_LOW, 500, function() {
64 //TIMEOUT", _this.queue.length , _this.queueRunning].join(', '));
65 if (!_this.queue.length || _this.queueRunning) {
68 var last = Math.floor(((new Date()) - this.lastAdd) / 100);
69 if (last < 4) { // wait 1/2 a seconnd before running.
76 Monitor.prototype.start.call(this);
77 var notification = new Notify.Notification({
79 body : gitlive + "\nMonitoring " + this.monitors.length + " Directories"
82 notification.set_timeout(2000);
87 * - pulls the items off the queue
88 * (as commands run concurrently and new items may get added while it's running)
89 * - runs the queue items
95 this.queueRunning = true;
97 //this.queue.forEach(function (q) {
101 this.action_queue.forEach(function (q) {
104 //this.queue = []; // empty queue!
105 this.action_queue = [];
111 function readResult(sp) {
112 switch (sp.result * 1) {
114 success.push(sp.args.join(' '));
115 if (sp.output.length) success.push(sp.output + '');
116 // if (sp.stderr.length) success.push(sp.stderr + '');
119 failure.push(sp.args.join(' '));
120 if (sp.output.length) failure.push(sp.output);
121 if (sp.stderr.length) failure.push(sp.stderr);
126 cmds.forEach(function(cmd) {
127 // prevent duplicate calls..
128 if (done.indexOf(JSON.stringify(cmd)) > -1) {
131 done.push(JSON.stringify(cmd));
132 // --- we keep a list of repositories that will be pushed to at the end..
134 if (repos.indexOf(cmd.repos) < 0) {
135 repos.push(cmd.repos);
136 // Git.run(cmd.repos , 'pull'); // pull before we push!
139 var gp = gitlive + '/' + cmd.repo;
141 switch( cmd.action ) {
143 readResult(Git.run(gp, 'add', cmd.file ));
144 readResult(Git.run(gp, 'commit', src.file, { message: cmd.file} ));
148 readResult(Git.run(gp, 'rm', cmd.file ));
149 readResult(Git.run(gp, 'commit', { all: true, message: cmd.file} ));
153 readResult(Git.run(gp, 'commit', cmd.file , { message: cmd.file} ));
157 readResult(Git.run(gp, 'mv', cmd.file , cmd.target));
158 readResult(Git.run(gp, 'commit', cmd.file , cmd.target,
159 { message: 'MOVED ' + src.file +' to ' + dest.target } ));
168 repos.forEach(function(r) {
169 var sp = Git.run(gitlive + '/' +r , 'push', { all: true } );
176 if (success.length) {
177 print(success.join("\n"));
178 var notification = new Notify.Notification({
179 summary: "Git Live Commited",
180 body : success.join("\n")
184 notification.set_timeout(2000);
187 if (failure.length) {
189 var notification = new Notify.Notification({
190 summary: "Git Live ERROR!!",
191 body : failure.join("\n")
195 notification.set_timeout(5000); // show errros for longer
198 this.queueRunning = false;
201 shouldIgnore: function(f)
203 if (f.name[0] == '.') {
205 if (f.name == '.htaccess') {
211 if (f.name.match(/~$/)) {
214 // ignore anything in top level!!!!
215 if (!f.vpath.length) {
224 * set gitpath and vpath
229 parsePath: function(f)
232 var vpath_ar = f.path.substring(gitlive.length +1).split('/');
233 f.repo = vpath_ar.shift();
234 f.gitpath = gitlive + '/' + f.repo;
235 f.vpath = vpath_ar.join('/');
242 onChanged : function(src)
244 return; // always ignore this..?
245 //this.parsePath(src);
249 * results in git add + git commit..
255 onChangesDoneHint : function(src)
258 if (this.shouldIgnore(src)) {
263 if (typeof(this.just_created[src.path]) !='undefined') {
264 delete this.just_created[src.path];
265 this.lastAdd = new Date();
267 // [ src.gitpath, 'add', src.vpath ],
268 // [ src.gitpath, 'commit', src.vpath, { message: src.vpath} ]
271 this.action_queue.push({
281 this.lastAdd = new Date();
283 // [ src.gitpath, 'add', src.vpath ],
284 // [ src.gitpath, 'commit', src.vpath, { message: src.vpath} ]
288 this.action_queue.push({
296 onDeleted : function(src)
299 if (this.shouldIgnore(src)) {
302 // should check if monitor needs removing..
303 // it should also check if it was a directory.. - so we dont have to commit all..
305 this.lastAdd = new Date();
307 // [ src.gitpath, 'rm' , src.vpath ],
308 // [ src.gitpath, 'commit', { all: true, message: src.vpath} ]
311 this.action_queue.push({
318 onCreated : function(src)
321 if (this.shouldIgnore(src)) {
325 if (!GLib.file_test(src.path, GLib.FileTest.IS_DIR)) {
326 this.just_created[src.path] = true;
327 return; // we do not handle file create flags... - use done hint.
329 // director has bee created
330 this.monitor(src.path);
333 since git does not really handle directory adds...
335 this.lastAdd = new Date();
336 this.action_queue.push({
343 [ src.gitpath, 'add' , src.vpath, { all: true } ],
344 [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
351 onAttributeChanged : function(src) {
353 if (this.shouldIgnore(src)) {
356 this.lastAdd = new Date();
360 // [ src.gitpath, 'commit' , src.vpath, { message: src.vpath} ]
362 this.action_queue.push({
371 onMoved : function(src,dest)
374 this.parsePath(dest);
376 if (src.gitpath != dest.gitpath) {
378 this.onCreated(dest);
379 this.onChangedDoneHint(dest);
382 // needs to handle move to/from unsupported types..
384 if (this.shouldIgnore(src)) {
387 if (this.shouldIgnore(dest)) {
390 this.lastAdd = new Date();
392 // [ src.gitpath, 'mv', '-k', src.vpath, dest.vpath ],
393 // [ src.gitpath, 'commit' , src.vpath, dest.vpath ,
394 // { message: 'MOVED ' + src.vpath +' to ' + dest.vpath} ]
397 this.action_queue.push({
413 function errorDialog(data) {
414 var msg = new Gtk.MessageDialog({
415 message_type: Gtk.MessageType.ERROR,
416 buttons : Gtk.ButtonsType.OK,
428 // need a better icon...
434 Notify.init("gitlive");
436 monitor.add(GLib.get_home_dir() + "/gitlive");
439 //icon.signal["activate"].connect(on_left_click);