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
62 this.lastAdd = new Date();
64 GLib.timeout_add(GLib.PRIORITY_LOW, 500, function() {
65 //TIMEOUT", _this.queue.length , _this.queueRunning].join(', '));
66 if (!_this.queue.length || _this.queueRunning) {
69 var last = Math.floor(((new Date()) - this.lastAdd) / 100);
70 if (last < 4) { // wait 1/2 a seconnd before running.
77 Monitor.prototype.start.call(this);
78 var notification = new Notify.Notification({
80 body : gitlive + "\nMonitoring " + this.monitors.length + " Directories"
83 notification.set_timeout(2000);
88 * - pulls the items off the queue
89 * (as commands run concurrently and new items may get added while it's running)
90 * - runs the queue items
96 this.queueRunning = true;
98 //this.queue.forEach(function (q) {
102 this.action_queue.forEach(function (q) {
105 //this.queue = []; // empty queue!
106 this.action_queue = [];
112 function readResult(sp) {
113 switch (sp.result * 1) {
115 success.push(sp.args.join(' '));
116 if (sp.output.length) success.push(sp.output + '');
117 // if (sp.stderr.length) success.push(sp.stderr + '');
120 failure.push(sp.args.join(' '));
121 if (sp.output.length) failure.push(sp.output);
122 if (sp.stderr.length) failure.push(sp.stderr);
127 cmds.forEach(function(cmd) {
128 // prevent duplicate calls..
129 if (done.indexOf(JSON.stringify(cmd)) > -1) {
132 done.push(JSON.stringify(cmd));
133 // --- we keep a list of repositories that will be pushed to at the end..
135 if (repos.indexOf(cmd.repos) < 0) {
136 repos.push(cmd.repos);
137 // Git.run(cmd.repos , 'pull'); // pull before we push!
140 var gp = gitlive + '/' + cmd.repo;
142 switch( cmd.action ) {
144 readResult(Git.run(gp, 'add', cmd.file ));
145 readResult(Git.run(gp, 'commit', src.file, { message: cmd.file} ));
149 readResult(Git.run(gp, 'rm', cmd.file ));
150 readResult(Git.run(gp, 'commit', { all: true, message: cmd.file} ));
154 readResult(Git.run(gp, 'commit', cmd.file , { message: cmd.file} ));
158 readResult(Git.run(gp, 'mv', cmd.file , cmd.target));
159 readResult(Git.run(gp, 'commit', cmd.file , cmd.target,
160 { message: 'MOVED ' + src.file +' to ' + dest.target } ));
169 repos.forEach(function(r) {
170 var sp = Git.run(gitlive + '/' +r , 'push', { all: true } );
177 if (success.length) {
178 print(success.join("\n"));
179 var notification = new Notify.Notification({
180 summary: "Git Live Commited",
181 body : success.join("\n")
185 notification.set_timeout(2000);
188 if (failure.length) {
190 var notification = new Notify.Notification({
191 summary: "Git Live ERROR!!",
192 body : failure.join("\n")
196 notification.set_timeout(5000); // show errros for longer
199 this.queueRunning = false;
202 shouldIgnore: function(f)
204 if (f.name[0] == '.') {
206 if (f.name == '.htaccess') {
212 if (f.name.match(/~$/)) {
215 // ignore anything in top level!!!!
216 if (!f.vpath.length) {
225 * set gitpath and vpath
230 parsePath: function(f)
233 var vpath_ar = f.path.substring(gitlive.length +1).split('/');
234 f.repo = vpath_ar.shift();
235 f.gitpath = gitlive + '/' + f.repo;
236 f.vpath = vpath_ar.join('/');
243 onChanged : function(src)
245 return; // always ignore this..?
246 //this.parsePath(src);
250 * results in git add + git commit..
256 onChangesDoneHint : function(src)
259 if (this.shouldIgnore(src)) {
264 if (typeof(this.just_created[src.path]) !='undefined') {
265 delete this.just_created[src.path];
266 this.lastAdd = new Date();
268 // [ src.gitpath, 'add', src.vpath ],
269 // [ src.gitpath, 'commit', src.vpath, { message: src.vpath} ]
272 this.action_queue.push({
282 this.lastAdd = new Date();
284 // [ src.gitpath, 'add', src.vpath ],
285 // [ src.gitpath, 'commit', src.vpath, { message: src.vpath} ]
289 this.action_queue.push({
297 onDeleted : function(src)
300 if (this.shouldIgnore(src)) {
303 // should check if monitor needs removing..
304 // it should also check if it was a directory.. - so we dont have to commit all..
306 this.lastAdd = new Date();
308 // [ src.gitpath, 'rm' , src.vpath ],
309 // [ src.gitpath, 'commit', { all: true, message: src.vpath} ]
312 this.action_queue.push({
319 onCreated : function(src)
322 if (this.shouldIgnore(src)) {
326 if (!GLib.file_test(src.path, GLib.FileTest.IS_DIR)) {
327 this.just_created[src.path] = true;
328 return; // we do not handle file create flags... - use done hint.
330 // director has bee created
331 this.monitor(src.path);
334 since git does not really handle directory adds...
336 this.lastAdd = new Date();
337 this.action_queue.push({
344 [ src.gitpath, 'add' , src.vpath, { all: true } ],
345 [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
352 onAttributeChanged : function(src) {
354 if (this.shouldIgnore(src)) {
357 this.lastAdd = new Date();
361 // [ src.gitpath, 'commit' , src.vpath, { message: src.vpath} ]
363 this.action_queue.push({
372 onMoved : function(src,dest)
375 this.parsePath(dest);
377 if (src.gitpath != dest.gitpath) {
379 this.onCreated(dest);
380 this.onChangedDoneHint(dest);
383 // needs to handle move to/from unsupported types..
385 if (this.shouldIgnore(src)) {
388 if (this.shouldIgnore(dest)) {
391 this.lastAdd = new Date();
393 // [ src.gitpath, 'mv', '-k', src.vpath, dest.vpath ],
394 // [ src.gitpath, 'commit' , src.vpath, dest.vpath ,
395 // { message: 'MOVED ' + src.vpath +' to ' + dest.vpath} ]
398 this.action_queue.push({
414 function errorDialog(data) {
415 var msg = new Gtk.MessageDialog({
416 message_type: Gtk.MessageType.ERROR,
417 buttons : Gtk.ButtonsType.OK,
429 // need a better icon...
435 Notify.init("gitlive");
437 monitor.add(GLib.get_home_dir() + "/gitlive");
440 //icon.signal["activate"].connect(on_left_click);