-
Notifications
You must be signed in to change notification settings - Fork 882
feat(but): add but open command
#12749
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
slarse
wants to merge
1
commit into
master
Choose a base branch
from
but-open
base: master
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
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 |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ pub enum CommandName { | |
| Rub, | ||
| Diff, | ||
| Edit, | ||
| Open, | ||
| Show, | ||
| Commit, | ||
| CommitEmpty, | ||
|
|
||
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,33 @@ | ||
| use anyhow::{Result, bail}; | ||
| use but_ctx::Context; | ||
|
|
||
| use crate::{CliId, IdMap, tui::get_text}; | ||
|
|
||
| pub(crate) fn open_target(ctx: &mut Context, target: &str) -> Result<()> { | ||
| let id_map = IdMap::new_from_context(ctx, None)?; | ||
| let cli_ids = id_map.parse_using_context(target, ctx)?; | ||
| if cli_ids.is_empty() { | ||
| bail!("ID '{target}' not found") | ||
| } | ||
|
|
||
| if cli_ids.len() > 1 { | ||
| bail!( | ||
| "ID '{target}' is ambiguous. Found {} matches", | ||
| cli_ids.len() | ||
| ) | ||
| } | ||
|
|
||
| let cli_id = &cli_ids[0]; | ||
|
|
||
| match cli_id { | ||
| CliId::Uncommitted(uncommitted_id) => { | ||
| if !uncommitted_id.is_entire_file { | ||
| bail!("Cannot open part of file") | ||
| } | ||
|
|
||
| let path = &uncommitted_id.hunk_assignments.head.path; | ||
| get_text::launch_editor(path.as_ref()) | ||
| } | ||
| _ => bail!("Can only open uncommitted files"), | ||
| } | ||
| } |
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.
Since
path_safe_intentis so special, could it be explained in the doc string? Also, I am trying to establish a pattern where every parameter is named in the doc-string, soThe editor commandwould probably be ``editor_cmdis….Uh oh!
There was an error while loading. Please reload this page.
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 haven't thoroughly gone through the code yet so docs are not entirely accurate.
But this highlights something important that I've been thinking about: the path passed in here is not safe. It's an arbitrary path from the repository. It could be a file called
$(rm dad-jokes.md)for example. Very much not a safe path, the intent is entirely unknown, and I've been trying to inject badness here but without success.From what I can tell in
gix, any single added.arg()is passed separately to to exec, so it seems fine?I'll address the docs before trying to get this merged, just want a sanity check on using a potentially unsafe path as input here.
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.
Maybe this tells us that we'd want to have a path which proves that a path is inside the worktree (and probably not inside
.git).Git actually has the notion of a
prefix, which is the subdirectory the user's CWD is in compared to the worktree. It is used to normalise input paths to be worktree-relative, and to denormalise (?) output paths to once again be relative to the prefix, i.e. the user's CWD.Also, to be clear about the "let's have the best-possible docs" directive: this is based on thinking that the non-command parts of the
butCLI are like its own little SDK, so it should be very re-usable and thus well documented. Much like thebut-apior the plumbing crates, which should all excel in helping to use it correctly with docs (and types). Or said differently: docs capture the expert knowledge that isn't obvious when looking at the code, and that will soon be gone once the human moved on.Uh oh!
There was an error while loading. Please reload this page.
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.
Perhaps, or perhaps we want a path that is provably accessible to GitButler given the context of the current repository. Excluding the changes I've made here, none of the paths we open in this module are actually relative to the worktree, they're all temporary files (that would usually end up in
/tmp, but it's fundamentally up to the OS). So that doesn't work if we don't change that mechanism. I believe Git always opens its temporary files in the.git/directory (e.g..git/COMMIT_EDITMSG), and doesn't make use of temporary files the way we do. But maybe we should do the same there and create temporary files in.git/gitbutler. At least macOS and Linux allow us to specify the directoryI'm really aching for the kind of functionality that
but openwould provide, and it can be further expanded with all kinds of interesting stuff, but we need to think through the security implications carefully. I think I'll finish up the basic implementation and then we'll take it from there, I'm sure we can make it secure and useful all in one big lovely package.I'm not opposed to any of that. I would probably delimit that effort to public functions, however. High-quality docs are harder to maintain than code as they cannot be automatically checked (although LLMs do help a bit, but it's far from bullet proof).
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!
While type-safety around this is nothing I'd even suggest to tackle here, it's something I always eyed but never accomplished. Maybe one day.
Regarding docs, while this one isn't public, it still triggered this conversation about that path parameter, which is clear sign that expanding the docs to contain this expert knowledge should be worthwhile.