add gen.php
[gitlive] / Git.js
1 ///<script type="text/javascript">
2
3 Gio      = imports.gi.Gio;
4 GLib      = imports.gi.GLib;
5
6 Spawn   = imports.Spawn.Spawn;
7 File    = imports.File.File;
8 /**
9  * @namespace Git
10  * 
11  * Class to handle git operations..???
12  * 
13  * usage:
14  * 
15  * Git = import.Git.Git;
16  * 
17  * var g = new Git(  '/home/me/git' );
18  * 
19  * g.run('commit', { all : true , message : 'test' }, 'filename',) 
20  * 
21  * or 
22  * print(Git.run('/home/me/git', 'log'))
23  * 
24  * 
25  *  
26  */
27
28
29 /**
30  * @class Git
31  * @param repo {String} directory that the repo is in, either bare or not.
32  * 
33  * 
34  */
35 //var prototypeInit = false;
36 function Git( repo) {
37     
38     if (!GLib.file_test(repo, GLib.FileTest.IS_DIR)) {
39         throw "Repo does not exist";
40     }
41     this.repo = repo;
42     /*
43     if (!prototypeInit) {
44         // proto type on set up yet..
45         // we could list this to generate methods.. /usr/lib/git-core/
46         var props = Gil.prototypeInit();
47         for (var i in props) {
48             this[i]= props[i];
49         }
50     }
51     */
52     
53 }
54 Git.prototype = {
55     repo : '',
56     /**
57      * @method run
58      * @arg command {String} command to run
59      * @arg arguments.... {String|Object}  arguments to send to command
60      * { xxxx : yyyy} -> --xxxx YYYYY
61      * { x : yyyy} -> -x  yyyy
62      * 
63      */
64     run : function() {
65         //print("GIT RUN");
66         var args = ['git'];
67         
68         
69         for (var i=0;i< arguments.length;i++) {
70             if (typeof(arguments[i]) == 'string') {
71                 args.push(arguments[i]);
72                 continue;
73             }
74             if (typeof(arguments[i]) == 'object') {
75                 for(var k in arguments[i]) {
76                     var v = arguments[i][k];
77                     
78                     args.push(k.length > 1 ? ('--' + k) : ('-' + k));
79                     
80                     if (v === true) {
81                         continue;
82                     }
83                     args.push(v);
84                 }
85             }
86              
87         }
88         var env =  [  "HOME=" + GLib.get_home_dir() ];
89         
90         if (File.exists(this.repo + '/.git/config')) {
91             env.push("GITPATH=" + this.repo );
92         }
93         
94         
95         //print(args.join( ' '));
96         var sp = new Spawn({
97             //env : [ "GITPATH=" + this.repo , "HOME=" + GLib.get_home_dir() ],
98             env : env,
99             cwd : this.repo,
100             args: args,
101             debug: true,
102             exceptions : false,
103             async : false
104         });
105         var out = sp.run();
106         // parse output for some commands ?
107         return out;
108     }
109 }
110
111
112 /**
113  * @function run
114  * @arg command {String} command to run
115  * @arg arguments.... {String|Object}  arguments to send to command
116  * 
117  * 
118  */
119
120 function run() {
121   //  print("Git.run()");
122     var args = Array.prototype.slice.call(arguments);
123     //print(JSON.stringify(args));
124     var repo = args.shift(args);
125     var x = new Git(repo);
126    // print(JSON.stringify(args));
127     return x.run.apply(x, args);
128     
129 }
130
131 // test.
132
133 //print(run('/home/alan/gitlive/gitlive', 'log'));
134
135  
136