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 var GI = imports.gi.GIRepository;
13 var Gio = imports.gi.Gio;
14 var GLib = imports.gi.GLib;
15 var Gtk = imports.gi.Gtk;
16 var Notify = imports.gi.Notify;
18 var Spawn = imports.Spawn;
19 var Git = imports.Git;
20 var StatusIcon = imports.StatusIcon.StatusIcon;
21 var Monitor = imports.Monitor.Monitor;
24 //File = imports[__script_path__+'/../introspection-doc-generator/File.js'].File
25 Gtk.init (null, null);
27 var gitlive = GLib.get_home_dir() + "/gitlive";
29 if (!GLib.file_test(gitlive, GLib.FileTest.IS_DIR)) {
30 var msg = new Gtk.MessageDialog({message_type:
31 Gtk.MessageType.INFO, buttons : Gtk.ButtonsType.OK, text: "GIT Live - ~/gitlive does not exist."});
39 var monitor = new Monitor({
46 this.lastAdd = new Date();
48 GLib.timeout_add(GLib.PRIORITY_LOW, 500, function() {
49 //TIMEOUT", _this.queue.length , _this.queueRunning].join(', '));
50 if (!_this.queue.length || _this.queueRunning) {
53 var last = Math.floor(((new Date()) - this.lastAdd) / 100);
54 if (last < 4) { // wait 1/2 a seconnd before running.
61 Monitor.prototype.start.call(this);
62 var notification = new Notify.Notification({
64 body : gitlive + "\nMonitoring " + this.monitors.length + " Directories"
67 notification.set_timeout(2000);
72 * - pulls the items off the queue
73 * (as commands run concurrently and new items may get added while it's running)
74 * - runs the queue items
80 this.queueRunning = true;
82 this.queue.forEach(function (q) {
85 this.queue = []; // empty queue!
91 cmds.forEach(function(cmd) {
92 // prevent duplicate calls..
93 if (done.indexOf(cmd.join(',')) > -1) {
96 done.push(cmd.join(','));
98 if (repos.indexOf(cmd[0]) < 0) {
100 Git.run(cmd[0] , 'pull'); // pull before we push!
102 var sp = Git.run.apply(Git,cmd);
104 switch (sp.result * 1) {
106 success.push(sp.args.join(' '));
107 if (sp.output.length) success.push(sp.output + '');
108 // if (sp.stderr.length) success.push(sp.stderr + '');
111 failure.push(sp.args.join(' '));
112 if (sp.output.length) failure.push(sp.output);
113 if (sp.stderr.length) failure.push(sp.stderr);
120 repos.forEach(function(r) {
121 var sp = Git.run(r , 'push', { all: true } );
128 if (success.length) {
129 print(success.join("\n"));
130 var notification = new Notify.Notification({
131 summary: "Git Live Commited",
132 body : success.join("\n")
136 notification.set_timeout(2000);
139 if (failure.length) {
141 var notification = new Notify.Notification({
142 summary: "Git Live ERROR!!",
143 body : failure.join("\n")
147 notification.set_timeout(5000); // show errros for longer
150 this.queueRunning = false;
153 shouldIgnore: function(f)
155 if (f.name[0] == '.') {
157 if (f.name == '.htaccess') {
163 if (f.name.match(/~$/)) {
166 // ignore anything in top level!!!!
167 if (!f.vpath.length) {
175 parsePath: function(f) {
177 var vpath_ar = f.path.substring(gitlive.length +1).split('/');
179 f.gitpath = gitlive + '/' + vpath_ar.shift();
180 f.vpath = vpath_ar.join('/');
187 onChanged : function(src)
189 return; // always ignore this..?
190 //this.parsePath(src);
192 onChangesDoneHint : function(src)
195 if (this.shouldIgnore(src)) {
200 if (typeof(this.just_created[src.path]) !='undefined') {
201 delete this.just_created[src.path];
202 this.lastAdd = new Date();
204 [ src.gitpath, 'add', src.vpath ],
205 [ src.gitpath, 'commit', src.vpath, { message: src.vpath} ]
211 this.lastAdd = new Date();
213 [ src.gitpath, 'add', src.vpath ],
214 [ src.gitpath, 'commit', src.vpath, { message: src.vpath} ]
221 onDeleted : function(src)
224 if (this.shouldIgnore(src)) {
227 // should check if monitor needs removing..
228 // it should also check if it was a directory.. - so we dont have to commit all..
230 this.lastAdd = new Date();
232 [ src.gitpath, 'rm' , src.vpath ],
233 [ src.gitpath, 'commit', { all: true, message: src.vpath} ]
239 onCreated : function(src)
242 if (this.shouldIgnore(src)) {
246 if (!GLib.file_test(src.path, GLib.FileTest.IS_DIR)) {
247 this.just_created[src.path] = true;
248 return; // we do not handle file create flags... - use done hint.
250 // director has bee created
251 this.monitor(src.path);
252 this.lastAdd = new Date();
254 [ src.gitpath, 'add' , src.vpath, { all: true } ],
255 [ src.gitpath, 'commit' , { all: true, message: src.vpath} ]
261 onAttributeChanged : function(src) {
263 if (this.shouldIgnore(src)) {
266 this.lastAdd = new Date();
268 [ src.gitpath, 'commit' , src.vpath, { message: src.vpath} ]
274 onMoved : function(src,dest)
277 this.parsePath(dest);
279 if (src.gitpath != dest.gitpath) {
281 this.onCreated(dest);
282 this.onChangedDoneHint(dest);
285 // needs to handle move to/from unsupported types..
287 if (this.shouldIgnore(src)) {
290 if (this.shouldIgnore(dest)) {
293 this.lastAdd = new Date();
295 [ src.gitpath, 'mv', '-k', src.vpath, dest.vpath ],
296 [ src.gitpath, 'commit' , src.vpath, dest.vpath ,
297 { message: 'MOVED ' + src.vpath +' to ' + dest.vpath} ]
308 function errorDialog(data) {
309 var msg = new Gtk.MessageDialog({
310 message_type: Gtk.MessageType.ERROR,
311 buttons : Gtk.ButtonsType.OK,
323 // need a better icon...
329 Notify.init("gitlive");
331 monitor.add(GLib.get_home_dir() + "/gitlive");
334 //icon.signal["activate"].connect(on_left_click);