-
Notifications
You must be signed in to change notification settings - Fork 28
PROTOTYPE: feat(init): git init features #1149
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
bepri
wants to merge
3
commits into
main
Choose a base branch
from
work/init-git-features/CRAFT-5060
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| # Copyright 2024 Canonical Ltd. | ||
| # Copyright 2026 Canonical Ltd. | ||
| # | ||
| # This program is free software: you can redistribute it and/or modify it | ||
| # under the terms of the GNU Lesser General Public License version 3, as | ||
|
|
@@ -90,7 +90,13 @@ def fill_parser(self, parser: argparse.ArgumentParser) -> None: | |
| f"choices are {humanize_list(self.profiles, 'and')})" | ||
| ), | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "--vcs", | ||
| type=str, | ||
| choices=["git", "none"], | ||
| default="git", | ||
| help="Initialize a version control system.", | ||
| ) | ||
| parser.add_argument( | ||
| "--base", | ||
| type=str, | ||
|
|
@@ -122,6 +128,11 @@ def profiles(self) -> list[str]: | |
| ] | ||
| return sorted([template.name for template in template_dirs]) | ||
|
|
||
| @property | ||
| def vcs_ignore_globs(self) -> list[str]: | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The intention here was to create an easy way to customize what entries appear in a generated gitignore. For example, if Snapcraft overrides this to return |
||
| """A list of globs that should be ignored when creating a .gitignore file.""" | ||
| return [] | ||
|
|
||
| def run(self, parsed_args: argparse.Namespace) -> None: | ||
| """Run the command.""" | ||
| # If the user provided a "name" and it's not valid, the command fails. | ||
|
|
@@ -149,8 +160,59 @@ def run(self, parsed_args: argparse.Namespace) -> None: | |
| project_name=project_name, | ||
| template_dir=template_dir, | ||
| ) | ||
|
|
||
| self.initialize_vcs(parsed_args.vcs, project_dir) | ||
|
|
||
| craft_cli.emit.message("Successfully initialised project.") | ||
|
|
||
| def initialize_vcs( | ||
| self, | ||
| vcs: str, | ||
| project_dir: pathlib.Path, | ||
| ) -> None: | ||
| """Initialize VCS features for a project. | ||
|
|
||
| Currently only supports Git. | ||
|
|
||
| If the Git repository already exists, it will be reused. | ||
|
|
||
| If a `.gitignore` file is already present, Craft-specific content will be | ||
| appended. | ||
| """ | ||
| if vcs == "none": | ||
| return | ||
|
|
||
| craft_cli.emit.debug(f"Setting up VCS in {str(project_dir)!r}.") | ||
|
|
||
| from craft_application.git import GitRepo # noqa: PLC0415 | ||
|
|
||
| _ = GitRepo(project_dir) | ||
|
|
||
| self._create_git_ignore(project_dir) | ||
|
|
||
| def _create_git_ignore(self, project_dir: pathlib.Path) -> None: | ||
| ignore_lines = [ | ||
| f"# Added by {self._app.name.capitalize()}", | ||
| *self.vcs_ignore_globs, | ||
| ] | ||
|
|
||
| # Nothing to ignore | ||
| if len(ignore_lines) == 1: | ||
| return | ||
|
|
||
| ignore_content = "\n".join(ignore_lines) | ||
|
|
||
| ignore = project_dir / ".gitignore" | ||
| ignore_existed = ignore.exists() | ||
|
|
||
| with ignore.open("at") as ignore_f: | ||
| # Assuming the user leaves a trailing newline on their files, this will give a cleanly | ||
| # separated "Added by Craft" section | ||
|
bepri marked this conversation as resolved.
|
||
| if ignore_existed: | ||
| ignore_f.write("\n") | ||
|
|
||
| ignore_f.write(ignore_content) | ||
|
|
||
| def _get_template_dir(self, parsed_args: argparse.Namespace) -> pathlib.Path: | ||
| """Get the template directory for the selected profile and base.""" | ||
| base = getattr(parsed_args, "base", None) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| # Copyright 2026 Canonical Ltd. | ||
| # | ||
| # This program is free software: you can redistribute it and/or modify it | ||
| # under the terms of the GNU Lesser General Public License version 3, as | ||
| # published by the Free Software Foundation. | ||
| # | ||
| # This program is distributed in the hope that it will be useful, but WITHOUT | ||
| # ANY WARRANTY; without even the implied warranties of MERCHANTABILITY, | ||
| # SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. | ||
| # See the GNU Lesser General Public License for more details. | ||
| # | ||
| # You should have received a copy of the GNU Lesser General Public License along | ||
| # with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| """Command to initialize a project.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from craft_application.commands import InitCommand as BaseInitCommand | ||
| from typing_extensions import override | ||
|
|
||
|
|
||
| class InitCommand(BaseInitCommand): | ||
| """Init command override for Testcraft.""" | ||
|
|
||
| @override | ||
| @property | ||
| def vcs_ignore_globs(self) -> list[str]: | ||
| return [*super().vcs_ignore_globs, "/*.test"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| summary: test testcraft init with VCS features | ||
|
|
||
| environment: | ||
| VCS_STYLE/blank: "" | ||
| VCS_STYLE/git: "git" | ||
| VCS_STYLE/none: "none" | ||
|
|
||
| execute: | | ||
| mkdir init-test | ||
| cd init-test | ||
|
|
||
| [ -n "$VCS_STYLE" ] && vcs_flag="--vcs $VCS_STYLE" || vcs_flag="" | ||
| testcraft init $vcs_flag | ||
|
|
||
| # Check that they do exist if using VCS, or that they _don't_ exist if VCS is "none" | ||
| test -d .git; test $? -ne $([ "$VCS_STYLE" = "none" ]; echo $?) | ||
| test -f .gitignore; test $? -ne $([ "$VCS_STYLE" = "none" ]; echo $?) | ||
|
|
||
| restore: | | ||
| rm -rf init-test |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This copies how uv handles it, which is certainly future-proof, but I'm considering doing
--no-vcsas an opt-out flag and removing--vcsas a flag since I doubt we'll support anything other than git any time soon.