pvpender/git-kphp

用于在 KPHP 中操作 Git 仓库的库。

v1.2.0 2023-01-16 00:18 UTC

This package is auto-updated.

Last update: 2024-09-22 20:20:01 UTC


README

Total Downloads PHP Version Require

用于在 KPHP 中操作 Git 仓库的 FFI 库。

下载最新包 或使用 Composer

composer require pvpender/git-kphp

库需要 PHP 7.4,最新版本的 KPHPgit 客户端(Git 的路径必须在系统变量 PATH 中)。另外,如果您想使用需要登录 git 的命令,您应该使用私钥/公钥或设置 -global 参数来调整无密码认证。

警告! 这是一个 FFI 库。这意味着它将使用一些用于正常工作的 C 代码。在 proc_open 不支持之前,此库使用 FFI,这可能会非常不安全。如果您非常重视安全性,请不要使用此库。

开始之前

这是一个 FFI 库,这意味着在开始使用此库之前,您应该预加载 .c 文件。不要担心,所有内容已经在 Systemc 类中,您只需要在

use pvpender\GitKphp\Systemc;
Systemc::load();

您的 main.php 文件顶部写入:

使用方法

use pvpender\GitKphp\Git;
use pvpender\GitKphp\Systemc;

Systemc::load();
$git = new pvpender\GitKphp\Git;
// create repo object
$repo = $git->open(__DIR__);

// or you can just clone repo
// $repo = $git->cloneRepository('https://github.com/user/repo');

// create a new file in repo
$filename = $repo->getRepositoryPath() . '/newfile.txt';
file_put_contents($filename, "Hello world!");

// commit
$repo->addFile($filename);
$repo->commit('init commit');

初始化空仓库

$repo = $git->init('/path/to/repo-directory');

使用参数

$repo = $git->init('/path/to/repo-directory', [
	'--bare', // creates bare repo
]);

克隆仓库

// Cloning of repository into subdirectory 'git-kphp' in current working directory
$repo = $git->cloneRepository('https://github.com/pvpender/git-kphp.git');
// Cloning of repository into own directory
$repo = $git->cloneRepository('https://github.com/pvpender/git-kphp.git', '/path/to/my/subdir');

基本操作

$repo->commit('commit message');
$repo->merge('branch-name');
$repo->checkout('master');
$repo->getRepositoryPath();
// adds files into commit
$repo->addFile('file.txt');
$repo->addFile('file1.txt', 'file2.txt');
$repo->addFile(['file3.txt', 'file4.txt']);
// renames files in repository
$repo->renameFile('old.txt', 'new.txt');
$repo->renameFile([
    'old1.txt' => 'new1.txt',
    'old2.txt' => 'new2.txt',
]);
// removes files from repository
$repo->removeFile('file.txt');
$repo->removeFile('file1.txt', 'file2.txt');
$repo->removeFile(['file3.txt', 'file4.txt']);
// adds all changes in repository
$repo->addAllChanges();

分支

// gets list of all repository branches (remotes & locals)
$repo->getBranches();
// gets list of all local branches
$repo->getLocalBranches();
// gets name of current branch
$repo->getCurrentBranchName();
// creates new branch
$repo->createBranch('new-branch');
// creates new branch and checkout
$repo->createBranch('patch-1', TRUE);
// removes branch
$repo->removeBranch('branch-name');

标签

// gets list of all tags in repository
$repo->getTags();
// creates new tag
$repo->createTag('v1.0.0');
$repo->createTag('v1.0.0', $options);
$repo->createTag('v1.0.0', [
	'-m' => 'message',
]);
// renames tag
$repo->renameTag('old-tag-name', 'new-tag-name');
// removes tag
$repo->removeTag('tag-name');

历史记录

这些函数通过读取 .git/ 中的文件来工作。

// returns last commit ID on current branch
$commitId = $repo->getLastCommitId();
$commitId->getId(); // or (string) $commitId
// returns commit data
$commit = $repo->getCommit('734713bc047d87bf7eac9674765ae793478c50d3');
$commit->getId(); // instance of CommitId
$commit->getSubject();
$commit->getBody();
$commit->getAuthorName();
$commit->getAuthorEmail();
$commit->getAuthorDate();
$commit->getCommitterName();
$commit->getCommitterEmail();
$commit->getCommitterDate();
$commit->getDate();
// returns commit data of last commit on current branch
$commit = $repo->getLastCommit();

远程仓库

// pulls changes from remote
$repo->pull('remote-name', ['--options']);
$repo->pull('origin');
// pushs changes to remote
$repo->push('remote-name', ['--options']);
$repo->push('origin');
$repo->push(['origin', 'master'], ['-u']);
// fetchs changes from remote
$repo->fetch('remote-name', ['--options']);
$repo->fetch('origin');
$repo->fetch(['origin', 'master']);
// adds remote repository
$repo->addRemote('remote-name', 'repository-url', ['--options']);
$repo->addRemote('origin', 'git@github.com:pvpender/git-kphp.git');
// renames remote
$repo->renameRemote('old-remote-name', 'new-remote-name');
$repo->renameRemote('origin', 'upstream');
// removes remote
$repo->removeRemote('remote-name');
$repo->removeRemote('origin');
// changes remote URL
$repo->setRemoteUrl('remote-name', 'new-repository-url');
$repo->setRemoteUrl('upstream', 'https://github.com/pvpender/git-kphp.git');

基于 https://github.com/czproject/git-php