Skip to content
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

Create a function to return the git-link itself #69

Closed
wants to merge 1 commit into from

Conversation

futuro
Copy link

@futuro futuro commented Apr 16, 2020

git-link is incredibly useful, but it doesn't return the URL it
creates. This introduces a new function which does so, allowing more
tooling to make use of this great feature.

This builds from the conversation in #38 and issue #37.

@sshaw
Copy link
Owner

sshaw commented Apr 17, 2020

Hi @futuro, thanks for the PR.

This is definitely something that's needed. I believe my concern with the other PRs was compatibility in that the signatures and/or return value must be changed in order to have a solid function and that calling git-link or whatever non-interactively should not add to the kill ring.

This certainly avoids that (yay) but what about the rest of the global state? For example, if you're using if for an org capture template then won't all the checks git-link-url is doing on the current buffer produce wrong or non-deterministic results?

@sshaw sshaw mentioned this pull request Apr 17, 2020
@futuro
Copy link
Author

futuro commented Apr 22, 2020

Hey @sshaw,

I'll admit that I'm totally ignorant about global state (and emacs hacking in general), so I don't really have a good answer for this. I'll say that the function which is calling git-link-url is wrapped inside a with-current-buffer, so it's running (from what I understand) inside of the buffer for the file I want the git link for.

I'd welcome any pointers on what to read to understand the global state that's at play, or idiomatic design patterns for issues like this.

@futuro
Copy link
Author

futuro commented May 20, 2020

Thinking about this more, @sshaw is your question about whether git-link-url can be called as a pure function, versus needing to be called inside the buffer you want the link from?

@sshaw
Copy link
Owner

sshaw commented May 20, 2020

Hi @futuro, sorry for the holdup

is your question about whether git-link-url can be called as a pure function, versus needing to be called inside the buffer you want the link from?

Yes, I think that a standalone function should be used. Something like:

(git-link-url file-or-buffer &optional start end remote use-branch)

@sshaw sshaw removed the more info label May 22, 2020
@sshaw sshaw mentioned this pull request Sep 6, 2020
@dotemacs
Copy link

Hi @sshaw,

Thanks for the super useful package!

Is there a blocker for this feature to be merged? Thanks

@sshaw
Copy link
Owner

sshaw commented Sep 16, 2021

Is there a blocker for this feature to be merged? Thanks

It does not implement a standalone version of git-link-url (or similar). In its current form its return value depends on global state (unless I'm missing something?).

@futuro
Copy link
Author

futuro commented Dec 13, 2021

I'll see if I can shake that out today.

@futuro
Copy link
Author

futuro commented Dec 13, 2021

While I sort this out, @sshaw, I've noticed that git-link works in the same way as my change -- my change is just to extract some functionality from git-link -- and that much of git-link depends on global state, such as with an interactive call or within a with-current-buffer body.

My current thought is to have git-link-url contain a with-current-buffer call and have everything else be within that macro, but I'd love your thoughts on this and whether it's violating some kind of elisp design principle.

@sshaw
Copy link
Owner

sshaw commented Dec 14, 2021

Hi,

Thanks for getting back to this!

I've noticed that git-link works in the same way as my change ...
and that much of git-link depends on global state ...

The default case for git-link for interactive use, i.e., get a link of/based on the current buffer's contents and cursor position. This is okay.

My current thought is to have git-link-url contain a with-current-buffer call
... but I'd love your thoughts on this and whether it's violating some kind of elisp design principle.

This is where the problem lies. If it is going to be a legit stand-alone function it must not depend on things like the current buffer, cursor position, etc... Instead of being for interactive use it should have a signature similar to that outlined here and must return the URL and not add it to the kill ring.

I would say this is general principle for functions, etc...: don't depend on global state.

Once we have this baseline function it can be used for all use cases, including the interactive one.

Does that help?

@sshaw
Copy link
Owner

sshaw commented Dec 14, 2021

I would say this is general principle for functions, etc...: don't depend on global state.

Though we can depend on the output of git, which is sorta global state. If we didn't then we'd have more function arguments and I think the function would loose some of its utility then, as it would be forcing the caller to parse and pass in git output.

@futuro
Copy link
Author

futuro commented Dec 16, 2021

@sshaw after reviewing more of the code, I think I'm starting to see the way this could work. One question: what's the goal of the optional arg use-branch?

@sshaw
Copy link
Owner

sshaw commented Dec 16, 2021

... what's the goal of the optional arg use-branch?

That is supposed to be the equivalent of sorts of git-link-use-commit. Currently the default is to generate the link using the branch but time has shown that it's better to use the commit. So I think eventually git-link-use-commit will be git-link-use-branch to better reflect this. This argument is named to reflect that.

@futuro
Copy link
Author

futuro commented Dec 16, 2021

That is supposed to be the equivalent of sorts of git-link-use-commit.

Rockin, that makes sense.

Another question! You'd put the remote as an optional argument, but the function can't work without a specified remote (as far as I understand it, at least). I'm thinking about moving this to be a required argument; am I missing anything?

@futuro
Copy link
Author

futuro commented Dec 16, 2021

Ok @sshaw, this should be ready.

I went ahead with making remote a required argument, but let me know if I missed something and it should be optional.

@futuro
Copy link
Author

futuro commented Dec 16, 2021

J/k, sorry. I need to fix a void-variable reference. Will ping you once it's solid.

@futuro
Copy link
Author

futuro commented Dec 16, 2021

Ok, tested, and should be good to go for review @sshaw.

@sshaw
Copy link
Owner

sshaw commented Dec 22, 2021

Thanks a lot. I will have a looksy at or before Christmas Day 🎅🤶

git-link.el Outdated Show resolved Hide resolved
git-link.el Outdated Show resolved Hide resolved
@sshaw
Copy link
Owner

sshaw commented Dec 25, 2021

Hi, @futuro, lookin' good. Aside from my 2 comments I would squash this PR into a single commit.

Merry Christmas! 🎄

@futuro
Copy link
Author

futuro commented Jan 5, 2022

Hey @sshaw, thanks for the suggestions on user-error! I've never done error signaling/handling in elisp, so that was a fun excursion.

I've just pushed up that change, and it should be ready for review.

Just to clarify, are you asking me to squash all the commits in this PR, or are you saying that you're going to do a squash merge afterwards?

Thanks! And Happy New Year!

@sshaw
Copy link
Owner

sshaw commented Jan 5, 2022

Just to clarify, are you asking me to squash all the commits in this PR, or are you saying that you're going to do a squash merge afterwards?

Squash all commits, please.

@futuro futuro force-pushed the master branch 2 times, most recently from 84da7f4 to 26ca72c Compare January 11, 2022 20:11
@futuro
Copy link
Author

futuro commented Jan 11, 2022

@sshaw commits are squashed and everything should be ready to go.

Copy link
Owner

@sshaw sshaw left a comment

Choose a reason for hiding this comment

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

Hi, thanks for squashing.

I had an in-depth look now and there are a few issues. See comments.

git-link.el Outdated
(if (bufferp file-or-buffer)
(buffer-file-name file-or-buffer)
file-or-buffer)))
(remote-url (git-link--remote-url remote))
Copy link
Owner

Choose a reason for hiding this comment

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

We must check for a nil remote-url first, like we do here: https://github.com/sshaw/git-link/pull/69/files#diff-a86ea92b0e180064b421a637b78c7d5ae152de93c60a82136b77a6a72c90b35cL692
Otherwise we get errors when the remote does not exist.

Copy link
Owner

Choose a reason for hiding this comment

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

A case where the remote does not exist would be trying to link from a buffer that is not part of a git repo.

git-link.el Outdated Show resolved Hide resolved
@@ -666,6 +666,54 @@ return (FILENAME . REVISION) otherwise nil."
(git-link--remote)))

;;;###autoload
(defun git-link-url (file-or-buffer remote &optional start end use-branch-p)
Copy link
Owner

Choose a reason for hiding this comment

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

Some thoughts/issues here:

  1. I think we should default remote to git-link-default-remote.
  2. If the current buffer is in directory X but this is called like (git-link-url "/path/to/Y/foo.txt" "origin" 10), where Y is a different repository root, it should return a link to Y/foo.txt's repo but now it links to something random in repo X.
  3. What to do with invalid line numbers, e.g., when end is after or equal to start, when start is nil and end is given? Thinking we check for these cases and signal an error.

Copy link
Author

@futuro futuro Mar 16, 2022

Choose a reason for hiding this comment

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

I agree with items 1 and 3, but item 2 strikes me as out of scope for this PR. Specifically, my first thought for handling this is to change every function that interacts with git-link--exec such that they can pass a buffer/directory to run from, thus isolating the side-effect of changing our working directory.

Do you see another way of achieving 2 that only impacts git-link-url?

Copy link
Owner

Choose a reason for hiding this comment

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

... but item 2 strikes me as out of scope for this PR.

When calling it as a separate function it cannot depend on state of some buffer. For example, if one creates an emacs package that calls git-link-url the call will depend on the state of some git repo outside the function call so bugs would occur. Make sense?

I agree with items 1 and 3,

Maybe 2/3 is fine? Do they togeather outweigh the bug?

Do you see another way of achieving 2 that only impacts git-link-url?

I think this is part of the reason why this function has taken so long to exist. If I remember correctly you have to change a bit more than meets the eye. It has been since Dec or so since I had a look at the code. Will have to wait to look again. But if you think you gotz a solution then add it. But if you are concerned about your time then maybe wait 'till I can have an in-depth look again.

Copy link
Owner

Choose a reason for hiding this comment

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

Also test coverage not that good. Exhibit A.

`git-link` is incredibly useful, but it doesn't return the URL it
creates. This introduces a new function which does so, allowing more
tooling to make use of this great feature.

Wrapped up in this I did some refactoring to drop the use of `setq`
internally to the function, as that defies the purely functional
nature of the call. This _shouldn't_ break anything.
@sshaw
Copy link
Owner

sshaw commented Jan 8, 2023

Closing to due lack of feedback/progress.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants