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[0];
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 cmds.forEach(function(cmd) {
125 // prevent duplicate calls..
126 if (done.indexOf(cmd.join(',')) > -1) {
129 done.push(cmd.join(','));
131 if (repos.indexOf(cmd[0]) < 0) {
133 //Git.run(cmd[0] , 'pull'); // pull before we push!
135 var sp = Git.run.apply(Git,cmd);
137 switch (sp.result * 1) {
139 success.push(sp.args.join(' '));
140 if (sp.output.length) success.push(sp.output + '');
141 // if (sp.stderr.length) success.push(sp.stderr + '');
144 failure.push(sp.args.join(' '));
145 if (sp.output.length) failure.push(sp.output);
146 if (sp.stderr.length) failure.push(sp.stderr);
153 repos.forEach(function(r) {
154 var sp = Git.run(r , 'push', { all: true } );
161 // catch notification failures.. so we can carry on..
162 if (success.length) {
163 print(success.join("\n"));
165 var notification = new Notify.Notification({
166 summary: "Git Live Commited",
167 body : success.join("\n"),
172 notification.set_timeout(5);
176 if (failure.length) {
178 var notification = new Notify.Notification({
179 summary: "Git Live ERROR!!",
180 body : failure.join("\n"),
185 notification.set_timeout(5); // show errros for longer
192 this.queueRunning = false;
195 shouldIgnore: function(f)
197 if (f.name[0] == '.') {
199 if (f.name == '.htaccess') {
205 if (f.name.match(/~$/)) {
208 // ignore anything in top level!!!!
209 if (!f.vpath.length) {
219 * Fill in gitlive, vpath and repo
222 parsePath: function(f)
225 var vpath_ar = f.path.substring(gitlive.length +1).split('/');
227 f.gitpath = gitlive + '/' + vpath_ar.shift();
228 f.vpath = vpath_ar.join('/');
229 //f.repo = new imports.Scm.Git.Repo({ repopath: f.gitpath })
236 onChanged : function(src)
238 return; // always ignore this..?
239 //this.parsePath(src);
246 * results in git add + git commit..
249 onChangesDoneHint : function(src)
252 if (this.shouldIgnore(src)) {
258 if (typeof(this.just_created[src.path]) !='undefined') {
259 delete this.just_created[src.path];
260 this.lastAdd = new Date();
262 [ src.gitpath, 'add', src.vpath ],
263 [ src.gitpath, 'commit', src.vpath, { message: src.vpath} ]
269 this.lastAdd = new Date();
271 [ src.gitpath, 'add', src.vpath ],
272 [ src.gitpath, 'commit', src.vpath, { message: src.vpath} ]
279 onDeleted : function(src)
282 if (this.shouldIgnore(src)) {
285 // should check if monitor needs removing..
286 // it should also check if it was a directory.. - so we dont have to commit all..
288 this.lastAdd = new Date();
290 [ src.gitpath, 'rm' , src.vpath ],
291 [ src.gitpath, 'commit', { all: true, message: src.vpath} ]
297 onCreated : function(src)
300 if (this.shouldIgnore(src)) {
304 if (!GLib.file_test(src.path, GLib.FileTest.IS_DIR)) {
305 this.just_created[src.path] = true;
306 return; // we do not handle file create flags... - use done hint.
308 // director has bee created
309 this.monitor(src.path);
310 this.lastAdd = new Date();
312 [ src.gitpath, 'add' , src.vpath, { all: true } ],
313 [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
319 onAttributeChanged : function(src) {
321 if (this.shouldIgnore(src)) {
324 this.lastAdd = new Date();
326 [ src.gitpath, 'commit' , src.vpath, { message: src.vpath} ]
332 onMoved : function(src,dest)
335 this.parsePath(dest);
337 if (src.gitpath != dest.gitpath) {
339 this.onCreated(dest);
340 this.onChangedDoneHint(dest);
343 // needs to handle move to/from unsupported types..
345 if (this.shouldIgnore(src)) {
348 if (this.shouldIgnore(dest)) {
351 this.lastAdd = new Date();
353 [ src.gitpath, 'mv', '-k', src.vpath, dest.vpath ],
354 [ src.gitpath, 'commit' , src.vpath, dest.vpath ,
355 { message: 'MOVED ' + src.vpath +' to ' + dest.vpath} ]
366 function errorDialog(data) {
367 var msg = new Gtk.MessageDialog({
368 message_type: Gtk.MessageType.ERROR,
369 buttons : Gtk.ButtonsType.OK,
381 // need a better icon...
387 Notify.init("gitlive");
389 monitor.add(GLib.get_home_dir() + "/gitlive");
392 //icon.signal["activate"].connect(on_left_click);