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

add option to have http links #115

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ Name of the remote branch to link to, defaults to the current branch.
If `t` also open the link via `browse-url`. To use an alternate function set to
that function's symbol. Defaults to `nil`.

##### `git-link-http-link`

If `t` use http instead of https in the link. Defaults to `nil`.

##### `git-link-use-commit`

If non-`nil` use the latest commit's hash in the link instead of the branch name, defaults to `nil`.
Expand Down Expand Up @@ -109,7 +113,7 @@ by [setting `sourcegraph` as the default remote](#settings).
**Note** that the remote can be named anything but its URL's host must match what's in the associated link function's alist.
This defaults to `"sourcegraph"` but can be changed. See [Building Links and Adding Services](#building-links-and-adding-services).

URLs with ports or an http scheme will not work. It's a trivial fix so if it's a problem for you please open an issue.
URLs with ports will not work. It's a trivial fix so if it's a problem for you please open an issue.

### Building Links and Adding Services

Expand Down
16 changes: 16 additions & 0 deletions git-link-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,19 @@

(should (equal '("go.googlesource.com" "go")
(git-link--parse-remote "https://go.googlesource.com/go"))))

(ert-deftest git-link--parse-remote-http-test ()
;; test https (defaults)
(should (equal "https"
(git-link--link-type)))

(should (equal "https://hostname/dirname/blob/branch/filename#L7-L11"
(git-link-github "hostname" "dirname" "filename" "branch" "commit" 7 11)))

;; test http
(setq git-link-http-link t)
(should (equal "http"
(git-link--link-type)))

(should (equal "http://hostname/dirname/blob/branch/filename#L7-L11"
(git-link-github "hostname" "dirname" "filename" "branch" "commit" 7 11))))
80 changes: 56 additions & 24 deletions git-link.el
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,11 @@ function set to that function's symbol."
:type '(choice boolean function)
:group 'git-link)

(defcustom git-link-http-link nil
"If t use http instead of https in the link."
:type '(choice boolean function)
:group 'git-link)

(defcustom git-link-use-commit nil
"If non-nil use the latest commit's hash in the link instead of the branch name."
:type 'boolean
Expand Down Expand Up @@ -286,6 +291,11 @@ we can prevent that behaviour."
:type 'list
:group 'git-link)

(defun git-link--link-type ()
(if git-link-http-link
"http"
"https"))

(defun git-link--exec(&rest args)
(ignore-errors
(with-temp-buffer
Expand Down Expand Up @@ -527,7 +537,8 @@ return (FILENAME . REVISION) otherwise nil."
(browse-url link))))

(defun git-link-codeberg (hostname dirname filename branch commit start end)
(format "https://%s/%s/src/%s/%s"
(format "%s://%s/%s/src/%s/%s"
(git-link--link-type)
hostname
dirname
(or branch commit)
Expand All @@ -539,7 +550,8 @@ return (FILENAME . REVISION) otherwise nil."
(format "L%s" start)))))))

(defun git-link-gitlab (hostname dirname filename branch commit start end)
(format "https://%s/%s/-/blob/%s/%s"
(format "%s://%s/%s/-/blob/%s/%s"
(git-link--link-type)
hostname
dirname
(or branch commit)
Expand All @@ -551,7 +563,8 @@ return (FILENAME . REVISION) otherwise nil."
(format "L%s" start)))))))

(defun git-link-github (hostname dirname filename branch commit start end)
(format "https://%s/%s/blob/%s/%s"
(format "%s://%s/%s/blob/%s/%s"
Copy link
Owner

Choose a reason for hiding this comment

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

Hi, thanks for the PR.

When would github ever use an http scheme?

You mentioned in #114 that you only use this for custom gitlab. For that case couldn't you use:

(eval-after-load 'git-link
 '(progn
   (add-to-list 'git-link-remote-alist
     '("mygitlabhost git-link-gitlab))

Certainly not opposed to this feature, but if there's not an http use case for the service, why add the code for the world to maintain? And if there is, what is upside to this over git-link-remote-alist addition above (Aside from being terser 😉)?

Copy link
Author

Choose a reason for hiding this comment

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

Indeed, my implementation is overly-simplistic and handles all hosts in the same way (regardless of the fact that the feature makes little sense when it comes to some of them). Point taken.

To be concrete, your suggestion (as far as I understand it) would look like:

(use-package git-link
  :config
  (eval-after-load 'git-link
    '(progn
       (add-to-list
	'git-link-remote-alist
	'("mygitlabhost"
	  (lambda (hostname dirname filename branch commit start end)
	    (format "http://%s/%s/-/blob/%s/%s"
		    hostname
		    dirname
		    (or branch commit)
		    (concat filename
			    (when start
			      (concat "#"
				      (if end
					  (format "L%s-%s" start end)
					(format "L%s" start))))))))))))

Sure, this achieves the desired effect, but I will have to maintain in my configuration a very explicit implementation of something that should be rather an internal detail (and I would have to make sure to keep it in sync as your library evolves). Furthermore, I would have to do a similar thing for git-link-commit-gitlab (quite verbose).

In this respect, my "advice" hack in #114 seems simpler to support (even though it relies on git-link--new).

Let me know if you have another suggestion. If not, I can live with the current state of things (all this seems kind of a detail).

Thanks for the support.

Copy link
Owner

Choose a reason for hiding this comment

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

Yes I see, still have to rewrite function due to hardcoding https. Not nice.

As is, if one wanted http on GitLab they'd also get it for GitHub :octocat: etc... right? It may work but I can see someone opening an issue X months later for this "bug". Any ideas on how to do it per service?

Maybe we could use local repo setting like we do for other things: git-link.scheme

This looks best because you can still have http for local GitLab and https for others.

Maybe we can make scheme last argument to callback and override:

(use-package git-link
  :config
  (eval-after-load 'git-link
    '(progn
       (add-to-list
	'git-link-remote-alist
	'("mygitlabhost"
          (lambda (hostname dirname filename branch commit start end) (git-link-gitlab hostname dirname filename branch commit start end "http"))))

Still not that clean.

Thoughts?

Copy link
Owner

Choose a reason for hiding this comment

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

      (lambda (hostname dirname filename branch commit start end) (git-link-gitlab hostname dirname filename branch commit start end "http"))))

Slightly cleaner with &rest argument to lambda instead.

Copy link
Author

Choose a reason for hiding this comment

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

I agree with "... opening an issue X months later for this "bug" ..."

Your suggestions goes in the right direction. It boils down to:

(use-package git-link
  :config
  (eval-after-load 'git-link
    (cl-loop
     for (alist . function) in '((git-link-remote-alist          . git-link-gitlab)
				 (git-link-commit-remote-alist   . git-link-commit-gitlab)
				 (git-link-homepage-remote-alist . git-link-homepage-github))
     do (add-to-list
	 alist
	 `("mygitlabhost"
	   (lambda (&rest args) (apply #',function (append args '("http")))))))))

This requires to add &optional scheme to the arguments of git-link-gitlab, git-link-commit-gitlab and git-link-homepage-github and in the format statement to use e.g.,

(format "%s://%s/%s/-/blob/%s/%s"
    (or scheme "https")
    ...

Note that I had to use git-link-homepage-github because you reuse it in almost all entries in git-link-homepage-remote-alist so probably we have to define a dedicated function git-link-homepage-gitlab and I think that it would be good to rename git-link-homepage-github to something like git-link-homepage-common.

I am not sure how using git-link.scheme would help. For me, having to add properties repository by repository to get a features like this is inconvenient.

Having said all this, as a user, I would expect to have to do something like:

(use-package git-link
  :config
  (git-link-register-gitlab-host "mygitlabhost" "http"))

where the library itself provides something like:

(defun git-link-register-gitlab-host (host-name &optional scheme)
  (eval-after-load 'git-link
    (cl-loop
     for (alist . function) in '((git-link-remote-alist          . git-link-gitlab)
				 (git-link-commit-remote-alist   . git-link-commit-gitlab)
				 (git-link-homepage-remote-alist . git-link-homepage-github))
     do (add-to-list
	 alist
	 `(,host-name
	   (lambda (&rest args) (apply #',function (append args (when ,scheme '(,scheme))))))))))	   

but as an interface this looks strange (we handle one case - gitlab).

Copy link
Owner

Choose a reason for hiding this comment

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

I am not sure how using git-link.scheme would help. For me, having to add properties repository by repository to get a features like this is inconvenient.

We want to avoid setting git-link-http-link for custom GitLab, causing http to get used for normal GitLab, GitHub, etc... This is a per-repo setting. git-link.scheme is per-repo.

On the elisp side, what's the repo/project version of a buffer-local variable? I don't think they exist.

Having said all this, as a user, I would expect to have to do something like:

(use-package git-link
:config
(git-link-register-gitlab-host "mygitlabhost" "http"))

Is this any more inconvenient than git-link.scheme? This package is 10 years old and this is the first time someone has needed it (at least publicly). http is the exception case. How often will one truly be inconvenienced by this?

Copy link
Author

Choose a reason for hiding this comment

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

On the elisp side, what's the repo/project version of a buffer-local variable? I don't think they exist.

What do you mean?

We want to avoid setting git-link-http-link for custom GitLab, causing http to get used for normal GitLab, GitHub, etc... This is a per-repo setting. git-link.scheme is per-repo.

I don't insist about any of this - I just share my opinion. For me the most convenient way to resolve this is to define "properties" per host (and not per project). Doing something per project wouldn't fit my workflow (but this is only me, I guess).

This package is 10 years old and this is the first time someone has needed it

I can live with my own hack (there is no problem).

Copy link
Owner

Choose a reason for hiding this comment

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

On the elisp side, what's the repo/project version of a buffer-local variable? I don't think they exist.

What do you mean?

The http/https setting is per project, right? In the current PR setting git-link-http-link will set it globally. As far as I know there's no way to do this via Emacs Lisp using a variable. There's buffer-local which kinda works but buffer scope is way too narrow.

I don't insist about any of this - I just share my opinion. For me the most convenient way to resolve this is to define "properties" per host (and not per project). Doing something per project wouldn't fit my workflow (but this is only me, I guess).

To me it seems very simple for current PR to replace git-link-http-link check with git-link.scheme git config check. The property approach is broader than that, and may even be functionality on top of this. Let me take a look at your config a bit more to make sure I understand.

(git-link--link-type)
hostname
dirname
(or branch commit)
Expand All @@ -563,7 +576,8 @@ return (FILENAME . REVISION) otherwise nil."
(format "L%s" start)))))))

(defun git-link-googlesource (hostname dirname filename branch commit start end)
(format "https://%s/%s/+/%s/%s"
(format "%s://%s/%s/+/%s/%s"
(git-link--link-type)
hostname
dirname
(or branch commit)
Expand All @@ -573,7 +587,8 @@ return (FILENAME . REVISION) otherwise nil."
))))

(defun git-link-azure (hostname dirname filename branch commit start end)
(format "https://%s/%s?path=%%2F%s&version=%s&line=%s&lineEnd=%s&lineStartColumn=1&lineEndColumn=9999&lineStyle=plain"
(format "%s://%s/%s?path=%%2F%s&version=%s&line=%s&lineEnd=%s&lineStartColumn=1&lineEndColumn=9999&lineStyle=plain"
(git-link--link-type)
hostname
dirname
filename
Expand All @@ -582,7 +597,8 @@ return (FILENAME . REVISION) otherwise nil."
(or end start "")))

(defun git-link-sourcehut (hostname dirname filename branch commit start end)
(format "https://%s/%s/tree/%s/%s"
(format "%s://%s/%s/tree/%s/%s"
(git-link--link-type)
hostname
dirname
(or branch commit)
Expand All @@ -594,54 +610,62 @@ return (FILENAME . REVISION) otherwise nil."
(format "L%s" start)))))))

(defun git-link-commit-gitlab (hostname dirname commit)
(format "https://%s/%s/-/commit/%s"
(format "%s://%s/%s/-/commit/%s"
(git-link--link-type)
hostname
dirname
commit))

(defun git-link-commit-github (hostname dirname commit)
(format "https://%s/%s/commit/%s"
(format "%s://%s/%s/commit/%s"
(git-link--link-type)
hostname
dirname
commit))

(defun git-link-commit-googlesource (hostname dirname commit)
(format "https://%s/%s/+/%s"
(format "%s://%s/%s/+/%s"
(git-link--link-type)
hostname
dirname
commit))

(defun git-link-commit-azure (hostname dirname commit)
(format "https://%s/%s/commit/%s"
(format "%s://%s/%s/commit/%s"
(git-link--link-type)
hostname
dirname

;; Azure only supports full 32 characters SHA
(car (git-link--exec "rev-parse" commit))))

(defun git-link-commit-codeberg (hostname dirname commit)
(format "https://%s/%s/commit/%s"
(format "%s://%s/%s/commit/%s"
(git-link--link-type)
hostname
dirname
commit))

(defun git-link-gitorious (hostname dirname filename _branch commit start _end)
(format "https://%s/%s/source/%s:%s#L%s"
(format "%s://%s/%s/source/%s:%s#L%s"
(git-link--link-type)
hostname
dirname
commit
filename
start))

(defun git-link-commit-gitorious (hostname dirname commit)
(format "https://%s/%s/commit/%s"
(format "%s://%s/%s/commit/%s"
(git-link--link-type)
hostname
dirname
commit))

(defun git-link-bitbucket (hostname dirname filename _branch commit start end)
;; ?at=branch-name
(format "https://%s/%s/src/%s/%s"
(format "%s://%s/%s/src/%s/%s"
(git-link--link-type)
hostname
dirname
commit
Expand All @@ -657,13 +681,15 @@ return (FILENAME . REVISION) otherwise nil."

(defun git-link-commit-bitbucket (hostname dirname commit)
;; ?at=branch-name
(format "https://%s/%s/commits/%s"
(format "%s://%s/%s/commits/%s"
(git-link--link-type)
hostname
dirname
commit))

(defun git-link-cgit (hostname dirname filename branch commit start _end)
(format "https://%s/%s/tree/%s?h=%s"
(format "%s://%s/%s/tree/%s?h=%s"
(git-link--link-type)
hostname
dirname
filename
Expand All @@ -673,7 +699,8 @@ return (FILENAME . REVISION) otherwise nil."
(concat "#" (format "n%s" start))))))

(defun git-link-commit-cgit (hostname dirname commit)
(format "https://%s/%s/commit/?id=%s"
(format "%s://%s/%s/commit/?id=%s"
(git-link--link-type)
hostname
dirname
commit))
Expand All @@ -698,7 +725,8 @@ return (FILENAME . REVISION) otherwise nil."
(t "")))
(branch-or-commit (or branch commit))
(dir-file-name (directory-file-name dirname)))
(format "https://%s/%s@%s/-/blob/%s%s"
(format "%s://%s/%s@%s/-/blob/%s%s"
(git-link--link-type)
hostname
dir-file-name
branch-or-commit
Expand All @@ -707,18 +735,21 @@ return (FILENAME . REVISION) otherwise nil."

(defun git-link-commit-sourcegraph (hostname dirname commit)
(let ((dir-file-name (directory-file-name dirname)))
(format "https://%s/%s/-/commit/%s"
(format "%s://%s/%s/-/commit/%s"
(git-link--link-type)
hostname
dir-file-name
commit)))

(defun git-link-homepage-github (hostname dirname)
(format "https://%s/%s"
(format "%s://%s/%s"
(git-link--link-type)
hostname
dirname))

(defun git-link-homepage-savannah (hostname dirname)
(format "https://%s/cgit/%s.git/"
(format "%s://%s/cgit/%s.git/"
(git-link--link-type)
hostname
dirname))

Expand All @@ -729,7 +760,8 @@ return (FILENAME . REVISION) otherwise nil."
commit
start
end)
(format "https://%s/%s/browse/refs/heads/%s/--/%s"
(format "%s://%s/%s/browse/refs/heads/%s/--/%s"
(git-link--link-type)
hostname
dirname
(or branch commit)
Expand All @@ -740,10 +772,10 @@ return (FILENAME . REVISION) otherwise nil."
(or end start))))))

(defun git-link-commit-codecommit (hostname dirname commit)
(format "https://%s/%s/commit/%s" hostname dirname commit))
(format "%s://%s/%s/commit/%s" (git-link--link-type) hostname dirname commit))

(defun git-link-homepage-codecommit (hostname dirname)
(format "https://%s/%s/browse" hostname dirname))
(format "%s://%s/%s/browse" (git-link--link-type) hostname dirname))

(define-obsolete-function-alias
'git-link-homepage-svannah 'git-link-homepage-savannah "cf947f9")
Expand Down
Loading