Skip to content

local_repo

This module contains functions used to set up a local git repository with ssb-project.

create_project_from_template(project_name, description, template_repo_url, checkout, working_directory, license_year=None, name=None, email=None, override_dir=None)

Creates a project from CookieCutter template.

Parameters:

Name Type Description Default
project_name str

Name of project

required
description str

Project description

required
license_year Optional[str]

Year to be inserted into the LICENSE

None
template_repo_url str

The Cookiecutter template URI.

required
checkout str | None

The git reference to check against. Supports branches, tags and commit hashes.

required
working_directory Path

Working directory

required
name Optional[str]

Optional name of project owner

None
email Optional[str]

Optional email of project owner

None
override_dir Optional[Path]

Used to hard set working_directory.

None

Returns:

Name Type Description
Path Path

Path of project.

Source code in ssb_project_cli/ssb_project/create/local_repo.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
def create_project_from_template(
    project_name: str,
    description: str,
    template_repo_url: str,
    checkout: str | None,
    working_directory: Path,
    license_year: Optional[str] = None,
    name: Optional[str] = None,
    email: Optional[str] = None,
    override_dir: Optional[Path] = None,
) -> Path:
    """Creates a project from CookieCutter template.

    Args:
        project_name: Name of project
        description: Project description
        license_year: Year to be inserted into the LICENSE
        template_repo_url: The Cookiecutter template URI.
        checkout: The git reference to check against. Supports branches, tags and commit hashes.
        working_directory: Working directory
        name: Optional name of project owner
        email: Optional email of project owner
        override_dir: Used to hard set working_directory.

    Returns:
        Path: Path of project.
    """
    if override_dir is None:
        project_dir = working_directory
    else:
        project_dir = override_dir

    if not (name and email):
        name, email = extract_name_email()
        if not (name and email):
            name, email = request_name_email()

    template_info = {
        "project_name": project_name,
        "description": description,
        "full_name": name,
        "email": email,
        "license_year": license_year or str(datetime.now().year),
    }
    cruft.create(
        template_git_url=template_repo_url,
        checkout=checkout,
        output_dir=project_dir,
        no_input=(template_repo_url == STAT_TEMPLATE_REPO_URL),
        extra_context=template_info,
    )
    project_root = project_dir / project_name
    check_and_fix_onprem_source(project_root)
    poetry_update_lockfile_dependencies(project_root)

    return project_dir

extract_name_email()

Grabs email and name from git config.

Returns:

Name Type Description
name str

Value of user.name from git config element

email str

Value of user.email from git config element

Source code in ssb_project_cli/ssb_project/create/local_repo.py
84
85
86
87
88
89
90
91
92
93
def extract_name_email() -> tuple[str, str]:
    """Grabs email and name from git config.

    Returns:
        name: Value of user.name from git config element
        email: Value of user.email from git config element
    """
    name = get_gitconfig_element("user.name")
    email = get_gitconfig_element("user.email")
    return name, email

get_gitconfig_element(element)

Grabs a property from git config.

Parameters:

Name Type Description Default
element str

Name of the git config element retrive

required

Returns:

Name Type Description
str str

Value of git config element

Source code in ssb_project_cli/ssb_project/create/local_repo.py
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
def get_gitconfig_element(element: str) -> str:
    """Grabs a property from git config.

    Args:
        element: Name of the git config element retrive

    Returns:
        str: Value of git config element
    """
    cmd = ["git", "config", "--get", element]
    result = subprocess.run(  # noqa: S603 no untrusted input
        cmd, stdout=subprocess.PIPE, encoding="utf-8"
    )

    return result.stdout.strip()

make_and_init_git_repo(repo_dir)

Makes and pushes a GitHub repository.

Inits a local repository, adds all files and commits.

Parameters:

Name Type Description Default
repo_dir Path

Path to local Repository

required

Returns:

Name Type Description
Repo Repo

Repository

Source code in ssb_project_cli/ssb_project/create/local_repo.py
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
def make_and_init_git_repo(repo_dir: Path) -> Repo:
    """Makes and pushes a GitHub repository.

    Inits a local repository, adds all files and commits.

    Args:
        repo_dir: Path to local Repository

    Returns:
        Repo: Repository
    """
    repo = Repo.init(repo_dir)
    repo.git.add("-A")
    repo.index.commit("Initial commit")
    repo.git.branch("-M", "main")
    return repo

make_git_repo_and_push(github_token, github_url, repo_dir)

Makes and pushes a GitHub repository.

Inits a local repository and tries to push it to GitHub, for more information see TempGitRemote.

Parameters:

Name Type Description Default
github_token str

GitHub personal access token

required
github_url str

Repository url

required
repo_dir Path

Path to local Repository

required
Source code in ssb_project_cli/ssb_project/create/local_repo.py
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
def make_git_repo_and_push(github_token: str, github_url: str, repo_dir: Path) -> None:
    """Makes and pushes a GitHub repository.

    Inits a local repository and tries to push it to GitHub,
     for more information see TempGitRemote.

    Args:
        github_token: GitHub personal access token
        github_url: Repository url
        repo_dir: Path to local Repository
    """
    repo = make_and_init_git_repo(repo_dir)

    github_username = get_github_username(
        get_environment_specific_github_object(github_token), github_token
    )
    credential_url = mangle_url(github_url, github_token)
    username_url = mangle_url(github_url, github_username)

    with temp_git_repo.TempGitRemote(repo, credential_url, username_url):
        repo.git.push("--set-upstream", "origin", "main")

mangle_url(url, mangle_name)

Create url mangled with a string: credential or username.

Source code in ssb_project_cli/ssb_project/create/local_repo.py
154
155
156
157
158
def mangle_url(url: str, mangle_name: str) -> str:
    """Create url mangled with a string: credential or username."""
    mangle_name = mangle_name + "@"
    split_index = url.find("//") + 2
    return url[:split_index] + mangle_name + url[split_index:]

reset_project_git_configuration(project_name, template_repo_url, checkout, project_directory)

Overrides .gitattributes and .gitignore inn a given project directory.

Parameters:

Name Type Description Default
project_name str

Name of project.

required
template_repo_url str

URL for the chosen template.

required
checkout str | None

The git reference to check against. Supports branches, tags and commit hashes.

required
project_directory Path

Directory of the project.

required
Source code in ssb_project_cli/ssb_project/create/local_repo.py
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
def reset_project_git_configuration(
    project_name: str,
    template_repo_url: str,
    checkout: str | None,
    project_directory: Path,
) -> None:
    """Overrides .gitattributes and .gitignore inn a given project directory.

    Args:
        project_name: Name of project.
        template_repo_url: URL for the chosen template.
        checkout: The git reference to check against. Supports branches, tags and commit hashes.
        project_directory: Directory of the project.
    """
    files = [".gitattributes", ".gitignore"]
    try:
        with tempfile.TemporaryDirectory() as tempdir:
            create_project_from_template(
                project_name,
                "",
                template_repo_url,
                checkout,
                Path(tempdir),
                name=None,
                email=None,
                override_dir=Path(tempdir),
            )
            for file in files:
                shutil.copy(
                    tempdir / Path(project_name) / Path(file),
                    project_directory / Path(file),
                )
    except Exception as e:
        print(":x:\tCould not restore .gitattributes .gitignore.")
        create_error_log(f"{e}", "reset_project_git_configuration")
    print(":white_check_mark:\tRestored recommended .gitattributes .gitignore.")