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                 this.git( { "add" } + f);
69             } catch (Error e) {
70                 ret += e.message  + "\n";
71             }        
72
73         }
74     }
75     
76       /**
77      * remove:
78      * remove files to track.
79      *
80      * @argument {Array} files the files to add.
81      */
82     public string remove  ( Array<GitMonitorQueue> files )
83     {
84         // this may fail if files do not exist..
85         // should really find out if these are untracked files each..
86         // we run multiple versions to make sure that if one failes, it does not ignore the whole lot..
87         // not sure if that is how git works.. but just be certian.
88         var ret = "";
89
90         for (var i = 0; i < files.length;i++) {
91             var f = files.index(i).vname;
92             try {
93                 this.git( { "rm",  "-f" } + f  );
94             } catch (Error e) {
95                 ret += e.message  + "\n";
96             }        
97         }
98
99         return ret;
100
101     }
102     
103     /**
104      * commit:
105      * perform a commit.
106      *
107      * @argument {Object} cfg commit configuration
108      * 
109      * @property {String} name (optional)
110      * @property {String} email (optional)
111      * @property {String} changed (date) (optional)
112      * @property {String} reason (optional)
113      * @property {Array} files - the files that have changed. 
114      * 
115      */
116      
117     public string commit ( string message, Array<GitMonitorQueue> files  )
118     {
119         
120
121         /*
122         var env = [];
123
124         if (typeof(cfg.name) != 'undefined') {
125             args.push( {
126                 'author' : cfg.name + ' <' + cfg.email + '>'
127             });
128             env.push(
129                 "GIT_COMMITTER_NAME" + cfg.name,
130                 "GIT_COMMITTER_EMAIL" + cfg.email
131             );
132         }
133
134         if (typeof(cfg.changed) != 'undefined') {
135             env.push("GIT_AUTHOR_DATE= " + cfg.changed )
136             
137         }
138         */
139         var args = { "commit", "-m", };
140         arg +=  (message.length > 0  ? message : "Changed" );
141         for (var i = 0; i< files.length ; i++ ) {
142             args += files.index(i).vname; // full path?
143         }
144          
145         return this.git(args, env);
146     }
147     
148     /**
149      * pull:
150      * Fetch and merge remote repo changes into current branch..
151      *
152      * At present we just need this to update the current working branch..
153      * -- maybe later it will have a few options and do more stuff..
154      *
155      */
156     public string pull ()
157     {
158         // should probably hand error conditions better... 
159         return this.git({ "pull" });
160         
161         
162     }
163     /**
164      * push:
165      * Send local changes to remote repo(s)
166      *
167      * At present we just need this to push the current branch.
168      * -- maybe later it will have a few options and do more stuff..
169      *
170      */
171     public string push ()
172     {
173         // should 
174         return this.git({ "push" });
175         
176     }
177      /**
178      * git:
179      * The meaty part.. run spawn.. with git..
180      *
181      *
182      */
183     
184     public string git(string[] args_in, string[] env = {}) throws Error, SpawnError
185     {
186         // convert arguments.
187         
188
189         string[]  args = { "git" };
190         args +=  "--git-dir";
191         args +=  this.gitdir,
192         args +=  "--no-pager";
193
194
195         if (this.gitdir != this.repopath) {
196             args +=   "--work-tree";
197             args += this.repopath; 
198         }
199         for (var i = 0; i < args_in.length;i++) {
200             args += args_in[i];
201         }            
202
203         //this.lastCmd = args.join(" ");
204         if(this.debug) {
205          
206             print(  string.joinv (", ", args)); 
207         }
208         
209         env +=  ("HOME=" + Enviroment.get_home_dir() );
210         // do not need to set gitpath..
211         //if (File.exists(this.repo + '/.git/config')) {
212             //env.push("GITPATH=" + this.repo );
213         //}
214         
215
216         var cfg = new SpawnConfig(this.repopath, args , env);
217         
218
219        // may throw error...
220         var sp = new Spawn(cfg);
221                 
222         //print("GOT: " + output)
223         // parse output for some commands ?
224         return sp.output;
225     }
226
227 }