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;
18 // we add this in, as it appears to get lost sometimes if we set it using the ENV. variable in builder.sh
19 GI.Repository.prepend_search_path(GLib.get_home_dir() + '/.Builder/girepository-1.2');
22 var Gio = imports.gi.Gio;
23 var Gtk = imports.gi.Gtk;
24 var Notify = imports.gi.Notify;
26 var Spawn = imports.Spawn;
27 var Git = imports.Git;
28 var StatusIcon = imports.StatusIcon.StatusIcon;
29 var Monitor = imports.Monitor.Monitor;
32 //File = imports[__script_path__+'/../introspection-doc-generator/File.js'].File
33 Gtk.init (null, null);
35 var gitlive = GLib.get_home_dir() + "/gitlive";
37 if (!GLib.file_test(gitlive, GLib.FileTest.IS_DIR)) {
38 var msg = new Gtk.MessageDialog({message_type:
39 Gtk.MessageType.INFO, buttons : Gtk.ButtonsType.OK, text: "GIT Live - ~/gitlive does not exist."});
47 var monitor = new Monitor({
54 this.lastAdd = new Date();
56 GLib.timeout_add(GLib.PRIORITY_LOW, 500, function() {
57 //TIMEOUT", _this.queue.length , _this.queueRunning].join(', '));
58 if (!_this.queue.length || _this.queueRunning) {
61 var last = Math.floor(((new Date()) - this.lastAdd) / 100);
62 if (last < 4) { // wait 1/2 a seconnd before running.
69 Monitor.prototype.start.call(this);
70 var notification = new Notify.Notification({
72 body : gitlive + "\nMonitoring " + this.monitors.length + " Directories"
75 notification.set_timeout(2000);
80 * - pulls the items off the queue
81 * (as commands run concurrently and new items may get added while it's running)
82 * - runs the queue items
88 this.queueRunning = true;
90 this.queue.forEach(function (q) {
93 this.queue = []; // empty queue!
99 cmds.forEach(function(cmd) {
100 // prevent duplicate calls..
101 if (done.indexOf(cmd.join(',')) > -1) {
104 done.push(cmd.join(','));
106 if (repos.indexOf(cmd[0]) < 0) {
108 Git.run(cmd[0] , 'pull'); // pull before we push!
110 var sp = Git.run.apply(Git,cmd);
112 switch (sp.result * 1) {
114 success.push(sp.args.join(' '));
115 if (sp.output.length) success.push(sp.output + '');
116 // if (sp.stderr.length) success.push(sp.stderr + '');
119 failure.push(sp.args.join(' '));
120 if (sp.output.length) failure.push(sp.output);
121 if (sp.stderr.length) failure.push(sp.stderr);
128 repos.forEach(function(r) {
129 var sp = Git.run(r , 'push', { all: true } );
136 if (success.length) {
137 print(success.join("\n"));
138 var notification = new Notify.Notification({
139 summary: "Git Live Commited",
140 body : success.join("\n")
144 notification.set_timeout(2000);
147 if (failure.length) {
149 var notification = new Notify.Notification({
150 summary: "Git Live ERROR!!",
151 body : failure.join("\n")
155 notification.set_timeout(5000); // show errros for longer
158 this.queueRunning = false;
161 shouldIgnore: function(f)
163 if (f.name[0] == '.') {
165 if (f.name == '.htaccess') {
171 if (f.name.match(/~$/)) {
174 // ignore anything in top level!!!!
175 if (!f.vpath.length) {
183 parsePath: function(f) {
185 var vpath_ar = f.path.substring(gitlive.length +1).split('/');
187 f.gitpath = gitlive + '/' + vpath_ar.shift();
188 f.vpath = vpath_ar.join('/');
195 onChanged : function(src)
197 return; // always ignore this..?
198 //this.parsePath(src);
200 onChangesDoneHint : function(src)
203 if (this.shouldIgnore(src)) {
208 if (typeof(this.just_created[src.path]) !='undefined') {
209 delete this.just_created[src.path];
210 this.lastAdd = new Date();
212 [ src.gitpath, 'add', src.vpath ],
213 [ src.gitpath, 'commit', src.vpath, { message: src.vpath} ]
219 this.lastAdd = new Date();
221 [ src.gitpath, 'add', src.vpath ],
222 [ src.gitpath, 'commit', src.vpath, { message: src.vpath} ]
229 onDeleted : function(src)
232 if (this.shouldIgnore(src)) {
235 // should check if monitor needs removing..
236 // it should also check if it was a directory.. - so we dont have to commit all..
238 this.lastAdd = new Date();
240 [ src.gitpath, 'rm' , src.vpath ],
241 [ src.gitpath, 'commit', { all: true, message: src.vpath} ]
247 onCreated : function(src)
250 if (this.shouldIgnore(src)) {
254 if (!GLib.file_test(src.path, GLib.FileTest.IS_DIR)) {
255 this.just_created[src.path] = true;
256 return; // we do not handle file create flags... - use done hint.
258 // director has bee created
259 this.monitor(src.path);
260 this.lastAdd = new Date();
262 [ src.gitpath, 'add' , src.vpath, { all: true } ],
263 [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
269 onAttributeChanged : function(src) {
271 if (this.shouldIgnore(src)) {
274 this.lastAdd = new Date();
276 [ src.gitpath, 'commit' , src.vpath, { message: src.vpath} ]
282 onMoved : function(src,dest)
285 this.parsePath(dest);
287 if (src.gitpath != dest.gitpath) {
289 this.onCreated(dest);
290 this.onChangedDoneHint(dest);
293 // needs to handle move to/from unsupported types..
295 if (this.shouldIgnore(src)) {
298 if (this.shouldIgnore(dest)) {
301 this.lastAdd = new Date();
303 [ src.gitpath, 'mv', '-k', src.vpath, dest.vpath ],
304 [ src.gitpath, 'commit' , src.vpath, dest.vpath ,
305 { message: 'MOVED ' + src.vpath +' to ' + dest.vpath} ]
316 function errorDialog(data) {
317 var msg = new Gtk.MessageDialog({
318 message_type: Gtk.MessageType.ERROR,
319 buttons : Gtk.ButtonsType.OK,
331 // need a better icon...
337 Notify.init("gitlive");
339 monitor.add(GLib.get_home_dir() + "/gitlive");
342 //icon.signal["activate"].connect(on_left_click);