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