gclient_scm_test.py 47 KB

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