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