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 GIRepository = imports.gi.GIRepository
14 GLib = imports.gi.GLib;
16 //print(JSON.stringify(GI, null,4));
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');
19 GIRepository.Repository.prepend_search_path(GLib.get_home_dir() + '/.Builder/girepository-1.2');
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 this.top.forEach(this.monitor, this);
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.
71 var notification = new Notify.Notification({
73 body : gitlive + "\nMonitoring " + this.monitors.length + " Directories",
77 notification.set_timeout(5);
87 * - pulls the items off the queue
88 * (as commands run concurrently and new items may get added while it's running)
89 * - runs the queue items
95 this.queueRunning = true;
97 this.queue.forEach(function (q) {
100 this.queue = []; // empty queue!
107 // first build a array of repo's to work with
112 cmds.forEach(function(cmd) {
113 var gitpath = cmd.shift();
114 if (typeof(repo_list[gitpath] != 'undefined')) {
115 repo_list[gitpath] = new imports.Scm.Git.Repo.Repo( { repopath : gitpath });
116 repo_list[gitpath].cmds = [];
117 repo_list[gitpath].pull();
119 repo_list[gitpath].cmds.push(cmd);
124 for (var gitpath in repo_list) {
125 var repo = repo_list[gitpath];
127 repo.cmds.forEach(function(cmd) {
128 var name = cmd.shift();
131 add_files.push(cmd.shift());
134 remove_files.push(cmd.shift());
137 messages.push(cmd.shift().message )
151 // finally merge all the commit messages.
154 cmds.forEach(function(cmd) {
155 // prevent duplicate calls..
156 if (done.indexOf(cmd.join(',')) > -1) {
159 done.push(cmd.join(','));
161 if (repos.indexOf(cmd[0]) < 0) {
163 //Git.run(cmd[0] , 'pull'); // pull before we push!
165 var sp = Git.run.apply(Git,cmd);
167 switch (sp.result * 1) {
169 success.push(sp.args.join(' '));
170 if (sp.output.length) success.push(sp.output + '');
171 // if (sp.stderr.length) success.push(sp.stderr + '');
174 failure.push(sp.args.join(' '));
175 if (sp.output.length) failure.push(sp.output);
176 if (sp.stderr.length) failure.push(sp.stderr);
183 repos.forEach(function(r) {
184 var sp = Git.run(r , 'push', { all: true } );
191 // catch notification failures.. so we can carry on..
192 if (success.length) {
193 print(success.join("\n"));
195 var notification = new Notify.Notification({
196 summary: "Git Live Commited",
197 body : success.join("\n"),
202 notification.set_timeout(5);
206 if (failure.length) {
208 var notification = new Notify.Notification({
209 summary: "Git Live ERROR!!",
210 body : failure.join("\n"),
215 notification.set_timeout(5); // show errros for longer
222 this.queueRunning = false;
225 shouldIgnore: function(f)
227 if (f.name[0] == '.') {
229 if (f.name == '.htaccess') {
235 if (f.name.match(/~$/)) {
238 // ignore anything in top level!!!!
239 if (!f.vpath.length) {
249 * Fill in gitlive, vpath and repo
252 parsePath: function(f)
255 var vpath_ar = f.path.substring(gitlive.length +1).split('/');
257 f.gitpath = gitlive + '/' + vpath_ar.shift();
258 f.vpath = vpath_ar.join('/');
259 //f.repo = new imports.Scm.Git.Repo({ repopath: f.gitpath })
266 onChanged : function(src)
268 return; // always ignore this..?
269 //this.parsePath(src);
276 * results in git add + git commit..
279 onChangesDoneHint : function(src)
282 if (this.shouldIgnore(src)) {
288 if (typeof(this.just_created[src.path]) !='undefined') {
289 delete this.just_created[src.path];
290 this.lastAdd = new Date();
292 [ src.gitpath, 'add', src.vpath ],
293 [ src.gitpath, 'commit', src.vpath, { message: src.vpath} ]
299 this.lastAdd = new Date();
301 [ src.gitpath, 'add', src.vpath ],
302 [ src.gitpath, 'commit', src.vpath, { message: src.vpath} ]
309 onDeleted : function(src)
312 if (this.shouldIgnore(src)) {
315 // should check if monitor needs removing..
316 // it should also check if it was a directory.. - so we dont have to commit all..
318 this.lastAdd = new Date();
320 [ src.gitpath, 'rm' , src.vpath ],
321 [ src.gitpath, 'commit', { all: true, message: src.vpath} ]
327 onCreated : function(src)
330 if (this.shouldIgnore(src)) {
334 if (!GLib.file_test(src.path, GLib.FileTest.IS_DIR)) {
335 this.just_created[src.path] = true;
336 return; // we do not handle file create flags... - use done hint.
338 // director has bee created
339 this.monitor(src.path);
340 this.lastAdd = new Date();
342 [ src.gitpath, 'add' , src.vpath, { all: true } ],
343 [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
349 onAttributeChanged : function(src) {
351 if (this.shouldIgnore(src)) {
354 this.lastAdd = new Date();
357 //[ src.gitpath, 'commit' , src.vpath, { message: src.vpath} ]
358 [ src.gitpath, 'add' , src.vpath ],
359 [ src.gitpath, 'commit' , { message: "Attribute Changed :" + src.vpath} ]
365 onMoved : function(src,dest)
368 this.parsePath(dest);
370 if (src.gitpath != dest.gitpath) {
372 this.onCreated(dest);
373 this.onChangedDoneHint(dest);
376 // needs to handle move to/from unsupported types..
378 if (this.shouldIgnore(src)) {
381 if (this.shouldIgnore(dest)) {
384 this.lastAdd = new Date();
386 // [ src.gitpath, 'mv', '-k', src.vpath, dest.vpath ],
387 [ src.gitpath, 'add', dest.vpath ],
388 [ src.gitpath, 'rm', src.vpath ],
390 [ src.gitpath, 'commit' ,
391 { message: 'MOVED ' + src.vpath +' to ' + dest.vpath} ],
403 function errorDialog(data) {
404 var msg = new Gtk.MessageDialog({
405 message_type: Gtk.MessageType.ERROR,
406 buttons : Gtk.ButtonsType.OK,
418 // need a better icon...
424 Notify.init("gitlive");
426 monitor.add(GLib.get_home_dir() + "/gitlive");
429 //icon.signal["activate"].connect(on_left_click);