node实现对git仓库的管理

一、项目背景

一份代码,发布多个小程序。想让技术支持部通过脚本自己获取代码,并通过脚本自动提交到客户的小程序后台。他们自行发布。

现已注册第三方平台,开发人员通过“开发小程序”上传模板。开发人员把代码上传到模板,支持人员选择模板进行发布小程序。好像git代码管理也不需要了。

二、主要代码

app.js

const express = require('express');
const fs = require('fs');
const simpleGit = require('simple-git');
const cors = require('cors'); // 引入 cors 模块

const app = express();

const gitUrl = 'xxx'; //配置
const branch = '5.0.1';
const codePath = 'unpackage/dist/build/mp-weixin';

// 本地临时文件夹
const tempFolder = 'temp';

// 使用 cors 中间件
app.use(cors());

//clone项目
app.get('/api/gitClone', (req, res) => {
    simpleGit().clone(gitUrl, tempFolder, ['-b', branch], (err, data) => {
        if (err) {
            console.error('Clone failed:', err);
        } else {
            console.log('Clone success');
            res.json({
                message: 'clone成功'
            });

            // 2. 复制代码到指定路径
            // 这里可能需要根据你的实际需求使用适当的文件复制方法
            // 例如使用 fs-extra 库的 copy 方法

            // 3. 执行特定文件
            const filePath = `${tempFolder}/${codePath}/your-executable-file.js`;

            try {
                // execSync(`node ${filePath}`, {
                //     stdio: 'inherit'
                // });
                console.log('Execution success');
            } catch (error) {
                console.error('Execution failed:', error);

            }
        }
    });
});


//git 获取所有分支
app.get('/api/gitBranches', (req, res) => {
    // 获取所有分支
    simpleGit(tempFolder).branch((err, branchSummary) => {
        console.log('branchSummary: ', branchSummary);
        if (err) {
            console.error('Error:', err);
            res.status(500).json({
                error: 'Failed to fetch branches'
            });
        } else {
            // 将分支信息转换为 JSON 格式并返回
            const branchesJson = {
                branches: branchSummary.all
            };

            // current: '5.0.1',  返回的字段为当前分支
            //{"branches":["5.0.1","remotes/origin/1.0.0","remotes/origin/2.0.0","remotes/origin/2.0.1","remotes/origin/2.0.2","remotes/origin/3.0.0","remotes/origin/4.0.0","remotes/origin/4.1.0","remotes/origin/4.2.0","remotes/origin/4.2.1","remotes/origin/5.0.1","remotes/origin/6.0.0","remotes/origin/master"]}
            res.json(branchesJson);
        }
    });
});

//git 切换分支
app.get('/api/checkoutBranch', (req, res) => {
    // 获取所有分支
    simpleGit(tempFolder).checkout('6.0.0', (err) => {
        if (err) {
            console.error('Error:', err);
        } else {
            console.log('Switched to branch 6.0.0');
            res.json({
                message: '切换成功'
            });
        }
    });
});

const branchToPull = '6.0.0'; // 要拉取的分支名称

//git 拉取指定分支代码
app.get('/api/pull', (req, res) => {
    // 获取所有分支
    // 拉取分支代码
    simpleGit(tempFolder).pull('origin', branchToPull, (err, update) => {
        if (err) {
            console.error('Error:', err);
            res.json({
                message: '拉取错误'
            });
        } else if (update && update.summary.changes) {
            console.log(`Pulled ${update.summary.changes} changes from branch ${branchToPull}`);
            res.json({
                message: '最新成功'
            });
        } else {
            console.log('Already up to date');
            res.json({
                message: '已经是最新'
            });
        }
    });
});

app.listen(8000, () => {
    console.log(`Server is running on port 8000`);
});

三、这个错误是因为你试图推送一个超过 100 MB 的文件到 Gitee,而 Gitee 对单个文件的大小有限制。
在这里插入图片描述
解决方案:
查找是哪一个文件:
在这里插入图片描述

git rev-list --objects --all | grep da95f1d072438f13037ee3f90473ce8d9d2ab11f

删除该文件:

git filter-branch --force --index-filter \
  "git rm --cached --ignore-unmatch dist.zip" \
  --prune-empty --tag-name-filter cat -- --all

你需要强制推送你的改动到远程仓库

git push origin --force --all
  • 10
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值