最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python獲取當(dāng)前git的repo地址的示例代碼

 更新時(shí)間:2024年09月29日 08:41:09   作者:胡少俠7  
大家好,當(dāng)談及版本控制系統(tǒng)時(shí),Git是最為廣泛使用的一種,而Python作為一門多用途的編程語(yǔ)言,在處理Git倉(cāng)庫(kù)時(shí)也展現(xiàn)了其強(qiáng)大的能力,本文給大家介紹了python獲取當(dāng)前git的repo地址的方法,需要的朋友可以參考下

要獲取當(dāng)前 Git 倉(cāng)庫(kù)的遠(yuǎn)程地址,可以使用 subprocess 模塊執(zhí)行 Git 命令。下面是如何做到這一點(diǎn)的示例代碼:

import subprocess

def get_git_remote_url():
    try:
        # 獲取遠(yuǎn)程 URL
        result = subprocess.run(
            ['git', 'config', '--get', 'remote.origin.url'],
            check=True,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            text=True
        )
        
        # 獲取并返回輸出
        remote_url = result.stdout.strip()
        return remote_url

    except subprocess.CalledProcessError as e:
        print(f"An error occurred: {e}")
        return None

# 使用示例
remote_url = get_git_remote_url()
if remote_url:
    print(f"Remote URL: {remote_url}")
else:
    print("Failed to retrieve the remote URL.")

注意事項(xiàng):

  • Git 必須安裝:確保本地環(huán)境已安裝 Git 并且正在 Git 倉(cāng)庫(kù)的目錄中運(yùn)行。
  • 錯(cuò)誤處理:代碼簡(jiǎn)單處理了可能發(fā)生的錯(cuò)誤,可根據(jù)需要增加異常處理和日志記錄。
  • 遠(yuǎn)程名稱:示例使用了默認(rèn)的 origin,若遠(yuǎn)程名稱不同,請(qǐng)更改命令中的相應(yīng)部分。

拓展:python操作git gitpython模塊

安裝模塊

pip3 install gitpython

基本使用

import os
from git.repo import Repo

# 創(chuàng)建本地路徑用來存放遠(yuǎn)程倉(cāng)庫(kù)下載的代碼
download_path = os.path.join('NB')
# 拉取代碼
Repo.clone_from('https://github.com/DominicJi/TeachTest.git',to_path=download_path,branch='master')

其他常見操作

# ############## 2. pull最新代碼 ##############
import os
from git.repo import Repo
 
local_path = os.path.join('NB')
repo = Repo(local_path)
repo.git.pull()


# ############## 3. 獲取所有分支 ##############
import os
from git.repo import Repo
 
local_path = os.path.join('NB')
repo = Repo(local_path)
 
branches = repo.remote().refs
for item in branches:
    print(item.remote_head)
    

# ############## 4. 獲取所有版本 ##############
import os
from git.repo import Repo
 
local_path = os.path.join('NB')
repo = Repo(local_path)
 
for tag in repo.tags:
    print(tag.name)


# ############## 5. 獲取所有commit ##############
import os
from git.repo import Repo
 
local_path = os.path.join('NB')
repo = Repo(local_path)
 
# 將所有提交記錄結(jié)果格式成json格式字符串 方便后續(xù)反序列化操作
commit_log = repo.git.log('--pretty={"commit":"%h","author":"%an","summary":"%s","date":"%cd"}', max_count=50,
                          date='format:%Y-%m-%d %H:%M')
log_list = commit_log.split("\n")
real_log_list = [eval(item) for item in log_list]
print(real_log_list)
 

 # ############## 6. 切換分支 ##############
import os
from git.repo import Repo
 
local_path = os.path.join('NB')
repo = Repo(local_path)
 
before = repo.git.branch()
print(before)
repo.git.checkout('master')
after = repo.git.branch()
print(after)
repo.git.reset('--hard', '854ead2e82dc73b634cbd5afcf1414f5b30e94a8')


 
# ############## 7. 打包代碼 ##############
import os
from git.repo import Repo

local_path = os.path.join(NB')
repo = Repo(local_path)

with open(os.path.join('NB.tar'), 'wb') as fp:
    repo.archive(fp)

所有的方法封裝到類中

import os
from git.repo import Repo
from git.repo.fun import is_git_dir


class GitRepository(object):
    """
    git倉(cāng)庫(kù)管理
    """
    def __init__(self, local_path, repo_url, branch='master'):
        self.local_path = local_path
        self.repo_url = repo_url
        self.repo = None
        self.initial(repo_url, branch)

    def initial(self, repo_url, branch):
        """
        初始化git倉(cāng)庫(kù)
        :param repo_url:
        :param branch:
        :return:
        """
        if not os.path.exists(self.local_path):
            os.makedirs(self.local_path)

        git_local_path = os.path.join(self.local_path, '.git')
        if not is_git_dir(git_local_path):
            self.repo = Repo.clone_from(repo_url, to_path=self.local_path, branch=branch)
        else:
            self.repo = Repo(self.local_path)

    def pull(self):
        """
        從線上拉最新代碼
        :return:
        """
        self.repo.git.pull()

    def branches(self):
        """
        獲取所有分支
        :return:
        """
        branches = self.repo.remote().refs
        return [item.remote_head for item in branches if item.remote_head not in ['HEAD', ]]

    def commits(self):
        """
        獲取所有提交記錄
        :return:
        """
        commit_log = self.repo.git.log('--pretty={"commit":"%h","author":"%an","summary":"%s","date":"%cd"}',
                                       max_count=50,
                                       date='format:%Y-%m-%d %H:%M')
        log_list = commit_log.split("\n")
        return [eval(item) for item in log_list]

    def tags(self):
        """
        獲取所有tag
        :return:
        """
        return [tag.name for tag in self.repo.tags]

    def change_to_branch(self, branch):
        """
        切換分值
        :param branch:
        :return:
        """
        self.repo.git.checkout(branch)

    def change_to_commit(self, branch, commit):
        """
        切換commit
        :param branch:
        :param commit:
        :return:
        """
        self.change_to_branch(branch=branch)
        self.repo.git.reset('--hard', commit)

    def change_to_tag(self, tag):
        """
        切換tag
        :param tag:
        :return:
        """
        self.repo.git.checkout(tag)


if __name__ == '__main__':
    local_path = os.path.join('codes', 'luffycity')
    repo = GitRepository(local_path,remote_path)
    branch_list = repo.branches()
    print(branch_list)
    repo.change_to_branch('dev')
    repo.pull()

到此這篇關(guān)于python獲取當(dāng)前git的repo地址的示例代碼的文章就介紹到這了,更多相關(guān)python獲取git repo地址內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳談Python 窗體(tkinter)表格數(shù)據(jù)(Treeview)

    詳談Python 窗體(tkinter)表格數(shù)據(jù)(Treeview)

    今天小編就為大家分享一篇詳談Python 窗體(tkinter)表格數(shù)據(jù)(Treeview),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10
  • pandas庫(kù)中?DataFrame的用法小結(jié)

    pandas庫(kù)中?DataFrame的用法小結(jié)

    這篇文章主要介紹了pandas庫(kù)中?DataFrame的用法,利用pandas.DataFrame可以構(gòu)建表格,通過列標(biāo)屬性調(diào)用列對(duì)象,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2023-05-05
  • Python抓取聚劃算商品分析頁(yè)面獲取商品信息并以XML格式保存到本地

    Python抓取聚劃算商品分析頁(yè)面獲取商品信息并以XML格式保存到本地

    這篇文章主要為大家詳細(xì)介紹了Python抓取聚劃算商品分析頁(yè)面獲取商品信息,并以XML格式保存到本地的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-02-02
  • Python 通過監(jiān)聽端口實(shí)現(xiàn)唯一腳本運(yùn)行方式

    Python 通過監(jiān)聽端口實(shí)現(xiàn)唯一腳本運(yùn)行方式

    這篇文章主要介紹了Python 通過監(jiān)聽端口實(shí)現(xiàn)唯一腳本運(yùn)行方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-05-05
  • Python中if語(yǔ)句的使用方法及實(shí)例代碼

    Python中if語(yǔ)句的使用方法及實(shí)例代碼

    if語(yǔ)句能夠進(jìn)行條件測(cè)試,并依據(jù)一定的條件進(jìn)行具體的操作,下面這篇文章主要給大家介紹了關(guān)于Python中if語(yǔ)句的使用方法及實(shí)例代碼,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-11-11
  • python pandas多數(shù)據(jù)操作的完整指南

    python pandas多數(shù)據(jù)操作的完整指南

    在數(shù)據(jù)分析工作中,我們經(jīng)常需要處理多個(gè)數(shù)據(jù)集并將它們以各種方式組合起來,Pandas 提供了多種強(qiáng)大的多數(shù)據(jù)操作方法,下面就跟隨小編一起了解一下吧
    2025-04-04
  • python 中random模塊的常用方法總結(jié)

    python 中random模塊的常用方法總結(jié)

    這篇文章主要介紹了python 中random的常用方法總結(jié)的相關(guān)資料,需要的朋友可以參考下
    2017-07-07
  • Python中yield函數(shù)的用法詳解

    Python中yield函數(shù)的用法詳解

    這篇文章詳細(xì)介紹了Python中的yield關(guān)鍵字及其用法,yield關(guān)鍵字用于生成器函數(shù)中,使得函數(shù)可以像迭代器一樣工作,但不會(huì)一次性將所有結(jié)果加載到內(nèi)存中,文中將用法介紹的非常詳細(xì),需要的朋友可以參考下
    2025-03-03
  • Python調(diào)用百度AI實(shí)現(xiàn)人像分割詳解

    Python調(diào)用百度AI實(shí)現(xiàn)人像分割詳解

    本文主要介紹了如何通過Python調(diào)用百度AI從而實(shí)現(xiàn)人像的分割與合成,文中的示例代碼對(duì)我們的工作或?qū)W習(xí)有一定的幫助,需要的朋友可以參考一下
    2021-12-12
  • Python 模擬生成動(dòng)態(tài)產(chǎn)生驗(yàn)證碼圖片的方法

    Python 模擬生成動(dòng)態(tài)產(chǎn)生驗(yàn)證碼圖片的方法

    這篇文章主要介紹了Python 模擬生成動(dòng)態(tài)產(chǎn)生驗(yàn)證碼圖片的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02

最新評(píng)論

林口县| 青河县| 太仓市| 米脂县| 永德县| 万宁市| 霍邱县| 开平市| 周至县| 彰武县| 图木舒克市| 五河县| 渑池县| 奉节县| 手机| 宝应县| 三门峡市| 凉山| 竹北市| 离岛区| 昭觉县| 扶绥县| 遂昌县| 玉田县| 吐鲁番市| 庆阳市| 安远县| 泰兴市| 萨嘎县| 通江县| 望奎县| 贺州市| 兖州市| 兴城市| 水富县| 莱阳市| 额敏县| 嵊泗县| 台东市| 崇仁县| 布尔津县|