Github manual merge

Fixes the manual merge instructions on github

目前為 2024-02-19 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Github manual merge
// @namespace    https://github.com/lordwelch/
// @version      1.0
// @description  Fixes the manual merge instructions on github
// @license      MIT
// @author       lordwelch
// @match        https://github.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=github.com
// @grant        none
// @run-at       document-idle
// ==/UserScript==

(async function() {
    'use strict';
    console.log("script started");
    var pushState = history.pushState;
    var replaceState = history.replaceState;

    history.pushState = function() {
        pushState.apply(history, arguments);
        window.dispatchEvent(new Event('pushstate'));
        window.dispatchEvent(new Event('locationchange'));
    };

    history.replaceState = function() {
        replaceState.apply(history, arguments);
        window.dispatchEvent(new Event('replacestate'));
        window.dispatchEvent(new Event('locationchange'));
    };

    window.addEventListener('popstate', function() {
        window.dispatchEvent(new Event('locationchange'));
    });

    window.addEventListener('locationchange', async function(){
        var location = document.location.pathname.match(/\/([^/]+)\/([^/]+)\/pull\/(\d+)$/);
        if (location == null) {
            console.log("incorrect page");
            return;
        }
        var owner = location[1];
        var repo = location[2];
        var prNum = location[3];
        var branchInstructions = document.querySelector("#clone-help-step-1");
        var pushInstructions = document.querySelector("#clone-help-step-2 > span:nth-child(3)");
        var urlObj = document.getElementById("clone-help-git-url");
        var url = '';
        var branchName = '';
        var repoOwner = '';
        while (branchInstructions == null || pushInstructions == null) {
            location = document.location.pathname.match(/\/([^/]+)\/([^/]+)\/pull\/(\d+)$/);
            if (location == null) {
                console.log("incorrect page");
                return;
            }
            owner = location[1];
            repo = location[2];
            prNum = location[3];
            console.log("Waiting for pr to load");
            await new Promise(r => setTimeout(r, 100));
            branchInstructions = document.querySelector("#clone-help-step-1");
            pushInstructions = document.querySelector("#clone-help-step-2 > span:nth-child(3)");
            urlObj = document.getElementById("clone-help-git-url");
        }
        pushInstructions.remove();
        var primaryBranch = branchInstructions.textContent.trim().split("\n")[0].split(" ").at(-1);
        branchName = branchInstructions.textContent.trim().split(" ").at(-1);
        url = urlObj.value.substring(0, urlObj.value.length - 4);
        repoOwner = url.split("/").at(-2);
        branchInstructions.textContent = `git stash\ngit checkout ${primaryBranch}\ngit fetch --force ${url} +${branchName}:${repoOwner}/${branchName}\ngit checkout ${repoOwner}/${branchName}`;
    });
})();