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