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 = [];
114 cmds.forEach(function(cmd) {
115 // prevent duplicate calls..
116 if (done.indexOf(JSON.stringify(cmd)) > -1) {
119 done.push(cmd.join(','));
121 if (repos.indexOf(cmd[0]) < 0) {
123 Git.run(cmd[0] , 'pull'); // pull before we push!
125 var sp = Git.run.apply(Git,cmd);
127 switch (sp.result * 1) {
129 success.push(sp.args.join(' '));
130 if (sp.output.length) success.push(sp.output + '');
131 // if (sp.stderr.length) success.push(sp.stderr + '');
134 failure.push(sp.args.join(' '));
135 if (sp.output.length) failure.push(sp.output);
136 if (sp.stderr.length) failure.push(sp.stderr);
143 repos.forEach(function(r) {
144 var sp = Git.run(r , 'push', { all: true } );
151 if (success.length) {
152 print(success.join("\n"));
153 var notification = new Notify.Notification({
154 summary: "Git Live Commited",
155 body : success.join("\n")
159 notification.set_timeout(2000);
162 if (failure.length) {
164 var notification = new Notify.Notification({
165 summary: "Git Live ERROR!!",
166 body : failure.join("\n")
170 notification.set_timeout(5000); // show errros for longer
173 this.queueRunning = false;
176 shouldIgnore: function(f)
178 if (f.name[0] == '.') {
180 if (f.name == '.htaccess') {
186 if (f.name.match(/~$/)) {
189 // ignore anything in top level!!!!
190 if (!f.vpath.length) {
199 * set gitpath and vpath
204 parsePath: function(f)
207 var vpath_ar = f.path.substring(gitlive.length +1).split('/');
208 f.repo = vpath_ar.shift();
209 f.gitpath = gitlive + '/' + f.repo;
210 f.vpath = vpath_ar.join('/');
217 onChanged : function(src)
219 return; // always ignore this..?
220 //this.parsePath(src);
224 * results in git add + git commit..
230 onChangesDoneHint : function(src)
233 if (this.shouldIgnore(src)) {
238 if (typeof(this.just_created[src.path]) !='undefined') {
239 delete this.just_created[src.path];
240 this.lastAdd = new Date();
242 // [ src.gitpath, 'add', src.vpath ],
243 // [ src.gitpath, 'commit', src.vpath, { message: src.vpath} ]
246 this.action_queue.push({
256 this.lastAdd = new Date();
258 // [ src.gitpath, 'add', src.vpath ],
259 // [ src.gitpath, 'commit', src.vpath, { message: src.vpath} ]
263 this.action_queue.push({
271 onDeleted : function(src)
274 if (this.shouldIgnore(src)) {
277 // should check if monitor needs removing..
278 // it should also check if it was a directory.. - so we dont have to commit all..
280 this.lastAdd = new Date();
282 // [ src.gitpath, 'rm' , src.vpath ],
283 // [ src.gitpath, 'commit', { all: true, message: src.vpath} ]
286 this.action_queue.push({
293 onCreated : function(src)
296 if (this.shouldIgnore(src)) {
300 if (!GLib.file_test(src.path, GLib.FileTest.IS_DIR)) {
301 this.just_created[src.path] = true;
302 return; // we do not handle file create flags... - use done hint.
304 // director has bee created
305 this.monitor(src.path);
308 since git does not really handle directory adds...
310 this.lastAdd = new Date();
311 this.action_queue.push({
318 [ src.gitpath, 'add' , src.vpath, { all: true } ],
319 [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
326 onAttributeChanged : function(src) {
328 if (this.shouldIgnore(src)) {
331 this.lastAdd = new Date();
335 // [ src.gitpath, 'commit' , src.vpath, { message: src.vpath} ]
337 this.action_queue.push({
346 onMoved : function(src,dest)
349 this.parsePath(dest);
351 if (src.gitpath != dest.gitpath) {
353 this.onCreated(dest);
354 this.onChangedDoneHint(dest);
357 // needs to handle move to/from unsupported types..
359 if (this.shouldIgnore(src)) {
362 if (this.shouldIgnore(dest)) {
365 this.lastAdd = new Date();
367 // [ src.gitpath, 'mv', '-k', src.vpath, dest.vpath ],
368 // [ src.gitpath, 'commit' , src.vpath, dest.vpath ,
369 // { message: 'MOVED ' + src.vpath +' to ' + dest.vpath} ]
372 this.action_queue.push({
388 function errorDialog(data) {
389 var msg = new Gtk.MessageDialog({
390 message_type: Gtk.MessageType.ERROR,
391 buttons : Gtk.ButtonsType.OK,
403 // need a better icon...
409 Notify.init("gitlive");
411 monitor.add(GLib.get_home_dir() + "/gitlive");
414 //icon.signal["activate"].connect(on_left_click);