[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/differential/storage/ -> DifferentialHunk.php (source)

   1  <?php
   2  
   3  abstract class DifferentialHunk extends DifferentialDAO
   4    implements PhabricatorPolicyInterface {
   5  
   6    protected $changesetID;
   7    protected $oldOffset;
   8    protected $oldLen;
   9    protected $newOffset;
  10    protected $newLen;
  11  
  12    private $changeset;
  13  
  14    const FLAG_LINES_ADDED     = 1;
  15    const FLAG_LINES_REMOVED   = 2;
  16    const FLAG_LINES_STABLE    = 4;
  17  
  18    public function getAddedLines() {
  19      return $this->makeContent($include = '+');
  20    }
  21  
  22    public function getRemovedLines() {
  23      return $this->makeContent($include = '-');
  24    }
  25  
  26    public function makeNewFile() {
  27      return implode('', $this->makeContent($include = ' +'));
  28    }
  29  
  30    public function makeOldFile() {
  31      return implode('', $this->makeContent($include = ' -'));
  32    }
  33  
  34    public function makeChanges() {
  35      return implode('', $this->makeContent($include = '-+'));
  36    }
  37  
  38    public function getContentWithMask($mask) {
  39      $include = array();
  40  
  41      if (($mask & self::FLAG_LINES_ADDED)) {
  42        $include[] = '+';
  43      }
  44  
  45      if (($mask & self::FLAG_LINES_REMOVED)) {
  46        $include[] = '-';
  47      }
  48  
  49      if (($mask & self::FLAG_LINES_STABLE)) {
  50        $include[] = ' ';
  51      }
  52  
  53      $include = implode('', $include);
  54  
  55      return implode('', $this->makeContent($include));
  56    }
  57  
  58    final private function makeContent($include) {
  59      $results = array();
  60      $lines = explode("\n", $this->getChanges());
  61  
  62      // NOTE: To determine whether the recomposed file should have a trailing
  63      // newline, we look for a "\ No newline at end of file" line which appears
  64      // after a line which we don't exclude. For example, if we're constructing
  65      // the "new" side of a diff (excluding "-"), we want to ignore this one:
  66      //
  67      //    - x
  68      //    \ No newline at end of file
  69      //    + x
  70      //
  71      // ...since it's talking about the "old" side of the diff, but interpret
  72      // this as meaning we should omit the newline:
  73      //
  74      //    - x
  75      //    + x
  76      //    \ No newline at end of file
  77  
  78      $n = (strpos($include, '+') !== false ?
  79        $this->newOffset :
  80        $this->oldOffset);
  81      $use_next_newline = false;
  82      foreach ($lines as $line) {
  83        if (!isset($line[0])) {
  84          continue;
  85        }
  86  
  87        if ($line[0] == '\\') {
  88          if ($use_next_newline) {
  89            $results[last_key($results)] = rtrim(end($results), "\n");
  90          }
  91        } else if (strpos($include, $line[0]) === false) {
  92          $use_next_newline = false;
  93        } else {
  94          $use_next_newline = true;
  95          $results[$n] = substr($line, 1)."\n";
  96        }
  97  
  98        if ($line[0] == ' ' || strpos($include, $line[0]) !== false) {
  99          $n++;
 100        }
 101      }
 102  
 103      return $results;
 104    }
 105  
 106    public function getChangeset() {
 107      return $this->assertAttached($this->changeset);
 108    }
 109  
 110    public function attachChangeset(DifferentialChangeset $changeset) {
 111      $this->changeset = $changeset;
 112      return $this;
 113    }
 114  
 115  
 116  /* -(  PhabricatorPolicyInterface  )----------------------------------------- */
 117  
 118  
 119    public function getCapabilities() {
 120      return array(
 121        PhabricatorPolicyCapability::CAN_VIEW,
 122      );
 123    }
 124  
 125    public function getPolicy($capability) {
 126      return $this->getChangeset()->getPolicy($capability);
 127    }
 128  
 129    public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {
 130      return $this->getChangeset()->hasAutomaticCapability($capability, $viewer);
 131    }
 132  
 133    public function describeAutomaticCapability($capability) {
 134      return null;
 135    }
 136  
 137  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1