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!?
14 GI = imports.gi.GIRepository;
15 GLib = imports.gi.GLib;
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');
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.
68 Monitor.prototype.start.call(this);
69 var notification = new Notify.Notification({
71 body : gitlive + "\nMonitoring " + this.monitors.length + " Directories"
74 notification.set_timeout(2000);
79 * - pulls the items off the queue
80 * (as commands run concurrently and new items may get added while it's running)
81 * - runs the queue items
87 this.queueRunning = true;
89 this.queue.forEach(function (q) {
92 this.queue = []; // empty queue!
98 cmds.forEach(function(cmd) {
99 // prevent duplicate calls..
100 if (done.indexOf(cmd.join(',')) > -1) {
103 done.push(cmd.join(','));
105 if (repos.indexOf(cmd[0]) < 0) {
107 Git.run(cmd[0] , 'pull'); // pull before we push!
109 var sp = Git.run.apply(Git,cmd);
111 switch (sp.result * 1) {
113 success.push(sp.args.join(' '));
114 if (sp.output.length) success.push(sp.output + '');
115 // if (sp.stderr.length) success.push(sp.stderr + '');
118 failure.push(sp.args.join(' '));
119 if (sp.output.length) failure.push(sp.output);
120 if (sp.stderr.length) failure.push(sp.stderr);
127 repos.forEach(function(r) {
128 var sp = Git.run(r , 'push', { all: true } );
135 if (success.length) {
136 print(success.join("\n"));
137 var notification = new Notify.Notification({
138 summary: "Git Live Commited",
139 body : success.join("\n")
143 notification.set_timeout(2000);
146 if (failure.length) {
148 var notification = new Notify.Notification({
149 summary: "Git Live ERROR!!",
150 body : failure.join("\n")
154 notification.set_timeout(5000); // show errros for longer
157 this.queueRunning = false;
160 shouldIgnore: function(f)
162 if (f.name[0] == '.') {
164 if (f.name == '.htaccess') {
170 if (f.name.match(/~$/)) {
173 // ignore anything in top level!!!!
174 if (!f.vpath.length) {
182 parsePath: function(f) {
184 var vpath_ar = f.path.substring(gitlive.length +1).split('/');
186 f.gitpath = gitlive + '/' + vpath_ar.shift();
187 f.vpath = vpath_ar.join('/');
194 onChanged : function(src)
196 return; // always ignore this..?
197 //this.parsePath(src);
199 onChangesDoneHint : function(src)
202 if (this.shouldIgnore(src)) {
207 if (typeof(this.just_created[src.path]) !='undefined') {
208 delete this.just_created[src.path];
209 this.lastAdd = new Date();
211 [ src.gitpath, 'add', src.vpath ],
212 [ src.gitpath, 'commit', src.vpath, { message: src.vpath} ]
218 this.lastAdd = new Date();
220 [ src.gitpath, 'add', src.vpath ],
221 [ src.gitpath, 'commit', src.vpath, { message: src.vpath} ]
228 onDeleted : function(src)
231 if (this.shouldIgnore(src)) {
234 // should check if monitor needs removing..
235 // it should also check if it was a directory.. - so we dont have to commit all..
237 this.lastAdd = new Date();
239 [ src.gitpath, 'rm' , src.vpath ],
240 [ src.gitpath, 'commit', { all: true, message: src.vpath} ]
246 onCreated : function(src)
249 if (this.shouldIgnore(src)) {
253 if (!GLib.file_test(src.path, GLib.FileTest.IS_DIR)) {
254 this.just_created[src.path] = true;
255 return; // we do not handle file create flags... - use done hint.
257 // director has bee created
258 this.monitor(src.path);
259 this.lastAdd = new Date();
261 [ src.gitpath, 'add' , src.vpath, { all: true } ],
262 [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
268 onAttributeChanged : function(src) {
270 if (this.shouldIgnore(src)) {
273 this.lastAdd = new Date();
275 [ src.gitpath, 'commit' , src.vpath, { message: src.vpath} ]
281 onMoved : function(src,dest)
284 this.parsePath(dest);
286 if (src.gitpath != dest.gitpath) {
288 this.onCreated(dest);
289 this.onChangedDoneHint(dest);
292 // needs to handle move to/from unsupported types..
294 if (this.shouldIgnore(src)) {
297 if (this.shouldIgnore(dest)) {
300 this.lastAdd = new Date();
302 [ src.gitpath, 'mv', '-k', src.vpath, dest.vpath ],
303 [ src.gitpath, 'commit' , src.vpath, dest.vpath ,
304 { message: 'MOVED ' + src.vpath +' to ' + dest.vpath} ]
315 function errorDialog(data) {
316 var msg = new Gtk.MessageDialog({
317 message_type: Gtk.MessageType.ERROR,
318 buttons : Gtk.ButtonsType.OK,
330 // need a better icon...
336 Notify.init("gitlive");
338 monitor.add(GLib.get_home_dir() + "/gitlive");
341 //icon.signal["activate"].connect(on_left_click);