test_cli_merge.py 18 KB

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