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 //print(JSON.stringify(cmds));
114 cmds.forEach(function(cmd) {
115 var gitpath = cmd.shift();
116 if (typeof(repo_list[gitpath]) == 'undefined') {
117 repo_list[gitpath] = new imports.Scm.Git.Repo.Repo( { repopath : gitpath });
118 repo_list[gitpath].cmds = [];
119 repo_list[gitpath].pull();
121 repo_list[gitpath].cmds.push(cmd);
124 // build add, remove and commit message list..
127 for (var gitpath in repo_list) {
128 var repo = repo_list[gitpath];
130 var remove_files = [];
132 repo.cmds.forEach(function(cmd) {
133 //print(JSON.stringify(cmd));
134 var name = cmd.shift();
135 var arg = cmd.shift();
139 if (add_files.indexOf(arg) > -1) break;
144 remove_files.push(arg);
148 messages.push(arg.message);
153 // these can fail... at present... as we wildcard stuff.
155 repo.remove(remove_files);
157 success.push(repo.commit({
158 reason : messages.join("\n"),
161 success.push(repo.push());
166 // finally merge all the commit messages.
169 // catch notification failures.. so we can carry on..
170 if (success.length) {
171 print(success.join("\n"));
173 var notification = new Notify.Notification({
174 summary: "Git Live Commited",
175 body : success.join("\n"),
180 notification.set_timeout(5);
184 if (failure.length) {
186 var notification = new Notify.Notification({
187 summary: "Git Live ERROR!!",
188 body : failure.join("\n"),
193 notification.set_timeout(5); // show errros for longer
200 this.queueRunning = false;
203 shouldIgnore: function(f)
205 if (f.name[0] == '.') {
207 if (f.name == '.htaccess') {
213 if (f.name.match(/~$/)) {
216 // ignore anything in top level!!!!
217 if (!f.vpath.length) {
227 * Fill in gitlive, vpath and repo
230 parsePath: function(f)
233 var vpath_ar = f.path.substring(gitlive.length +1).split('/');
235 f.gitpath = gitlive + '/' + vpath_ar.shift();
236 f.vpath = vpath_ar.join('/');
237 //f.repo = new imports.Scm.Git.Repo({ repopath: f.gitpath })
244 onChanged : function(src)
246 return; // always ignore this..?
247 //this.parsePath(src);
254 * results in git add + git commit..
257 onChangesDoneHint : function(src)
260 if (this.shouldIgnore(src)) {
266 if (typeof(this.just_created[src.path]) !='undefined') {
267 delete this.just_created[src.path];
268 this.lastAdd = new Date();
270 [ src.gitpath, 'add', src.vpath ],
271 [ src.gitpath, 'commit', { message: src.vpath} ]
277 this.lastAdd = new Date();
279 [ src.gitpath, 'add', src.vpath ],
280 [ src.gitpath, 'commit', { message: src.vpath} ]
285 onDeleted : function(src)
288 if (this.shouldIgnore(src)) {
291 // should check if monitor needs removing..
292 // it should also check if it was a directory.. - so we dont have to commit all..
294 this.lastAdd = new Date();
296 [ src.gitpath, 'rm' , src.vpath ],
297 [ src.gitpath, 'commit', { all: true, message: src.vpath} ]
303 onCreated : function(src)
306 if (this.shouldIgnore(src)) {
310 if (!GLib.file_test(src.path, GLib.FileTest.IS_DIR)) {
311 this.just_created[src.path] = true;
312 return; // we do not handle file create flags... - use done hint.
314 // director has bee created
315 this.monitor(src.path);
316 this.lastAdd = new Date();
318 [ src.gitpath, 'add' , src.vpath, { all: true } ],
319 [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
325 onAttributeChanged : function(src) {
327 if (this.shouldIgnore(src)) {
330 this.lastAdd = new Date();
333 //[ src.gitpath, 'commit' , src.vpath, { message: src.vpath} ]
334 [ src.gitpath, 'add' , src.vpath ],
335 [ src.gitpath, 'commit' , { message: "Attribute Changed :" + src.vpath} ]
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, 'add', dest.vpath ],
364 [ src.gitpath, 'rm', src.vpath ],
366 [ src.gitpath, 'commit' ,
367 { message: 'MOVED ' + src.vpath +' to ' + dest.vpath}
379 function errorDialog(data) {
380 var msg = new Gtk.MessageDialog({
381 message_type: Gtk.MessageType.ERROR,
382 buttons : Gtk.ButtonsType.OK,
394 // need a better icon...
400 Notify.init("gitlive");
402 monitor.add(GLib.get_home_dir() + "/gitlive");
405 //icon.signal["activate"].connect(on_left_click);