GitRepo.vala
[gitlive] / GitRepo.vala
1
2 /**
3  * @class Scm.Git.Repo
4  *
5  * @extends Scm.Repo
6  * 
7  *
8  *
9  */
10 public class GitRepo : Object
11 {
12     
13     public Array<GitMonitorQueue> cmds;
14
15
16     public string gitdir;
17     public bool debug = false;
18
19     /**
20     * index of.. matching gitpath..
21     */
22      public static int indexOfAdd( Array<GitRepo> repos, string gitpath) {
23         for(var i =0; i < repos.length; i++) {
24             if (repos.index(i).gitdir == gitpath) {
25                 return i;
26             }
27         }
28         return -1;
29     
30     }
31
32
33
34    
35     /**
36      * constructor:
37      * 
38      * @param {Object} cfg - Configuration
39      *     (basically repopath is currently only critical one.)
40      *
41      */
42      
43     public GitRepo(string path) {
44         // cal parent?
45         
46         this.gitdir = path + "/.git";
47         if (!FileUtils.test(this.gitdir , FileTest.IS_DIR)) {
48             this.gitdir = path; // naked...
49         }
50         this.cmds = new  Array<GitMonitorQueue> ();
51         //Repo.superclass.constructor.call(this,cfg);
52         
53     } 
54     /**
55      * add:
56      * add files to track.
57      *
58      * @argument {Array} files the files to add.
59      */
60     public string add ( Array<GitMonitorQueue> files ) throws Error, SpawnError
61     {
62         // should really find out if these are untracked files each..
63         // we run multiple versions to make sure that if one failes, it does not ignore the whole lot..
64         // not sure if that is how git works.. but just be certian.
65         var ret = "";
66         for (var i = 0; i < files.length;i++) {
67             var f = files.index(i).vname;
68             try {
69                 string[] cmd = { "add",    f  };
70                 this.git( cmd );
71             } catch (Error e) {
72                 ret += e.message  + "\n";
73             }        
74
75         }
76         return ret;
77     }
78     
79       /**
80      * remove:
81      * remove files to track.
82      *
83      * @argument {Array} files the files to add.
84      */
85     public string remove  ( Array<GitMonitorQueue> files ) throws Error, SpawnError
86     {
87         // this may fail if files do not exist..
88         // should really find out if these are untracked files each..
89         // we run multiple versions to make sure that if one failes, it does not ignore the whole lot..
90         // not sure if that is how git works.. but just be certian.
91         var ret = "";
92
93         for (var i = 0; i < files.length;i++) {
94             var f = files.index(i).vname;
95             try {
96                 string[] cmd = { "rm",  "-f" ,  f  };
97                 this.git( cmd );
98             } catch (Error e) {
99                 ret += e.message  + "\n";
100             }        
101         }
102
103         return ret;
104
105     }
106     
107     /**
108      * commit:
109      * perform a commit.
110      *
111      * @argument {Object} cfg commit configuration
112      * 
113      * @property {String} name (optional)
114      * @property {String} email (optional)
115      * @property {String} changed (date) (optional)
116      * @property {String} reason (optional)
117      * @property {Array} files - the files that have changed. 
118      * 
119      */
120      
121     public string commit ( string message, Array<GitMonitorQueue> files  ) throws Error, SpawnError
122     {
123         
124
125         /*
126         var env = [];
127
128         if (typeof(cfg.name) != 'undefined') {
129             args.push( {
130                 'author' : cfg.name + ' <' + cfg.email + '>'
131             });
132             env.push(
133                 "GIT_COMMITTER_NAME" + cfg.name,
134                 "GIT_COMMITTER_EMAIL" + cfg.email
135             );
136         }
137
138         if (typeof(cfg.changed) != 'undefined') {
139             env.push("GIT_AUTHOR_DATE= " + cfg.changed )
140             
141         }
142         */
143         string[] args = { "commit", "-m", };
144         args +=  (message.length > 0  ? message : "Changed" );
145         for (var i = 0; i< files.length ; i++ ) {
146             args += files.index(i).vname; // full path?
147         }
148          
149         return this.git(args);
150     }
151     
152     /**
153      * pull:
154      * Fetch and merge remote repo changes into current branch..
155      *
156      * At present we just need this to update the current working branch..
157      * -- maybe later it will have a few options and do more stuff..
158      *
159      */
160     public string pull () throws Error, SpawnError
161     {
162         // should probably hand error conditions better... 
163         string[] cmd = { "pull" };
164         return this.git( cmd );
165
166         
167         
168     }
169     /**
170      * push:
171      * Send local changes to remote repo(s)
172      *
173      * At present we just need this to push the current branch.
174      * -- maybe later it will have a few options and do more stuff..
175      *
176      */
177     public string push () throws Error, SpawnError
178     {
179         // should 
180         return this.git({ "push" });
181         
182     }
183      /**
184      * git:
185      * The meaty part.. run spawn.. with git..
186      *
187      *
188      */
189     
190     public string git(string[] args_in, ) throws Error, SpawnError
191     {
192         // convert arguments.
193         
194
195         string[]  args = { "git" };
196         args +=  "--git-dir";
197         args +=  this.gitdir;
198         args +=  "--no-pager";
199
200          
201
202         
203
204         //if (this.gitdir != this.repopath) {
205         //    args +=   "--work-tree";
206          //   args += this.repopath; 
207         //}
208         for (var i = 0; i < args_in.length;i++) {
209             args += args_in[i];
210         }            
211
212         //this.lastCmd = args.join(" ");
213         if(this.debug) {
214          
215             print(  string.joinv (", ", args)); 
216         }
217         string[]   env = {};
218         string  home = "HOME=" + Environment.get_home_dir() ;
219         env +=  home ;
220         // do not need to set gitpath..
221         //if (File.exists(this.repo + '/.git/config')) {
222             //env.push("GITPATH=" + this.repo );
223         //}
224         
225
226         var cfg = new SpawnConfig(this.gitdir, args , env);
227         
228
229        // may throw error...
230         var sp = new Spawn(cfg);
231                 
232         //print("GOT: " + output)
233         // parse output for some commands ?
234         return sp.output;
235     }
236
237 }