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);
154 repo.remove(remove_files);
156 success.push(repo.commit({
157 reason : messages.join("\n"),
160 success.push(repo.push());
165 // finally merge all the commit messages.
168 // catch notification failures.. so we can carry on..
169 if (success.length) {
170 print(success.join("\n"));
172 var notification = new Notify.Notification({
173 summary: "Git Live Commited",
174 body : success.join("\n"),
179 notification.set_timeout(5);
183 if (failure.length) {
185 var notification = new Notify.Notification({
186 summary: "Git Live ERROR!!",
187 body : failure.join("\n"),
192 notification.set_timeout(5); // show errros for longer
199 this.queueRunning = false;
202 shouldIgnore: function(f)
204 if (f.name[0] == '.') {
206 if (f.name == '.htaccess') {
212 if (f.name.match(/~$/)) {
215 // ignore anything in top level!!!!
216 if (!f.vpath.length) {
226 * Fill in gitlive, vpath and repo
229 parsePath: function(f)
232 var vpath_ar = f.path.substring(gitlive.length +1).split('/');
234 f.gitpath = gitlive + '/' + vpath_ar.shift();
235 f.vpath = vpath_ar.join('/');
236 //f.repo = new imports.Scm.Git.Repo({ repopath: f.gitpath })
243 onChanged : function(src)
245 return; // always ignore this..?
246 //this.parsePath(src);
253 * results in git add + git commit..
256 onChangesDoneHint : function(src)
259 if (this.shouldIgnore(src)) {
265 if (typeof(this.just_created[src.path]) !='undefined') {
266 delete this.just_created[src.path];
267 this.lastAdd = new Date();
269 [ src.gitpath, 'add', src.vpath ],
270 [ src.gitpath, 'commit', { message: src.vpath} ]
276 this.lastAdd = new Date();
278 [ src.gitpath, 'add', src.vpath ],
279 [ src.gitpath, 'commit', { message: src.vpath} ]
284 onDeleted : function(src)
287 if (this.shouldIgnore(src)) {
290 // should check if monitor needs removing..
291 // it should also check if it was a directory.. - so we dont have to commit all..
293 this.lastAdd = new Date();
295 [ src.gitpath, 'rm' , src.vpath ],
296 [ src.gitpath, 'commit', { all: true, message: src.vpath} ]
302 onCreated : function(src)
305 if (this.shouldIgnore(src)) {
309 if (!GLib.file_test(src.path, GLib.FileTest.IS_DIR)) {
310 this.just_created[src.path] = true;
311 return; // we do not handle file create flags... - use done hint.
313 // director has bee created
314 this.monitor(src.path);
315 this.lastAdd = new Date();
317 [ src.gitpath, 'add' , src.vpath, { all: true } ],
318 [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
324 onAttributeChanged : function(src) {
326 if (this.shouldIgnore(src)) {
329 this.lastAdd = new Date();
332 //[ src.gitpath, 'commit' , src.vpath, { message: src.vpath} ]
333 [ src.gitpath, 'add' , src.vpath ],
334 [ src.gitpath, 'commit' , { message: "Attribute Changed :" + src.vpath} ]
340 onMoved : function(src,dest)
343 this.parsePath(dest);
345 if (src.gitpath != dest.gitpath) {
347 this.onCreated(dest);
348 this.onChangedDoneHint(dest);
351 // needs to handle move to/from unsupported types..
353 if (this.shouldIgnore(src)) {
356 if (this.shouldIgnore(dest)) {
359 this.lastAdd = new Date();
361 // [ src.gitpath, 'mv', '-k', src.vpath, dest.vpath ],
362 [ src.gitpath, 'add', dest.vpath ],
363 [ src.gitpath, 'rm', src.vpath ],
365 [ src.gitpath, 'commit' ,
366 { message: 'MOVED ' + src.vpath +' to ' + dest.vpath}
378 function errorDialog(data) {
379 var msg = new Gtk.MessageDialog({
380 message_type: Gtk.MessageType.ERROR,
381 buttons : Gtk.ButtonsType.OK,
393 // need a better icon...
399 Notify.init("gitlive");
401 monitor.add(GLib.get_home_dir() + "/gitlive");
404 //icon.signal["activate"].connect(on_left_click);