Source

VersionControl_Hg / src / VersionControl / Hg / Command / Status.php

The default branch has multiple heads

Full commit
  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
<?php
/**
 * Contains the definition of the Status command
 *
 * PHP version 5
 *
 * @category   VersionControl
 * @package    Hg
 * @subpackage Command
 * @author     Michael Gatto <mgatto@lisantra.com>
 * @copyright  2011 Lisantra Technologies, LLC
 * @license    http://www.opensource.org/licenses/mit-license.html  MIT License
 * @link       http://pear.php.net/package/VersionControl_Hg
 */

/**
 * Implements the status command.
 *
 * The codes used to show the status of files are:
 *  M = modified
 *  A = added
 *  R = removed
 *  C = clean
 *  ! = missing (deleted by non-hg command, but still tracked)
 *  ? = not tracked
 *  I = ignored
 *
 * PHP version 5
 *
 * @category   VersionControl
 * @package    Hg
 * @subpackage Command
 * @author     Michael Gatto <mgatto@lisantra.com>
 * @copyright  2011 Lisantra Technologies, LLC
 * @license    http://www.opensource.org/licenses/mit-license.html  MIT License
 * @link       http://pear.php.net/package/VersionControl_Hg
 */
class VersionControl_Hg_Command_Status
    extends VersionControl_Hg_Command_Abstract
        implements VersionControl_Hg_Command_Interface
{
    /**
     * The name of the mercurial command implemented here
     *
     * @var string
     */
    protected $command = 'status';

    /**
     * Required options for this specific command. These may not be required
     * by Mercurial itself, but are required for the proper functioning of
     * this package.
     *
     * @var mixed
     */
    protected $required_options = array(
        'noninteractive' => null,
        'repository' => null,
    );

    /**
     * Permissable options.
     *
     * The actual option must be the key, while 'null' is a value here to
     * accommodate the current implementation of setting options.
     *
     * @var mixed
     */
    protected $allowed_options = array(
        'all' => null,
        'modified' => null,
        'added' => null,
        'removed' => null,
        'deleted' => null,
        'clean' => null,
        //could be 'not tracked' or unversioned? but we need one word
        'unknown' => null,
        'ignored' => null,
        'files' => null,
        'rev' => null,
    );

    /**
     * Mapping between native Hg output codes and human readable outputs.
     *
     * @var mixed
     * @TODO add optional functionality for this to parent::parseOutput()
     */
    protected $output_map = array(
        'M' => 'modified',
        'A' => 'added',
        'R' => 'removed',
        'C' => 'clean',
        '!' => 'missing',
        //should be unknown here to match above, but HG docs use 'not tracked'
        '?' => 'not tracked', //or 'unversioned'
        'I' => 'ignored',
    );

    /**
     * Constructor
     *
     * @param mixed             $params One or more parameters for the command
     * @param VersionControl_Hg $hg     Instance of the base object
     *
     * @return void
     */
    public function __construct($params = null, VersionControl_Hg $hg)
    {
        /* Make $hg available to option methods */
        $this->hg = $hg;

        /* should always be called so we have a full array of valid options */
        $this->setOptions($params);
    }

    /**
     * Execute the command and return the results.
     *
     * Running this with no arguments (eg. $hg->status()->run(); ) will only
     * show what Hg would show: only changes and not all files. Specify
     * status('all') or add all() to the method chain to get all files.
     *
     * @param mixed             $params Options passed to the Log command
     * @param VersionControl_Hg $hg     Instance of the base object
     *
     * @return string
     */
    public function execute(array $params = null, VersionControl_Hg $hg = null)
    {
        /* take care of options passed in as such:
         * $hg->status(array('revision' => 3, 'all' => null));
         * We need 'all' to be the key, and not have it interpreted as
         * 	revision => 3, 0 => all  */
        if ( ! empty($params) ) {
            $this->setOptions($params);
        }

        /* --noninteractive is required since issuing the command is
         * unattended by nature of using this package.
         * --repository PATH is required since the PWD on which hg is invoked
         * will not be within the working copy of the repo. */
        $this->addOptions(
            array(
                'noninteractive' => null,
                'repository' => $this->hg->getRepository()->getPath(),
                'cwd' => $this->hg->getRepository()->getPath(),
                /* by default we want to exclude any git or svn cruft */
                'exclude' => '.git**', //adding  '.svn**' makes it fail, but why??
            )
        );
        /* Despite its being so not variable, we need to set the command string
         * only after manually setting options and other command-specific data */
        $this->setCommandString();

        /* no var assignment, since 2nd param holds output */
        exec($this->command_string, $this->output, $this->status);

        if ( $this->status !== 0 ) {
            throw new VersionControl_Hg_Command_Exception(
                VersionControl_Hg_Command_Exception::COMMANDLINE_ERROR
            );
        }

        return $this->parseOutput(
            $this->output,
            array(array('status' => $this->output_map), 'file')
        );
    }

    /**
     * Adds 'all' to the stack of command line options
     *
     * Returns all files in the repository no matter their status.
     *
     * Usage:
     * <code>$hg->status()->all()->run();</code>
     * or
     * <code>$hg->status('all')->run();</code>
     *
     * @return VersionControl_Hg_Command_Status
     */
    public function all()
    {
        $this->addOption('all');

        /* for the fluent API */
        return $this;
    }

    /**
     * Adds 'modified' to the stack of command line options
     *
     * Returns only files which have been modified in the working copy.
     *
     * Usage:
     * <code>$hg->status()->modified();</code>
     * or
     * <code>$hg->status('modified')->run();</code>
     *
     * @return VersionControl_Hg_Command_Status
     */
    public function modified()
    {
        $this->addOption('modified');

        /* For the fluent API */
        return $this;
    }

    /**
     * Adds 'added' to the stack of command line options
     *
     * Returns only files newly added to the repository.
     *
     * Usage:
     * <code>$hg->status()->all()->run();</code>
     * or
     * <code>$hg->status('all')->run();</code>
     *
     * @return VersionControl_Hg_Command_Status
     */
    public function added()
    {
        $this->addOption('added');

        /* For the fluent API */
        return $this;
    }

    /**
     * Adds 'removed' to the stack of command line options
     *
     * Returns only files which have been removed from the working copy
     * and are no longer tracked by Mercurial.
     *
     * Usage:
     * <code>$hg->status()->removed()->run();</code>
     * or
     * <code>$hg->status('removed')->run();</code>
     *
     * @return VersionControl_Hg_Command_Status
     */
    public function removed()
    {
        $this->addOption('removed');

        /* For the fluent API */
        return $this;
    }

    /**
     * Adds 'deleted' to the stack of command line options
     *
     * Returns all files which have been deleted from the working copy.
     *
     * Usage:
     * <code>$hg->status()->deleted()->run();</code>
     * or
     * <code>$hg->status('deleted')->run();</code>
     *
     * @return VersionControl_Hg_Command_Status
     */
    public function deleted()
    {
        $this->addOption('deleted');

        /* For the fluent API */
        return $this;
    }

    /**
     * Adds 'clean' to the stack of command line options
     *
     * Returns files which have no changes; i.e. they are identical in both
     * the repository and working copy.
     *
     * Usage:
     * <code>$hg->status()->clean()->run();</code>
     * or
     * <code>$hg->status('clean')->run();</code>
     *
     * @return VersionControl_Hg_Command_Status
     */
    public function clean()
    {
        $this->addOption('clean');

        /* For the fluent API */
        return $this;
    }

    /**
     * Adds 'unknown' to the stack of command line options
     *
     * Returns all files not being tracked by Mercurial (i.e. not added).
     *
     * Usage:
     * <code>$hg->status()->unknown()->run();</code>
     * or
     * <code>$hg->status('unknown')->run();</code>
     *
     * @return VersionControl_Hg_Command_Status
     */
    public function unknown()
    {
        $this->addOption('unknown');

        /* For the fluent API */
        return $this;
    }

    /**
     * Adds'ignored' to the stack of command line options
     *
     * Returns only files ignored on purpose by Mercurial (in .hgignore)
     *
     * Usage:
     * <code>$hg->status()->ignored()->run();</code>
     * or
     * <code>$hg->status('ignored')->run();</code>
     *
     * @return VersionControl_Hg_Command_Status
     */
    public function ignored()
    {
        $this->addOption('ignored');

        /* For the fluent API */
        return $this;
    }

    /**
     * Adds a list of files to the stack of command line options
     *
     * List status information for only the files specified. Abstract::formatOptions
     * will automatically place this as the last option since a files list
     * must be the last item on the command line.
     *
     * Usage:
     * <code>$hg->status()->files(array('index.php'))->run();</code>
     * or
     * <code>$hg->status(array('files' => array('index.php')))->run();</code>
     *
     * @param mixed $files Only show the status for these files
     *
     * @return VersionControl_Hg_Command_Status
     *
     * @TODO how to ensure this is the final option??
     */
    public function files(array $files)
    {
        /* is it a pattern or a simple array of files?
         * the scheme must be the very first key.
         * Must cast to string since numerical zero seems to always be in an
         * array?! (string '0' is not!) */
        if ( in_array((string) key($files), array('glob','re','set','path','listfile')) ) {
            /* Yup, its a scheme:pattern */
            $filter = $this->parseFilter($files);
        } else {
            $filter = join(' ', $files);
        }

        $this->addOption('files', $filter);

        /* For the fluent API */
        return $this;
    }

    /**
     * Adds 'ignored' to the stack of command line options
     *
     * Returns only files ignored within the working copy
     *
     * Usage:
     * <code>$hg->status()->ignored()->run();</code>
     * or
     * <code>$hg->status('ignored')->run();</code>
     *
     * @return VersionControl_Hg_Command_Status
     */
    public function ignored()
    {
        $this->addOption('ignored');

        /* For the fluent API */
        return $this;
    }

    /**
     * Adds 'rev' to the stack of command line options
     *
     * Specified the revision to restrict the status operation to

     * Usage:
     * <code>$hg->status('all')->revision(7)->run();</code>
     * or
     * <code>$hg->status(array('revision' => 7 ))->all()->run();</code>
     *
     * @param string $revision The revision for which to get the status
     *
     * @return VersionControl_Hg_Command_Status
     */
    public function revision($revision = 'tip')
    {
        $this->addOption('rev', $revision);

        /* For the fluent API */
        return $this;
    }

    /**
     * Restricts status output to changesets between two revisions $from and $to
     *
     * Usage:
     * <code>$hg->status()->between(3, 17)->run();</code>
     *
     * @param string $from Show status entries from this revision forward through $to
     * @param string $to   Show status entries only before this revision
     *
     * @return VersionControl_Hg_Command_Log
     */
    public function between($from = 0, $to = 'tip')
    {
        $this->addOption('rev', $from);
        $this->addOption('rev', $to);

        /* For the fluent API */
        return $this;
    }
}