-
Notifications
You must be signed in to change notification settings - Fork 323
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
Load gitignore file #1273
Load gitignore file #1273
Conversation
Thanks for submitting your first pull request! You are awesome! 🤗 |
@kentarolim10 the main branch has been updated to JupyterLab 4 - to ease the work here, I changed the target to the branch |
Great, thank you so much.
Kentaro
…On Tue, Oct 24, 2023 at 6:32 AM Frédéric Collonval ***@***.***> wrote:
@kentarolim10 <https://github.com/kentarolim10> the main branch has been
updated to JupyterLab 4 - to ease the work here, I changed the target to
the branch jlab-3 to avoid complex rebasing.
—
Reply to this email directly, view it on GitHub
<#1273 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AOXIKUG7DRGS6IOVCUUWERTYA67P3AVCNFSM6AAAAAA5WIXW4SVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONZXGIYTGOBXG4>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
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.
Thanks @kentarolim10
This looks really good. I have only one large concern about the detection if .gitignore is accessible and how to get its content; we should try to split the code in more limited feature. Let me know if my proposal for improvement is not clear.
jupyterlab_git/git.py
Outdated
""" | ||
try: | ||
file = pathlib.Path(path) | ||
if file.stat().st_size > 0: |
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.
Is there a specific reason for testing this?
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.
I just wanted to check if the file was empty and then return an empty string if it was. I guess since if the file is empty, then it would be an empty string regardless. I will remove it.
jupyterlab_git/git.py
Outdated
@@ -1681,6 +1681,22 @@ async def remote_remove(self, path, name): | |||
|
|||
return response | |||
|
|||
async def read_file(self, path): |
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 is actually not asynchronous 😉
async def read_file(self, path): | |
def read_file(self, path): |
jupyterlab_git/git.py
Outdated
@@ -1721,6 +1737,32 @@ async def ignore(self, path, file_path): | |||
return {"code": -1, "message": str(error)} | |||
return {"code": 0} | |||
|
|||
async def writeGitignore(self, path, content): |
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.
Let's be consistent with the naming of Python (even if I find myself often bitten by this one too):
async def writeGitignore(self, path, content): | |
async def write_gitignore(self, path, content): |
jupyterlab_git/git.py
Outdated
with gitignore.open("a") as f: | ||
f.truncate(0) | ||
f.seek(0) | ||
f.write(content) | ||
if content[-1] != "\n": | ||
f.write("\n") |
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.
You could write directly the file content:
with gitignore.open("a") as f: | |
f.truncate(0) | |
f.seek(0) | |
f.write(content) | |
if content[-1] != "\n": | |
f.write("\n") | |
if content and content[-1] != "\n": | |
content += "\n" | |
gitignore.write_text(content) |
See the doc for more info: https://docs.python.org/3/library/pathlib.html#pathlib.Path.write_text
src/commandsAndMenu.tsx
Outdated
editor.disposed.connect(() => { | ||
model.dispose(); | ||
}); | ||
const preview = new PreviewMainAreaWidget({ |
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.
Let's use a MainAreaWidget
directly here as the intention is to edit the file content:
const preview = new PreviewMainAreaWidget({ | |
const preview = new MainAreaWidget({ |
src/git.ts
Outdated
if (response.status === 403) { | ||
throw new Git.HiddenFile(data.content); | ||
} |
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.
We unfortunately gonna need to change the logic because this is not robust (we may get a 403 code for other reasons).
src/commandsAndMenu.tsx
Outdated
await showGitignore(error); | ||
} else { | ||
await showGitignoreHiddenFile(error); |
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.
Could you change a bit the signature of showGitignoreHiddenFile
to take a second optional argument that will decide if the user should be prompted with a dialog or not? showGitignore
will therefore be an implementation detail of showGitignoreHiddenFile
; the goal is to reduce the API between the various part of the code.
jupyterlab_git/handlers.py
Outdated
if not self.serverapp.contents_manager.allow_hidden and not content: | ||
self.set_status(403, "hidden files cannot be accessed") | ||
body["content"] = await self.git.read_file(local_path + "/.gitignore") |
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.
We need to change the logic because that code is increasing the complexity of the endpoint and returns the file content in any case even if the user wants to respect the configuration of not accessing the hidden file.
src/tokens.ts
Outdated
content: string; | ||
constructor(content: string) { | ||
super('File is hidden'); | ||
this.name = 'hiddenFile'; | ||
this.message = 'File is hidden and cannot be accessed.'; | ||
this.content = content; |
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.
content
will not be needed
content: string; | |
constructor(content: string) { | |
super('File is hidden'); | |
this.name = 'hiddenFile'; | |
this.message = 'File is hidden and cannot be accessed.'; | |
this.content = content; | |
constructor(content: string) { | |
super('File is hidden'); | |
this.name = 'hiddenFile'; | |
this.message = 'File is hidden and cannot be accessed.'; |
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.
Almost done; would you mind addressing the new comment and that one?
src/model.ts
Outdated
async readGitIgnore(): Promise<{ code: number; content: string }> { | ||
const path = await this._getPathRepository(); | ||
|
||
const res = (await requestAPI(URLExt.join(path, 'ignore'), 'GET')) as { | ||
code: number; | ||
content: string; | ||
}; | ||
await this.refreshStatus(); | ||
return res; |
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.
Let's make the method more useful by returning directly the content. And we don't need to refresh the status as it does not change the gitignore file.
async readGitIgnore(): Promise<{ code: number; content: string }> { | |
const path = await this._getPathRepository(); | |
const res = (await requestAPI(URLExt.join(path, 'ignore'), 'GET')) as { | |
code: number; | |
content: string; | |
}; | |
await this.refreshStatus(); | |
return res; | |
async readGitIgnore(): Promise<string> { | |
const path = await this._getPathRepository(); | |
return (await requestAPI(URLExt.join(path, 'ignore'), 'GET')).content; |
almost there @kentarolim10 You only need to fix (it is the consequence of the API returning now directly the gitignore content): ERROR: src/commandsAndMenu.tsx:329:45 - error TS2339: Property 'content' does not exist on type 'string'.
329 model.sharedModel.setSource(contentData.content ? contentData.content : '');
~~~~~~~
src/commandsAndMenu.tsx:329:67 - error TS2339: Property 'content' does not exist on type 'string'.
329 model.sharedModel.setSource(contentData.content ? contentData.content : ''); |
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.
Thanks a lot @kentarolim10
Let's merge 😉
@meeseeksdev please backport to main |
Owee, I'm MrMeeseeks, Look at me. There seem to be a conflict, please backport manually. Here are approximate instructions:
And apply the correct labels and milestones. Congratulations — you did some good work! Hopefully your backport PR will be tested by the continuous integration and merged soon! Remember to remove the If these instructions are inaccurate, feel free to suggest an improvement. |
@all-contributors please add @kentarolim10 for code |
I've put up a pull request to add @kentarolim10! 🎉 |
* Throw 403 if file is hidden * Add Helpful GUI message for hidden files * Fetch file contents if hidden file * Show hidden file in widget * Format gitignore file * Show hidden file message when adding file to .gitignore * Add ability to save .gitignore * Fix bug where you can open two .gitignore files * Add option to hide hidden file warning * Fix PR requests for gitignore bug * Fix prettier styles for gitignore * Improve translation * Improve gitignore model and add hiddenFile option to schema * Fix eslint * Fix .gitignore content sending --------- Co-authored-by: Frédéric Collonval <[email protected]> (cherry picked from commit 23fa764)
* Load gitignore file (#1273) * Throw 403 if file is hidden * Add Helpful GUI message for hidden files * Fetch file contents if hidden file * Show hidden file in widget * Format gitignore file * Show hidden file message when adding file to .gitignore * Add ability to save .gitignore * Fix bug where you can open two .gitignore files * Add option to hide hidden file warning * Fix PR requests for gitignore bug * Fix prettier styles for gitignore * Improve translation * Improve gitignore model and add hiddenFile option to schema * Fix eslint * Fix .gitignore content sending --------- Co-authored-by: Frédéric Collonval <[email protected]> (cherry picked from commit 23fa764) * Fix tests --------- Co-authored-by: Kentaro Lim <[email protected]>
Description
-Throws a 403 if the user tries to open a .gitignore file but the file is hidden
-Displays a helpful message showing how to enable hidden files
-Adds checkbox to not show helpful message in the feature
-Allow user to open, view and edit the .gitignore file
Temporary fix for the opening the .gitignore file in #1168