Fix #5560 - Gitlive - branching wip
[gitlive] / old_seed_version / Scm / Repo.js
1 var XObject = imports.XObject.XObject;
2
3 var GLib  = imports.gi.GLib;
4 var File = imports.File.File;
5 // mix of SCM and Repo class from mtrack.
6
7 //var SCM = imports.SCM.SCM;
8
9 _abstract = function() {
10     throw "abstract function not implemented";
11 }
12
13 Repo = XObject.define(
14     function(cfg) {
15         // cal parent?
16         if (typeof(cfg) != 'object') {
17             return;
18         } 
19         XObject.extend(cfg);
20          
21     },
22     Object,
23     {
24             
25         id : null,
26         scmtype : null,
27          browserurl : null,
28         browsertype : null,
29         description : null,
30         parent : '',
31         clonedfrom : null,
32         serverurl : null,
33
34     
35         links_to_add : false,
36         links_to_remove : false,
37         links : false,
38         
39         
40         repopath : '',
41         
42        
43         /** Returns an array keyed by possible branch names.
44         * The data associated with the branches is implementation
45         * defined.
46         * If the SCM does not have a concept of first-class branch
47         * objects, this function returns null */
48         getBranches : _abstract,
49         
50         /** Returns an array keyed by possible tag names.
51         * The data associated with the tags is implementation
52         * defined.
53         * If the SCM does not have a concept of first-class tag
54         * objects, this function returns null */
55         getTags : _abstract,
56         
57         /** Enumerates the files/dirs that are present in the specified
58         * location of the repository that match the specified revision,
59         * branch or tag information.  If no revision, branch or tag is
60         * specified, then the appropriate default is assumed.
61         *
62         * The second and third parameters are optional; the second
63         * parameter is one of 'rev', 'branch', or 'tag', and if specifed
64         * the third parameter must be the corresponding revision, branch
65         * or tag identifier.
66         *
67         * The return value is an array of MTrackSCMFile objects present
68         * at that location/revision of the repository.
69         */
70         readdir : _abstract,
71         
72         /** Queries information on a specific file in the repository.
73         *
74         * Parameters are as for readdir() above.
75         *
76         * This function returns a single MTrackSCMFile for the location
77         * in question.
78         */
79         file : _abstract,
80         
81         /** Queries history for a particular location in the repo.
82         *
83         * Parameters are as for readdir() above, except that path can be
84         * left unspecified to query the history for the entire repo.
85         *
86         * The limit parameter limits the number of entries returned; it it is
87         * a number, it specifies the number of events, otherwise it is assumed
88         * to be a date in the past; only events since that date will be returned.
89         *
90         * Returns an array of MTrackSCMEvent objects.
91         */
92         history : _abstract,
93         
94         /** Obtain the diff text representing a change to a file.
95         *
96         * You may optionally provide one or two revisions as context.
97         *
98         * If no revisions are passed in, then the change associated
99         * with the location will be assumed.
100         *
101         * If one revision is passed, then the change associated with
102         * that event will be assumed.
103         *
104         * If two revisions are passed, then the difference between
105         * the two events will be assumed.
106         */
107         diff : _abstract,
108         
109         /** Determine the next and previous revisions for a given
110         * changeset.
111         *
112         * Returns an array: the 0th element is an array of prior revisions,
113         * and the 1st element is an array of successor revisions.
114         *
115         * There will usually be one prior and one successor revision for a
116         * given change, but some SCMs will return multiples in the case of
117         * merges.
118         */
119         getRelatedChanges : _abstract,
120         
121         /** Returns a working copy object for the repo
122         *
123         * The intended purpose is to support wiki page modifications, and
124         * as such, is not meant to be an especially efficient means to do so.
125         */
126         getWorkingCopy : _abstract,
127         
128         /** Returns meta information about the SCM type; this is used in the
129         * UI and tooling to let the user know their options.
130         *
131         * Returns an array with the following keys:
132         * 'name' => 'Mercurial', // human displayable name
133         * 'tools' => array('hg'), // list of tools to find during setup
134         */
135         getSCMMetaData : _abstract,
136         
137         /** Returns the default 'root' location in the repository.
138         * For SCMs that have a concept of branches, this is the empty string.
139         * For SCMs like SVN, this is the trunk dir */
140         getDefaultRoot : function() {
141              return '';
142         },
143
144         
145           
146         
147         /* takes an MTrackSCM as a parameter because in some bootstrapping
148          * cases, we're actually MTrack_Repo and not the end-class.
149          * MTrack_Repo calls the end-class method and passes itself in for
150          * context */
151         reconcileRepoSettings  : _abstract,
152         
153         
154         
155         
156         getBrowseRootName   : _abstract, 
157         /***
158          *
159          * resolve Revision
160          *
161          * @param {string} rev - a fixed revision - always returns this if it is not false;
162          * @param {string} object [rev|branch|tag]- object type
163          * @param {ident} object id (eg. rev nun, branch name, tag name)
164          *
165          *
166          */
167         resolveRevision :function(rev, object, ident)
168         {
169             if (rev !== false) {
170                 return rev;
171             }
172             
173             if (object === false) {
174                 return false;
175             }
176             
177             switch (object) {
178                 case 'rev':
179                     rev = ident;
180                     break;
181                 
182                 case 'branch':
183                     
184                     // technically we should check it exists..
185                     rev = ident;
186                     break;
187                     //var branches = this.getBranches();
188                     // branches is now an array - not a map.
189                      
190                     //rev = typeof(branches[ident]) == 'undefined' ? false : branches[ident];
191                     //break;
192                 
193                 case 'tag':
194                     tags = this.getTags();
195                     rev = typeof(tags[ident]) == 'undefined' ? false : tags[ident];
196                     break;
197             }
198             if (rev === false) {
199                 throw   "don't know which revision to use (rev,object,ident) got" + object;
200             }
201             return rev;
202         }
203         
204     
205      /*
206     
207     function reconcileRepoSettings()
208     {
209         $c = self::Factory(array('scmtype'=>$this->scmtype));
210         $s->reconcileRepoSettings($this);
211     }
212     
213     function getServerURL()
214     {
215         if ($this->serverurl) {
216             return $this->serverurl;
217         }
218         
219         return null;
220     }
221
222     function getCheckoutCommand() {
223         $url = $this->getServerURL();
224         if (strlen($url)) {
225           return $this->scmtype . ' clone ' . $this->getServerURL();
226         }
227         return null;
228     }
229
230     function canFork() {
231         return false;
232     }
233
234     function getWorkingCopy() {
235          throw new Exception("cannot getWorkingCopy from a generic repo object");
236     }
237     /*
238     function deleteRepo(MTrackChangeset $CS) {
239         MTrackDB::q('delete from repos where repoid = ?', $this->repoid);
240         mtrack_rmdir($this->repopath);
241     }
242      
243   
244
245 // these are needed just to implement the abstract interface..
246     function getBranches() {}
247     function getTags() {}
248     function readdir($path, $object = null, $ident = null) {}
249     function file($path, $object = null, $ident = null) {}
250     function history($path, $limit = null, $object = null, $ident = null){}
251     function diff($path, $from = null, $to = null) {}
252     function getRelatedChanges($revision) {}
253     function getSCMMetaData() { return null; }
254     /**
255      *  converts a commit log message (cached locally into a working object..)
256      *  see Browse.php
257      */
258     /*
259     function commitLogToEvent($str) {
260         throw new Exception("no implementation for commitLogToEvent");
261     }
262     */
263 });
264
265
266 /*
267  
268     static $scms = array();
269     static function factory($ar)
270     {
271         //print_r($ar);
272         $type = ucfirst($ar['scmtype']);
273         $fn = 'MTrack/SCM/'.$type .'/Repo.php';
274         $cls = 'MTrack_SCM_'.$type .'_Repo';
275         require_once $fn;
276         
277         $ret = new $cls($ar);
278         
279         return $ret;
280         
281     }
282     
283  
284     static function getAvailableSCMs()
285     {
286         $ret = array();
287         $ar = scandir(dirname(__FILE__).'/SCM');
288         
289         foreach($ar as $a) {
290             if (empty($a) || $a[0] == '.') {
291                 continue;
292             }
293             $fn = dirname(__FILE__).'/SCM/'.$a.'/Repo.php';
294             if (!file_exists($fn)) {
295                 continue;
296             } 
297             $ret[$a] = MTrack_Repo::factory(array('scmtype'=> $a));
298             
299         }
300         return $ret;
301     }  
302 */
303
304 Repo._list  = false;
305 Repo.list = function()
306 {
307     
308     if (Repo._list !== false) {
309         return Repo._list;
310     }
311     Repo._list  = [];
312     var dir = GLib.get_home_dir() + '/gitlive';
313     var ar = File.list(dir );
314     print(JSON.stringify(ar));
315     ar.forEach(function(f) {
316         if (File.exists(dir + '/' + f +'/.git')) {
317             Repo._list.push(new imports.Scm.Git.Repo.Repo(  {
318                 repopath : dir  +'/' + f,
319                 name : f
320             }))
321         }
322     });
323     
324     return Repo._list;
325       
326 }
327 Repo.get = function(path_or_name)
328 {
329     var tr = Repo.list();
330     for (var i =0;i < tr.length; i++) {
331         if (tr[i].name == path_or_name) {
332             return tr[i];
333         }
334         
335         if (tr[i].repopath == path_or_name) {
336             return tr[i];
337         }
338         
339     }
340     return false;
341     
342 }