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