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
65 this.lastAdd = new Date();
67 GLib.timeout_add(GLib.PRIORITY_LOW, 500, function() {
68 //TIMEOUT", _this.queue.length , _this.queueRunning].join(', '));
69 if (!_this.queue.length || _this.queueRunning) {
72 var last = Math.floor(((new Date()) - this.lastAdd) / 100);
73 if (last < 4) { // wait 1/2 a seconnd before running.
80 Monitor.prototype.start.call(this);
81 var notification = new Notify.Notification({
83 body : gitlive + "\nMonitoring " + this.monitors.length + " Directories"
86 notification.set_timeout(2000);
91 * - pulls the items off the queue
92 * (as commands run concurrently and new items may get added while it's running)
93 * - runs the queue items
99 this.queueRunning = true;
101 //this.queue.forEach(function (q) {
105 this.action_queue.forEach(function (q) {
108 //this.queue = []; // empty queue!
109 this.action_queue = [];
115 function readResult(sp) {
116 switch (sp.result * 1) {
118 success.push(sp.args.join(' '));
119 if (sp.output.length) success.push(sp.output + '');
120 // if (sp.stderr.length) success.push(sp.stderr + '');
123 failure.push(sp.args.join(' '));
124 if (sp.output.length) failure.push(sp.output);
125 if (sp.stderr.length) failure.push(sp.stderr);
130 cmds.forEach(function(cmd) {
131 // prevent duplicate calls..
132 if (done.indexOf(JSON.stringify(cmd)) > -1) {
135 done.push(JSON.stringify(cmd));
136 // --- we keep a list of repositories that will be pushed to at the end..
138 if (repos.indexOf(cmd.repos) < 0) {
139 repos.push(cmd.repos);
140 // Git.run(cmd.repos , 'pull'); // pull before we push!
145 switch( cmd.action ) {
147 readResult(Git.run(gitlive + '/' + cmd.repo, 'add', cmd.file ));
148 readResult(Git.run(gitlive + '/' + cmd.repo, 'commit', src.file, { message: cmd.file} ));
152 readResult(Git.run(gitlive + '/' + cmd.repo, 'rm', cmd.file ));
153 readResult(Git.run(gitlive + '/' + cmd.repo, 'commit', { all: true, message: cmd.file} ));
157 readResult(Git.run(gitlive + '/' + cmd.repo, 'commit', cmd.file , { message: cmd.file} ));
160 // [ src.gitpath, 'commit' , src.vpath, { message: src.vpath} ]
162 this.action_queue.push({
177 repos.forEach(function(r) {
178 var sp = Git.run(r , 'push', { all: true } );
185 if (success.length) {
186 print(success.join("\n"));
187 var notification = new Notify.Notification({
188 summary: "Git Live Commited",
189 body : success.join("\n")
193 notification.set_timeout(2000);
196 if (failure.length) {
198 var notification = new Notify.Notification({
199 summary: "Git Live ERROR!!",
200 body : failure.join("\n")
204 notification.set_timeout(5000); // show errros for longer
207 this.queueRunning = false;
210 shouldIgnore: function(f)
212 if (f.name[0] == '.') {
214 if (f.name == '.htaccess') {
220 if (f.name.match(/~$/)) {
223 // ignore anything in top level!!!!
224 if (!f.vpath.length) {
233 * set gitpath and vpath
238 parsePath: function(f)
241 var vpath_ar = f.path.substring(gitlive.length +1).split('/');
242 f.repo = vpath_ar.shift();
243 f.gitpath = gitlive + '/' + f.repo;
244 f.vpath = vpath_ar.join('/');
251 onChanged : function(src)
253 return; // always ignore this..?
254 //this.parsePath(src);
258 * results in git add + git commit..
264 onChangesDoneHint : function(src)
267 if (this.shouldIgnore(src)) {
272 if (typeof(this.just_created[src.path]) !='undefined') {
273 delete this.just_created[src.path];
274 this.lastAdd = new Date();
276 // [ src.gitpath, 'add', src.vpath ],
277 // [ src.gitpath, 'commit', src.vpath, { message: src.vpath} ]
280 this.action_queue.push({
290 this.lastAdd = new Date();
292 // [ src.gitpath, 'add', src.vpath ],
293 // [ src.gitpath, 'commit', src.vpath, { message: src.vpath} ]
297 this.action_queue.push({
305 onDeleted : function(src)
308 if (this.shouldIgnore(src)) {
311 // should check if monitor needs removing..
312 // it should also check if it was a directory.. - so we dont have to commit all..
314 this.lastAdd = new Date();
316 // [ src.gitpath, 'rm' , src.vpath ],
317 // [ src.gitpath, 'commit', { all: true, message: src.vpath} ]
320 this.action_queue.push({
327 onCreated : function(src)
330 if (this.shouldIgnore(src)) {
334 if (!GLib.file_test(src.path, GLib.FileTest.IS_DIR)) {
335 this.just_created[src.path] = true;
336 return; // we do not handle file create flags... - use done hint.
338 // director has bee created
339 this.monitor(src.path);
342 since git does not really handle directory adds...
344 this.lastAdd = new Date();
345 this.action_queue.push({
352 [ src.gitpath, 'add' , src.vpath, { all: true } ],
353 [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
360 onAttributeChanged : function(src) {
362 if (this.shouldIgnore(src)) {
365 this.lastAdd = new Date();
369 // [ src.gitpath, 'commit' , src.vpath, { message: src.vpath} ]
371 this.action_queue.push({
380 onMoved : function(src,dest)
383 this.parsePath(dest);
385 if (src.gitpath != dest.gitpath) {
387 this.onCreated(dest);
388 this.onChangedDoneHint(dest);
391 // needs to handle move to/from unsupported types..
393 if (this.shouldIgnore(src)) {
396 if (this.shouldIgnore(dest)) {
399 this.lastAdd = new Date();
401 // [ src.gitpath, 'mv', '-k', src.vpath, dest.vpath ],
402 // [ src.gitpath, 'commit' , src.vpath, dest.vpath ,
403 // { message: 'MOVED ' + src.vpath +' to ' + dest.vpath} ]
406 this.action_queue.push({
422 function errorDialog(data) {
423 var msg = new Gtk.MessageDialog({
424 message_type: Gtk.MessageType.ERROR,
425 buttons : Gtk.ButtonsType.OK,
437 // need a better icon...
443 Notify.init("gitlive");
445 monitor.add(GLib.get_home_dir() + "/gitlive");
448 //icon.signal["activate"].connect(on_left_click);