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     public string name;
16     public string gitdir;
17     public string git_working_dir;
18     public bool debug = false;
19     
20     public Gee.HashMap<string,bool> ignore_files;
21
22     /**
23     * index of.. matching gitpath..
24     */
25     public static int indexOf( Array<GitRepo> repos, string gitpath) {
26         // make a fake object to compare against..
27         var test_repo = new GitRepo(gitpath);
28         
29         for(var i =0; i < repos.length; i++) {
30             if (repos.index(i).gitdir == test_repo.gitdir) {
31                 return i;
32             }
33         }
34         return -1;
35     
36     }
37     
38     
39     public static   Array<GitRepo> list()
40     {
41         
42         //if (GitRepo.list_cache !=  null) {
43         //    unowned  Array<GitRepo>    ret = GitRepo.list_cache;
44          //   return ret;
45         //}
46         
47         var list_cache = new Array<GitRepo>();
48         
49         var dir = Environment.get_home_dir() + "/gitlive";
50         
51         var f = File.new_for_path(dir);
52         FileEnumerator file_enum;
53         try {
54             file_enum = f.enumerate_children(
55                 FileAttribute.STANDARD_DISPLAY_NAME + ","+ 
56                 FileAttribute.STANDARD_TYPE,
57                 FileQueryInfoFlags.NONE,
58                 null);
59         } catch (Error e) {
60             
61             return list_cache;
62             
63         }
64         
65         FileInfo next_file; 
66         
67         while (true) {
68             
69             try {
70                 next_file = file_enum.next_file(null);
71                 if (next_file == null) {
72                     break;
73                 }
74                 
75             } catch (Error e) {
76                 print("Error: %s\n",e.message);
77                 break;
78             }
79          
80             //print("got a file " + next_file.sudo () + '?=' + Gio.FileType.DIRECTORY);
81             
82             if (next_file.get_file_type() !=  FileType.DIRECTORY) {
83                 next_file = null;
84                 continue;
85             }
86             
87             if (next_file.get_file_type() ==  FileType.SYMBOLIC_LINK) {
88                 next_file = null;
89                 continue;
90             }
91             
92             if (next_file.get_display_name()[0] == '.') {
93                 next_file = null;
94                 continue;
95             }
96             var sp = dir+"/"+next_file.get_display_name();
97            
98             var gitdir = dir + "/" + next_file.get_display_name() + "/.git";
99             
100             if (!FileUtils.test(gitdir, FileTest.IS_DIR)) {
101                 continue;
102             }
103             
104              list_cache.append_val(new GitRepo(  sp )) ;
105              
106             
107         }
108     
109         return list_cache;
110         
111          
112           
113 }
114     
115  
116    
117     /**
118      * constructor:
119      * 
120      * @param {Object} cfg - Configuration
121      *     (basically repopath is currently only critical one.)
122      *
123      */
124      
125     public GitRepo(string path) {
126         // cal parent?
127         this.name =   File.new_for_path(path).get_basename();
128         this.ignore_files = new Gee.HashMap<string,bool>();
129         
130         this.git_working_dir = path;
131         this.gitdir = path + "/.git";
132         if (!FileUtils.test(this.gitdir , FileTest.IS_DIR)) {
133             this.gitdir = path; // naked...
134         }
135         this.cmds = new  Array<GitMonitorQueue> ();
136         //Repo.superclass.constructor.call(this,cfg);
137         
138     } 
139     /**
140      * add:
141      * add files to track.
142      *
143      * @argument {Array} files the files to add.
144      */
145     public string add ( Array<GitMonitorQueue> files ) throws Error, SpawnError
146     {
147         // should really find out if these are untracked files each..
148         // we run multiple versions to make sure that if one failes, it does not ignore the whole lot..
149         // not sure if that is how git works.. but just be certian.
150         var ret = "";
151         for (var i = 0; i < files.length;i++) {
152             var f = files.index(i).vname;
153             try {
154                 string[] cmd = { "add",    f  };
155                 this.git( cmd );
156             } catch (Error e) {
157                 ret += e.message  + "\n";
158             }        
159
160         }
161         return ret;
162     }
163         
164     public bool is_ignore(string fname) throws Error, SpawnError
165     {
166                 if (fname == ".gitignore") {
167                         this.ignore_files.clear();
168                 }
169                 
170                 if (this.ignore_files.has_key(fname)) {
171                         return this.ignore_files.get(fname);
172                 }
173                 
174                 try {
175                         var ret = this.git( { "check-ignore" , fname } );
176                         this.ignore_files.set(fname, ret.length >  0);
177                         return ret.length > 0;
178                 } catch (SpawnError e) {
179                         this.ignore_files.set(fname, false);
180                         return false;
181                 }
182                  
183     } 
184     
185     
186       /**
187      * remove:
188      * remove files to track.
189      *
190      * @argument {Array} files the files to add.
191      */
192     public string remove  ( Array<GitMonitorQueue> files ) throws Error, SpawnError
193     {
194         // this may fail if files do not exist..
195         // should really find out if these are untracked files each..
196         // we run multiple versions to make sure that if one failes, it does not ignore the whole lot..
197         // not sure if that is how git works.. but just be certian.
198         var ret = "";
199
200         for (var i = 0; i < files.length;i++) {
201             var f = files.index(i).vname;
202             try {
203                 string[] cmd = { "rm",  "-f" ,  f  };
204                 this.git( cmd );
205             } catch (Error e) {
206                 ret += e.message  + "\n";
207             }        
208         }
209
210         return ret;
211
212     }
213     
214     
215     /**
216      * commit:
217      * perform a commit.
218      *
219      * @argument {Object} cfg commit configuration
220      * 
221      * @property {String} name (optional)
222      * @property {String} email (optional)
223      * @property {String} changed (date) (optional)
224      * @property {String} reason (optional)
225      * @property {Array} files - the files that have changed. 
226      * 
227      */
228      
229     public string commit ( string message, Array<GitMonitorQueue> files  ) throws Error, SpawnError
230     {
231         
232
233         /*
234         var env = [];
235
236         if (typeof(cfg.name) != 'undefined') {
237             args.push( {
238                 'author' : cfg.name + ' <' + cfg.email + '>'
239             });
240             env.push(
241                 "GIT_COMMITTER_NAME" + cfg.name,
242                 "GIT_COMMITTER_EMAIL" + cfg.email
243             );
244         }
245
246         if (typeof(cfg.changed) != 'undefined') {
247             env.push("GIT_AUTHOR_DATE= " + cfg.changed )
248             
249         }
250         */
251         string[] args = { "commit", "-m" };
252         args +=  (message.length > 0  ? message : "Changed" );
253         for (var i = 0; i< files.length ; i++ ) {
254             args += files.index(i).vname; // full path?
255         }
256          
257         return this.git(args);
258     }
259     
260     /**
261      * pull:
262      * Fetch and merge remote repo changes into current branch..
263      *
264      * At present we just need this to update the current working branch..
265      * -- maybe later it will have a few options and do more stuff..
266      *
267      */
268     public string pull () throws Error, SpawnError
269     {
270         // should probably hand error conditions better... 
271         string[] cmd = { "pull" , "--no-edit" };
272         return this.git( cmd );
273
274         
275     }
276     
277     public delegate void GitAsyncCallback (string str)
278     async public string pull_async(GitAsyncCallback cb) {
279     
280          
281     
282     }
283     
284     /**
285      * push:
286      * Send local changes to remote repo(s)
287      *
288      * At present we just need this to push the current branch.
289      * -- maybe later it will have a few options and do more stuff..
290      *
291      */
292     public string push () throws Error, SpawnError
293     {
294         // should 
295         return this.git({ "push", "origin", "HEAD" });
296         
297     }
298     
299     
300     
301      /**
302      * git:
303      * The meaty part.. run spawn.. with git..
304      *
305      *
306      */
307     
308     public string git(string[] args_in ) throws Error, SpawnError
309     {
310         // convert arguments.
311         
312         string[]  args = { "git" };
313         //args +=  "--git-dir";
314         //args +=  this.gitdir;
315         args +=  "--no-pager";
316  
317  
318         //if (this.gitdir != this.repopath) {
319         //    args +=   "--work-tree";
320          //   args += this.repopath; 
321         //}
322         for (var i = 0; i < args_in.length;i++) {
323             args += args_in[i];
324         }            
325
326         //this.lastCmd = args.join(" ");
327         //if(this.debug) {
328             stdout.printf( "CWD=%s\n",  this.git_working_dir ); 
329             print( "cmd: %s\n", string.joinv (" ", args)); 
330         //}
331
332         string[]   env = {};
333         string  home = "HOME=" + Environment.get_home_dir() ;
334         env +=  home ;
335         // do not need to set gitpath..
336         //if (File.exists(this.repo + '/.git/config')) {
337             //env.push("GITPATH=" + this.repo );
338         //}
339         
340
341         var cfg = new SpawnConfig(this.git_working_dir , args , env);
342         
343
344        // may throw error...
345         var sp = new Spawn(cfg);
346       
347
348         stdout.printf( "GOT: %s\n" , sp.output);
349         // parse output for some commands ?
350         return sp.output;
351     }
352         
353         
354         public string git_async(GitAsyncCallback cb, string[] args_in ) throws Error, SpawnError
355     {
356         // convert arguments.
357         
358         string[]  args = { "git" };
359         //args +=  "--git-dir";
360         //args +=  this.gitdir;
361         args +=  "--no-pager";
362  
363  
364         //if (this.gitdir != this.repopath) {
365         //    args +=   "--work-tree";
366          //   args += this.repopath; 
367         //}
368         for (var i = 0; i < args_in.length;i++) {
369             args += args_in[i];
370         }            
371
372         //this.lastCmd = args.join(" ");
373         //if(this.debug) {
374             stdout.printf( "CWD=%s\n",  this.git_working_dir ); 
375             print( "cmd: %s\n", string.joinv (" ", args)); 
376         //}
377
378         string[]   env = {};
379         string  home = "HOME=" + Environment.get_home_dir() ;
380         env +=  home ;
381         // do not need to set gitpath..
382         //if (File.exists(this.repo + '/.git/config')) {
383             //env.push("GITPATH=" + this.repo );
384         //}
385         
386
387         var cfg = new SpawnConfig(this.git_working_dir , args , env);
388         cfg.async = true;
389         cfg.onFinish((err) {
390                 this.
391         });
392
393        // may throw error...
394         var sp = new Spawn(cfg);
395       
396
397         stdout.printf( "GOT: %s\n" , sp.output);
398         // parse output for some commands ?
399         return sp.output;
400     }
401 }