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 GIRepository = imports.gi.GIRepository
14 GLib = imports.gi.GLib;
16 //print(JSON.stringify(GI, null,4));
17 // we add this in, as it appears to get lost sometimes if we set it using the ENV. variable in builder.sh
18 //GI.Repository.prepend_search_path(GLib.get_home_dir() + '/.Builder/girepository-1.1');
19 GIRepository.Repository.prepend_search_path(GLib.get_home_dir() + '/.Builder/girepository-1.2');
21 var Gio = imports.gi.Gio;
22 var Gtk = imports.gi.Gtk;
23 var Notify = imports.gi.Notify;
25 var Spawn = imports.Spawn;
26 var Git = imports.Git;
27 var StatusIcon = imports.StatusIcon.StatusIcon;
28 var Monitor = imports.Monitor.Monitor;
31 //File = imports[__script_path__+'/../introspection-doc-generator/File.js'].File
32 Gtk.init (null, null);
34 var gitlive = GLib.get_home_dir() + "/gitlive";
36 if (!GLib.file_test(gitlive, GLib.FileTest.IS_DIR)) {
37 var msg = new Gtk.MessageDialog({message_type:
38 Gtk.MessageType.INFO, buttons : Gtk.ButtonsType.OK, text: "GIT Live - ~/gitlive does not exist."});
46 var monitor = new Monitor({
53 this.lastAdd = new Date();
55 GLib.timeout_add(GLib.PRIORITY_LOW, 500, function() {
56 //TIMEOUT", _this.queue.length , _this.queueRunning].join(', '));
57 if (!_this.queue.length || _this.queueRunning) {
60 var last = Math.floor(((new Date()) - this.lastAdd) / 100);
61 if (last < 4) { // wait 1/2 a seconnd before running.
69 var notification = new Notify.Notification({
71 body : gitlive + "\nMonitoring " + this.monitors.length + " Directories",
75 notification.set_timeout(5);
85 * - pulls the items off the queue
86 * (as commands run concurrently and new items may get added while it's running)
87 * - runs the queue items
93 this.queueRunning = true;
95 this.queue.forEach(function (q) {
98 this.queue = []; // empty queue!
104 cmds.forEach(function(cmd) {
105 // prevent duplicate calls..
106 if (done.indexOf(cmd.join(',')) > -1) {
109 done.push(cmd.join(','));
111 if (repos.indexOf(cmd[0]) < 0) {
113 Git.run(cmd[0] , 'pull'); // pull before we push!
115 var sp = Git.run.apply(Git,cmd);
117 switch (sp.result * 1) {
119 success.push(sp.args.join(' '));
120 if (sp.output.length) success.push(sp.output + '');
121 // if (sp.stderr.length) success.push(sp.stderr + '');
124 failure.push(sp.args.join(' '));
125 if (sp.output.length) failure.push(sp.output);
126 if (sp.stderr.length) failure.push(sp.stderr);
133 repos.forEach(function(r) {
134 var sp = Git.run(r , 'push', { all: true } );
141 // catch notification failures.. so we can carry on..
142 if (success.length) {
143 print(success.join("\n"));
145 var notification = new Notify.Notification({
146 summary: "Git Live Commited",
147 body : success.join("\n"),
152 notification.set_timeout(5);
156 if (failure.length) {
158 var notification = new Notify.Notification({
159 summary: "Git Live ERROR!!",
160 body : failure.join("\n"),
165 notification.set_timeout(5); // show errros for longer
172 this.queueRunning = false;
175 shouldIgnore: function(f)
177 if (f.name[0] == '.') {
179 if (f.name == '.htaccess') {
185 if (f.name.match(/~$/)) {
188 // ignore anything in top level!!!!
189 if (!f.vpath.length) {
197 parsePath: function(f) {
199 var vpath_ar = f.path.substring(gitlive.length +1).split('/');
201 f.gitpath = gitlive + '/' + vpath_ar.shift();
202 f.vpath = vpath_ar.join('/');
209 onChanged : function(src)
211 return; // always ignore this..?
212 //this.parsePath(src);
217 * results in git add + git commit..
222 >>>>>>> a198afafefd3a4f8857af843a2a8433158ee8720
223 onChangesDoneHint : function(src)
226 if (this.shouldIgnore(src)) {
232 if (typeof(this.just_created[src.path]) !='undefined') {
233 delete this.just_created[src.path];
234 this.lastAdd = new Date();
236 [ src.gitpath, 'add', src.vpath ],
237 [ src.gitpath, 'commit', src.vpath, { message: src.vpath} ]
243 this.lastAdd = new Date();
245 [ src.gitpath, 'add', src.vpath ],
246 [ src.gitpath, 'commit', src.vpath, { message: src.vpath} ]
253 onDeleted : function(src)
256 if (this.shouldIgnore(src)) {
259 // should check if monitor needs removing..
260 // it should also check if it was a directory.. - so we dont have to commit all..
262 this.lastAdd = new Date();
264 [ src.gitpath, 'rm' , src.vpath ],
265 [ src.gitpath, 'commit', { all: true, message: src.vpath} ]
271 onCreated : function(src)
274 if (this.shouldIgnore(src)) {
278 if (!GLib.file_test(src.path, GLib.FileTest.IS_DIR)) {
279 this.just_created[src.path] = true;
280 return; // we do not handle file create flags... - use done hint.
282 // director has bee created
283 this.monitor(src.path);
284 this.lastAdd = new Date();
286 [ src.gitpath, 'add' , src.vpath, { all: true } ],
287 [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
293 onAttributeChanged : function(src) {
295 if (this.shouldIgnore(src)) {
298 this.lastAdd = new Date();
300 [ src.gitpath, 'commit' , src.vpath, { message: src.vpath} ]
306 onMoved : function(src,dest)
309 this.parsePath(dest);
311 if (src.gitpath != dest.gitpath) {
313 this.onCreated(dest);
314 this.onChangedDoneHint(dest);
317 // needs to handle move to/from unsupported types..
319 if (this.shouldIgnore(src)) {
322 if (this.shouldIgnore(dest)) {
325 this.lastAdd = new Date();
327 [ src.gitpath, 'mv', '-k', src.vpath, dest.vpath ],
328 [ src.gitpath, 'commit' , src.vpath, dest.vpath ,
329 { message: 'MOVED ' + src.vpath +' to ' + dest.vpath} ]
340 function errorDialog(data) {
341 var msg = new Gtk.MessageDialog({
342 message_type: Gtk.MessageType.ERROR,
343 buttons : Gtk.ButtonsType.OK,
355 // need a better icon...
361 Notify.init("gitlive");
363 monitor.add(GLib.get_home_dir() + "/gitlive");
366 //icon.signal["activate"].connect(on_left_click);