Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,37 @@ | ||
--- | ||
title: GitHub のリポジトリ名やownerを取得するシェル | ||
date: 2023-05-03 00:00:00 | ||
topics: | ||
- GitHub | ||
- git | ||
- bash | ||
type: tech | ||
published: true | ||
emoji: 🐚 | ||
--- | ||
|
||
git のディレクトリで remote に設定されている GitHub リポジトリの owner や repo 名を取得するシェルです。 | ||
|
||
## remote URL の取得 | ||
|
||
```sh | ||
url=$(git config --get remote.origin.url) | ||
echo $url | ||
# git@github.com:elzup/kit-sh.git | ||
``` | ||
|
||
## url から owner と repo をパースする | ||
|
||
```sh | ||
id=$(echo $url |sed -e 's/.*github.com.\(.*\).git/\1/') | ||
# elzup/kit-sh | ||
|
||
echo ${id#*/} | ||
# kit-sh | ||
echo ${id%/*} | ||
# elzup | ||
``` | ||
|
||
bash の変数展開でパターン照合演算子を使っています。 | ||
`echo ${id#*/}` は `id` の先頭から `/` までを削除します。 | ||
`echo ${id%/*}` は `id` の末尾から `/` までを削除します。 |