/lib/Gitten/CommitFile.php

https://bitbucket.org/kayahr/gitten · PHP · 306 lines · 112 code · 33 blank · 161 comment · 4 complexity · a76bc880abb418bca37b53e0dafd64a7 MD5 · raw file

  1. <?php
  2. /*
  3. * Copyright (C) 2013 Klaus Reimer <k@ailis.de>
  4. * See LICENSE.md for licensing information.
  5. */
  6. namespace Gitten;
  7. /**
  8. * A file in a commit.
  9. *
  10. * @author Klaus Reimer <k@ailis.de>
  11. */
  12. class CommitFile
  13. {
  14. /** The repository. */
  15. private $repo;
  16. /** The commit. */
  17. private $commit;
  18. /** @var int The index inside the commit. */
  19. private $index;
  20. /** @var string The filename. */
  21. private $filename;
  22. /** @var string The change type (M, A, D, ...) */
  23. private $type;
  24. /** @var FileMode The source file mode. */
  25. private $srcMode;
  26. /** @var FileMode The destination file mode. */
  27. private $destMode;
  28. /** @var Hash The source hash. */
  29. private $srcHash;
  30. /** @var Hash The destination hash. */
  31. private $destHash;
  32. /** @var int The number of deletions. */
  33. private $deletions;
  34. /** @var int The number of additions. */
  35. private $additions;
  36. /** @var binary If file is binary or not. */
  37. private $binary;
  38. /**
  39. * Constructs a new commit file.
  40. *
  41. * @param Repo $repo
  42. * The repository.
  43. * @param Commit $commit
  44. * The commit.
  45. * @param int $index
  46. * The index inside the commit.
  47. * @param string $filename
  48. * The filename.
  49. */
  50. public function __construct(Repo $repo, Commit $commit, $index, $filename)
  51. {
  52. $this->repo = $repo;
  53. $this->commit = $commit;
  54. $this->index = $index;
  55. $this->filename = $filename;
  56. }
  57. /**
  58. * Sets raw data.
  59. *
  60. * @param string $type
  61. * The change type.
  62. * @param FileMode $srcMode
  63. * The source file mode.
  64. * @param FileMode $destMode
  65. * The destination file mode.
  66. * @param Hash $srcHash
  67. * The source hash.
  68. * @param Hash $destHash
  69. * The destination hash.
  70. */
  71. public function setRawData($type, FileMode $srcMode, FileMode $destMode,
  72. Hash $srcHash, Hash $destHash)
  73. {
  74. $this->type = strtolower($type);
  75. $this->srcMode = $srcMode;
  76. $this->destMode = $destMode;
  77. $this->destHash = $destHash;
  78. $this->srcHash = $srcHash;
  79. }
  80. /**
  81. * Sets num stat data.
  82. *
  83. * @param int $additions
  84. * The number of additions.
  85. * @param int $deletions
  86. * The number of deletions.
  87. * @param boolean $binary
  88. * If file is binary or not.
  89. */
  90. public function setNumStatData($additions, $deletions, $binary)
  91. {
  92. $this->additions = $additions;
  93. $this->deletions = $deletions;
  94. $this->binary = $binary;
  95. }
  96. /**
  97. * Returns the index inside the commit.
  98. *
  99. * @return int
  100. * The index.
  101. */
  102. public function getIndex()
  103. {
  104. return $this->index;
  105. }
  106. /**
  107. * Returns the filename.
  108. *
  109. * @return string
  110. * The filename.
  111. */
  112. public function getFilename()
  113. {
  114. return $this->filename;
  115. }
  116. /**
  117. * Returns the change type.
  118. *
  119. * @return string
  120. * The type.
  121. */
  122. public function getType()
  123. {
  124. return $this->type;
  125. }
  126. /**
  127. * Check if file was deleted.
  128. *
  129. * @return boolean
  130. * True if file was deleted, false if not.
  131. */
  132. public function isDeletion()
  133. {
  134. return $this->type == "d";
  135. }
  136. /**
  137. * Check if file was added.
  138. *
  139. * @return boolean
  140. * True if file was added, false if not.
  141. */
  142. public function isAddition()
  143. {
  144. return $this->type == "a";
  145. }
  146. /**
  147. * Check if file was modified.
  148. *
  149. * @return boolean
  150. * True if file was modified,false if not.
  151. */
  152. public function isModification()
  153. {
  154. return $this->type == "m";
  155. }
  156. /**
  157. * Returns the source file mode.
  158. *
  159. * @return FileMode
  160. * The source file mode.
  161. */
  162. public function getSrcMode()
  163. {
  164. return $this->srcMode;
  165. }
  166. /**
  167. * Returns the source file mode.
  168. *
  169. * @return FileMode
  170. * The source file mode.
  171. */
  172. public function getDestMode()
  173. {
  174. return $this->destMode;
  175. }
  176. /**
  177. * Returns the additions.
  178. *
  179. * @return int
  180. * The additions.
  181. */
  182. public function getAdditions()
  183. {
  184. return $this->additions;
  185. }
  186. /**
  187. * Returns the deletions.
  188. *
  189. * @return int
  190. * The deletions.
  191. */
  192. public function getDeletions()
  193. {
  194. return $this->deletions;
  195. }
  196. /**
  197. * Returns the modification (Additions plus deletions).
  198. *
  199. * @return int
  200. * The modifications.
  201. */
  202. public function getModifications()
  203. {
  204. return $this->deletions + $this->additions;
  205. }
  206. /**
  207. * Returns the source hash.
  208. *
  209. * @return Hash
  210. * The source hash.
  211. */
  212. public function getSrcHash()
  213. {
  214. return $this->srcHash;
  215. }
  216. /**
  217. * Returns the destination hash.
  218. *
  219. * @return Hash
  220. * The destination hash.
  221. */
  222. public function getDestHash()
  223. {
  224. return $this->destHash;
  225. }
  226. /**
  227. * Checks if file is binary or not.
  228. *
  229. * @return boolean
  230. * True if file is binary, false if not.
  231. */
  232. public function isBinary()
  233. {
  234. return $this->binary;
  235. }
  236. /**
  237. * Returns the URL to the source file.
  238. *
  239. * @return string
  240. * The URL to the source file.
  241. */
  242. public function getSrcUrl()
  243. {
  244. return $this->repo->getBlobUrl($this->filename,
  245. $this->commit->getParentHash()->getFull());
  246. }
  247. /**
  248. * Returns the URL to the destination file.
  249. *
  250. * @return string
  251. * The URL to the destination file.
  252. */
  253. public function getDestUrl()
  254. {
  255. return $this->repo->getBlobUrl($this->filename,
  256. $this->commit->getCommitHash()->getFull());
  257. }
  258. /**
  259. * Returns the URL to the source file if file was deleted or the
  260. * destination file if file was added or modified.
  261. *
  262. * @return string
  263. * The URL to the source or destination file depending on
  264. * the change type.
  265. */
  266. public function getUrl()
  267. {
  268. if ($this->isDeletion())
  269. return $this->getSrcUrl();
  270. else
  271. return $this->getDestUrl();
  272. }
  273. }