Debian Codegraph @

src/briefcase/integrations/git.py

raw
1 from __future__ import annotations
2
3 from types import ModuleType
4
5 from briefcase.exceptions import BriefcaseCommandError
6 from briefcase.integrations.base import Tool, ToolCache
7
8
9 class Git(Tool):
10 name = "git"
11 full_name = "Git"
12
13 MIN_VERSION = (2, 17, 0)
14 GIT_URL = "https://git-scm.com/"
15
16 @classmethod
17 def verify_install(cls, tools: ToolCache, **kwargs) -> ModuleType:
18 """Verify if git is installed.
19
20 Unfortunately, `import git` triggers a call on the operating system
21 to run the git executable. On some platforms (notably macOS), the git
22 binary has been instrumented such that if git *isn't* installed,
23 running git triggers a prompt to install Xcode. However, that messes
24 with the UX workflow.
25
26 So - we defer importing git until we actually know we need it. This
27 enables Briefcase to start to do other Xcode checks as part of the macOS
28 workflows, and ensures that "briefcase --help" works on other platforms
29 without raising an error.
30
31 :param tools: ToolCache of available tools
32 :returns: The git module, if `git` is installed and available.
33 """
34 # short circuit since already verified and available
35 if hasattr(tools, "git"):
36 return tools.git
37
38 # Check whether the git executable could be imported.
39 try:
40 import git
41 except ImportError as e: # pragma: no cover
42 # macOS provides git as part of the Xcode command line tools,
43 # and also hijacks /usr/bin/git with a trigger that prompts the
44 # installation of those tools. Customize the message to account
45 # for this.
46 if tools.host_os == "Darwin":
47 raise BriefcaseCommandError(
48 f"""\
49 Briefcase requires git, but it is not installed. Xcode provides git; you should
50 be shown a dialog prompting you to install Xcode and the Command Line Developer
51 Tools. Select "Install" to install the Command Line Developer Tools.
52
53 Alternatively, you can visit:
54
55 {cls.GIT_URL}
56
57 to download and install git manually.
58
59 If you have installed git recently and are still getting this error, you may
60 need to restart your terminal session.
61 """
62 ) from e
63
64 else:
65 raise BriefcaseCommandError(
66 f"""\
67 Briefcase requires git, but it is not installed (or is not on your PATH). Visit:
68
69 {cls.GIT_URL}
70
71 to download and install git manually.
72
73 If you have installed git recently and are still getting this error, you may
74 need to restart your terminal session.
75 """
76 ) from e
77
78 installed_version = git.cmd.Git().version_info
79 if installed_version < cls.MIN_VERSION:
80 raise BriefcaseCommandError(
81 f"At least Git v{'.'.join(map(str, cls.MIN_VERSION))} is required; "
82 f"however, v{'.'.join(map(str, installed_version))} is installed.\n"
83 "\n"
84 f"Please update Git; downloads are available at {cls.GIT_URL}.",
85 skip_logfile=True,
86 )
87
88 tools.console.configure_stdlib_logging("git")
89
90 tools.git = git
91 return git