无意间想到这个问题,如何用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 的感觉了,哈哈哈

php 如何实现 git diff的更多相关文章

  1. git diff 生成patch, git apply patch 打补丁方法说明,以及分支管理的简单操作。

    git diff 简易操作说明 先git log 查看commit ID, 记录你想要打的补丁的ID 比如说: git log commit 4ff35d800fa62123a28b7bda2a04e ...

  2. Saving the entire git diff between two commits in a text file

    git diff A10..A1 > my.diff 转自: http://stackoverflow.com/questions/19067896/saving-the-entire-git- ...

  3. git diff ^M的消除

    这是由于换行符在不同的操作系统上定义的区别造成的. Windows用CR LF来定义换行,Linux用LF. CR全称是Carriage Return ,或者表示为\r, 意思是回车. LF全称是Li ...

  4. 基于git diff进行的eslint代码检测

    缘起 在项目中, 通常都会使用代码检测工具来规范团队的代码风格, 比如eslint.随着代码的不断增加, eslint进行代码检测的时间也越来越久.每次检测的时候, 需要检测的文件和实际检测的文件极度 ...

  5. git diff提示filemode发生改变(old mode 100644、new mode 10075)

    今天clone代码,git status显示修改了大量文件,git diff提示filemode变化,如下: diff --git a/Android.mk b/Android.mkold mode ...

  6. git diff的用法

    在git提交环节,存在三大部分:working tree(工作区), index file(暂存区:stage), commit(分支:master) working tree:就是你所工作在的目录, ...

  7. git diff

    git diff  工作区与暂存区的差别 git diff -cached / git diff -staged  暂存区与版本库的差别 git diff HEAD  工作区与版本库的差别 git d ...

  8. git diff获取差异文件中文乱码的解决办法

    通过git的diff命令对两个commit id的版本进行差异化的对比.中文文件时出现乱码. git diff 6bded8d0c1fe1746c122121217dc0c88667091089 a9 ...

  9. Git diff 常见用法

      Git diff 用于比较两次修改的差异 1.1 比较工作区与暂存区 git diff  比较的是单个仓库的工作区与暂存区的差别,repo diff是对git diff的封装,用来分别显示各个项目 ...

  10. git diff patch

    如何生成patch:修改一个地方,然后git diff > xxx.patch 就会生成一个patch文件,这里的关键似乎是, 源文件的某个模块的版本要和线上发布的最新版本要一致,这样patch ...

随机推荐

  1. java_类属性&对象属性

    访问类属性方式有两种: 对象.类属性 类.类属性 但是理解上第一种方法存在小小小问题, 逻辑没问题 2022年7月31日18:44:29

  2. uniapp中,getApp()返回的实例到底是什么?为什么getApp()返回的实例无法访问vuex的$store

    按uniapp官方手册中说,getApp()函数用于获取当前应用实例.当前应用,也就是说当前应用程序.因为getApp()返回的实例可以用于访问app.vue中的globaldata,因此这个当前应用 ...

  3. [Udemy] AWS Certified Data Analytics Specialty - 6.Security

    S3 加密 SSE-S3 SSE-KMS SSE-C Client Side Encryption SSL/TLS S3 支持http/https 两种协议 KMS KMS最大能加密4KB的数据,再大 ...

  4. 支付宝小程序swiper video的坑

    在使用uniapp 开发多端应用的时候,踩了一个坑,有一个页面,是使用swiper作为滑动容器,然后每个swiper-item 嵌套不同的内容, 代码示意: <template> < ...

  5. grid网格布局

    https://ruanyifeng.com/blog/2019/03/grid-layout-tutorial.html Grid 布局只对项目生效 划分网格的线,称为"网格线" ...

  6. 最小代价的 SSO 单点登录方案

    现在有多个 WebApp,想用最小的代价实现 SSO 单点登录.所谓最小代价,我的理解就是对原有 WebApp 的改动最小,因此 在旁路增加一个 SsoWebApp 用于管理 SSO 的账号,进行身份 ...

  7. USB总线-Linux内核USB设备驱动ftrace分析(十一)

    1.简介 USB Gadget Driver定义了很多trace event,使用者可以在用户空间通过ftrace接口,追踪USB Gadget Driver的行为. USB设备控制器驱动定义的tra ...

  8. 数据库周刊54丨2020 年度报告:PingCAP、腾讯云数据库、人大金仓、GoldenDB ;CPU 100% SQL优化案例;Mysql内存溢出处理;sql server PK openGauss;Oracle 巡检说明书;避免删库跑路黑天鹅……

    热门资讯 1.PingCAP 2020 年度报告|相信开放的力量 [摘要]本文为PingCAP 2020年度报告.盘点了PingCAP里程碑大事件:完成D轮2.7亿美元融资,创造全球数据库历史新的里程 ...

  9. C# efcode 新建表格数据 增删改查

    using TestDbContext ctx = new TestDbContext(); var b1 = new Book { AuthorName = "杨中科", Tit ...

  10. 查找大量时序遥感文件缺失、不连贯的成像日期:Python代码

      本文介绍批量下载大量多时相的遥感影像文件后,基于Python语言与每一景遥感影像文件的文件名,对这些已下载的影像文件加以缺失情况的核对,并自动统计.列出未下载影像所对应的时相的方法.   批量下载 ...