srchub

srchub Git Source Tree


Root/indefero/src/IDF/Scm/Svn.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of InDefero, an open source project management application.
# Copyright (C) 2008-2011 Céondo Ltd and contributors.
#
# InDefero is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# InDefero is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# ***** END LICENSE BLOCK ***** */
 
/**
 * Subversion backend.
 * When a branch is not a branch.
 *
 * Contrary to most other SCMs, Subversion is using folders to manage
 * the branches and so what is either the commit or the branch in
 * other SCMs is the revision number with Subversion. So, do not be
 * surprised if you have the feeling that the methods are not really
 * returning what could be expected from their names.
 */
class IDF_Scm_Svn extends IDF_Scm
{
    public $username = '';
    public $password = '';
    private $assoc = array('dir' => 'tree',
                           'file' => 'blob');
 
    public function __construct($repo, $project=null)
    {
        $this->repo = $repo;
        $this->project = $project;
        $this->cache['commitmess'] = array();
    }
 
    public function isAvailable()
    {
        $cmd = $this->svnCmd(array('info', '--xml'), $this->repo);
        $xmlInfo = self::shell_exec('IDF_Scm_Svn::isAvailable', $cmd);
 
        try {
            $xml = simplexml_load_string($xmlInfo);
        }
        catch (Exception $e) {
            return false;
        }
        if (!isset($xml->entry->commit['revision'])) {
            return false;
        }
        if (0 == (int)$xml->entry->commit['revision']) {
            return false;
        }
        return true;
    }
 
    public function getRepositorySize()
    {
        if (strpos($this->repo, 'file://') !== 0) {
            return -1;
        }
        $cmd = Pluf::f('idf_exec_cmd_prefix', '').'du -sk '
            .escapeshellarg(substr($this->repo, 7));
        $out = explode(' ', self::shell_exec('IDF_Scm_Svn::getRepositorySize', $cmd), 2);
        return (int) $out[0]*1024;
    }
 
    /**
     * Given the string describing the author from the log find the
     * author in the database.
     *
     * @param string Author
     * @return mixed Pluf_User or null
     */
    public function findAuthor($author)
    {
        $sql = new Pluf_SQL('login=%s', array(trim($author)));
        $users = Pluf::factory('Pluf_User')->getList(array('filter'=>$sql->gen()));
        return ($users->count() > 0) ? $users[0] : null;
    }
 
    /**
     * Returns the URL of the subversion repository.
     *
     * @param IDF_Project
     * @param string
     * @return string URL
     */
    public static function getAnonymousAccessUrl($project,$commit=null)
    {
        $conf = $project->getConf();
        if (false !== ($url=$conf->getVal('svn_remote_url', false))
            && !empty($url)) {
            // Remote repository
            return $url;
        }
        return sprintf(Pluf::f('svn_remote_url'), $project->shortname);
    }
 
    /**
     * Returns the URL of the subversion repository.
     *
     * @param IDF_Project
     * @param string
     * @return string URL
     */
    public static function getAuthAccessUrl($project, $user, $commit=null)
    {
        $conf = $project->getConf();
        if (false !== ($url=$conf->getVal('svn_remote_url', false))
            && !empty($url)) {
            // Remote repository
            return $url;
        }
        return sprintf(Pluf::f('svn_remote_url'), $project->shortname);
    }
 
    /**
     * Returns this object correctly initialized for the project.
     *
     * @param IDF_Project
     * @return IDF_Scm_Svn
     */
    public static function factory($project)
    {
        $conf = $project->getConf();
        // Find the repository
        if (false !== ($rep=$conf->getVal('svn_remote_url', false))
            && !empty($rep)) {
            // Remote repository
            $scm = new IDF_Scm_Svn($rep, $project);
            $scm->username = $conf->getVal('svn_username');
            $scm->password = $conf->getVal('svn_password');
            return $scm;
        } else {
            $rep = sprintf(Pluf::f('svn_repositories'), $project->shortname);
            return new IDF_Scm_Svn($rep, $project);
        }
    }
 
    /**
     * Subversion revisions are either a number or 'HEAD'.
     */
    public function validateRevision($rev)
    {
        if ($rev == 'HEAD') {
            return IDF_Scm::REVISION_VALID;
        }
 
        $cmd = $this->svnCmd(array('info'), $this->repo, $rev);
        self::exec('IDF_Scm_Svn::validateRevision', $cmd, $out, $ret);
 
        if ($ret == 0)
            return IDF_Scm::REVISION_VALID;
        return IDF_Scm::REVISION_INVALID;
    }
 
    /**
     * Test a given object hash.
     *
     * @param string Object hash.
     * @return mixed false if not valid or 'blob', 'tree', 'commit'
     */
    public function testHash($rev, $path='')
    {
        // OK if HEAD on /
        if ($rev === 'HEAD' && $path === '') {
            return 'commit';
        }
 
        // Else, test the path on revision
        $cmd = $this->svnCmd(array('info', '--xml'),
                             $this->repo.'/'.self::smartEncode($path),
                             $rev);
        $xmlInfo = self::shell_exec('IDF_Scm_Svn::testHash', $cmd);
 
        // If exception is thrown, return false
        try {
            $xml = simplexml_load_string($xmlInfo);
        }
        catch (Exception $e) {
            return false;
        }
 
        // If the entry node does exists, params are wrong
        if (!isset($xml->entry)) {
            return false;
        }
 
        // Else, enjoy it :)
        return 'commit';
    }
 
    public function getTree($commit, $folder='/', $branch=null)
    {
        $cmd = $this->svnCmd(array('ls', '--xml'),
                             $this->repo.'/'.self::smartEncode($folder),
                             $commit);
        $xml = simplexml_load_string(self::shell_exec('IDF_Scm_Svn::getTree', $cmd));
        $res = array();
        $folder = (strlen($folder) and ($folder != '/')) ? $folder.'/' : '';
        foreach ($xml->list->entry as $entry) {
            $file = array();
            $file['type'] = $this->assoc[(string) $entry['kind']];
            $file['file'] = (string) $entry->name;
            $file['fullpath'] = $folder.((string) $entry->name);
            $file['efullpath'] = self::smartEncode($file['fullpath']);
            $file['date'] = gmdate('Y-m-d H:i:s',
                                   strtotime((string) $entry->commit->date));
            $file['rev'] = (string) $entry->commit['revision'];
            $file['log'] = $this->getCommitMessage($file['rev']);
            // Get the size if the type is blob
            if ($file['type'] == 'blob') {
                $file['size'] = (string) $entry->size;
            }
            $file['author'] = (string) $entry->commit->author;
            $file['perm'] = '';
            $res[] = (object) $file;
        }
        return $res;
    }
 
    /**
     * Get the commit message of a revision revision.
     *
     * @param string Commit ('HEAD')
     * @return String commit message
     */
    private function getCommitMessage($rev='HEAD')
    {
        if (isset($this->cache['commitmess'][$rev])) {
            return $this->cache['commitmess'][$rev];
        }
        $cmd = $this->svnCmd(array('log', '--xml', '--limit', '1'), $this->repo, $rev);
        try {
            $xml = simplexml_load_string(self::shell_exec('IDF_Scm_Svn::getCommitMessage', $cmd));
            $this->cache['commitmess'][$rev] = (string) $xml->logentry->msg;
        }
        catch (Exception $e) {
            $this->cache['commitmess'][$rev] = '';
        }
        return $this->cache['commitmess'][$rev];
    }
 
    public function getPathInfo($filename, $rev=null)
    {
        if ($rev == null) {
            $rev = 'HEAD';
        }
        $cmd = $this->svnCmd(array('info', '--xml'),
                             $this->repo.'/'.self::smartEncode($filename), $rev);
        $xml = simplexml_load_string(self::shell_exec('IDF_Scm_Svn::getPathInfo', $cmd));
        if (!isset($xml->entry)) {
            return false;
        }
        $entry = $xml->entry;
        $file = array();
        $file['fullpath'] = $filename;
        $file['hash'] = (string) $entry->repository->uuid;
        $file['type'] = $this->assoc[(string) $entry['kind']];
        $pathinfo = pathinfo($filename);
        $file['file'] = $pathinfo['basename'];
        $file['rev'] = $rev;
        $file['author'] = (string) $entry->author;
        $file['date'] = gmdate('Y-m-d H:i:s', strtotime((string) $entry->commit->date));
        $file['size'] = (string) $entry->size;
        $file['log'] = '';
        return (object) $file;
    }
 
    public function getFile($def, $cmd_only=false)
    {
        $cmd = $this->svnCmd(array('cat'),
                             $this->repo.'/'.self::smartEncode($def->fullpath),
                             $def->rev);
        return ($cmd_only) ?
            $cmd : self::shell_exec('IDF_Scm_Svn::getFile', $cmd);
    }
 
    /**
     * Subversion branches are folder based.
     *
     * One need to list the folder to know them.
     */
    public function getBranches()
    {
        if (isset($this->cache['branches'])) {
            return $this->cache['branches'];
        }
        $res = array();
        $cmd = $this->svnCmd(array('ls'), $this->repo.'/branches', 'HEAD');
        self::exec('IDF_Scm_Svn::getBranches', $cmd, $out, $ret);
        if ($ret == 0) {
            foreach ($out as $entry) {
                if (substr(trim($entry), -1) == '/') {
                    $branch = substr(trim($entry), 0, -1);
                    $res[$branch] = 'branches/'.$branch;
                }
            }
        }
        ksort($res);
 
        $cmd = $this->svnCmd(array('info'), $this->repo.'/trunk', 'HEAD');
        self::exec('IDF_Scm_Svn::getBranches', $cmd, $out, $ret);
        if ($ret == 0) {
            $res = array('trunk' => 'trunk') + $res;
        }
        $this->cache['branches'] = $res;
        return $res;
    }
 
    /**
     * Subversion tags are folder based.
     *
     * One need to list the folder to know them.
     */
    public function getTags()
    {
        if (isset($this->cache['tags'])) {
            return $this->cache['tags'];
        }
        $res = array();
        $cmd = $this->svnCmd(array('ls'), $this->repo.'/tags', 'HEAD');
        self::exec('IDF_Scm_Svn::getTags', $cmd, $out, $ret);
        if ($ret == 0) {
            foreach ($out as $entry) {
                if (substr(trim($entry), -1) == '/') {
                    $tag = substr(trim($entry), 0, -1);
                    $res[$tag] = 'tags/'.$tag;
                }
            }
        }
        ksort($res);
        $this->cache['tags'] = $res;
        return $res;
    }
 
    public function getMainBranch()
    {
        return 'HEAD';
    }
 
    public function inBranches($commit, $path)
    {
        foreach ($this->getBranches() as $branch => $bpath) {
            if ($bpath and 0 === strpos($path, $bpath)) {
                return array($branch);
            }
        }
        return array();
    }
 
    public function inTags($commit, $path)
    {
        foreach ($this->getTags() as $tag => $tpath) {
            if ($tpath and 0 === strpos($path, $tpath)) {
                return array($tag);
            }
        }
        return array();
    }
 
    /**
     * Get commit details.
     *
     * @param string Commit
     * @param bool Get commit diff (false)
     * @return array Changes
     */
    public function getCommit($commit, $getdiff=false)
    {
        if ($this->validateRevision($commit) != IDF_Scm::REVISION_VALID) {
            return false;
        }
        $res = array();
        $cmd = $this->svnCmd(array('log', '--xml', '--limit', '1', '-v'),
                             $this->repo, $commit);
        $xmlRes = self::shell_exec('IDF_Scm_Svn::getCommit', $cmd);
        $xml = simplexml_load_string($xmlRes);
        $res['author'] = (string) $xml->logentry->author;
        $res['date'] = gmdate('Y-m-d H:i:s', strtotime((string) $xml->logentry->date));
        $res['title'] = (string) $xml->logentry->msg;
        $res['commit'] = (string) $xml->logentry['revision'];
        $res['parents'] = $xml->logentry['revision'] > 1
            ? array((string) $xml->logentry['revision'] - 1)
            : array();
        $res['diff'] = ($getdiff) ? $this->getDiff($commit) : '';
        $res['tree'] = '';
        $res['branch'] = '';
        return (object) $res;
    }
 
    /**
     * Check if a commit is big.
     *
     * @param string Commit ('HEAD')
     * @return bool The commit is big
     */
    public function isCommitLarge($commit='HEAD')
    {
        if (substr($this->repo, 0, 7) != 'file://') {
            return false;
        }
        // We have a locally hosted repository, we can query it with
        // svnlook
        $repo = substr($this->repo, 7);
        $cmd = sprintf(Pluf::f('svnlook_path', 'svnlook').' changed -r %s %s',
                       escapeshellarg($commit),
                       escapeshellarg($repo));
        $cmd = Pluf::f('idf_exec_cmd_prefix', '').$cmd;
        $out = self::shell_exec('IDF_Scm_Svn::isCommitLarge', $cmd);
        $lines = preg_split("/\015\012|\015|\012/", $out);
        return (count($lines) > 100);
    }
 
    private function getDiff($rev='HEAD')
    {
        $res = array();
        $cmd = $this->svnCmd(array('diff', '-c', $rev), $this->repo);
        return self::shell_exec('IDF_Scm_Svn::getDiff', $cmd);
    }
 
    /**
     * @see IDF_Scm::getChanges()
     */
    public function getChanges($commit)
    {
        if ($this->validateRevision($commit) != IDF_Scm::REVISION_VALID) {
            return null;
        }
        $cmd = $this->svnCmd(array('log', '--xml', '-v'), $this->repo, $commit);
        $out = array();
        $out = self::shell_exec('IDF_Scm_Svn::getChanges', $cmd);
        $xml = simplexml_load_string($out);
        if (count($xml) == 0) {
            return null;
        }
        $entry = current($xml);
 
        $return = (object) array(
            'additions'  => array(),
            'deletions'  => array(),
            'patches'    => array(),
            // while SVN has support for attributes, we cannot see their changes
            // in the log's XML unfortunately
            'properties' => array(),
            'copies'     => array(),
            'renames'    => array(),
        );
 
        foreach ($entry->paths->path as $p) {
            $path = (string) $p;
            foreach ($p->attributes() as $k => $v) {
                $key = (string) $k;
                $val = (string) $v;
                if ($key != 'action')
                    continue;
                if ($val == 'M')
                    $return->patches[] = $path;
                else if ($val == 'A')
                    $return->additions[] = $path;
                else if ($val == 'D')
                    $return->deletions[] = $path;
            }
        }
 
        // copies are treated as renames if they have an add _and_ a drop;
        // only if they only have an add, but no drop, they're treated as copies
        foreach ($entry->paths->path as $p) {
            $trg = (string) $p;
            $src = null;
            foreach ($p->attributes() as $k => $v) {
                if ((string) $k == 'copyfrom-path') {
                    $src = (string) $v;
                    break;
                }
            }
 
            if ($src == null)
                continue;
 
            $srcidx = array_search($src, $return->deletions);
            $trgidx = array_search($trg, $return->additions);
            if ($srcidx !== false && $trgidx !== false) {
                $return->renames[$src] = $trg;
                unset($return->deletions[$srcidx]);
                unset($return->additions[$trgidx]);
                continue;
            }
            if ($srcidx === false && $trgidx !== false) {
                $return->copies[$src] = $trg;
                unset($return->additions[$trgidx]);
                continue;
            }
            // file sutures (counter-operation to copy) not supported
        }
 
        return $return;
    }
 
    /**
     * Get latest changes.
     *
     * @param string Revision or ('HEAD').
     * @param int Number of changes (10).
     *
     * @return array Changes.
     */
    public function getChangeLog($rev=null, $n=10)
    {
        if ($rev != 'HEAD' and !preg_match('/^\d+$/', $rev)) {
            // we accept only revisions or HEAD
            $rev = 'HEAD';
        }
        $res = array();
        if ($n) {
            $cmd = $this->svnCmd(array('log', '--xml', '-v', '--limit', $n),
                             $this->repo.'@'.$rev);
        } else {
            $cmd = $this->svnCmd(array('log', '--xml', '-v'),
                $this->repo.'@'.$rev);
        }
        $xmlRes = self::shell_exec('IDF_Scm_Svn::getChangeLog', $cmd);
        $xml = simplexml_load_string($xmlRes);
        foreach ($xml->logentry as $entry) {
            $log = array();
            $log['author'] = (string) $entry->author;
            $log['date'] = gmdate('Y-m-d H:i:s', strtotime((string) $entry->date));
            $split = preg_split("[\n\r]", (string) $entry->msg, 2);
            $log['title'] = $split[0];
            $log['commit'] = (string) $entry['revision'];
            $log['full_message'] = (isset($split[1])) ? trim($split[1]) : '';
            $res[] = (object) $log;
        }
        return $res;
    }
 
    /**
     * Get additionnals properties on path and revision
     *
     * @param string File
     * @param string Commit ('HEAD')
     * @return array
     */
    public function getProperties($rev, $path='')
    {
        $res = array();
        $cmd = $this->svnCmd(array('proplist', '--xml'),
                             $this->repo.'/'.self::smartEncode($path), $rev);
        $xmlProps = self::shell_exec('IDF_Scm_Svn::getProperties', $cmd);
        $props = simplexml_load_string($xmlProps);
 
        // No properties, returns an empty array
        if (!isset($props->target)) {
            return $res;
        }
 
        // Get the value of each property
        foreach ($props->target->property as $prop) {
            $key = (string) $prop['name'];
            $res[$key] = $this->getProperty($key, $rev, $path);
        }
 
        return $res;
    }
 
    /**
     * Get a specific additionnal property on path and revision
     *
     * @param string Property
     * @param string File
     * @param string Commit ('HEAD')
     * @return string the property value
     */
    private function getProperty($property, $rev, $path='')
    {
        $res = array();
        $cmd = $this->svnCmd(array('propget', $property, '--xml'),
                             $this->repo.'/'.self::smartEncode($path), $rev);
        $xmlProp = self::shell_exec('IDF_Scm_Svn::getProperty', $cmd);
        $prop = simplexml_load_string($xmlProp);
 
        return (string) $prop->target->property;
    }
 
    /**
     * Get the number of the last commit in the repository.
     *
     * @param string Commit ('HEAD').
     *
     * @return String last number commit
     */
    public function getLastCommit($rev='HEAD')
    {
        $xmlInfo = '';
        $cmd = $this->svnCmd(array('info', '--xml'), $this->repo, $rev);
        $xmlInfo = self::shell_exec('IDF_Scm_Svn::getLastCommit', $cmd);
 
        $xml = simplexml_load_string($xmlInfo);
        return (string) $xml->entry->commit['revision'];
    }
 
    private function svnCmd($args = array(), $repoarg = null, $revarg = null)
    {
        $cmdline = array();
        $cmdline[] = Pluf::f('idf_exec_cmd_prefix', '');
        $cmdline[] = Pluf::f('svn_path', 'svn');
        $cmdline[] = '--no-auth-cache';
        $cmdline[] = '--username='.escapeshellarg($this->username);
        $cmdline[] = '--password='.escapeshellarg($this->password);
 
        foreach ($args as $arg) {
            $cmdline[] = escapeshellarg($arg);
        }
 
        if ($repoarg != null) {
            if ($revarg != null) {
                $repoarg .= '@'.$revarg;
            }
            $cmdline[] = escapeshellarg($repoarg);
        }
 
        if ($revarg != null) {
            $cmdline[] = '--revision='.escapeshellarg($revarg);
        }
 
        return implode(' ', $cmdline);
    }
}

Archive Download this file

Branches

Number of commits:
Page rendered in 0.10193s using 11 queries.