4 * Populate a @{class:DiffusionCommitRef} with information about a specific
5 * commit in a repository. This is a low-level query which talks directly to
8 final class DiffusionLowLevelCommitQuery
9 extends DiffusionLowLevelQuery
{
13 public function withIdentifier($identifier) {
14 $this->identifier
= $identifier;
18 protected function executeQuery() {
19 if (!strlen($this->identifier
)) {
20 throw new PhutilInvalidStateException('withIdentifier');
23 $type = $this->getRepository()->getVersionControlSystem();
25 case PhabricatorRepositoryType
::REPOSITORY_TYPE_GIT
:
26 $result = $this->loadGitCommitRef();
28 case PhabricatorRepositoryType
::REPOSITORY_TYPE_MERCURIAL
:
29 $result = $this->loadMercurialCommitRef();
31 case PhabricatorRepositoryType
::REPOSITORY_TYPE_SVN
:
32 $result = $this->loadSubversionCommitRef();
35 throw new Exception(pht('Unsupported repository type "%s"!', $type));
41 private function loadGitCommitRef() {
42 $repository = $this->getRepository();
44 // See T5028. The "%B" (raw body) mode is not present in very old versions
45 // of Git. Use "%s" and "%b" ("subject" and "wrapped body") as an
48 $git_binary = PhutilBinaryAnalyzer
::getForBinary('git');
49 $git_version = $git_binary->getBinaryVersion();
50 if (version_compare($git_version, '1.7.2', '>=')) {
54 $body_format = '%s%x00%b';
63 $argv[] = '--encoding=UTF-8';
79 // The "git log" output includes a trailing newline. We want to
80 // faithfully capture only the exact text of the commit message,
81 // so include an explicit terminator: this makes sure the exact
82 // body text is surrounded by "\0" characters.
86 // Even though we pass --encoding here, git doesn't always succeed, so
87 // we try a little harder, since git *does* tell us what the actual encoding
88 // is correctly (unless it doesn't; encoding is sometimes empty).
89 list($info) = $repository->execxLocalCommand(
92 gitsprintf('%s', $this->identifier
));
94 $parts = explode("\0", $info);
95 $encoding = array_shift($parts);
97 foreach ($parts as $key => $part) {
99 $part = phutil_utf8_convert($part, 'UTF-8', $encoding);
101 $parts[$key] = phutil_utf8ize($part);
102 if (!strlen($parts[$key])) {
108 id(new DiffusionCommitHash())
109 ->setHashType(ArcanistDifferentialRevisionHash
::HASH_GIT_COMMIT
)
110 ->setHashValue($this->identifier
),
111 id(new DiffusionCommitHash())
112 ->setHashType(ArcanistDifferentialRevisionHash
::HASH_GIT_TREE
)
113 ->setHashValue($parts[4]),
116 $author_epoch = (int)$parts[5];
117 if (!$author_epoch) {
118 $author_epoch = null;
122 // Here, the body is: "subject", "\0", "wrapped body". Stitch the
123 // pieces back together by putting a newline between them if both
124 // parts are nonempty.
129 if (strlen($head) && strlen($tail)) {
130 $body = $head."\n\n".$tail;
131 } else if (strlen($head)) {
133 } else if (strlen($tail)) {
139 // Here, the body is the raw unwrapped body.
143 return id(new DiffusionCommitRef())
144 ->setCommitterName($parts[0])
145 ->setCommitterEmail($parts[1])
146 ->setAuthorName($parts[2])
147 ->setAuthorEmail($parts[3])
149 ->setAuthorEpoch($author_epoch)
153 private function loadMercurialCommitRef() {
154 $repository = $this->getRepository();
156 list($stdout) = $repository->execxLocalCommand(
157 'log --template %s --rev %s',
159 hgsprintf('%s', $this->identifier
));
161 list($author, $message) = explode("\n", $stdout, 2);
163 $author = phutil_utf8ize($author);
164 $message = phutil_utf8ize($message);
166 list($author_name, $author_email) = $this->splitUserIdentifier($author);
169 id(new DiffusionCommitHash())
170 ->setHashType(ArcanistDifferentialRevisionHash
::HASH_MERCURIAL_COMMIT
)
171 ->setHashValue($this->identifier
),
174 return id(new DiffusionCommitRef())
175 ->setAuthorName($author_name)
176 ->setAuthorEmail($author_email)
177 ->setMessage($message)
178 ->setHashes($hashes);
181 private function loadSubversionCommitRef() {
182 $repository = $this->getRepository();
184 list($xml) = $repository->execxRemoteCommand(
185 'log --xml --limit 1 %s',
186 $repository->getSubversionPathURI(null, $this->identifier
));
188 // Subversion may send us back commit messages which won't parse because
189 // they have non UTF-8 garbage in them. Slam them into valid UTF-8.
190 $xml = phutil_utf8ize($xml);
191 $log = new SimpleXMLElement($xml);
192 $entry = $log->logentry
[0];
194 $author = (string)$entry->author
;
195 $message = (string)$entry->msg
;
197 list($author_name, $author_email) = $this->splitUserIdentifier($author);
199 // No hashes in Subversion.
202 return id(new DiffusionCommitRef())
203 ->setAuthorName($author_name)
204 ->setAuthorEmail($author_email)
205 ->setMessage($message)
206 ->setHashes($hashes);
209 private function splitUserIdentifier($user) {
210 $email = new PhutilEmailAddress($user);
212 if ($email->getDisplayName() ||
$email->getDomainName()) {
213 $user_name = $email->getDisplayName();
214 $user_email = $email->getAddress();
216 $user_name = $email->getAddress();
220 return array($user_name, $user_email);