Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

More:git init#487

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
tony wants to merge6 commits intomaster
base:master
Choose a base branch
Loading
frommore-git-init
Open

More:git init#487

tony wants to merge6 commits intomasterfrommore-git-init

Conversation

@tony
Copy link
Member

@tonytony commentedFeb 22, 2025
edited
Loading

Changes

Cmd:Git.init improvements

This PR enhances theGit.init() method with improved parameter validation, comprehensive documentation, and extensive test coverage. Key improvements include:

  1. Enhanced Parameter Support:

    • Added support forref_format parameter (files/reftable)
    • Addeddefault parameter for default permissions
    • Addedmake_parents parameter to control directory creation
    • Expandedshared parameter to support all git-supported values
  2. Improved Type Handling:

    • Enhancedtemplate parameter to accept bothstr andpathlib.Path
    • Added comprehensive validation for all parameters
    • Added proper type hints and validation for shared repository modes
  3. Documentation Improvements:

    • Added detailed parameter descriptions
    • Included comprehensive examples for each parameter
    • Added proper return type and raises documentation
    • Enhanced docstring with real-world usage examples
  4. Validation & Error Handling:

    • Added validation for template directories
    • Added branch name validation
    • Added shared repository mode validation
    • Added proper error messages for invalid inputs

Examples

# Basic repository initializationgit=Git(path="/path/to/repo")git.init()# Create with custom branch namegit.init(branch="main")# Create bare repositorygit.init(bare=True)# Create shared repository with group permissionsgit.init(shared="group")# Create with custom permissionsgit.init(shared="0660")# Create with separate git directorygit.init(separate_git_dir="/path/to/git/dir")# Create with templategit.init(template="/path/to/template")# Create with SHA-256 object format (git >= 2.29.0)git.init(object_format="sha256")

Testing

The PR includes comprehensive test coverage. To run the tests:

# Run all testspytest tests/cmd/test_git.py# Run specific test casespytest tests/cmd/test_git.py::test_git_init_basicpytest tests/cmd/test_git.py::test_git_init_sharedpytest tests/cmd/test_git.py::test_git_init_validation_errors

Validation Tests

The PR includes tests for various validation scenarios:

# Invalid templategit.init(template=123)# Raises TypeError# Non-existent templategit.init(template="/nonexistent")# Raises ValueError# Invalid object formatgit.init(object_format="invalid")# Raises ValueError# Invalid branch namegit.init(branch="main branch")# Raises ValueError# Invalid shared valuegit.init(shared="invalid")# Raises ValueError

Breaking Changes

None. All changes are backward compatible while adding new functionality.

Dependencies

  • No new dependencies required
  • Some features (likeref_format="reftable") require git >= 2.37.0
  • SHA-256 object format requires git >= 2.29.0

Summary by Sourcery

Enhances theGit.init() method with improved parameter validation, comprehensive documentation, and extensive test coverage. It introduces new features such as support forref_format,default, andmake_parents parameters, and enhances existing parameters liketemplate andshared with improved type handling and validation.

New Features:

  • Adds support for theref_format parameter to specify the reference storage format.
  • Adds support for thedefault parameter to use default permissions for directories and files.
  • Adds support for themake_parents parameter to create parent directories if they don't exist.

Enhancements:

  • Enhances thetemplate parameter to accept bothstr andpathlib.Path types.
  • Expands theshared parameter to support all git-supported values, including boolean, string literals, and octal number strings.
  • Improves parameter validation fortemplate,object_format,branch, andshared parameters.
  • Adds type hints and validation for shared repository modes.

Tests:

  • Adds comprehensive test coverage for thegit init command, including tests for various parameters and validation scenarios.

@sourcery-ai
Copy link

sourcery-aibot commentedFeb 22, 2025
edited
Loading

Reviewer's Guide by Sourcery

This PR enhances theGit.init() method with improved parameter validation, comprehensive documentation, and extensive test coverage. It introduces new parameters likeref_format,default, andmake_parents, expands the functionality of theshared parameter, and improves type handling for thetemplate parameter. The changes also include comprehensive validation and error handling, along with detailed documentation and examples.

No diagrams generated as the changes look simple and do not need a visual representation.

File-Level Changes

ChangeDetailsFiles
Enhanced thegit init method with improved parameter validation, comprehensive documentation, and extensive test coverage.
  • Added support forref_format parameter
  • Addeddefault parameter for default permissions
  • Addedmake_parents parameter to control directory creation
  • Expandedshared parameter to support all git-supported values
  • Enhancedtemplate parameter to accept bothstr andpathlib.Path
  • Added comprehensive validation for all parameters
  • Added proper type hints and validation for shared repository modes
  • Added detailed parameter descriptions
  • Included comprehensive examples for each parameter
  • Added proper return type and raises documentation
  • Enhanced docstring with real-world usage examples
  • Added validation for template directories
  • Added branch name validation
  • Added shared repository mode validation
  • Added proper error messages for invalid inputs
tests/cmd/test_git.py
src/libvcs/cmd/git.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment@sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with@sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write@sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write@sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment@sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment@sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment@sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment@sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment@sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access yourdashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@codecov
Copy link

codecovbot commentedFeb 22, 2025
edited
Loading

Codecov Report

Attention: Patch coverage is95.29412% with8 lines in your changes missing coverage. Please review.

Project coverage is 56.30%. Comparing base(0cf3504) to head(a565845).

Files with missing linesPatch %Lines
src/libvcs/cmd/git.py90.47%2 Missing and 2 partials⚠️
tests/cmd/test_git.py96.87%4 Missing⚠️
Additional details and impacted files
@@            Coverage Diff             @@##           master     #487      +/-   ##==========================================+ Coverage   54.05%   56.30%   +2.24%==========================================  Files          40       40                Lines        3637     3799     +162       Branches      794      808      +14     ==========================================+ Hits         1966     2139     +173+ Misses       1319     1306      -13- Partials      352      354       +2

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report?Share it here.

🚀 New features to boost your workflow:
  • ❄️Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link

@sourcery-aisourcery-aibot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Hey@tony - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding a helper function to reduce the duplication in the validation logic for theshared parameter.
  • Theinitial_branch parameter is an alias forbranch, but it's not clear from the code that they are interchangeable - consider explicitly settingbranch = initial_branch.
Here's what I looked at during the review
  • 🟡General issues: 2 issues found
  • 🟢Security: all looks good
  • 🟡Testing: 2 issues found
  • 🟡Complexity: 1 issue found
  • 🟢Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

sourcery-ai[bot] reacted with thumbs up emojisourcery-ai[bot] reacted with thumbs down emoji
Comment on lines +1221 to +1230
ifsharedisnotNone:
valid_shared_values= {
"false",
"true",
"umask",
"group",
"all",
"world",
"everybody",
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

issue (bug_risk): Boolean shared value check appends flag even for false.

The updated code checks if shared is an instance of bool and then unconditionally appends "--shared". This means that if shared is False (explicitly turning off sharing), the flag will still be added. To maintain behavior consistent with earlier logic—where the flag was only added for True—it would be better to explicitly check that shared is True (e.g. usingif shared is True instead).

sourcery-ai[bot] reacted with thumbs up emojisourcery-ai[bot] reacted with thumbs down emoji
"or a valid octal number between 0000 and 0777"
)
raiseValueError(msg)
local_flags.append(f"--shared={shared}")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

suggestion (bug_risk): Consistency in handling non-boolean shared values.

After converting the non-boolean shared value to lower case (stored in shared_str) for validation, the flag is appended using the original shared variable rather than the normalized one. Consider using the lowercased value in constructing the flag to ensure consistent representation of the flag value.

Suggested change
local_flags.append(f"--shared={shared}")
local_flags.append(f"--shared={shared_str}")

sourcery-ai[bot] reacted with thumbs up emojisourcery-ai[bot] reacted with thumbs down emoji
result=repo.init(bare=True)
assert"Initialized empty Git repository"inresult
# Bare repos have files directly in the directory
assert (tmp_path/"HEAD").exists()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

suggestion (testing): Missing assertion for the--bare flag's effect on config.

While the existence ofHEAD is a good indicator, it's not definitive proof of a bare repository. A stronger assertion would be to check thecore.bare config setting within the.git directory. This ensures the repository is truly configured as bare.

Suggested change
assert (tmp_path/"HEAD").exists()
assert (tmp_path/"HEAD").exists()
config_path=tmp_path/"config"
assertconfig_path.exists(),"Config file does not exist in bare repository"
config_text=config_path.read_text()
assert"bare = true"inconfig_text,"Repository core.bare flag not set to true"

sourcery-ai[bot] reacted with thumbs up emojisourcery-ai[bot] reacted with thumbs down emoji
Comment on lines +115 to +126
# Note: sha256 test is commented out as it might not be supported in all
# git versions
# repo_dir = tmp_path / "sha256"
# repo_dir.mkdir()
# repo = git.Git(path=repo_dir)
# result = repo.init(object_format="sha256")
# assert "Initialized empty Git repository" in result

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

suggestion (testing): Handle potential unsupportedsha256.

Instead of commenting out thesha256 test, usepytest.mark.skipif to conditionally skip the test if the current Git version doesn't supportsha256. This provides a cleaner way to handle unsupported features and keeps the test suite more comprehensive.

Suggested implementation:

importpathlibimportgitimportpytestimportsubprocessdefsupports_sha256()->bool:try:output=subprocess.check_output(["git","--version"],universal_newlines=True)version_str=output.strip().split()[2]major,minor,*_=map(int,version_str.split('.'))# SHA256 support is assumed for Git versions 2.29 and above.return (major,minor)>= (2,29)exceptException:returnFalse
deftest_git_init_object_format(tmp_path:pathlib.Path)->None:"""Test git init with different object formats."""repo=git.Git(path=tmp_path)# Test with sha1 (default)result=repo.init(object_format="sha1")assert"Initialized empty Git repository"inresultassertrepo.path==tmp_path
@pytest.mark.skipif(notsupports_sha256(),reason="sha256 not supported by current git version")deftest_git_init_object_format_sha256(tmp_path:pathlib.Path)->None:"""Test git init with sha256 object format."""repo_dir=tmp_path/"sha256"repo_dir.mkdir()repo=git.Git(path=repo_dir)result=repo.init(object_format="sha256")assert"Initialized empty Git repository"inresult

Make sure that the new helper function supports_sha256() and the import for pytest and subprocess do not duplicate existing imports in your project. Adjust the version threshold if your requirements are different.

sourcery-ai[bot] reacted with thumbs up emojisourcery-ai[bot] reacted with thumbs down emoji
)

definit(
self,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

issue (complexity): Consider extracting parameter validation logic into dedicated helper functions to reduce nesting and improve readability.

Consider extracting parameter validation logic into dedicated helper functions to simplify the nesting. For example, you can extract the template and shared validations. This will make theinit method shorter and easier to read without changing its behavior.

Example for extracting template validation:

def_validate_template(template:str|pathlib.Path)->str:ifnotisinstance(template, (str,pathlib.Path)):raiseTypeError("template must be a string or Path")template_path=pathlib.Path(template)ifnottemplate_path.is_dir():raiseValueError(f"template directory does not exist:{template}")returnf"--template={template}"

Example for extracting shared validation:

def_validate_shared(shared:bool|t.Literal["false","true","umask","group","all","world","everybody"]|str|None)->str|None:ifsharedisNone:returnNonevalid_shared_values= {"false","true","umask","group","all","world","everybody"}ifisinstance(shared,bool):return"--shared"ifsharedelseNoneshared_str=str(shared).lower()ifnot (shared_strinvalid_shared_valuesor (shared_str.isdigit()andlen(shared_str)<=4andall(cinstring.octdigitsforcinshared_str)andint(shared_str,8)<=0o777        )    ):raiseValueError(f"Invalid shared value. Must be one of{valid_shared_values} or a valid octal number between 0000 and 0777"        )returnf"--shared={shared}"

Then, in theinit method, you can simplify the code:

iftemplateisnotNone:local_flags.append(_validate_template(template))# ... other flags ...ifsharedisnotNone:shared_flag=_validate_shared(shared)ifshared_flag:local_flags.append(shared_flag)

This refactoring reduces nesting and centralizes validation logic, making it easier to maintain and test.

sourcery-ai[bot] reacted with thumbs up emojisourcery-ai[bot] reacted with thumbs down emoji
Comment on lines +1222 to +1230
valid_shared_values= {
"false",
"true",
"umask",
"group",
"all",
"world",
"everybody",
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

suggestion (code-quality): We've found these issues:

Suggested change
valid_shared_values= {
"false",
"true",
"umask",
"group",
"all",
"world",
"everybody",
}
valid_shared_values= {
"false",
"true",
"umask",
"group",
"all",
"world",
"everybody",
}


Explanation
The quality score for this function is below the quality threshold of 25%.
This score is a combination of the method length, cognitive complexity and working memory.

How can you solve this?

It might be worth refactoring this function to make it shorter and more readable.

  • Reduce the function length by extracting pieces of functionality out into
    their own functions. This is the most important thing you can do - ideally a
    function should be less than 10 lines.
  • Reduce nesting, perhaps by introducing guard clauses to return early.
  • Ensure that variables are tightly scoped, so that code using related concepts
    sits together within the function rather than being scattered.

sourcery-ai[bot] reacted with thumbs up emojisourcery-ai[bot] reacted with thumbs down emoji
- Add support for all git-init options (template, separate_git_dir, object_format, etc.)- Add comprehensive tests for each option- Fix path handling for separate_git_dir- Fix string formatting for bytes paths- Update docstrings with examples for all options
- Enhance Git.init docstrings with detailed parameter descriptions- Add comprehensive examples including SHA-256 object format- Add return value and exception documentation- Improve type hints for shared parameter with Literal types- Add extensive validation tests for all parameters
- Add validation for template parameter type and existence- Add validation for object_format parameter values- Improve type formatting for shared parameter- Complete docstring example output
- Add ref-format parameter support for git init- Add make_parents parameter to control directory creation- Improve type hints and validation for template and shared parameters- Add comprehensive tests for all shared values and octal permissions- Add validation for octal number range in shared parameter
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

1 more reviewer

@sourcery-aisourcery-ai[bot]sourcery-ai[bot] left review comments

Reviewers whose approvals may not affect merge requirements

At least 1 approving review is required to merge this pull request.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

2 participants

@tony

[8]ページ先頭

©2009-2025 Movatter.jp