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!?
15 GI = imports.gi.GIRepository;
16 GLib = imports.gi.GLib;
19 // we add this in, as it appears to get lost sometimes if we set it using the ENV. variable in builder.sh
20 GI.Repository.prepend_search_path(GLib.get_home_dir() + '/.Builder/girepository-1.2');
23 var Gio = imports.gi.Gio;
24 var Gtk = imports.gi.Gtk;
25 var Notify = imports.gi.Notify;
27 var Spawn = imports.Spawn;
28 var Git = imports.Git;
29 var StatusIcon = imports.StatusIcon.StatusIcon;
30 var Monitor = imports.Monitor.Monitor;
33 //File = imports[__script_path__+'/../introspection-doc-generator/File.js'].File
34 Gtk.init (null, null);
36 var gitlive = GLib.get_home_dir() + "/gitlive";
38 if (!GLib.file_test(gitlive, GLib.FileTest.IS_DIR)) {
39 var msg = new Gtk.MessageDialog({message_type:
40 Gtk.MessageType.INFO, buttons : Gtk.ButtonsType.OK, text: "GIT Live - ~/gitlive does not exist."});
48 var monitor = new Monitor({
55 this.lastAdd = new Date();
57 GLib.timeout_add(GLib.PRIORITY_LOW, 500, function() {
58 //TIMEOUT", _this.queue.length , _this.queueRunning].join(', '));
59 if (!_this.queue.length || _this.queueRunning) {
62 var last = Math.floor(((new Date()) - this.lastAdd) / 100);
63 if (last < 4) { // wait 1/2 a seconnd before running.
70 Monitor.prototype.start.call(this);
71 var notification = new Notify.Notification({
73 body : gitlive + "\nMonitoring " + this.monitors.length + " Directories"
76 notification.set_timeout(2000);
81 * - pulls the items off the queue
82 * (as commands run concurrently and new items may get added while it's running)
83 * - runs the queue items
89 this.queueRunning = true;
91 this.queue.forEach(function (q) {
94 this.queue = []; // empty queue!
100 cmds.forEach(function(cmd) {
101 // prevent duplicate calls..
102 if (done.indexOf(cmd.join(',')) > -1) {
105 done.push(cmd.join(','));
107 if (repos.indexOf(cmd[0]) < 0) {
109 Git.run(cmd[0] , 'pull'); // pull before we push!
111 var sp = Git.run.apply(Git,cmd);
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);
129 repos.forEach(function(r) {
130 var sp = Git.run(r , 'push', { all: true } );
137 if (success.length) {
138 print(success.join("\n"));
139 var notification = new Notify.Notification({
140 summary: "Git Live Commited",
141 body : success.join("\n")
145 notification.set_timeout(2000);
148 if (failure.length) {
150 var notification = new Notify.Notification({
151 summary: "Git Live ERROR!!",
152 body : failure.join("\n")
156 notification.set_timeout(5000); // show errros for longer
159 this.queueRunning = false;
162 shouldIgnore: function(f)
164 if (f.name[0] == '.') {
166 if (f.name == '.htaccess') {
172 if (f.name.match(/~$/)) {
175 // ignore anything in top level!!!!
176 if (!f.vpath.length) {
184 parsePath: function(f) {
186 var vpath_ar = f.path.substring(gitlive.length +1).split('/');
188 f.gitpath = gitlive + '/' + vpath_ar.shift();
189 f.vpath = vpath_ar.join('/');
196 onChanged : function(src)
198 return; // always ignore this..?
199 //this.parsePath(src);
201 onChangesDoneHint : function(src)
204 if (this.shouldIgnore(src)) {
209 if (typeof(this.just_created[src.path]) !='undefined') {
210 delete this.just_created[src.path];
211 this.lastAdd = new Date();
213 [ src.gitpath, 'add', src.vpath ],
214 [ src.gitpath, 'commit', src.vpath, { message: src.vpath} ]
220 this.lastAdd = new Date();
222 [ src.gitpath, 'add', src.vpath ],
223 [ src.gitpath, 'commit', src.vpath, { message: src.vpath} ]
230 onDeleted : function(src)
233 if (this.shouldIgnore(src)) {
236 // should check if monitor needs removing..
237 // it should also check if it was a directory.. - so we dont have to commit all..
239 this.lastAdd = new Date();
241 [ src.gitpath, 'rm' , src.vpath ],
242 [ src.gitpath, 'commit', { all: true, message: src.vpath} ]
248 onCreated : function(src)
251 if (this.shouldIgnore(src)) {
255 if (!GLib.file_test(src.path, GLib.FileTest.IS_DIR)) {
256 this.just_created[src.path] = true;
257 return; // we do not handle file create flags... - use done hint.
259 // director has bee created
260 this.monitor(src.path);
261 this.lastAdd = new Date();
263 [ src.gitpath, 'add' , src.vpath, { all: true } ],
264 [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
270 onAttributeChanged : function(src) {
272 if (this.shouldIgnore(src)) {
275 this.lastAdd = new Date();
277 [ src.gitpath, 'commit' , src.vpath, { message: src.vpath} ]
283 onMoved : function(src,dest)
286 this.parsePath(dest);
288 if (src.gitpath != dest.gitpath) {
290 this.onCreated(dest);
291 this.onChangedDoneHint(dest);
294 // needs to handle move to/from unsupported types..
296 if (this.shouldIgnore(src)) {
299 if (this.shouldIgnore(dest)) {
302 this.lastAdd = new Date();
304 [ src.gitpath, 'mv', '-k', src.vpath, dest.vpath ],
305 [ src.gitpath, 'commit' , src.vpath, dest.vpath ,
306 { message: 'MOVED ' + src.vpath +' to ' + dest.vpath} ]
317 function errorDialog(data) {
318 var msg = new Gtk.MessageDialog({
319 message_type: Gtk.MessageType.ERROR,
320 buttons : Gtk.ButtonsType.OK,
332 // need a better icon...
338 Notify.init("gitlive");
340 monitor.add(GLib.get_home_dir() + "/gitlive");
343 //icon.signal["activate"].connect(on_left_click);