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!?
12 * A) have a gitlive branch - where all our commits go.. - so they can be replicated on the server
13 * B) HEAD branch - where things get merged to..
14 * -- eventually on closing issues..
15 * -- currently when we switch from one feature to another..
18 * git log -n 1 --pretty=format:%H BRANCHNAME
22 * Notes on feature branch implementation
23 * We need to add a gitlive branch on the remote server..
24 * git push origin origin:refs/heads/gitlive
25 * git checkout --track -b gitlive origin/gitlive << means pull will use both branches..
28 * On our feature tree..
29 * git push origin origin:refs/heads/feature_2
31 * we clone directory into gitlive_feature/XXXXX
32 * git branch issue_XXX
33 * git checkout issue_XXX
35 * run this on the feature branch it merges and commits..
36 * git pull origin master << or gitlive..
39 * Standard change file (same bug as before..)
41 * commit etc.. (with bug no..)
43 * git pull origin gitlive
48 * Change to new bug number..
50 * git checkout -b master origin/master
51 * git checkout master <<< make sure
52 * git pull --squash origin gitlive
53 * git commit -m 'done with old bug number'
58 * git push origin origin:refs/heads/feature_XXX
59 * git checkout feature_XXX
61 * commit etc. (with new bug number)
63 * git pull origin gitlive
70 GI = imports.gi.GIRepository
71 GLib = imports.gi.GLib;
73 // we add this in, as it appears to get lost sometimes if we set it using the ENV. variable in builder.sh
74 GI.IRepository.prepend_search_path(GLib.get_home_dir() + '/.Builder/girepository-1.1');
78 Notify = imports.gi.Notify;
80 Spawn = imports.Spawn;
82 StatusIcon = imports.StatusIcon.StatusIcon;
83 Monitor = imports.Monitor.Monitor;
84 File = imports.File.File;
88 //File = imports[__script_path__+'/../introspection-doc-generator/File.js'].File
89 Gtk.init (null, null);
91 var gitlive = GLib.get_home_dir() + "/gitlive";
93 if (!GLib.file_test(gitlive, GLib.FileTest.IS_DIR)) {
94 var msg = new Gtk.MessageDialog({message_type:
95 Gtk.MessageType.INFO, buttons : Gtk.ButtonsType.OK, text: "GIT Live - ~/gitlive does not exist."});
103 var monitor = new Monitor({
107 * action: 'add' | rm | update
115 queueRunning : false,
122 this.dot_gitlive = GLib.get_home_dir() + "/.gitlive";
123 if (!File.exists(this.dot_gitlive)) {
124 File.mkdir(this.dot_gitlive);
129 this.lastAdd = new Date();
131 // start monitoring first..
132 Monitor.prototype.start.call(this);
134 // then start our queue runner..
135 GLib.timeout_add(GLib.PRIORITY_LOW, 500, function() {
136 //TIMEOUT", _this.queue.length , _this.queueRunning].join(', '));
137 if (!_this.queue.length || _this.queueRunning) {
140 var last = Math.floor(((new Date()) - this.lastAdd) / 100);
141 if (last < 4) { // wait 1/2 a seconnd before running.
149 var notification = new Notify.Notification({
151 body : gitlive + "\nMonitoring " + this.monitors.length + " Directories"
154 notification.set_timeout(2000);
158 initRepo : function(src)
160 print("INIT REPO " + src);
162 function logrun(sp) {
163 print("LOGRUN?" + typeof(sp));
164 switch (sp.result * 1) {
166 print(sp.args.join(' '));
167 if (sp.output.length) print(sp.output + '');
168 // if (sp.stderr.length) success.push(sp.stderr + '');
171 print(sp.args.join(' '));
172 if (sp.output.length) print(sp.output);
173 if (sp.stderr.length) print(sp.stderr);
178 // make hour mirrored directory
179 var rname = src.split('/').pop();
180 var dotdir = this.dot_gitlive + '/' + rname;
182 print("CHECK WORKING CACHE " + dotdir);
184 if (!File.isDirectory(dotdir)) {
185 // print("Not a dir?");
186 logrun(Git.run(this.dot_gitlive, 'clone', src , { shared : true } ));
188 // refresh - always..
189 logrun(Git.run(src, 'pull'));
191 // create and checkout gitlive branch.
192 logrun(Git.run(src, 'push', 'origin', 'origin:refs/heads/gitlive'));
193 logrun(Git.run(src, 'checkout', { track : true , 'b' : 'gitlive' } , 'origin/gitlive'));
207 * - pulls the items off the queue
208 * (as commands run concurrently and new items may get added while it's running)
209 * - runs the queue items
215 this.queueRunning = true;
217 //this.queue.forEach(function (q) {
221 this.action_queue.forEach(function (q) {
224 //this.queue = []; // empty queue!
225 this.action_queue = [];
231 function readResult(sp) {
232 switch (sp.result * 1) {
234 success.push(sp.args.join(' '));
235 if (sp.output.length) success.push(sp.output + '');
236 // if (sp.stderr.length) success.push(sp.stderr + '');
239 failure.push(sp.args.join(' '));
240 if (sp.output.length) failure.push(sp.output);
241 if (sp.stderr.length) failure.push(sp.stderr);
246 cmds.forEach(function(cmd) {
247 // prevent duplicate calls..
248 if (done.indexOf(JSON.stringify(cmd)) > -1) {
251 done.push(JSON.stringify(cmd));
252 // --- we keep a list of repositories that will be pushed to at the end..
254 if (repos.indexOf(cmd.repos) < 0) {
255 repos.push(cmd.repos);
256 // Git.run(cmd.repos , 'pull'); // pull before we push!
260 // at this point we need to know the issue number...
263 var gp = gitlive + '/' + cmd.repo;
264 var dotdir = this.dot_gitlive + '/' + rname;
266 // create feature branch. - if it's a new feature..
267 readResult(Git.run(dotdir, 'push', 'origin', 'origin:refs/heads/feature_XXX'));
268 readResult(Git.run(dotdir, 'checkout', 'feature_XXX'));
270 switch( cmd.action ) {
272 readResult(Git.run(gp, 'add', cmd.file ));
273 readResult(Git.run(gp, 'commit', src.file, { message: cmd.file} ));
278 readResult(Git.run(gp, 'rm', cmd.file ));
279 readResult(Git.run(gp, 'commit', { all: true, message: cmd.file} ));
283 readResult(Git.run(gp, 'commit', cmd.file , { message: cmd.file} ));
287 readResult(Git.run(gp, 'mv', cmd.file , cmd.target));
288 readResult(Git.run(gp, 'commit', cmd.file , cmd.target,
289 { message: 'MOVED ' + src.file +' to ' + dest.target } ));
292 // duplicate the changes into the feature dir, and push it back into the data..
293 readResult(Git.run(dotdir, 'pull', 'origin', 'gitlive'));
294 readResult(Git.run(dotdir, 'push'));
300 repos.forEach(function(r) {
301 var sp = Git.run(gitlive + '/' +r , 'push', { all: true } );
308 if (success.length) {
309 print(success.join("\n"));
310 var notification = new Notify.Notification({
311 summary: "Git Live Commited",
312 body : success.join("\n")
316 notification.set_timeout(2000);
319 if (failure.length) {
321 var notification = new Notify.Notification({
322 summary: "Git Live ERROR!!",
323 body : failure.join("\n")
327 notification.set_timeout(5000); // show errros for longer
330 this.queueRunning = false;
333 shouldIgnore: function(f)
335 if (f.name[0] == '.') {
337 if (f.name == '.htaccess') {
343 if (f.name.match(/~$/)) {
346 // ignore anything in top level!!!!
347 if (!f.vpath.length) {
356 * set gitpath and vpath
361 parsePath: function(f)
364 var vpath_ar = f.path.substring(gitlive.length +1).split('/');
365 f.repo = vpath_ar.shift();
366 f.gitpath = gitlive + '/' + f.repo;
367 f.vpath = vpath_ar.join('/');
374 onChanged : function(src)
376 return; // always ignore this..?
377 //this.parsePath(src);
381 * results in git add + git commit..
387 onChangesDoneHint : function(src)
390 if (this.shouldIgnore(src)) {
395 if (typeof(this.just_created[src.path]) !='undefined') {
396 delete this.just_created[src.path];
397 this.lastAdd = new Date();
399 // [ src.gitpath, 'add', src.vpath ],
400 // [ src.gitpath, 'commit', src.vpath, { message: src.vpath} ]
403 this.action_queue.push({
413 this.lastAdd = new Date();
415 // [ src.gitpath, 'add', src.vpath ],
416 // [ src.gitpath, 'commit', src.vpath, { message: src.vpath} ]
420 this.action_queue.push({
428 onDeleted : function(src)
431 if (this.shouldIgnore(src)) {
434 // should check if monitor needs removing..
435 // it should also check if it was a directory.. - so we dont have to commit all..
437 this.lastAdd = new Date();
439 // [ src.gitpath, 'rm' , src.vpath ],
440 // [ src.gitpath, 'commit', { all: true, message: src.vpath} ]
443 this.action_queue.push({
450 onCreated : function(src)
453 if (this.shouldIgnore(src)) {
457 if (!GLib.file_test(src.path, GLib.FileTest.IS_DIR)) {
458 this.just_created[src.path] = true;
459 return; // we do not handle file create flags... - use done hint.
461 // director has bee created
462 this.monitor(src.path);
465 since git does not really handle directory adds...
467 this.lastAdd = new Date();
468 this.action_queue.push({
475 [ src.gitpath, 'add' , src.vpath, { all: true } ],
476 [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
483 onAttributeChanged : function(src) {
485 if (this.shouldIgnore(src)) {
488 this.lastAdd = new Date();
492 // [ src.gitpath, 'commit' , src.vpath, { message: src.vpath} ]
494 this.action_queue.push({
503 onMoved : function(src,dest)
506 this.parsePath(dest);
508 if (src.gitpath != dest.gitpath) {
510 this.onCreated(dest);
511 this.onChangedDoneHint(dest);
514 // needs to handle move to/from unsupported types..
516 if (this.shouldIgnore(src)) {
519 if (this.shouldIgnore(dest)) {
522 this.lastAdd = new Date();
524 // [ src.gitpath, 'mv', '-k', src.vpath, dest.vpath ],
525 // [ src.gitpath, 'commit' , src.vpath, dest.vpath ,
526 // { message: 'MOVED ' + src.vpath +' to ' + dest.vpath} ]
529 this.action_queue.push({
545 function errorDialog(data) {
546 var msg = new Gtk.MessageDialog({
547 message_type: Gtk.MessageType.ERROR,
548 buttons : Gtk.ButtonsType.OK,
560 // need a better icon...
566 Notify.init("gitlive");
568 monitor.add(GLib.get_home_dir() + "/gitlive");
571 //icon.signal["activate"].connect(on_left_click);