首页 > 编程语言 >php 如何实现 git diff

php 如何实现 git diff

时间:2023-04-03 17:55:05浏览次数:49  
标签:5.0 php git composer diff sebastian

无意间想到这个问题,如何用php来实现git diff,如果实现了这个功能,岂不是能够使用php对在线编辑文件的功能做更进一步的优化和提升?

查了一下还真有这样的库,话不多说,开始执行

composer require --dev sebastian/diff

得到结果

Info from https://repo.packagist.org: #StandWithUkraine
Using version ^5.0 for sebastian/diff
./composer.json has been created
Running composer update sebastian/diff
Loading composer repositories with package information
Updating dependencies
Lock file operations: 1 install, 0 updates, 0 removals
  - Locking sebastian/diff (5.0.1)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Downloading sebastian/diff (5.0.1)
  - Installing sebastian/diff (5.0.1): Extracting archive
Generating autoload files
1 package you are using is looking for funding.
Use the `composer fund` command to find out more!

好,安装成功,创建index.php如下

<?php
include("./vendor/autoload.php");

use SebastianBergmann\Diff\Differ;
use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder;

$builder = new UnifiedDiffOutputBuilder(
    "--- Original\n+++ New\n", // custom header
    true                      // do not add line numbers to the diff 
);

$differ = new Differ($builder);
$before_string = file_get_contents("functions.php");
$after_string = file_get_contents("functions.php-new");
$diff_value = $differ->diff($before_string, $after_string);
echo $diff_value;

好,然后创建两个文件用来代表我们修改过文件的内容,修改之前的文件叫 functions.php 内容是

<?php


function get_rand_id ($id = 0) {
    static $id;
    if ($id) {
        return $id;
    }
    $id = rand(1, 1000);
    $id += 1000;
    return $id;
}

修改之后的文件叫 functions.php-new 内容是

<?php


function get_random_id (int $id = 0) {
    static $id;
    if (!$id) {
        $id = rand(1, 1000);
        $id = $id * $id;
    }
    return $id;
}

执行一下看看

php index.php &> diff.patch

得到结果

--- Original
+++ New
@@ -1,12 +1,11 @@
 <?php
 
 
-function get_rand_id ($id = 0) {
+function get_random_id (int $id = 0) {
     static $id;
-    if ($id) {
-        return $id;
+    if (!$id) {
+        $id = rand(1, 1000);
+        $id = $id * $id;
     }
-    $id = rand(1, 1000);
-    $id += 1000;
     return $id;
 }

那这样的结果呢也能作为patch查看,要怎么有更好的查看方式呢?

我们可以在vscode编辑器里面安装一个叫做 Diff Viewer 的插件 点击这里

 

 

 安装完毕, 打开这个 diff.patch 文件后是这样的

 怎么样,是不是很不错,有点像 git 的感觉了,哈哈哈

 

标签:5.0,php,git,composer,diff,sebastian
From: https://www.cnblogs.com/lizhaoyao/p/17283836.html

相关文章

  • PHPExcel 中文使用手册详解
     1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848......
  • linux使用php动态安装模块mysqli.so(ext/mysqlnd/mysqlnd.h: 没有那个文件或目录)
     由于我先安装的php,再安装的mysql!正常过程: 1、安装mysql 2、安装phpconfigure时带–with-mysql参数现在我不想重装,因此使用phpize动态安装mysqli,php版本为php-7.2.13 1先查看php下phpize路径  得知路劲为/usr/local/php/bin/phpize2切换到php源码包目录php-7......
  • 如何在PHP7中扩展mysql,先安装php7.2。后安装mysql
     相对与PHP5,PHP7的最大变化之一是移除了mysql扩展,推荐使用mysqli或者pdo_mysql,实际上在PHP5.5开始,PHP就着手开始准备弃用mysql扩展,如果你使用mysql扩展,可能看到过这样的提示”Deprecated:mysql_connect():Themysqlextensionisdeprecatedandwillberemovedinthefu......
  • git远程分支与本地分支同步
    1.先执行gitfetch2.再执行gitcheckout分支名查看所有本地分支gitbranch查看所有远程分支gitbranch-r查看远程分支和本地分支的关联关系的命令gitremoteshoworigin命令解析gitremoteshoworigin命令会显示与名为origin的远程代码库相关的信息,包括远......
  • CentOS7 安装git 配置秘钥公钥克隆代码
    第一步:安装git客户端,默认安装在/usr/libexec/git-core目录yum-yinstallgit#查看版本git--version第二步:配置git信息gitconfig--globaluser.name"username"gitconfig--globaluser.email"[email protected]"第三步:生成密钥和公钥,后续只需要按回车即可ssh-keygen-......
  • git 选择合并
    需求:有两个分支,develop,master,需要把develop的提交记录,选择性合并到master1. 将ideal 切换到master分支,checkout 2.   3.根据提交记录,右键cherrypick   4.再执行push操作。合并完成......
  • git提交443
    通过IDEALPUSH时提示以下错误fatal:unabletoaccess'https://github.com/18618450592/mygit.git/':OpenSSLSSL_connect:SSL_ERROR_SYSCALLinconnectiontogithub.com:44315:21:59.671:[..\..\mygit]git-ccredential.helper=-ccore.quotepath=false-cl......
  • github git push报错处理
    报错如下:D:\code\springcloud2022_new_new\springcloud2022>gitpushfatal:unabletoaccess'https://github.com/zhaowenqiao/springcloud2022.git/':Failedtoconnecttogithub.comport443:Timedout 解决办法命令如下:#关闭git证书校验gitcon......
  • gitlab推送代码触发jenkins构建
    预期:推送devloop或者master分支的代码,自动执行jenkins发布测试环境首先,jenkins中需要安装如下插件打开一个任务配置,构建触发器中勾选"BuildwhenachangeispushedtoGitLab."并过滤指定分支,这里需要记下GitLabwebhookURL一会儿配置到gitlab上3.gitlab中添......
  • git服务器搭建过程以及遇到的问题
    git自动化部署在Git服务器上为用户配置SSH公钥git@Linux:~$mkdir.sshgit@Linux:~$touch.ssh/authorized_keysgit@Linux:~$chmod600.ssh/authorized_keysgit@Linux:~$authorized_keys文件可以保存多个用户的SSH公钥,所有公钥被添加到这个文件中的用户,就都可......