INSTALL.txt
[gitlive] / Git.js
1 ///<script type="text/javascript">
2
3 var Gio      = imports.gi.Gio;
4 var GLib      = imports.gi.GLib;
5
6 var Spawn = imports.Spawn.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                     if (v === true) {
76                         continue;
77                     }
78                     args.push(v);
79                 }
80             }
81              
82         }
83         
84         var sp = new Spawn({
85             env : [ "GITPATH=" + this.repo , "HOME=" + GLib.get_home_dir() ],
86             cwd : this.repo,
87             args: args,
88             debug: true,
89             exceptions : false,
90             async : false
91         });
92         var out = sp.run();
93         // parse output for some commands ?
94         return out;
95     }
96 }
97
98
99 /**
100  * @function run
101  * @arg command {String} command to run
102  * @arg arguments.... {String|Object}  arguments to send to command
103  * 
104  * 
105  */
106
107 function run() {
108     var args = Array.prototype.slice.call(arguments);
109   
110     var repo = args.shift(args);
111     var x = new Git(repo);
112     
113     return x.run.apply(x, args);
114     
115 }
116
117
118 // test.
119
120 //print(run('/home/alan/gitlive/gitlive', 'log'));
121
122  
123