Используйте GitPython, чтобы проверить новую ветку и отправить ее на удаленный сервер.

Учитывая repo из GitPython, как я могу создать новую локальную ветку, добавить несколько файлов и отправить ее на удаленный компьютер с помощью GitPython?

Чтобы создать repo:

from git import *

curr_dir = os.path.dirname(os.path.realpath(__file__))
repo = Repo(curr_dir)

Пока я просто использую subprocess:

def publish_changes_to_git(commit_msg):
    curr_time = time.time()
    ts = datetime.datetime.fromtimestamp(curr_time).strftime('%Y-%m-%d-%H-%M-%S')
    branch_name = "auto-commit-{ts}".format(ts=ts)
    subprocess.check_output(["git", "checkout", "-b", branch_name])
    subprocess.check_output(["git", "add", SOME_PATH])
    subprocess.check_output(
        ["git", "commit", "-m", "auto-git-commit: {msg}".format(msg=commit_msg)])

person etlsh    schedule 15.06.2016    source источник


Ответы (2)


Я сделал что-то вроде создания txt в удаленной ветке из вновь созданной ветки и зафиксировал, отправил на удаленный. Вот мой код

import git
import datetime
import os
from time import *
from os import path
from git import Repo

def commit_files():
    if repo != None:
        new_branch = 'your_new_branch'
        current = repo.create_head(new_branch)
        current.checkout()
        master = self.repo.heads.master
        repo.git.pull('origin', master)
        #creating file
        dtime = strftime('%d-%m-%Y %H:%M:%S', localtime())
        with open(self.local_repo_path + path.sep + 'lastCommit' + '.txt', 'w') as f:
            f.write(str(dtime))
        if not path.exists(self.local_repo_path):
            os.makedirs(self.local_repo_path)
        print('file created---------------------')

        if repo.index.diff(None) or repo.untracked_files:

            repo.git.add(A=True)
            repo.git.commit(m='msg')
            repo.git.push('--set-upstream', 'origin', current)
            print('git push')
        else:
            print('no changes')
person BharathKumarMurugan    schedule 23.08.2017

gitypython выглядит так, как будто он предоставляет доступ к git как на уровне, так и на низком уровне, но вы можете вызывать команды git, см. http://gitpython.readthedocs.io/en/stable/reference.html#module-git.cmd.

В качестве альтернативы рассмотрите возможность использования http://www.pygit2.org/ вместо http://www.pygit2.org/ для доступа более высокого уровня. Несколько примеров здесь http://www.pygit2.org/recipes.html

person DavidN    schedule 16.06.2016
comment
спасибо, но использование модуля git для команды точно так же, как подпроцесс. я углублюсь в pygit2, но модуль, кажется, не хватает вещей, которые мне нужны (или их очень сложно достичь) - person etlsh; 17.06.2016