sync
[gitlive] / old_seed_version / Scm / Git / Repo.vala
1
2 /**
3  * @class Scm.Git.Repo
4  *
5  * @extends Scm.Repo
6  * 
7  *
8  *
9  */
10 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 (reos.index(i).gitpath == add) {
21                     return i;
22                 }
23             }
24             return -1;
25         }
26     }
27
28
29
30      Array<GitMontitorQueue> 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(path) {
43         // cal parent?
44         
45         this.gitdir = path + "/.git";
46         if (!GLib.file_test(this.gitdir , GLib.FileTest.IS_DIR)) {
47             this.gitdir = path; // naked...
48         }
49         this.cmds = new  Array<GitMontitorQueue> ();
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<GitMontitorQueue> 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.item(i);
66             
67             if (!GLib.FileUtils.test(this.repopath +"/" + f, GLib.FileTest.EXISTS)) {
68                 continue;
69             }
70             printf("Checked %s - exists\n", this.repopath +"/" + f);
71             try {
72                 this.git( { "add",   f });
73             } catch (Error e) {
74                 ret += e.message  + "\n";
75             }        
76
77         }
78     }
79     
80       /**
81      * remove:
82      * remove files to track.
83      *
84      * @argument {Array} files the files to add.
85      */
86     public string remove : function ( Array<GitMontitorQueue> files )
87     {
88         // this may fail if files do not exist..
89         // should really find out if these are untracked files each..
90         // we run multiple versions to make sure that if one failes, it does not ignore the whole lot..
91         // not sure if that is how git works.. but just be certian.
92         var ret = "";
93
94         for (var i = 0; i < files.length;i++) {
95             var f = files.item(i);
96             try {
97                 this.git( { "rm",  "-f" ,  f });
98             } catch (Error e) {
99                 ret += e.message  + "\n";
100             }        
101         }
102
103         return ret;
104
105     },
106     
107     /**
108      * commit:
109      * perform a commit.
110      *
111      * @argument {Object} cfg commit configuration
112      * 
113      * @property {String} name (optional)
114      * @property {String} email (optional)
115      * @property {String} changed (date) (optional)
116      * @property {String} reason (optional)
117      * @property {Array} files - the files that have changed. 
118      * 
119      */
120      
121     public string commit ( string message, Array<GitMontitorQueue> files  )
122     {
123         
124
125         /*
126         var env = [];
127
128         if (typeof(cfg.name) != 'undefined') {
129             args.push( {
130                 'author' : cfg.name + ' <' + cfg.email + '>'
131             });
132             env.push(
133                 "GIT_COMMITTER_NAME" + cfg.name,
134                 "GIT_COMMITTER_EMAIL" + cfg.email
135             );
136         }
137
138         if (typeof(cfg.changed) != 'undefined') {
139             env.push("GIT_AUTHOR_DATE= " + cfg.changed )
140             
141         }
142         */
143         var args = { "commit", "-m", message.length ? message : "Changed" }
144         for (var i = i< files.length ; i++ ) {
145             args += files.items(i).path; // full path?
146         }
147          
148         return this.git(args, env);
149     },
150     
151     /**
152      * pull:
153      * Fetch and merge remote repo changes into current branch..
154      *
155      * At present we just need this to update the current working branch..
156      * -- maybe later it will have a few options and do more stuff..
157      *
158      */
159     pull : function ()
160     {
161         // should probably hand error conditions better... 
162         return this.git({ "pull" });
163         
164         
165     },
166     /**
167      * push:
168      * Send local changes to remote repo(s)
169      *
170      * At present we just need this to push the current branch.
171      * -- maybe later it will have a few options and do more stuff..
172      *
173      */
174     push : function ()
175     {
176         // should 
177         return this.git({ "push" });
178         
179     },
180      /**
181      * git:
182      * The meaty part.. run spawn.. with git..
183      *
184      *
185      */
186     
187     git: function(string[] args_in, string[] env = {})
188     {
189         // convert arguments.
190         
191
192         var args = {
193            "git", 
194             "--git-dir", this.gitdir,
195             "--no-pager",
196         }; 
197
198
199         if (this.gitdir != this.repopath) {
200             args +=   "--work-tree"
201             args += this.repopath; 
202         }
203         for (var i = i; 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_list);); 
211         }
212         
213         env +=  ("HOME=" + GLib.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.repopath, 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 }