Debian Codegraph @

tests/testutils/repo/gitrepo.py

raw
1 import os
2 import shutil
3 import subprocess
4
5 import pytest
6
7 from buildstream._testing import Repo
8
9 from ..site import GIT, GIT_ENV, HAVE_GIT
10
11
12 class Git(Repo):
13 def __init__(self, directory, subdir="repo"):
14 if not HAVE_GIT:
15 pytest.skip("git is not available")
16
17 self.submodules = {}
18
19 super().__init__(directory, subdir)
20
21 self.env = os.environ.copy()
22 self.env.update(GIT_ENV)
23
24 def _run_git(self, *args, **kwargs):
25 argv = [GIT]
26 argv.extend(args)
27 if "env" not in kwargs:
28 kwargs["env"] = dict(self.env, PWD=self.repo)
29 kwargs.setdefault("cwd", self.repo)
30 kwargs.setdefault("check", True)
31 return subprocess.run(argv, **kwargs) # pylint: disable=subprocess-run-check
32
33 def create(self, directory):
34 self.copy_directory(directory, self.repo)
35 self._run_git("init", ".")
36 self._run_git("add", ".")
37 self._run_git("commit", "-m", "Initial commit")
38 return self.latest_commit()
39
40 def add_tag(self, tag):
41 self._run_git("tag", tag)
42
43 def add_annotated_tag(self, tag, message):
44 self._run_git("tag", "-a", tag, "-m", message)
45
46 def add_commit(self):
47 self._run_git("commit", "--allow-empty", "-m", "Additional commit")
48 return self.latest_commit()
49
50 def add_file(self, filename):
51 shutil.copy(filename, self.repo)
52 self._run_git("add", os.path.basename(filename))
53 self._run_git("commit", "-m", "Added {}".format(os.path.basename(filename)))
54 return self.latest_commit()
55
56 def modify_file(self, new_file, path):
57 shutil.copy(new_file, os.path.join(self.repo, path))
58 self._run_git("commit", path, "-m", "Modified {}".format(os.path.basename(path)))
59 return self.latest_commit()
60
61 def add_submodule(self, subdir, url=None, checkout=None):
62 submodule = {}
63 if checkout is not None:
64 submodule["checkout"] = checkout
65 if url is not None:
66 submodule["url"] = url
67 self.submodules[subdir] = submodule
68 self._run_git("-c", "protocol.file.allow=always", "submodule", "add", url, subdir)
69 self._run_git("commit", "-m", "Added the submodule")
70 return self.latest_commit()
71
72 # This can also be used to a file or a submodule
73 def remove_path(self, path):
74 self._run_git("rm", path)
75 self._run_git("commit", "-m", "Removing {}".format(path))
76 return self.latest_commit()
77
78 def source_config(self, ref=None):
79 return self.source_config_extra(ref)
80
81 def source_config_extra(self, ref=None, checkout_submodules=None):
82 config = {
83 "kind": "git",
84 "url": "file://" + self.repo,
85 "track": "master",
86 }
87 if ref is not None:
88 config["ref"] = ref
89 if checkout_submodules is not None:
90 config["checkout-submodules"] = checkout_submodules
91
92 if self.submodules:
93 config["submodules"] = dict(self.submodules)
94
95 return config
96
97 def latest_commit(self):
98 return self._run_git(
99 "rev-parse",
100 "HEAD",
101 stdout=subprocess.PIPE,
102 universal_newlines=True,
103 ).stdout.strip()
104
105 def branch(self, branch_name):
106 self._run_git("checkout", "-b", branch_name)
107
108 def delete_tag(self, tag_name):
109 self._run_git("tag", "-d", tag_name)
110
111 def checkout(self, commit):
112 self._run_git("checkout", commit)
113
114 def merge(self, commit):
115 self._run_git("merge", "-m", "Merge", commit)
116 return self.latest_commit()
117
118 def rev_parse(self, rev):
119 return self._run_git(
120 "rev-parse",
121 rev,
122 stdout=subprocess.PIPE,
123 universal_newlines=True,
124 ).stdout.strip()