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