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) {
104 this.queue = []; // empty queue!
105 this.action_queue = [];
110 cmds.forEach(function(cmd) {
111 // prevent duplicate calls..
112 if (done.indexOf(cmd.join(',')) > -1) {
115 done.push(cmd.join(','));
117 if (repos.indexOf(cmd[0]) < 0) {
119 Git.run(cmd[0] , 'pull'); // pull before we push!
121 var sp = Git.run.apply(Git,cmd);
123 switch (sp.result * 1) {
125 success.push(sp.args.join(' '));
126 if (sp.output.length) success.push(sp.output + '');
127 // if (sp.stderr.length) success.push(sp.stderr + '');
130 failure.push(sp.args.join(' '));
131 if (sp.output.length) failure.push(sp.output);
132 if (sp.stderr.length) failure.push(sp.stderr);
139 repos.forEach(function(r) {
140 var sp = Git.run(r , 'push', { all: true } );
147 if (success.length) {
148 print(success.join("\n"));
149 var notification = new Notify.Notification({
150 summary: "Git Live Commited",
151 body : success.join("\n")
155 notification.set_timeout(2000);
158 if (failure.length) {
160 var notification = new Notify.Notification({
161 summary: "Git Live ERROR!!",
162 body : failure.join("\n")
166 notification.set_timeout(5000); // show errros for longer
169 this.queueRunning = false;
172 shouldIgnore: function(f)
174 if (f.name[0] == '.') {
176 if (f.name == '.htaccess') {
182 if (f.name.match(/~$/)) {
185 // ignore anything in top level!!!!
186 if (!f.vpath.length) {
195 * set gitpath and vpath
200 parsePath: function(f)
203 var vpath_ar = f.path.substring(gitlive.length +1).split('/');
204 f.repo = vpath_ar.shift();
205 f.gitpath = gitlive + '/' + f.repo;
206 f.vpath = vpath_ar.join('/');
213 onChanged : function(src)
215 return; // always ignore this..?
216 //this.parsePath(src);
220 * results in git add + git commit..
226 onChangesDoneHint : function(src)
229 if (this.shouldIgnore(src)) {
234 if (typeof(this.just_created[src.path]) !='undefined') {
235 delete this.just_created[src.path];
236 this.lastAdd = new Date();
238 [ src.gitpath, 'add', src.vpath ],
239 [ src.gitpath, 'commit', src.vpath, { message: src.vpath} ]
242 this.action_queue.push({
252 this.lastAdd = new Date();
254 // [ src.gitpath, 'add', src.vpath ],
255 // [ src.gitpath, 'commit', src.vpath, { message: src.vpath} ]
258 this.action_queue.push({
266 onDeleted : function(src)
269 if (this.shouldIgnore(src)) {
272 // should check if monitor needs removing..
273 // it should also check if it was a directory.. - so we dont have to commit all..
275 this.lastAdd = new Date();
277 // [ src.gitpath, 'rm' , src.vpath ],
278 // [ src.gitpath, 'commit', { all: true, message: src.vpath} ]
281 this.action_queue.push({
288 onCreated : function(src)
291 if (this.shouldIgnore(src)) {
295 if (!GLib.file_test(src.path, GLib.FileTest.IS_DIR)) {
296 this.just_created[src.path] = true;
297 return; // we do not handle file create flags... - use done hint.
299 // director has bee created
300 this.monitor(src.path);
303 since git does not really handle directory adds...
305 this.lastAdd = new Date();
306 this.action_queue.push({
313 [ src.gitpath, 'add' , src.vpath, { all: true } ],
314 [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
321 onAttributeChanged : function(src) {
323 if (this.shouldIgnore(src)) {
326 this.lastAdd = new Date();
330 // [ src.gitpath, 'commit' , src.vpath, { message: src.vpath} ]
332 this.action_queue.push({
341 onMoved : function(src,dest)
344 this.parsePath(dest);
346 if (src.gitpath != dest.gitpath) {
348 this.onCreated(dest);
349 this.onChangedDoneHint(dest);
352 // needs to handle move to/from unsupported types..
354 if (this.shouldIgnore(src)) {
357 if (this.shouldIgnore(dest)) {
360 this.lastAdd = new Date();
362 // [ src.gitpath, 'mv', '-k', src.vpath, dest.vpath ],
363 // [ src.gitpath, 'commit' , src.vpath, dest.vpath ,
364 // { message: 'MOVED ' + src.vpath +' to ' + dest.vpath} ]
367 this.action_queue.push({
383 function errorDialog(data) {
384 var msg = new Gtk.MessageDialog({
385 message_type: Gtk.MessageType.ERROR,
386 buttons : Gtk.ButtonsType.OK,
398 // need a better icon...
404 Notify.init("gitlive");
406 monitor.add(GLib.get_home_dir() + "/gitlive");
409 //icon.signal["activate"].connect(on_left_click);