Debian Codegraph @

azure_functions_devops_build/repository/local_git_utils.py

raw
1 # --------------------------------------------------------------------------------------------
2 # Copyright (c) Microsoft Corporation. All rights reserved.
3 # Licensed under the MIT License. See License.txt in the project root for license information.
4 # --------------------------------------------------------------------------------------------
5 import os
6 import re
7
8 # Backward Compatible with Python 2.7
9 try:
10 from subprocess import DEVNULL
11 except ImportError:
12 DEVNULL = open(os.devnull, 'w')
13 from subprocess import STDOUT, check_call, check_output, CalledProcessError
14 from ..exceptions import GitOperationException
15
16
17 def does_git_exist():
18 try:
19 check_call("git", stdout=DEVNULL, stderr=STDOUT)
20 except CalledProcessError as cpe:
21 # Git will return exit code 1 when it exist
22 return cpe.returncode == 1
23 except FileNotFoundError:
24 return False
25 return True
26
27
28 def does_local_git_repository_exist():
29 return os.path.exists(".git")
30
31
32 def does_git_remote_exist(remote_name):
33 command = ["git", "remote", "show"]
34 try:
35 output = check_output(command).decode("utf-8").split("\n")
36 except CalledProcessError:
37 raise GitOperationException(message=" ".join(command))
38
39 return remote_name in output
40
41
42 def does_git_has_credential_manager():
43 command = ["git", "config", "--list"]
44 try:
45 output = check_output(command).decode("utf-8").split("\n")
46 except CalledProcessError:
47 raise GitOperationException(message=" ".join(command))
48
49 return "credential.helper=manager" in output
50
51
52 def git_init():
53 command = ["git", "init"]
54 try:
55 check_call(command, stdout=DEVNULL, stderr=STDOUT)
56 except CalledProcessError:
57 raise GitOperationException(message=" ".join(command))
58
59
60 def git_add_remote(remote_name, remote_url):
61 command = ["git", "remote", "add", remote_name, remote_url]
62 try:
63 check_call(command, stdout=DEVNULL, stderr=STDOUT)
64 except CalledProcessError:
65 raise GitOperationException(message=" ".join(command))
66
67
68 def git_remove_remote(remote_name):
69 command = ["git", "remote", "remove", remote_name]
70 try:
71 check_call(command, stdout=DEVNULL, stderr=STDOUT)
72 except CalledProcessError:
73 raise GitOperationException(message=" ".join(command))
74
75
76 def git_stage_all():
77 command = ["git", "add", "--all"]
78 try:
79 check_call(command, stdout=DEVNULL, stderr=STDOUT)
80 except CalledProcessError:
81 raise GitOperationException(message=" ".join(command))
82
83
84 def git_commit(message):
85 command = ["git", "commit", "--allow-empty", "--message", message]
86 try:
87 check_call(command, stdout=DEVNULL, stderr=STDOUT)
88 except CalledProcessError:
89 raise GitOperationException(message=" ".join(command))
90
91
92 def git_push(remote_name, force=False):
93 command = ["git", "push", remote_name, "--all"]
94
95 if force:
96 command.append("--force")
97
98 try:
99 check_call(command, stdout=DEVNULL, stderr=STDOUT)
100 except CalledProcessError:
101 raise GitOperationException(message=" ".join(command))
102
103
104 def _sanitize_git_remote_name(organization_name, project_name, repository_name):
105 concatenated_remote_name = "{organization_name}_{project_name}_{repository_name}".format(
106 organization_name=organization_name,
107 project_name=project_name,
108 repository_name=repository_name
109 )
110 sanitized_remote_name = re.sub(r"[^A-Za-z0-9_-]|\s", "-", concatenated_remote_name)
111 return sanitized_remote_name
112
113
114 def construct_git_remote_name(organization_name, project_name, repository_name, remote_prefix):
115 remote_name = "_{prefix}_{name}".format(
116 prefix=remote_prefix,
117 name=_sanitize_git_remote_name(organization_name, project_name, repository_name)
118 )
119 return remote_name
120
121
122 def construct_git_remote_url(organization_name, project_name, repository_name, domain_name="dev.azure.com"):
123 url = "https://{domain}/{org}/{proj}/_git/{repo}".format(
124 domain=domain_name,
125 org=organization_name,
126 proj=project_name,
127 repo=repository_name
128 )
129 return url