gclient_scm_test.py 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343
  1. #!/usr/bin/env python
  2. # Copyright (c) 2012 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """Unit tests for gclient_scm.py."""
  6. # pylint: disable=E1103
  7. from shutil import rmtree
  8. from subprocess import Popen, PIPE, STDOUT
  9. import json
  10. import logging
  11. import os
  12. import re
  13. import sys
  14. import tempfile
  15. import unittest
  16. if sys.version_info.major == 2:
  17. from cStringIO import StringIO
  18. else:
  19. from io import StringIO
  20. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  21. from third_party import mock
  22. from testing_support import fake_repos
  23. from testing_support import test_case_utils
  24. import gclient_scm
  25. import git_cache
  26. import subprocess2
  27. # Disable global git cache
  28. git_cache.Mirror.SetCachePath(None)
  29. # Shortcut since this function is used often
  30. join = gclient_scm.os.path.join
  31. TIMESTAMP_RE = re.compile('\[[0-9]{1,2}:[0-9]{2}:[0-9]{2}\] (.*)', re.DOTALL)
  32. def strip_timestamps(value):
  33. lines = value.splitlines(True)
  34. for i in range(len(lines)):
  35. m = TIMESTAMP_RE.match(lines[i])
  36. if m:
  37. lines[i] = m.group(1)
  38. return ''.join(lines)
  39. class BasicTests(unittest.TestCase):
  40. @mock.patch('gclient_scm.scm.GIT.Capture')
  41. def testGetFirstRemoteUrl(self, mockCapture):
  42. REMOTE_STRINGS = [('remote.origin.url E:\\foo\\bar', 'E:\\foo\\bar'),
  43. ('remote.origin.url /b/foo/bar', '/b/foo/bar'),
  44. ('remote.origin.url https://foo/bar', 'https://foo/bar'),
  45. ('remote.origin.url E:\\Fo Bar\\bax', 'E:\\Fo Bar\\bax'),
  46. ('remote.origin.url git://what/"do', 'git://what/"do')]
  47. FAKE_PATH = '/fake/path'
  48. mockCapture.side_effect = [question for question, _ in REMOTE_STRINGS]
  49. for _, answer in REMOTE_STRINGS:
  50. self.assertEqual(
  51. gclient_scm.SCMWrapper._get_first_remote_url(FAKE_PATH), answer)
  52. expected_calls = [
  53. mock.call(['config', '--local', '--get-regexp', r'remote.*.url'],
  54. cwd=FAKE_PATH)
  55. for _ in REMOTE_STRINGS
  56. ]
  57. self.assertEqual(mockCapture.mock_calls, expected_calls)
  58. class BaseGitWrapperTestCase(unittest.TestCase, test_case_utils.TestCaseUtils):
  59. """This class doesn't use pymox."""
  60. class OptionsObject(object):
  61. def __init__(self, verbose=False, revision=None):
  62. self.auto_rebase = False
  63. self.verbose = verbose
  64. self.revision = revision
  65. self.deps_os = None
  66. self.force = False
  67. self.reset = False
  68. self.nohooks = False
  69. self.no_history = False
  70. self.upstream = False
  71. self.cache_dir = None
  72. self.merge = False
  73. self.jobs = 1
  74. self.break_repo_locks = False
  75. self.delete_unversioned_trees = False
  76. self.patch_ref = None
  77. self.patch_repo = None
  78. self.rebase_patch_ref = True
  79. self.reset_patch_ref = True
  80. sample_git_import = """blob
  81. mark :1
  82. data 6
  83. Hello
  84. blob
  85. mark :2
  86. data 4
  87. Bye
  88. reset refs/heads/master
  89. commit refs/heads/master
  90. mark :3
  91. author Bob <bob@example.com> 1253744361 -0700
  92. committer Bob <bob@example.com> 1253744361 -0700
  93. data 8
  94. A and B
  95. M 100644 :1 a
  96. M 100644 :2 b
  97. blob
  98. mark :4
  99. data 10
  100. Hello
  101. You
  102. blob
  103. mark :5
  104. data 8
  105. Bye
  106. You
  107. commit refs/heads/origin
  108. mark :6
  109. author Alice <alice@example.com> 1253744424 -0700
  110. committer Alice <alice@example.com> 1253744424 -0700
  111. data 13
  112. Personalized
  113. from :3
  114. M 100644 :4 a
  115. M 100644 :5 b
  116. blob
  117. mark :7
  118. data 5
  119. Mooh
  120. commit refs/heads/feature
  121. mark :8
  122. author Bob <bob@example.com> 1390311986 -0000
  123. committer Bob <bob@example.com> 1390311986 -0000
  124. data 6
  125. Add C
  126. from :3
  127. M 100644 :7 c
  128. reset refs/heads/master
  129. from :3
  130. """
  131. def Options(self, *args, **kwargs):
  132. return self.OptionsObject(*args, **kwargs)
  133. def checkstdout(self, expected):
  134. value = sys.stdout.getvalue()
  135. sys.stdout.close()
  136. # pylint: disable=no-member
  137. self.assertEqual(expected, strip_timestamps(value))
  138. @staticmethod
  139. def CreateGitRepo(git_import, path):
  140. """Do it for real."""
  141. try:
  142. Popen(['git', 'init', '-q'], stdout=PIPE, stderr=STDOUT,
  143. cwd=path).communicate()
  144. except OSError:
  145. # git is not available, skip this test.
  146. return False
  147. Popen(['git', 'fast-import', '--quiet'], stdin=PIPE, stdout=PIPE,
  148. stderr=STDOUT, cwd=path).communicate(input=git_import.encode())
  149. Popen(['git', 'checkout', '-q'], stdout=PIPE, stderr=STDOUT,
  150. cwd=path).communicate()
  151. Popen(['git', 'remote', 'add', '-f', 'origin', '.'], stdout=PIPE,
  152. stderr=STDOUT, cwd=path).communicate()
  153. Popen(['git', 'checkout', '-b', 'new', 'origin/master', '-q'], stdout=PIPE,
  154. stderr=STDOUT, cwd=path).communicate()
  155. Popen(['git', 'push', 'origin', 'origin/origin:origin/master', '-q'],
  156. stdout=PIPE, stderr=STDOUT, cwd=path).communicate()
  157. Popen(['git', 'config', '--unset', 'remote.origin.fetch'], stdout=PIPE,
  158. stderr=STDOUT, cwd=path).communicate()
  159. Popen(['git', 'config', 'user.email', 'someuser@chromium.org'], stdout=PIPE,
  160. stderr=STDOUT, cwd=path).communicate()
  161. Popen(['git', 'config', 'user.name', 'Some User'], stdout=PIPE,
  162. stderr=STDOUT, cwd=path).communicate()
  163. return True
  164. def _GetAskForDataCallback(self, expected_prompt, return_value):
  165. def AskForData(prompt, options):
  166. self.assertEqual(prompt, expected_prompt)
  167. return return_value
  168. return AskForData
  169. def setUp(self):
  170. unittest.TestCase.setUp(self)
  171. test_case_utils.TestCaseUtils.setUp(self)
  172. self.url = 'git://foo'
  173. # The .git suffix allows gclient_scm to recognize the dir as a git repo
  174. # when cloning it locally
  175. self.root_dir = tempfile.mkdtemp('.git')
  176. self.relpath = '.'
  177. self.base_path = join(self.root_dir, self.relpath)
  178. self.enabled = self.CreateGitRepo(self.sample_git_import, self.base_path)
  179. self._original_GitBinaryExists = gclient_scm.GitWrapper.BinaryExists
  180. mock.patch('gclient_scm.GitWrapper.BinaryExists',
  181. staticmethod(lambda : True)).start()
  182. mock.patch('sys.stdout', StringIO()).start()
  183. self.addCleanup(mock.patch.stopall)
  184. self.addCleanup(lambda: rmtree(self.root_dir))
  185. class ManagedGitWrapperTestCase(BaseGitWrapperTestCase):
  186. def testRevertMissing(self):
  187. if not self.enabled:
  188. return
  189. options = self.Options()
  190. file_path = join(self.base_path, 'a')
  191. scm = gclient_scm.GitWrapper(self.url, self.root_dir,
  192. self.relpath)
  193. file_list = []
  194. scm.update(options, None, file_list)
  195. gclient_scm.os.remove(file_path)
  196. file_list = []
  197. scm.revert(options, self.args, file_list)
  198. self.assertEqual(file_list, [file_path])
  199. file_list = []
  200. scm.diff(options, self.args, file_list)
  201. self.assertEqual(file_list, [])
  202. sys.stdout.close()
  203. def testRevertNone(self):
  204. if not self.enabled:
  205. return
  206. options = self.Options()
  207. scm = gclient_scm.GitWrapper(self.url, self.root_dir,
  208. self.relpath)
  209. file_list = []
  210. scm.update(options, None, file_list)
  211. file_list = []
  212. scm.revert(options, self.args, file_list)
  213. self.assertEqual(file_list, [])
  214. self.assertEqual(scm.revinfo(options, self.args, None),
  215. 'a7142dc9f0009350b96a11f372b6ea658592aa95')
  216. sys.stdout.close()
  217. def testRevertModified(self):
  218. if not self.enabled:
  219. return
  220. options = self.Options()
  221. scm = gclient_scm.GitWrapper(self.url, self.root_dir,
  222. self.relpath)
  223. file_list = []
  224. scm.update(options, None, file_list)
  225. file_path = join(self.base_path, 'a')
  226. with open(file_path, 'a') as f:
  227. f.writelines('touched\n')
  228. file_list = []
  229. scm.revert(options, self.args, file_list)
  230. self.assertEqual(file_list, [file_path])
  231. file_list = []
  232. scm.diff(options, self.args, file_list)
  233. self.assertEqual(file_list, [])
  234. self.assertEqual(scm.revinfo(options, self.args, None),
  235. 'a7142dc9f0009350b96a11f372b6ea658592aa95')
  236. sys.stdout.close()
  237. def testRevertNew(self):
  238. if not self.enabled:
  239. return
  240. options = self.Options()
  241. scm = gclient_scm.GitWrapper(self.url, self.root_dir,
  242. self.relpath)
  243. file_list = []
  244. scm.update(options, None, file_list)
  245. file_path = join(self.base_path, 'c')
  246. with open(file_path, 'w') as f:
  247. f.writelines('new\n')
  248. Popen(['git', 'add', 'c'], stdout=PIPE,
  249. stderr=STDOUT, cwd=self.base_path).communicate()
  250. file_list = []
  251. scm.revert(options, self.args, file_list)
  252. self.assertEqual(file_list, [file_path])
  253. file_list = []
  254. scm.diff(options, self.args, file_list)
  255. self.assertEqual(file_list, [])
  256. self.assertEqual(scm.revinfo(options, self.args, None),
  257. 'a7142dc9f0009350b96a11f372b6ea658592aa95')
  258. sys.stdout.close()
  259. def testStatusNew(self):
  260. if not self.enabled:
  261. return
  262. options = self.Options()
  263. file_path = join(self.base_path, 'a')
  264. with open(file_path, 'a') as f:
  265. f.writelines('touched\n')
  266. scm = gclient_scm.GitWrapper(self.url, self.root_dir,
  267. self.relpath)
  268. file_list = []
  269. scm.status(options, self.args, file_list)
  270. self.assertEqual(file_list, [file_path])
  271. self.checkstdout(
  272. ('\n________ running \'git -c core.quotePath=false diff --name-status '
  273. '069c602044c5388d2d15c3f875b057c852003458\' in \'%s\'\n\nM\ta\n') %
  274. join(self.root_dir, '.'))
  275. def testStatus2New(self):
  276. if not self.enabled:
  277. return
  278. options = self.Options()
  279. expected_file_list = []
  280. for f in ['a', 'b']:
  281. file_path = join(self.base_path, f)
  282. with open(file_path, 'a') as f:
  283. f.writelines('touched\n')
  284. expected_file_list.extend([file_path])
  285. scm = gclient_scm.GitWrapper(self.url, self.root_dir,
  286. self.relpath)
  287. file_list = []
  288. scm.status(options, self.args, file_list)
  289. expected_file_list = [join(self.base_path, x) for x in ['a', 'b']]
  290. self.assertEqual(sorted(file_list), expected_file_list)
  291. self.checkstdout(
  292. ('\n________ running \'git -c core.quotePath=false diff --name-status '
  293. '069c602044c5388d2d15c3f875b057c852003458\' in \'%s\'\n\nM\ta\nM\tb\n')
  294. % join(self.root_dir, '.'))
  295. def testUpdateUpdate(self):
  296. if not self.enabled:
  297. return
  298. options = self.Options()
  299. expected_file_list = [join(self.base_path, x) for x in ['a', 'b']]
  300. scm = gclient_scm.GitWrapper(self.url, self.root_dir,
  301. self.relpath)
  302. file_list = []
  303. scm.update(options, (), file_list)
  304. self.assertEqual(file_list, expected_file_list)
  305. self.assertEqual(scm.revinfo(options, (), None),
  306. 'a7142dc9f0009350b96a11f372b6ea658592aa95')
  307. sys.stdout.close()
  308. def testUpdateMerge(self):
  309. if not self.enabled:
  310. return
  311. options = self.Options()
  312. options.merge = True
  313. scm = gclient_scm.GitWrapper(self.url, self.root_dir,
  314. self.relpath)
  315. scm._Run(['checkout', '-q', 'feature'], options)
  316. rev = scm.revinfo(options, (), None)
  317. file_list = []
  318. scm.update(options, (), file_list)
  319. self.assertEqual(file_list, [join(self.base_path, x)
  320. for x in ['a', 'b', 'c']])
  321. # The actual commit that is created is unstable, so we verify its tree and
  322. # parents instead.
  323. self.assertEqual(scm._Capture(['rev-parse', 'HEAD:']),
  324. 'd2e35c10ac24d6c621e14a1fcadceb533155627d')
  325. self.assertEqual(scm._Capture(['rev-parse', 'HEAD^1']), rev)
  326. self.assertEqual(scm._Capture(['rev-parse', 'HEAD^2']),
  327. scm._Capture(['rev-parse', 'origin/master']))
  328. sys.stdout.close()
  329. def testUpdateRebase(self):
  330. if not self.enabled:
  331. return
  332. options = self.Options()
  333. scm = gclient_scm.GitWrapper(self.url, self.root_dir,
  334. self.relpath)
  335. scm._Run(['checkout', '-q', 'feature'], options)
  336. file_list = []
  337. # Fake a 'y' key press.
  338. scm._AskForData = self._GetAskForDataCallback(
  339. 'Cannot fast-forward merge, attempt to rebase? '
  340. '(y)es / (q)uit / (s)kip : ', 'y')
  341. scm.update(options, (), file_list)
  342. self.assertEqual(file_list, [join(self.base_path, x)
  343. for x in ['a', 'b', 'c']])
  344. # The actual commit that is created is unstable, so we verify its tree and
  345. # parent instead.
  346. self.assertEqual(scm._Capture(['rev-parse', 'HEAD:']),
  347. 'd2e35c10ac24d6c621e14a1fcadceb533155627d')
  348. self.assertEqual(scm._Capture(['rev-parse', 'HEAD^']),
  349. scm._Capture(['rev-parse', 'origin/master']))
  350. sys.stdout.close()
  351. def testUpdateReset(self):
  352. if not self.enabled:
  353. return
  354. options = self.Options()
  355. options.reset = True
  356. dir_path = join(self.base_path, 'c')
  357. os.mkdir(dir_path)
  358. with open(join(dir_path, 'nested'), 'w') as f:
  359. f.writelines('new\n')
  360. file_path = join(self.base_path, 'file')
  361. with open(file_path, 'w') as f:
  362. f.writelines('new\n')
  363. scm = gclient_scm.GitWrapper(self.url, self.root_dir,
  364. self.relpath)
  365. file_list = []
  366. scm.update(options, (), file_list)
  367. self.assert_(gclient_scm.os.path.isdir(dir_path))
  368. self.assert_(gclient_scm.os.path.isfile(file_path))
  369. sys.stdout.close()
  370. def testUpdateResetUnsetsFetchConfig(self):
  371. if not self.enabled:
  372. return
  373. options = self.Options()
  374. options.reset = True
  375. scm = gclient_scm.GitWrapper(self.url, self.root_dir,
  376. self.relpath)
  377. scm._Run(['config', 'remote.origin.fetch',
  378. '+refs/heads/bad/ref:refs/remotes/origin/bad/ref'], options)
  379. file_list = []
  380. scm.update(options, (), file_list)
  381. self.assertEqual(scm.revinfo(options, (), None),
  382. '069c602044c5388d2d15c3f875b057c852003458')
  383. sys.stdout.close()
  384. def testUpdateResetDeleteUnversionedTrees(self):
  385. if not self.enabled:
  386. return
  387. options = self.Options()
  388. options.reset = True
  389. options.delete_unversioned_trees = True
  390. dir_path = join(self.base_path, 'dir')
  391. os.mkdir(dir_path)
  392. with open(join(dir_path, 'nested'), 'w') as f:
  393. f.writelines('new\n')
  394. file_path = join(self.base_path, 'file')
  395. with open(file_path, 'w') as f:
  396. f.writelines('new\n')
  397. scm = gclient_scm.GitWrapper(self.url, self.root_dir,
  398. self.relpath)
  399. file_list = []
  400. scm.update(options, (), file_list)
  401. self.assert_(not gclient_scm.os.path.isdir(dir_path))
  402. self.assert_(gclient_scm.os.path.isfile(file_path))
  403. sys.stdout.close()
  404. def testUpdateUnstagedConflict(self):
  405. if not self.enabled:
  406. return
  407. options = self.Options()
  408. scm = gclient_scm.GitWrapper(self.url, self.root_dir,
  409. self.relpath)
  410. file_path = join(self.base_path, 'b')
  411. with open(file_path, 'w') as f:
  412. f.writelines('conflict\n')
  413. try:
  414. scm.update(options, (), [])
  415. self.fail()
  416. except (gclient_scm.gclient_utils.Error, subprocess2.CalledProcessError):
  417. # The exact exception text varies across git versions so it's not worth
  418. # verifying it. It's fine as long as it throws.
  419. pass
  420. # Manually flush stdout since we can't verify it's content accurately across
  421. # git versions.
  422. sys.stdout.getvalue()
  423. sys.stdout.close()
  424. @unittest.skip('Skipping until crbug.com/670884 is resolved.')
  425. def testUpdateLocked(self):
  426. if not self.enabled:
  427. return
  428. options = self.Options()
  429. scm = gclient_scm.GitWrapper(self.url, self.root_dir,
  430. self.relpath)
  431. file_path = join(self.base_path, '.git', 'index.lock')
  432. with open(file_path, 'w'):
  433. pass
  434. with self.assertRaises(subprocess2.CalledProcessError):
  435. scm.update(options, (), [])
  436. sys.stdout.close()
  437. def testUpdateLockedBreak(self):
  438. if not self.enabled:
  439. return
  440. options = self.Options()
  441. options.break_repo_locks = True
  442. scm = gclient_scm.GitWrapper(self.url, self.root_dir,
  443. self.relpath)
  444. file_path = join(self.base_path, '.git', 'index.lock')
  445. with open(file_path, 'w'):
  446. pass
  447. scm.update(options, (), [])
  448. self.assertRegexpMatches(sys.stdout.getvalue(),
  449. "breaking lock.*\.git/index\.lock")
  450. self.assertFalse(os.path.exists(file_path))
  451. sys.stdout.close()
  452. def testUpdateConflict(self):
  453. if not self.enabled:
  454. return
  455. options = self.Options()
  456. scm = gclient_scm.GitWrapper(self.url, self.root_dir,
  457. self.relpath)
  458. file_path = join(self.base_path, 'b')
  459. with open(file_path, 'w') as f:
  460. f.writelines('conflict\n')
  461. scm._Run(['commit', '-am', 'test'], options)
  462. scm._AskForData = self._GetAskForDataCallback(
  463. 'Cannot fast-forward merge, attempt to rebase? '
  464. '(y)es / (q)uit / (s)kip : ', 'y')
  465. with self.assertRaises(gclient_scm.gclient_utils.Error) as e:
  466. scm.update(options, (), [])
  467. self.assertEqual(
  468. e.exception.args[0],
  469. 'Conflict while rebasing this branch.\n'
  470. 'Fix the conflict and run gclient again.\n'
  471. 'See \'man git-rebase\' for details.\n')
  472. with self.assertRaises(gclient_scm.gclient_utils.Error) as e:
  473. scm.update(options, (), [])
  474. self.assertEqual(
  475. e.exception.args[0],
  476. '\n____ . at refs/remotes/origin/master\n'
  477. '\tYou have unstaged changes.\n'
  478. '\tPlease commit, stash, or reset.\n')
  479. sys.stdout.close()
  480. def testRevinfo(self):
  481. if not self.enabled:
  482. return
  483. options = self.Options()
  484. scm = gclient_scm.GitWrapper(self.url, self.root_dir,
  485. self.relpath)
  486. rev_info = scm.revinfo(options, (), None)
  487. self.assertEqual(rev_info, '069c602044c5388d2d15c3f875b057c852003458')
  488. def testMirrorPushUrl(self):
  489. if not self.enabled:
  490. return
  491. fakes = fake_repos.FakeRepos()
  492. fakes.set_up_git()
  493. self.url = fakes.git_base + 'repo_1'
  494. self.root_dir = fakes.root_dir
  495. self.addCleanup(fake_repos.FakeRepos.tear_down_git, fakes)
  496. mirror = tempfile.mkdtemp()
  497. self.addCleanup(rmtree, mirror)
  498. # This should never happen, but if it does, it'd render the other assertions
  499. # in this test meaningless.
  500. self.assertFalse(self.url.startswith(mirror))
  501. git_cache.Mirror.SetCachePath(mirror)
  502. self.addCleanup(git_cache.Mirror.SetCachePath, None)
  503. options = self.Options()
  504. scm = gclient_scm.GitWrapper(self.url, self.root_dir, self.relpath)
  505. self.assertIsNotNone(scm._GetMirror(self.url, options))
  506. scm.update(options, (), [])
  507. fetch_url = scm._Capture(['remote', 'get-url', 'origin'])
  508. self.assertTrue(
  509. fetch_url.startswith(mirror),
  510. msg='\n'.join([
  511. 'Repository fetch url should be in the git cache mirror directory.',
  512. ' fetch_url: %s' % fetch_url,
  513. ' mirror: %s' % mirror]))
  514. push_url = scm._Capture(['remote', 'get-url', '--push', 'origin'])
  515. self.assertEqual(push_url, self.url)
  516. sys.stdout.close()
  517. class ManagedGitWrapperTestCaseMock(unittest.TestCase):
  518. class OptionsObject(object):
  519. def __init__(self, verbose=False, revision=None, force=False):
  520. self.verbose = verbose
  521. self.revision = revision
  522. self.deps_os = None
  523. self.force = force
  524. self.reset = False
  525. self.nohooks = False
  526. self.break_repo_locks = False
  527. # TODO(maruel): Test --jobs > 1.
  528. self.jobs = 1
  529. self.patch_ref = None
  530. self.patch_repo = None
  531. self.rebase_patch_ref = True
  532. def Options(self, *args, **kwargs):
  533. return self.OptionsObject(*args, **kwargs)
  534. def checkstdout(self, expected):
  535. value = sys.stdout.getvalue()
  536. sys.stdout.close()
  537. # pylint: disable=no-member
  538. self.assertEqual(expected, strip_timestamps(value))
  539. def setUp(self):
  540. self.fake_hash_1 = 't0ta11yf4k3'
  541. self.fake_hash_2 = '3v3nf4k3r'
  542. self.url = 'git://foo'
  543. self.root_dir = '/tmp' if sys.platform != 'win32' else 't:\\tmp'
  544. self.relpath = 'fake'
  545. self.base_path = os.path.join(self.root_dir, self.relpath)
  546. self.backup_base_path = os.path.join(self.root_dir,
  547. 'old_%s.git' % self.relpath)
  548. mock.patch('gclient_scm.scm.GIT.ApplyEnvVars').start()
  549. mock.patch('gclient_scm.GitWrapper._CheckMinVersion').start()
  550. mock.patch('gclient_scm.GitWrapper._Fetch').start()
  551. mock.patch('gclient_scm.GitWrapper._DeleteOrMove').start()
  552. mock.patch('sys.stdout', StringIO()).start()
  553. self.addCleanup(mock.patch.stopall)
  554. @mock.patch('scm.GIT.IsValidRevision')
  555. @mock.patch('os.path.isdir', lambda _: True)
  556. def testGetUsableRevGit(self, mockIsValidRevision):
  557. # pylint: disable=no-member
  558. options = self.Options(verbose=True)
  559. mockIsValidRevision.side_effect = lambda cwd, rev: rev != '1'
  560. git_scm = gclient_scm.GitWrapper(self.url, self.root_dir,
  561. self.relpath)
  562. # A [fake] git sha1 with a git repo should work (this is in the case that
  563. # the LKGR gets flipped to git sha1's some day).
  564. self.assertEqual(git_scm.GetUsableRev(self.fake_hash_1, options),
  565. self.fake_hash_1)
  566. # An SVN rev with an existing purely git repo should raise an exception.
  567. self.assertRaises(gclient_scm.gclient_utils.Error,
  568. git_scm.GetUsableRev, '1', options)
  569. @mock.patch('gclient_scm.GitWrapper._Clone')
  570. @mock.patch('os.path.isdir')
  571. @mock.patch('os.path.exists')
  572. @mock.patch('subprocess2.check_output')
  573. def testUpdateNoDotGit(
  574. self, mockCheckOutput, mockExists, mockIsdir, mockClone):
  575. mockIsdir.side_effect = lambda path: path == self.base_path
  576. mockExists.side_effect = lambda path: path == self.base_path
  577. mockCheckOutput.return_value = b''
  578. options = self.Options()
  579. scm = gclient_scm.GitWrapper(
  580. self.url, self.root_dir, self.relpath)
  581. scm.update(options, None, [])
  582. env = gclient_scm.scm.GIT.ApplyEnvVars({})
  583. self.assertEqual(
  584. mockCheckOutput.mock_calls,
  585. [
  586. mock.call(
  587. ['git', '-c', 'core.quotePath=false', 'ls-files'],
  588. cwd=self.base_path, env=env, stderr=-1),
  589. mock.call(
  590. ['git', 'rev-parse', '--verify', 'HEAD'],
  591. cwd=self.base_path, env=env, stderr=-1),
  592. ])
  593. mockClone.assert_called_with(
  594. 'refs/remotes/origin/master', self.url, options)
  595. self.checkstdout('\n')
  596. @mock.patch('gclient_scm.GitWrapper._Clone')
  597. @mock.patch('os.path.isdir')
  598. @mock.patch('os.path.exists')
  599. @mock.patch('subprocess2.check_output')
  600. def testUpdateConflict(
  601. self, mockCheckOutput, mockExists, mockIsdir, mockClone):
  602. mockIsdir.side_effect = lambda path: path == self.base_path
  603. mockExists.side_effect = lambda path: path == self.base_path
  604. mockCheckOutput.return_value = b''
  605. mockClone.side_effect = [
  606. gclient_scm.subprocess2.CalledProcessError(
  607. None, None, None, None, None),
  608. None,
  609. ]
  610. options = self.Options()
  611. scm = gclient_scm.GitWrapper(self.url, self.root_dir,
  612. self.relpath)
  613. scm.update(options, None, [])
  614. env = gclient_scm.scm.GIT.ApplyEnvVars({})
  615. self.assertEqual(
  616. mockCheckOutput.mock_calls,
  617. [
  618. mock.call(
  619. ['git', '-c', 'core.quotePath=false', 'ls-files'],
  620. cwd=self.base_path, env=env, stderr=-1),
  621. mock.call(
  622. ['git', 'rev-parse', '--verify', 'HEAD'],
  623. cwd=self.base_path, env=env, stderr=-1),
  624. ])
  625. mockClone.assert_called_with(
  626. 'refs/remotes/origin/master', self.url, options)
  627. self.checkstdout('\n')
  628. class UnmanagedGitWrapperTestCase(BaseGitWrapperTestCase):
  629. def checkInStdout(self, expected):
  630. value = sys.stdout.getvalue()
  631. sys.stdout.close()
  632. # pylint: disable=no-member
  633. self.assertIn(expected, value)
  634. def checkNotInStdout(self, expected):
  635. value = sys.stdout.getvalue()
  636. sys.stdout.close()
  637. # pylint: disable=no-member
  638. self.assertNotIn(expected, value)
  639. def getCurrentBranch(self):
  640. # Returns name of current branch or HEAD for detached HEAD
  641. branch = gclient_scm.scm.GIT.Capture(['rev-parse', '--abbrev-ref', 'HEAD'],
  642. cwd=self.base_path)
  643. if branch == 'HEAD':
  644. return None
  645. return branch
  646. def testUpdateClone(self):
  647. if not self.enabled:
  648. return
  649. options = self.Options()
  650. origin_root_dir = self.root_dir
  651. self.root_dir = tempfile.mkdtemp()
  652. self.relpath = '.'
  653. self.base_path = join(self.root_dir, self.relpath)
  654. scm = gclient_scm.GitWrapper(origin_root_dir,
  655. self.root_dir,
  656. self.relpath)
  657. expected_file_list = [join(self.base_path, "a"),
  658. join(self.base_path, "b")]
  659. file_list = []
  660. options.revision = 'unmanaged'
  661. scm.update(options, (), file_list)
  662. self.assertEqual(file_list, expected_file_list)
  663. self.assertEqual(scm.revinfo(options, (), None),
  664. '069c602044c5388d2d15c3f875b057c852003458')
  665. # indicates detached HEAD
  666. self.assertEqual(self.getCurrentBranch(), None)
  667. self.checkInStdout(
  668. 'Checked out refs/remotes/origin/master to a detached HEAD')
  669. rmtree(origin_root_dir)
  670. def testUpdateCloneOnCommit(self):
  671. if not self.enabled:
  672. return
  673. options = self.Options()
  674. origin_root_dir = self.root_dir
  675. self.root_dir = tempfile.mkdtemp()
  676. self.relpath = '.'
  677. self.base_path = join(self.root_dir, self.relpath)
  678. url_with_commit_ref = origin_root_dir +\
  679. '@a7142dc9f0009350b96a11f372b6ea658592aa95'
  680. scm = gclient_scm.GitWrapper(url_with_commit_ref,
  681. self.root_dir,
  682. self.relpath)
  683. expected_file_list = [join(self.base_path, "a"),
  684. join(self.base_path, "b")]
  685. file_list = []
  686. options.revision = 'unmanaged'
  687. scm.update(options, (), file_list)
  688. self.assertEqual(file_list, expected_file_list)
  689. self.assertEqual(scm.revinfo(options, (), None),
  690. 'a7142dc9f0009350b96a11f372b6ea658592aa95')
  691. # indicates detached HEAD
  692. self.assertEqual(self.getCurrentBranch(), None)
  693. self.checkInStdout(
  694. 'Checked out a7142dc9f0009350b96a11f372b6ea658592aa95 to a detached HEAD')
  695. rmtree(origin_root_dir)
  696. def testUpdateCloneOnBranch(self):
  697. if not self.enabled:
  698. return
  699. options = self.Options()
  700. origin_root_dir = self.root_dir
  701. self.root_dir = tempfile.mkdtemp()
  702. self.relpath = '.'
  703. self.base_path = join(self.root_dir, self.relpath)
  704. url_with_branch_ref = origin_root_dir + '@feature'
  705. scm = gclient_scm.GitWrapper(url_with_branch_ref,
  706. self.root_dir,
  707. self.relpath)
  708. expected_file_list = [join(self.base_path, "a"),
  709. join(self.base_path, "b"),
  710. join(self.base_path, "c")]
  711. file_list = []
  712. options.revision = 'unmanaged'
  713. scm.update(options, (), file_list)
  714. self.assertEqual(file_list, expected_file_list)
  715. self.assertEqual(scm.revinfo(options, (), None),
  716. '9a51244740b25fa2ded5252ca00a3178d3f665a9')
  717. # indicates detached HEAD
  718. self.assertEqual(self.getCurrentBranch(), None)
  719. self.checkInStdout(
  720. 'Checked out 9a51244740b25fa2ded5252ca00a3178d3f665a9 '
  721. 'to a detached HEAD')
  722. rmtree(origin_root_dir)
  723. def testUpdateCloneOnFetchedRemoteBranch(self):
  724. if not self.enabled:
  725. return
  726. options = self.Options()
  727. origin_root_dir = self.root_dir
  728. self.root_dir = tempfile.mkdtemp()
  729. self.relpath = '.'
  730. self.base_path = join(self.root_dir, self.relpath)
  731. url_with_branch_ref = origin_root_dir + '@refs/remotes/origin/feature'
  732. scm = gclient_scm.GitWrapper(url_with_branch_ref,
  733. self.root_dir,
  734. self.relpath)
  735. expected_file_list = [join(self.base_path, "a"),
  736. join(self.base_path, "b"),
  737. join(self.base_path, "c")]
  738. file_list = []
  739. options.revision = 'unmanaged'
  740. scm.update(options, (), file_list)
  741. self.assertEqual(file_list, expected_file_list)
  742. self.assertEqual(scm.revinfo(options, (), None),
  743. '9a51244740b25fa2ded5252ca00a3178d3f665a9')
  744. # indicates detached HEAD
  745. self.assertEqual(self.getCurrentBranch(), None)
  746. self.checkInStdout(
  747. 'Checked out refs/remotes/origin/feature to a detached HEAD')
  748. rmtree(origin_root_dir)
  749. def testUpdateCloneOnTrueRemoteBranch(self):
  750. if not self.enabled:
  751. return
  752. options = self.Options()
  753. origin_root_dir = self.root_dir
  754. self.root_dir = tempfile.mkdtemp()
  755. self.relpath = '.'
  756. self.base_path = join(self.root_dir, self.relpath)
  757. url_with_branch_ref = origin_root_dir + '@refs/heads/feature'
  758. scm = gclient_scm.GitWrapper(url_with_branch_ref,
  759. self.root_dir,
  760. self.relpath)
  761. expected_file_list = [join(self.base_path, "a"),
  762. join(self.base_path, "b"),
  763. join(self.base_path, "c")]
  764. file_list = []
  765. options.revision = 'unmanaged'
  766. scm.update(options, (), file_list)
  767. self.assertEqual(file_list, expected_file_list)
  768. self.assertEqual(scm.revinfo(options, (), None),
  769. '9a51244740b25fa2ded5252ca00a3178d3f665a9')
  770. # @refs/heads/feature is AKA @refs/remotes/origin/feature in the clone, so
  771. # should be treated as such by gclient.
  772. # TODO(mmoss): Though really, we should only allow DEPS to specify branches
  773. # as they are known in the upstream repo, since the mapping into the local
  774. # repo can be modified by users (or we might even want to change the gclient
  775. # defaults at some point). But that will take more work to stop using
  776. # refs/remotes/ everywhere that we do (and to stop assuming a DEPS ref will
  777. # always resolve locally, like when passing them to show-ref or rev-list).
  778. self.assertEqual(self.getCurrentBranch(), None)
  779. self.checkInStdout(
  780. 'Checked out refs/remotes/origin/feature to a detached HEAD')
  781. rmtree(origin_root_dir)
  782. def testUpdateUpdate(self):
  783. if not self.enabled:
  784. return
  785. options = self.Options()
  786. expected_file_list = []
  787. scm = gclient_scm.GitWrapper(self.url, self.root_dir,
  788. self.relpath)
  789. file_list = []
  790. options.revision = 'unmanaged'
  791. scm.update(options, (), file_list)
  792. self.assertEqual(file_list, expected_file_list)
  793. self.assertEqual(scm.revinfo(options, (), None),
  794. '069c602044c5388d2d15c3f875b057c852003458')
  795. self.checkstdout('________ unmanaged solution; skipping .\n')
  796. class CipdWrapperTestCase(unittest.TestCase):
  797. def setUp(self):
  798. # Create this before setting up mocks.
  799. self._cipd_root_dir = tempfile.mkdtemp()
  800. self._workdir = tempfile.mkdtemp()
  801. self._cipd_instance_url = 'https://chrome-infra-packages.appspot.com'
  802. self._cipd_root = gclient_scm.CipdRoot(
  803. self._cipd_root_dir,
  804. self._cipd_instance_url)
  805. self._cipd_packages = [
  806. self._cipd_root.add_package('f', 'foo_package', 'foo_version'),
  807. self._cipd_root.add_package('b', 'bar_package', 'bar_version'),
  808. self._cipd_root.add_package('b', 'baz_package', 'baz_version'),
  809. ]
  810. mock.patch('tempfile.mkdtemp', lambda: self._workdir).start()
  811. mock.patch('gclient_scm.CipdRoot.add_package').start()
  812. mock.patch('gclient_scm.CipdRoot.clobber').start()
  813. mock.patch('gclient_scm.CipdRoot.ensure').start()
  814. self.addCleanup(mock.patch.stopall)
  815. def tearDown(self):
  816. rmtree(self._cipd_root_dir)
  817. rmtree(self._workdir)
  818. def createScmWithPackageThatSatisfies(self, condition):
  819. return gclient_scm.CipdWrapper(
  820. url=self._cipd_instance_url,
  821. root_dir=self._cipd_root_dir,
  822. relpath='fake_relpath',
  823. root=self._cipd_root,
  824. package=self.getPackageThatSatisfies(condition))
  825. def getPackageThatSatisfies(self, condition):
  826. for p in self._cipd_packages:
  827. if condition(p):
  828. return p
  829. self.fail('Unable to find a satisfactory package.')
  830. def testRevert(self):
  831. """Checks that revert does nothing."""
  832. scm = self.createScmWithPackageThatSatisfies(lambda _: True)
  833. scm.revert(None, (), [])
  834. @mock.patch('gclient_scm.gclient_utils.CheckCallAndFilter')
  835. @mock.patch('gclient_scm.gclient_utils.rmtree')
  836. def testRevinfo(self, mockRmtree, mockCheckCallAndFilter):
  837. """Checks that revinfo uses the JSON from cipd describe."""
  838. scm = self.createScmWithPackageThatSatisfies(lambda _: True)
  839. expected_revinfo = '0123456789abcdef0123456789abcdef01234567'
  840. json_contents = {
  841. 'result': {
  842. 'pin': {
  843. 'instance_id': expected_revinfo,
  844. }
  845. }
  846. }
  847. describe_json_path = join(self._workdir, 'describe.json')
  848. with open(describe_json_path, 'w') as describe_json:
  849. json.dump(json_contents, describe_json)
  850. revinfo = scm.revinfo(None, (), [])
  851. self.assertEqual(revinfo, expected_revinfo)
  852. mockRmtree.assert_called_with(self._workdir)
  853. mockCheckCallAndFilter.assert_called_with([
  854. 'cipd', 'describe', 'foo_package',
  855. '-log-level', 'error',
  856. '-version', 'foo_version',
  857. '-json-output', describe_json_path,
  858. ])
  859. def testUpdate(self):
  860. """Checks that update does nothing."""
  861. scm = self.createScmWithPackageThatSatisfies(lambda _: True)
  862. scm.update(None, (), [])
  863. class GerritChangesFakeRepo(fake_repos.FakeReposBase):
  864. def populateGit(self):
  865. # Creates a tree that looks like this:
  866. #
  867. # 6 refs/changes/35/1235/1
  868. # |
  869. # 5 refs/changes/34/1234/1
  870. # |
  871. # 1--2--3--4 refs/heads/master
  872. # | |
  873. # | 11(5)--12 refs/heads/master-with-5
  874. # |
  875. # 7--8--9 refs/heads/feature
  876. # |
  877. # 10 refs/changes/36/1236/1
  878. #
  879. self._commit_git('repo_1', {'commit 1': 'touched'})
  880. self._commit_git('repo_1', {'commit 2': 'touched'})
  881. self._commit_git('repo_1', {'commit 3': 'touched'})
  882. self._commit_git('repo_1', {'commit 4': 'touched'})
  883. self._create_ref('repo_1', 'refs/heads/master', 4)
  884. # Create a change on top of commit 3 that consists of two commits.
  885. self._commit_git('repo_1',
  886. {'commit 5': 'touched',
  887. 'change': '1234'},
  888. base=3)
  889. self._create_ref('repo_1', 'refs/changes/34/1234/1', 5)
  890. self._commit_git('repo_1',
  891. {'commit 6': 'touched',
  892. 'change': '1235'})
  893. self._create_ref('repo_1', 'refs/changes/35/1235/1', 6)
  894. # Create a refs/heads/feature branch on top of commit 2, consisting of three
  895. # commits.
  896. self._commit_git('repo_1', {'commit 7': 'touched'}, base=2)
  897. self._commit_git('repo_1', {'commit 8': 'touched'})
  898. self._commit_git('repo_1', {'commit 9': 'touched'})
  899. self._create_ref('repo_1', 'refs/heads/feature', 9)
  900. # Create a change of top of commit 8.
  901. self._commit_git('repo_1',
  902. {'commit 10': 'touched',
  903. 'change': '1236'},
  904. base=8)
  905. self._create_ref('repo_1', 'refs/changes/36/1236/1', 10)
  906. # Create a refs/heads/master-with-5 on top of commit 3 which is a branch
  907. # where refs/changes/34/1234/1 (commit 5) has already landed as commit 11.
  908. self._commit_git('repo_1',
  909. # This is really commit 11, but has the changes of commit 5
  910. {'commit 5': 'touched',
  911. 'change': '1234'},
  912. base=3)
  913. self._commit_git('repo_1', {'commit 12': 'touched'})
  914. self._create_ref('repo_1', 'refs/heads/master-with-5', 12)
  915. class GerritChangesTest(fake_repos.FakeReposTestBase):
  916. FAKE_REPOS_CLASS = GerritChangesFakeRepo
  917. def setUp(self):
  918. super(GerritChangesTest, self).setUp()
  919. self.enabled = self.FAKE_REPOS.set_up_git()
  920. self.options = BaseGitWrapperTestCase.OptionsObject()
  921. self.url = self.git_base + 'repo_1'
  922. self.mirror = None
  923. def setUpMirror(self):
  924. self.mirror = tempfile.mkdtemp()
  925. git_cache.Mirror.SetCachePath(self.mirror)
  926. self.addCleanup(rmtree, self.mirror)
  927. self.addCleanup(git_cache.Mirror.SetCachePath, None)
  928. def assertCommits(self, commits):
  929. """Check that all, and only |commits| are present in the current checkout.
  930. """
  931. for i in commits:
  932. name = os.path.join(self.root_dir, 'commit ' + str(i))
  933. self.assertTrue(os.path.exists(name), 'Commit not found: %s' % name)
  934. all_commits = set(range(1, len(self.FAKE_REPOS.git_hashes['repo_1'])))
  935. for i in all_commits - set(commits):
  936. name = os.path.join(self.root_dir, 'commit ' + str(i))
  937. self.assertFalse(os.path.exists(name), 'Unexpected commit: %s' % name)
  938. def testCanCloneGerritChange(self):
  939. scm = gclient_scm.GitWrapper(self.url, self.root_dir, '.')
  940. file_list = []
  941. self.options.revision = 'refs/changes/35/1235/1'
  942. scm.update(self.options, None, file_list)
  943. self.assertEqual(self.githash('repo_1', 6), self.gitrevparse(self.root_dir))
  944. def testCanSyncToGerritChange(self):
  945. scm = gclient_scm.GitWrapper(self.url, self.root_dir, '.')
  946. file_list = []
  947. self.options.revision = self.githash('repo_1', 1)
  948. scm.update(self.options, None, file_list)
  949. self.assertEqual(self.githash('repo_1', 1), self.gitrevparse(self.root_dir))
  950. self.options.revision = 'refs/changes/35/1235/1'
  951. scm.update(self.options, None, file_list)
  952. self.assertEqual(self.githash('repo_1', 6), self.gitrevparse(self.root_dir))
  953. def testCanCloneGerritChangeMirror(self):
  954. self.setUpMirror()
  955. self.testCanCloneGerritChange()
  956. def testCanSyncToGerritChangeMirror(self):
  957. self.setUpMirror()
  958. self.testCanSyncToGerritChange()
  959. def testAppliesPatchOnTopOfMasterByDefault(self):
  960. """Test the default case, where we apply a patch on top of master."""
  961. scm = gclient_scm.GitWrapper(self.url, self.root_dir, '.')
  962. file_list = []
  963. # Make sure we don't specify a revision.
  964. self.options.revision = None
  965. scm.update(self.options, None, file_list)
  966. self.assertEqual(self.githash('repo_1', 4), self.gitrevparse(self.root_dir))
  967. scm.apply_patch_ref(
  968. self.url, 'refs/changes/35/1235/1', 'refs/heads/master', self.options,
  969. file_list)
  970. self.assertCommits([1, 2, 3, 4, 5, 6])
  971. self.assertEqual(self.githash('repo_1', 4), self.gitrevparse(self.root_dir))
  972. def testCheckoutOlderThanPatchBase(self):
  973. """Test applying a patch on an old checkout.
  974. We first checkout commit 1, and try to patch refs/changes/35/1235/1, which
  975. contains commits 5 and 6, and is based on top of commit 3.
  976. The final result should contain commits 1, 5 and 6, but not commits 2 or 3.
  977. """
  978. scm = gclient_scm.GitWrapper(self.url, self.root_dir, '.')
  979. file_list = []
  980. # Sync to commit 1
  981. self.options.revision = self.githash('repo_1', 1)
  982. scm.update(self.options, None, file_list)
  983. self.assertEqual(self.githash('repo_1', 1), self.gitrevparse(self.root_dir))
  984. # Apply the change on top of that.
  985. scm.apply_patch_ref(
  986. self.url, 'refs/changes/35/1235/1', 'refs/heads/master', self.options,
  987. file_list)
  988. self.assertCommits([1, 5, 6])
  989. self.assertEqual(self.githash('repo_1', 1), self.gitrevparse(self.root_dir))
  990. def testCheckoutOriginFeature(self):
  991. """Tests that we can apply a patch on a branch other than master."""
  992. scm = gclient_scm.GitWrapper(self.url, self.root_dir, '.')
  993. file_list = []
  994. # Sync to remote's refs/heads/feature
  995. self.options.revision = 'refs/heads/feature'
  996. scm.update(self.options, None, file_list)
  997. self.assertEqual(self.githash('repo_1', 9), self.gitrevparse(self.root_dir))
  998. # Apply the change on top of that.
  999. scm.apply_patch_ref(
  1000. self.url, 'refs/changes/36/1236/1', 'refs/heads/feature', self.options,
  1001. file_list)
  1002. self.assertCommits([1, 2, 7, 8, 9, 10])
  1003. self.assertEqual(self.githash('repo_1', 9), self.gitrevparse(self.root_dir))
  1004. def testCheckoutOriginFeatureOnOldRevision(self):
  1005. """Tests that we can apply a patch on an old checkout, on a branch other
  1006. than master."""
  1007. scm = gclient_scm.GitWrapper(self.url, self.root_dir, '.')
  1008. file_list = []
  1009. # Sync to remote's refs/heads/feature on an old revision
  1010. self.options.revision = self.githash('repo_1', 7)
  1011. scm.update(self.options, None, file_list)
  1012. self.assertEqual(self.githash('repo_1', 7), self.gitrevparse(self.root_dir))
  1013. # Apply the change on top of that.
  1014. scm.apply_patch_ref(
  1015. self.url, 'refs/changes/36/1236/1', 'refs/heads/feature', self.options,
  1016. file_list)
  1017. # We shouldn't have rebased on top of 2 (which is the merge base between
  1018. # remote's master branch and the change) but on top of 7 (which is the
  1019. # merge base between remote's feature branch and the change).
  1020. self.assertCommits([1, 2, 7, 10])
  1021. self.assertEqual(self.githash('repo_1', 7), self.gitrevparse(self.root_dir))
  1022. def testCheckoutOriginFeaturePatchBranch(self):
  1023. scm = gclient_scm.GitWrapper(self.url, self.root_dir, '.')
  1024. file_list = []
  1025. # Sync to the hash instead of remote's refs/heads/feature.
  1026. self.options.revision = self.githash('repo_1', 9)
  1027. scm.update(self.options, None, file_list)
  1028. self.assertEqual(self.githash('repo_1', 9), self.gitrevparse(self.root_dir))
  1029. # Apply refs/changes/34/1234/1, created for remote's master branch on top of
  1030. # remote's feature branch.
  1031. scm.apply_patch_ref(
  1032. self.url, 'refs/changes/35/1235/1', 'refs/heads/master', self.options,
  1033. file_list)
  1034. # Commits 5 and 6 are part of the patch, and commits 1, 2, 7, 8 and 9 are
  1035. # part of remote's feature branch.
  1036. self.assertCommits([1, 2, 5, 6, 7, 8, 9])
  1037. self.assertEqual(self.githash('repo_1', 9), self.gitrevparse(self.root_dir))
  1038. def testDoesntRebasePatchMaster(self):
  1039. """Tests that we can apply a patch without rebasing it.
  1040. """
  1041. scm = gclient_scm.GitWrapper(self.url, self.root_dir, '.')
  1042. file_list = []
  1043. self.options.rebase_patch_ref = False
  1044. scm.update(self.options, None, file_list)
  1045. self.assertEqual(self.githash('repo_1', 4), self.gitrevparse(self.root_dir))
  1046. # Apply the change on top of that.
  1047. scm.apply_patch_ref(
  1048. self.url, 'refs/changes/35/1235/1', 'refs/heads/master', self.options,
  1049. file_list)
  1050. self.assertCommits([1, 2, 3, 5, 6])
  1051. self.assertEqual(self.githash('repo_1', 4), self.gitrevparse(self.root_dir))
  1052. def testDoesntRebasePatchOldCheckout(self):
  1053. """Tests that we can apply a patch without rebasing it on an old checkout.
  1054. """
  1055. scm = gclient_scm.GitWrapper(self.url, self.root_dir, '.')
  1056. file_list = []
  1057. # Sync to commit 1
  1058. self.options.revision = self.githash('repo_1', 1)
  1059. self.options.rebase_patch_ref = False
  1060. scm.update(self.options, None, file_list)
  1061. self.assertEqual(self.githash('repo_1', 1), self.gitrevparse(self.root_dir))
  1062. # Apply the change on top of that.
  1063. scm.apply_patch_ref(
  1064. self.url, 'refs/changes/35/1235/1', 'refs/heads/master', self.options,
  1065. file_list)
  1066. self.assertCommits([1, 2, 3, 5, 6])
  1067. self.assertEqual(self.githash('repo_1', 1), self.gitrevparse(self.root_dir))
  1068. def testDoesntSoftResetIfNotAskedTo(self):
  1069. """Test that we can apply a patch without doing a soft reset."""
  1070. scm = gclient_scm.GitWrapper(self.url, self.root_dir, '.')
  1071. file_list = []
  1072. self.options.reset_patch_ref = False
  1073. scm.update(self.options, None, file_list)
  1074. self.assertEqual(self.githash('repo_1', 4), self.gitrevparse(self.root_dir))
  1075. scm.apply_patch_ref(
  1076. self.url, 'refs/changes/35/1235/1', 'refs/heads/master', self.options,
  1077. file_list)
  1078. self.assertCommits([1, 2, 3, 4, 5, 6])
  1079. # The commit hash after cherry-picking is not known, but it must be
  1080. # different from what the repo was synced at before patching.
  1081. self.assertNotEqual(self.githash('repo_1', 4),
  1082. self.gitrevparse(self.root_dir))
  1083. def testRecoversAfterPatchFailure(self):
  1084. scm = gclient_scm.GitWrapper(self.url, self.root_dir, '.')
  1085. file_list = []
  1086. self.options.revision = 'refs/changes/34/1234/1'
  1087. scm.update(self.options, None, file_list)
  1088. self.assertEqual(self.githash('repo_1', 5), self.gitrevparse(self.root_dir))
  1089. # Checkout 'refs/changes/34/1234/1' modifies the 'change' file, so trying to
  1090. # patch 'refs/changes/36/1236/1' creates a patch failure.
  1091. with self.assertRaises(subprocess2.CalledProcessError) as cm:
  1092. scm.apply_patch_ref(
  1093. self.url, 'refs/changes/36/1236/1', 'refs/heads/master', self.options,
  1094. file_list)
  1095. self.assertEqual(cm.exception.cmd[:2], ['git', 'cherry-pick'])
  1096. self.assertIn(b'error: could not apply', cm.exception.stderr)
  1097. # Try to apply 'refs/changes/35/1235/1', which doesn't have a merge
  1098. # conflict.
  1099. scm.apply_patch_ref(
  1100. self.url, 'refs/changes/35/1235/1', 'refs/heads/master', self.options,
  1101. file_list)
  1102. self.assertCommits([1, 2, 3, 5, 6])
  1103. self.assertEqual(self.githash('repo_1', 5), self.gitrevparse(self.root_dir))
  1104. def testIgnoresAlreadyMergedCommits(self):
  1105. scm = gclient_scm.GitWrapper(self.url, self.root_dir, '.')
  1106. file_list = []
  1107. self.options.revision = 'refs/heads/master-with-5'
  1108. scm.update(self.options, None, file_list)
  1109. self.assertEqual(self.githash('repo_1', 12),
  1110. self.gitrevparse(self.root_dir))
  1111. # When we try 'refs/changes/35/1235/1' on top of 'refs/heads/feature',
  1112. # 'refs/changes/34/1234/1' will be an empty commit, since the changes were
  1113. # already present in the tree as commit 11.
  1114. # Make sure we deal with this gracefully.
  1115. scm.apply_patch_ref(
  1116. self.url, 'refs/changes/35/1235/1', 'refs/heads/feature', self.options,
  1117. file_list)
  1118. self.assertCommits([1, 2, 3, 5, 6, 12])
  1119. self.assertEqual(self.githash('repo_1', 12),
  1120. self.gitrevparse(self.root_dir))
  1121. def testRecoversFromExistingCherryPick(self):
  1122. scm = gclient_scm.GitWrapper(self.url, self.root_dir, '.')
  1123. file_list = []
  1124. self.options.revision = 'refs/changes/34/1234/1'
  1125. scm.update(self.options, None, file_list)
  1126. self.assertEqual(self.githash('repo_1', 5), self.gitrevparse(self.root_dir))
  1127. # Checkout 'refs/changes/34/1234/1' modifies the 'change' file, so trying to
  1128. # cherry-pick 'refs/changes/36/1236/1' raises an error.
  1129. scm._Run(['fetch', 'origin', 'refs/changes/36/1236/1'], self.options)
  1130. with self.assertRaises(subprocess2.CalledProcessError) as cm:
  1131. scm._Run(['cherry-pick', 'FETCH_HEAD'], self.options)
  1132. self.assertEqual(cm.exception.cmd[:2], ['git', 'cherry-pick'])
  1133. # Try to apply 'refs/changes/35/1235/1', which doesn't have a merge
  1134. # conflict.
  1135. scm.apply_patch_ref(
  1136. self.url, 'refs/changes/35/1235/1', 'refs/heads/master', self.options,
  1137. file_list)
  1138. self.assertCommits([1, 2, 3, 5, 6])
  1139. self.assertEqual(self.githash('repo_1', 5), self.gitrevparse(self.root_dir))
  1140. if __name__ == '__main__':
  1141. level = logging.DEBUG if '-v' in sys.argv else logging.FATAL
  1142. logging.basicConfig(
  1143. level=level,
  1144. format='%(asctime).19s %(levelname)s %(filename)s:'
  1145. '%(lineno)s %(message)s')
  1146. unittest.main()
  1147. # vim: ts=2:sw=2:tw=80:et: