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