test_cli_merge.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. # test_cli_merge.py -- Tests for dulwich merge CLI command
  2. # Copyright (C) 2024 Jelmer Vernooij <jelmer@jelmer.uk>
  3. #
  4. # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  5. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  6. # General Public License as published by the Free Software Foundation; version 2.0
  7. # or (at your option) any later version. You can redistribute it and/or
  8. # modify it under the terms of either of these two licenses.
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. # You should have received a copy of the licenses; if not, see
  17. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  18. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  19. # License, Version 2.0.
  20. #
  21. """Tests for dulwich merge CLI command."""
  22. import importlib.util
  23. import os
  24. import tempfile
  25. import unittest
  26. from dulwich import porcelain
  27. from dulwich.cli import main
  28. from . import DependencyMissing, TestCase
  29. class CLIMergeTests(TestCase):
  30. """Tests for the dulwich merge CLI command."""
  31. def test_merge_fast_forward(self):
  32. """Test CLI merge with fast-forward."""
  33. with tempfile.TemporaryDirectory() as tmpdir:
  34. # Initialize repo
  35. porcelain.init(tmpdir)
  36. # Create initial commit
  37. with open(os.path.join(tmpdir, "file1.txt"), "w") as f:
  38. f.write("Initial content\n")
  39. porcelain.add(tmpdir, paths=["file1.txt"])
  40. porcelain.commit(tmpdir, message=b"Initial commit")
  41. # Create a branch
  42. porcelain.branch_create(tmpdir, "feature")
  43. porcelain.checkout(tmpdir, "feature")
  44. # Add a file on feature branch
  45. with open(os.path.join(tmpdir, "file2.txt"), "w") as f:
  46. f.write("Feature content\n")
  47. porcelain.add(tmpdir, paths=["file2.txt"])
  48. porcelain.commit(tmpdir, message=b"Add feature")
  49. # Go back to master
  50. porcelain.checkout(tmpdir, "master")
  51. # Test merge via CLI
  52. old_cwd = os.getcwd()
  53. try:
  54. os.chdir(tmpdir)
  55. with self.assertLogs("dulwich.cli", level="INFO") as cm:
  56. ret = main(["merge", "feature"])
  57. log_output = "\n".join(cm.output)
  58. self.assertEqual(ret, 0) # Success
  59. self.assertIn("Merge successful", log_output)
  60. # Check that file2.txt exists
  61. self.assertTrue(os.path.exists(os.path.join(tmpdir, "file2.txt")))
  62. finally:
  63. os.chdir(old_cwd)
  64. def test_merge_with_conflicts(self):
  65. """Test CLI merge with conflicts."""
  66. # Check if merge3 module is available
  67. if importlib.util.find_spec("merge3") is None:
  68. raise DependencyMissing("merge3")
  69. with tempfile.TemporaryDirectory() as tmpdir:
  70. # Initialize repo
  71. porcelain.init(tmpdir)
  72. # Create initial commit
  73. with open(os.path.join(tmpdir, "file1.txt"), "w") as f:
  74. f.write("Initial content\n")
  75. porcelain.add(tmpdir, paths=["file1.txt"])
  76. porcelain.commit(tmpdir, message=b"Initial commit")
  77. # Create a branch and modify file1
  78. porcelain.branch_create(tmpdir, "feature")
  79. porcelain.checkout(tmpdir, "feature")
  80. with open(os.path.join(tmpdir, "file1.txt"), "w") as f:
  81. f.write("Feature content\n")
  82. porcelain.add(tmpdir, paths=["file1.txt"])
  83. porcelain.commit(tmpdir, message=b"Modify file1 in feature")
  84. # Go back to master and modify file1 differently
  85. porcelain.checkout(tmpdir, "master")
  86. with open(os.path.join(tmpdir, "file1.txt"), "w") as f:
  87. f.write("Master content\n")
  88. porcelain.add(tmpdir, paths=["file1.txt"])
  89. porcelain.commit(tmpdir, message=b"Modify file1 in master")
  90. # Test merge via CLI - should exit with error
  91. old_cwd = os.getcwd()
  92. try:
  93. os.chdir(tmpdir)
  94. with self.assertLogs("dulwich.cli", level="WARNING") as cm:
  95. retcode = main(["merge", "feature"])
  96. self.assertEqual(retcode, 1)
  97. log_output = "\n".join(cm.output)
  98. self.assertIn("Merge conflicts", log_output)
  99. self.assertIn("file1.txt", log_output)
  100. finally:
  101. os.chdir(old_cwd)
  102. def test_merge_already_up_to_date(self):
  103. """Test CLI merge when already up to date."""
  104. with tempfile.TemporaryDirectory() as tmpdir:
  105. # Initialize repo
  106. porcelain.init(tmpdir)
  107. # Create initial commit
  108. with open(os.path.join(tmpdir, "file1.txt"), "w") as f:
  109. f.write("Initial content\n")
  110. porcelain.add(tmpdir, paths=["file1.txt"])
  111. porcelain.commit(tmpdir, message=b"Initial commit")
  112. # Test merge via CLI
  113. old_cwd = os.getcwd()
  114. try:
  115. os.chdir(tmpdir)
  116. with self.assertLogs("dulwich.cli", level="INFO") as cm:
  117. ret = main(["merge", "HEAD"])
  118. log_output = "\n".join(cm.output)
  119. self.assertEqual(ret, 0) # Success
  120. self.assertIn("Already up to date", log_output)
  121. finally:
  122. os.chdir(old_cwd)
  123. def test_merge_no_commit(self):
  124. """Test CLI merge with --no-commit."""
  125. with tempfile.TemporaryDirectory() as tmpdir:
  126. # Initialize repo
  127. porcelain.init(tmpdir)
  128. # Create initial commit
  129. with open(os.path.join(tmpdir, "file1.txt"), "w") as f:
  130. f.write("Initial content\n")
  131. porcelain.add(tmpdir, paths=["file1.txt"])
  132. porcelain.commit(tmpdir, message=b"Initial commit")
  133. # Create a branch
  134. porcelain.branch_create(tmpdir, "feature")
  135. porcelain.checkout(tmpdir, "feature")
  136. # Add a file on feature branch
  137. with open(os.path.join(tmpdir, "file2.txt"), "w") as f:
  138. f.write("Feature content\n")
  139. porcelain.add(tmpdir, paths=["file2.txt"])
  140. porcelain.commit(tmpdir, message=b"Add feature")
  141. # Go back to master and add another file
  142. porcelain.checkout(tmpdir, "master")
  143. with open(os.path.join(tmpdir, "file3.txt"), "w") as f:
  144. f.write("Master content\n")
  145. porcelain.add(tmpdir, paths=["file3.txt"])
  146. porcelain.commit(tmpdir, message=b"Add file3")
  147. # Test merge via CLI with --no-commit
  148. old_cwd = os.getcwd()
  149. try:
  150. os.chdir(tmpdir)
  151. with self.assertLogs("dulwich.cli", level="INFO") as cm:
  152. ret = main(["merge", "--no-commit", "feature"])
  153. log_output = "\n".join(cm.output)
  154. self.assertEqual(ret, 0) # Success
  155. self.assertIn("not committing", log_output)
  156. # Check that files are merged
  157. self.assertTrue(os.path.exists(os.path.join(tmpdir, "file2.txt")))
  158. self.assertTrue(os.path.exists(os.path.join(tmpdir, "file3.txt")))
  159. finally:
  160. os.chdir(old_cwd)
  161. def test_merge_no_ff(self):
  162. """Test CLI merge with --no-ff."""
  163. with tempfile.TemporaryDirectory() as tmpdir:
  164. # Initialize repo
  165. porcelain.init(tmpdir)
  166. # Create initial commit
  167. with open(os.path.join(tmpdir, "file1.txt"), "w") as f:
  168. f.write("Initial content\n")
  169. porcelain.add(tmpdir, paths=["file1.txt"])
  170. porcelain.commit(tmpdir, message=b"Initial commit")
  171. # Create a branch
  172. porcelain.branch_create(tmpdir, "feature")
  173. porcelain.checkout(tmpdir, "feature")
  174. # Add a file on feature branch
  175. with open(os.path.join(tmpdir, "file2.txt"), "w") as f:
  176. f.write("Feature content\n")
  177. porcelain.add(tmpdir, paths=["file2.txt"])
  178. porcelain.commit(tmpdir, message=b"Add feature")
  179. # Go back to master
  180. porcelain.checkout(tmpdir, "master")
  181. # Test merge via CLI with --no-ff
  182. old_cwd = os.getcwd()
  183. try:
  184. os.chdir(tmpdir)
  185. with self.assertLogs("dulwich.cli", level="INFO") as cm:
  186. ret = main(["merge", "--no-ff", "feature"])
  187. log_output = "\n".join(cm.output)
  188. self.assertEqual(ret, 0) # Success
  189. self.assertIn("Merge successful", log_output)
  190. self.assertIn("Created merge commit", log_output)
  191. finally:
  192. os.chdir(old_cwd)
  193. def test_merge_with_message(self):
  194. """Test CLI merge with custom message."""
  195. with tempfile.TemporaryDirectory() as tmpdir:
  196. # Initialize repo
  197. porcelain.init(tmpdir)
  198. # Create initial commit
  199. with open(os.path.join(tmpdir, "file1.txt"), "w") as f:
  200. f.write("Initial content\n")
  201. porcelain.add(tmpdir, paths=["file1.txt"])
  202. porcelain.commit(tmpdir, message=b"Initial commit")
  203. # Create a branch
  204. porcelain.branch_create(tmpdir, "feature")
  205. porcelain.checkout(tmpdir, "feature")
  206. # Add a file on feature branch
  207. with open(os.path.join(tmpdir, "file2.txt"), "w") as f:
  208. f.write("Feature content\n")
  209. porcelain.add(tmpdir, paths=["file2.txt"])
  210. porcelain.commit(tmpdir, message=b"Add feature")
  211. # Go back to master and add another file
  212. porcelain.checkout(tmpdir, "master")
  213. with open(os.path.join(tmpdir, "file3.txt"), "w") as f:
  214. f.write("Master content\n")
  215. porcelain.add(tmpdir, paths=["file3.txt"])
  216. porcelain.commit(tmpdir, message=b"Add file3")
  217. # Test merge via CLI with custom message
  218. old_cwd = os.getcwd()
  219. try:
  220. os.chdir(tmpdir)
  221. with self.assertLogs("dulwich.cli", level="INFO") as cm:
  222. ret = main(["merge", "-m", "Custom merge message", "feature"])
  223. log_output = "\n".join(cm.output)
  224. self.assertEqual(ret, 0) # Success
  225. self.assertIn("Merge successful", log_output)
  226. finally:
  227. os.chdir(old_cwd)
  228. def test_octopus_merge_three_branches(self):
  229. """Test CLI octopus merge with three branches."""
  230. with tempfile.TemporaryDirectory() as tmpdir:
  231. # Initialize repo
  232. porcelain.init(tmpdir)
  233. # Create initial commit with three files
  234. with open(os.path.join(tmpdir, "file1.txt"), "w") as f:
  235. f.write("File 1 content\n")
  236. with open(os.path.join(tmpdir, "file2.txt"), "w") as f:
  237. f.write("File 2 content\n")
  238. with open(os.path.join(tmpdir, "file3.txt"), "w") as f:
  239. f.write("File 3 content\n")
  240. porcelain.add(tmpdir, paths=["file1.txt", "file2.txt", "file3.txt"])
  241. porcelain.commit(tmpdir, message=b"Initial commit")
  242. # Create branch1 and modify file1
  243. porcelain.branch_create(tmpdir, "branch1")
  244. porcelain.checkout(tmpdir, "branch1")
  245. with open(os.path.join(tmpdir, "file1.txt"), "w") as f:
  246. f.write("Branch1 modified file1\n")
  247. porcelain.add(tmpdir, paths=["file1.txt"])
  248. porcelain.commit(tmpdir, message=b"Branch1 modifies file1")
  249. # Create branch2 and modify file2
  250. porcelain.checkout(tmpdir, "master")
  251. porcelain.branch_create(tmpdir, "branch2")
  252. porcelain.checkout(tmpdir, "branch2")
  253. with open(os.path.join(tmpdir, "file2.txt"), "w") as f:
  254. f.write("Branch2 modified file2\n")
  255. porcelain.add(tmpdir, paths=["file2.txt"])
  256. porcelain.commit(tmpdir, message=b"Branch2 modifies file2")
  257. # Create branch3 and modify file3
  258. porcelain.checkout(tmpdir, "master")
  259. porcelain.branch_create(tmpdir, "branch3")
  260. porcelain.checkout(tmpdir, "branch3")
  261. with open(os.path.join(tmpdir, "file3.txt"), "w") as f:
  262. f.write("Branch3 modified file3\n")
  263. porcelain.add(tmpdir, paths=["file3.txt"])
  264. porcelain.commit(tmpdir, message=b"Branch3 modifies file3")
  265. # Go back to master and octopus merge all three branches via CLI
  266. porcelain.checkout(tmpdir, "master")
  267. old_cwd = os.getcwd()
  268. try:
  269. os.chdir(tmpdir)
  270. with self.assertLogs("dulwich.cli", level="INFO") as cm:
  271. ret = main(["merge", "branch1", "branch2", "branch3"])
  272. log_output = "\n".join(cm.output)
  273. self.assertEqual(ret, 0) # Success
  274. self.assertIn("Octopus merge successful", log_output)
  275. # Check that all modifications are present
  276. with open(os.path.join(tmpdir, "file1.txt")) as f:
  277. self.assertEqual(f.read(), "Branch1 modified file1\n")
  278. with open(os.path.join(tmpdir, "file2.txt")) as f:
  279. self.assertEqual(f.read(), "Branch2 modified file2\n")
  280. with open(os.path.join(tmpdir, "file3.txt")) as f:
  281. self.assertEqual(f.read(), "Branch3 modified file3\n")
  282. finally:
  283. os.chdir(old_cwd)
  284. def test_octopus_merge_with_conflicts(self):
  285. """Test CLI octopus merge with conflicts."""
  286. # Check if merge3 module is available
  287. if importlib.util.find_spec("merge3") is None:
  288. raise DependencyMissing("merge3")
  289. with tempfile.TemporaryDirectory() as tmpdir:
  290. # Initialize repo
  291. porcelain.init(tmpdir)
  292. # Create initial commit
  293. with open(os.path.join(tmpdir, "file1.txt"), "w") as f:
  294. f.write("Initial content\n")
  295. porcelain.add(tmpdir, paths=["file1.txt"])
  296. porcelain.commit(tmpdir, message=b"Initial commit")
  297. # Create branch1 and modify file1
  298. porcelain.branch_create(tmpdir, "branch1")
  299. porcelain.checkout(tmpdir, "branch1")
  300. with open(os.path.join(tmpdir, "file1.txt"), "w") as f:
  301. f.write("Branch1 content\n")
  302. porcelain.add(tmpdir, paths=["file1.txt"])
  303. porcelain.commit(tmpdir, message=b"Branch1 modifies file1")
  304. # Create branch2 and modify file1 differently
  305. porcelain.checkout(tmpdir, "master")
  306. porcelain.branch_create(tmpdir, "branch2")
  307. porcelain.checkout(tmpdir, "branch2")
  308. with open(os.path.join(tmpdir, "file1.txt"), "w") as f:
  309. f.write("Branch2 content\n")
  310. porcelain.add(tmpdir, paths=["file1.txt"])
  311. porcelain.commit(tmpdir, message=b"Branch2 modifies file1")
  312. # Go back to master and try octopus merge via CLI - should fail
  313. porcelain.checkout(tmpdir, "master")
  314. old_cwd = os.getcwd()
  315. try:
  316. os.chdir(tmpdir)
  317. with self.assertLogs("dulwich.cli", level="WARNING") as cm:
  318. ret = main(["merge", "branch1", "branch2"])
  319. log_output = "\n".join(cm.output)
  320. self.assertEqual(ret, 1) # Error
  321. self.assertIn("Octopus merge failed", log_output)
  322. self.assertIn("refusing to merge with conflicts", log_output)
  323. finally:
  324. os.chdir(old_cwd)
  325. def test_octopus_merge_no_commit(self):
  326. """Test CLI octopus merge with --no-commit."""
  327. with tempfile.TemporaryDirectory() as tmpdir:
  328. # Initialize repo
  329. porcelain.init(tmpdir)
  330. # Create initial commit
  331. with open(os.path.join(tmpdir, "file1.txt"), "w") as f:
  332. f.write("File 1 content\n")
  333. with open(os.path.join(tmpdir, "file2.txt"), "w") as f:
  334. f.write("File 2 content\n")
  335. porcelain.add(tmpdir, paths=["file1.txt", "file2.txt"])
  336. porcelain.commit(tmpdir, message=b"Initial commit")
  337. # Create branch1 and modify file1
  338. porcelain.branch_create(tmpdir, "branch1")
  339. porcelain.checkout(tmpdir, "branch1")
  340. with open(os.path.join(tmpdir, "file1.txt"), "w") as f:
  341. f.write("Branch1 modified file1\n")
  342. porcelain.add(tmpdir, paths=["file1.txt"])
  343. porcelain.commit(tmpdir, message=b"Branch1 modifies file1")
  344. # Create branch2 and modify file2
  345. porcelain.checkout(tmpdir, "master")
  346. porcelain.branch_create(tmpdir, "branch2")
  347. porcelain.checkout(tmpdir, "branch2")
  348. with open(os.path.join(tmpdir, "file2.txt"), "w") as f:
  349. f.write("Branch2 modified file2\n")
  350. porcelain.add(tmpdir, paths=["file2.txt"])
  351. porcelain.commit(tmpdir, message=b"Branch2 modifies file2")
  352. # Go back to master and octopus merge with --no-commit
  353. porcelain.checkout(tmpdir, "master")
  354. old_cwd = os.getcwd()
  355. try:
  356. os.chdir(tmpdir)
  357. with self.assertLogs("dulwich.cli", level="INFO") as cm:
  358. ret = main(["merge", "--no-commit", "branch1", "branch2"])
  359. log_output = "\n".join(cm.output)
  360. self.assertEqual(ret, 0) # Success
  361. self.assertIn("not committing", log_output)
  362. # Check that files are merged
  363. with open(os.path.join(tmpdir, "file1.txt")) as f:
  364. self.assertEqual(f.read(), "Branch1 modified file1\n")
  365. with open(os.path.join(tmpdir, "file2.txt")) as f:
  366. self.assertEqual(f.read(), "Branch2 modified file2\n")
  367. finally:
  368. os.chdir(old_cwd)
  369. if __name__ == "__main__":
  370. unittest.main()