|
| 1 | +--[[ |
| 2 | +# MIT License |
| 3 | +# |
| 4 | +# Copyright (c) 2025 Mickaël Canouil |
| 5 | +# |
| 6 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +# of this software and associated documentation files (the "Software"), to deal |
| 8 | +# in the Software without restriction, including without limitation the rights |
| 9 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +# copies of the Software, and to permit persons to whom the Software is |
| 11 | +# furnished to do so, subject to the following conditions: |
| 12 | +# |
| 13 | +# The above copyright notice and this permission notice shall be included in all |
| 14 | +# copies or substantial portions of the Software. |
| 15 | +# |
| 16 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | +# SOFTWARE. |
| 23 | +]] |
| 24 | + |
| 25 | +--- MC Git - Git repository utilities for Quarto Lua filters and shortcodes |
| 26 | +--- @module git |
| 27 | +--- @author Mickaël Canouil |
| 28 | +--- @version 1.0.0 |
| 29 | + |
| 30 | +local git_module = {} |
| 31 | + |
| 32 | +-- ============================================================================ |
| 33 | +-- GIT REPOSITORY UTILITIES |
| 34 | +-- ============================================================================ |
| 35 | + |
| 36 | +--- Check if a string is empty or nil |
| 37 | +--- @param s string|nil The string to check |
| 38 | +--- @return boolean true if the string is nil or empty |
| 39 | +local function is_empty(s) |
| 40 | + return s == nil or s == '' |
| 41 | +end |
| 42 | + |
| 43 | +--- Get repository name from git remote origin URL |
| 44 | +--- Executes shell command to extract repository name from git remote |
| 45 | +--- @return string|nil The repository name (e.g., "owner/repo") or nil if not found |
| 46 | +--- @usage local repo = git_module.get_repository() |
| 47 | +function git_module.get_repository() |
| 48 | + local is_windows = package.config:sub(1, 1) == "\\" |
| 49 | + local remote_repository_command |
| 50 | + |
| 51 | + if is_windows then |
| 52 | + remote_repository_command = "(git remote get-url origin) -replace '.*[:/](.+?)(\\.git)?$', '$1'" |
| 53 | + else |
| 54 | + remote_repository_command = "git remote get-url origin 2>/dev/null | sed -E 's|.*[:/]([^/]+/[^/.]+)(\\.git)?$|\\1|'" |
| 55 | + end |
| 56 | + |
| 57 | + local handle = io.popen(remote_repository_command) |
| 58 | + if handle then |
| 59 | + local git_repo = handle:read("*a"):gsub("%s+$", "") |
| 60 | + handle:close() |
| 61 | + if not is_empty(git_repo) then |
| 62 | + return git_repo |
| 63 | + end |
| 64 | + end |
| 65 | + |
| 66 | + return nil |
| 67 | +end |
| 68 | + |
| 69 | +-- ============================================================================ |
| 70 | +-- MODULE EXPORT |
| 71 | +-- ============================================================================ |
| 72 | + |
| 73 | +return git_module |
0 commit comments