New FAMILUG

The PyMiers

Wednesday, 22 July 2015

git config, go get private repo và gitlab

Nghe có vẻ không hề liên quan gì nhưng thực tế là cực kỳ liên quan :3

1. git config
Git config được, và nội dung config của nó nằm ở ~/.gitconfig
Gõ lệnh ``git help config`` để xem đầy đủ.

2. go get với private repository
Khi code bằng Golang, việc dùng git để quản lý code và tống nó lên một private repo là một điều hết sức hiển nhiên. Chả ai mang code private ra nơi công cộng để cả (github public chẳng hạn).
Vậy nhưng ``go get`` không hỗ trợ clone private repo qua SSH (hay chính xác là không có option nào để chỉ cho ``go get`` rằng nó phải dùng ssh chứ không phải HTTPS).

Vậy nên khi gõ:
$ go get git.XXX.com/mana/salt

Fetching https://git.XXX.com/mana/salt?go-get=1 # tự thêm https ở đầu
package git.XXX.com/mana/salt: unrecognized import path "git.XXX.com/mana/salt"
Giải pháp cho vấn đề này là sử dụng git config để chuyển dòng https thành git@GIT_SERVER_ADDR.

Ví dụ với server là gitlab.com:

Thêm dòng sau vào file ~/.gitconfig
[url "git@gitlab.com:"]
        insteadOf = https://gitlab.com/
Thử lại:
$ go get -v gitlab.com/hvn_familug/pyfml1505
Fetching https://gitlab.com/hvn_familug/pyfml1505?go-get=1
Parsing meta tags from https://gitlab.com/hvn_familug/pyfml1505?go-get=1 (status code 200)
get "gitlab.com/hvn_familug/pyfml1505": found meta tag main.metaImport{Prefix:"gitlab.com/hvn_familug/pyfml1505", VCS:"git", RepoRoot:"https://gitlab.com/hvn_familug/pyfml1505.git"} at https://gitlab.com/hvn_familug/pyfml1505?go-get=1
gitlab.com/hvn_familug/pyfml1505 (download)
3. GitLab
Với cách trên, ta đã có thể dùng ``go get`` với github.com, gitlab.com
Nhưng mà không phải git server nào cũng hỗ trợ cái dòng này cả:
>> Parsing meta tags from https://gitlab.com/hvn_familug/pyfml1505?go-get=1 (status code 200) 

Với gitlab phiên bản < 7.9, phải config NGINX thêm phần hỗ trợ cho go client:

    if ($http_user_agent ~* "Go") {
        return 200 "
        <!DOCTYPE html>
        <head><meta content='$host$uri git $scheme://$host$uri.git' name='go-import'></head>
        </html>";
    }

Hoặc giải pháp khác là cài bản gitlab mới nhất (>7.9).

Bài viết sử dụng:
$ go version
go version go1.4.2 darwin/amd6
Tham khảo:
http://golang.org/doc/faq#git_https

Hết.
hvn AT familug.org

No comments:

Post a Comment