Skip to content

Commit 96a8c4b

Browse files
authored
Add get_merge_base to ddev git (#21340)
* method * changelog * changelog * test * test * changelog * fix
1 parent fc7560f commit 96a8c4b

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

ddev/changelog.d/21340.added

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Adds a new method `merge_base` in the `GitRepository` class.

ddev/src/ddev/utils/git.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ def capture(self, *args):
125125

126126
return process.stdout
127127

128+
def merge_base(self, ref_a: str, ref_b: str | None = "HEAD") -> str:
129+
return self.capture('merge-base', ref_a, ref_b).splitlines()[0]
130+
128131
@staticmethod
129132
def __is_warning_line(line):
130133
return line.startswith('warning: ') or 'original line endings' in line

ddev/tests/utils/test_git.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,41 @@ def test_fetch_tags(repository, mocker):
103103
check=True,
104104
),
105105
]
106+
107+
108+
def test_get_merge_base(repository):
109+
repo = Repository(repository.path.name, str(repository.path))
110+
base_commit = repo.git.latest_commit()
111+
repo.git.capture('checkout', '-b', 'test_merge_base')
112+
113+
(repo.path / 'test1.txt').touch()
114+
repo.git.capture('add', '.')
115+
repo.git.capture('commit', '-m', 'test1')
116+
117+
base = repo.git.merge_base('origin/master')
118+
119+
assert base == base_commit.sha
120+
121+
122+
def test_get_merge_base_two_branches(repository):
123+
repo = Repository(repository.path.name, str(repository.path))
124+
base_commit = repo.git.latest_commit()
125+
126+
repo.git.capture('checkout', '-b', 'test1_merge_base')
127+
128+
(repo.path / 'test1.txt').touch()
129+
repo.git.capture('add', '.')
130+
repo.git.capture('commit', '-m', 'test1')
131+
132+
repo.git.capture('branch', 'test2_merge_base')
133+
134+
(repo.path / 'test1_1.txt').touch()
135+
repo.git.capture('add', '.')
136+
repo.git.capture('commit', '-m', 'test1_1')
137+
138+
repo.git.capture('checkout', 'test2_merge_base')
139+
(repo.path / 'test2_1.txt').touch()
140+
repo.git.capture('add', '.')
141+
repo.git.capture('commit', '-m', 'test2')
142+
base = repo.git.merge_base('origin/master')
143+
assert base == base_commit.sha

0 commit comments

Comments
 (0)