auto-update-daily-20260202
 1;;; vde-vcs --- vcs related functions -*- lexical-binding: t -*-
 2
 3;; Copyright (C) 2025 Vincent Demeester
 4;; Author: Vincent Demeester <vincent@sbr.pm>
 5
 6;; This file is NOT part of GNU Emacs.
 7;;; Commentary:
 8
 9;;; Code:
10
11;;;###autoload
12(defun vde/vc-browse-remote (&optional current-line)
13  "Open the repository's remote URL in the browser.
14If CURRENT-LINE is non-nil, point to the current branch, file, and line.
15Otherwise, open the repository's main page."
16  (interactive "P")
17  (let* ((remote-url (string-trim (vc-git--run-command-string nil "config" "--get" "remote.origin.url")))
18	 (branch (string-trim (vc-git--run-command-string nil "rev-parse" "--abbrev-ref" "HEAD")))
19	 (file (string-trim (file-relative-name (buffer-file-name) (vc-root-dir))))
20	 (line (line-number-at-pos)))
21    (message "Opening remote on browser: %s" remote-url)
22    (if (and remote-url (string-match "\\(?:git@\\|https://\\)\\([^:/]+\\)[:/]\\(.+?\\)\\(?:\\.git\\)?$" remote-url))
23	(let ((host (match-string 1 remote-url))
24	      (path (match-string 2 remote-url)))
25	  ;; Convert SSH URLs to HTTPS (e.g., git@github.com:user/repo.git -> https://github.com/user/repo)
26	  (when (string-prefix-p "git@" host)
27	    (setq host (replace-regexp-in-string "^git@" "" host)))
28	  ;; Construct the appropriate URL based on CURRENT-LINE
29	  (browse-url
30	   (if current-line
31	       (format "https://%s/%s/blob/%s/%s#L%d" host path branch file line)
32	     (format "https://%s/%s" host path))))
33      (message "Could not determine repository URL"))))
34
35;;;###autoload
36(defun vde/gh-get-current-repo ()
37  "Get the current repository name using the `gh' command line."
38  (unless (executable-find "gh")
39    (error "GitHub CLI (gh) command not found"))
40
41  (with-temp-buffer
42    (let ((exit-code (call-process "gh" nil t nil "repo" "view" "--json" "owner,name" "--template" "{{.owner.login}}/{{.name}}")))
43      (unless (= exit-code 0)
44	(error "Failed to get repository info: gh command exited with code %d" exit-code))
45      (string-trim (buffer-string)))))
46
47
48(provide 'vde-vcs)
49;;; vde-vcs.el ends here