Skip to content

Commit 3941e79

Browse files
author
Chris Patterson
authored
git: always fetch specified source-commit before using (#2968)
Two cases where git will fail on specified source-commit: - commit ID is not in specified branch history (or master, if no branch is specified). - source-depth is used which doesn't contain neccessary commit ID. This commit changes the git module to fetch the reference from origin before attempting to use it. LP: #1817907 Signed-off-by: Chris Patterson <chris.patterson@canonical.com>
1 parent bfaec2d commit 3941e79

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

snapcraft/internal/sources/_git.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,19 @@ def _run_git_command(self, command: List[str]) -> None:
150150
output=error.output.decode(),
151151
)
152152

153+
def _fetch_origin_commit(self):
154+
self._run(
155+
[
156+
self.command,
157+
"-C",
158+
self.source_dir,
159+
"fetch",
160+
"origin",
161+
self.source_commit,
162+
],
163+
**self._call_kwargs
164+
)
165+
153166
def _pull_existing(self):
154167
refspec = "HEAD"
155168
if self.source_branch:
@@ -158,6 +171,7 @@ def _pull_existing(self):
158171
refspec = "refs/tags/" + self.source_tag
159172
elif self.source_commit:
160173
refspec = self.source_commit
174+
self._fetch_origin_commit()
161175

162176
reset_spec = refspec if refspec != "HEAD" else "origin/master"
163177

@@ -201,6 +215,8 @@ def _clone_new(self):
201215
self._run(command + [self.source, self.source_dir], **self._call_kwargs)
202216

203217
if self.source_commit:
218+
self._fetch_origin_commit()
219+
204220
self._run(
205221
[self.command, "-C", self.source_dir, "checkout", self.source_commit],
206222
**self._call_kwargs

tests/unit/sources/test_git.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,16 @@ def test_pull_commit(self):
248248
mock.call(
249249
["git", "clone", "--recursive", "git://my-source", "source_dir"]
250250
),
251+
mock.call(
252+
[
253+
"git",
254+
"-C",
255+
"source_dir",
256+
"fetch",
257+
"origin",
258+
"2514f9533ec9b45d07883e10a561b248497a8e3c",
259+
]
260+
),
251261
mock.call(
252262
[
253263
"git",

0 commit comments

Comments
 (0)