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       /**
164      * remove:
165      * remove files to track.
166      *
167      * @argument {Array} files the files to add.
168      */
169     public string remove  ( Array<GitMonitorQueue> files ) throws Error, SpawnError
170     {
171         // this may fail if files do not exist..
172         // should really find out if these are untracked files each..
173         // we run multiple versions to make sure that if one failes, it does not ignore the whole lot..
174         // not sure if that is how git works.. but just be certian.
175         var ret = "";
176
177         for (var i = 0; i < files.length;i++) {
178             var f = files.index(i).vname;
179             try {
180                 string[] cmd = { "rm",  "-f" ,  f  };
181                 this.git( cmd );
182             } catch (Error e) {
183                 ret += e.message  + "\n";
184             }        
185         }
186
187         return ret;
188
189     }
190     
191     /**
192      * commit:
193      * perform a commit.
194      *
195      * @argument {Object} cfg commit configuration
196      * 
197      * @property {String} name (optional)
198      * @property {String} email (optional)
199      * @property {String} changed (date) (optional)
200      * @property {String} reason (optional)
201      * @property {Array} files - the files that have changed. 
202      * 
203      */
204      
205     public string commit ( string message, Array<GitMonitorQueue> files  ) throws Error, SpawnError
206     {
207         
208
209         /*
210         var env = [];
211
212         if (typeof(cfg.name) != 'undefined') {
213             args.push( {
214                 'author' : cfg.name + ' <' + cfg.email + '>'
215             });
216             env.push(
217                 "GIT_COMMITTER_NAME" + cfg.name,
218                 "GIT_COMMITTER_EMAIL" + cfg.email
219             );
220         }
221
222         if (typeof(cfg.changed) != 'undefined') {
223             env.push("GIT_AUTHOR_DATE= " + cfg.changed )
224             
225         }
226         */
227         string[] args = { "commit", "-m" };
228         args +=  (message.length > 0  ? message : "Changed" );
229         for (var i = 0; i< files.length ; i++ ) {
230             args += files.index(i).vname; // full path?
231         }
232          
233         return this.git(args);
234     }
235     
236     /**
237      * pull:
238      * Fetch and merge remote repo changes into current branch..
239      *
240      * At present we just need this to update the current working branch..
241      * -- maybe later it will have a few options and do more stuff..
242      *
243      */
244     public string pull () throws Error, SpawnError
245     {
246         // should probably hand error conditions better... 
247         string[] cmd = { "pull" , "--no-edit" };
248         return this.git( cmd );
249
250         
251         
252     }
253     /**
254      * push:
255      * Send local changes to remote repo(s)
256      *
257      * At present we just need this to push the current branch.
258      * -- maybe later it will have a few options and do more stuff..
259      *
260      */
261     public string push () throws Error, SpawnError
262     {
263         // should 
264         return this.git({ "push" });
265         
266     }
267      /**
268      * git:
269      * The meaty part.. run spawn.. with git..
270      *
271      *
272      */
273     
274     public string git(string[] args_in ) throws Error, SpawnError
275     {
276         // convert arguments.
277         
278
279         string[]  args = { "git" };
280         //args +=  "--git-dir";
281         //args +=  this.gitdir;
282         args +=  "--no-pager";
283  
284
285         //if (this.gitdir != this.repopath) {
286         //    args +=   "--work-tree";
287          //   args += this.repopath; 
288         //}
289         for (var i = 0; i < args_in.length;i++) {
290             args += args_in[i];
291         }            
292
293         //this.lastCmd = args.join(" ");
294         //if(this.debug) {
295             stdout.printf( "CWD=%s\n",  this.git_working_dir ); 
296             print( "cmd: %s\n", string.joinv (" ", args)); 
297         //}
298
299         string[]   env = {};
300         string  home = "HOME=" + Environment.get_home_dir() ;
301         env +=  home ;
302         // do not need to set gitpath..
303         //if (File.exists(this.repo + '/.git/config')) {
304             //env.push("GITPATH=" + this.repo );
305         //}
306         
307
308         var cfg = new SpawnConfig(this.git_working_dir , args , env);
309         
310
311        // may throw error...
312         var sp = new Spawn(cfg);
313
314         stdout.printf( "GOT: %s\n" , sp.output);
315         // parse output for some commands ?
316         return sp.output;
317     }
318
319 }