Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Request] Update fork button #88

Closed
Bluscream opened this issue Jun 16, 2019 · 13 comments
Closed

[Request] Update fork button #88

Bluscream opened this issue Jun 16, 2019 · 13 comments

Comments

@Bluscream
Copy link

Bluscream commented Jun 16, 2019

Something like that and it should point to

https://github.com/<FORK>/<REPO>/compare/<BRANCH>...<SOURCE>:<BRANCH>

https://github.com/Bluscream/VRCModLoader/compare/master...Slaynash:master (Example for the repo shown above)
@Mottie
Copy link
Owner

Mottie commented Jun 17, 2019

Hi @Bluscream!

I haven't tested it, but are you saying that just linking to

https://github.com/<FORK>/<REPO>/compare/<BRANCH>...<SOURCE>:<BRANCH>

Will trigger an update fork? And you want a new "Update fork" link added in the same place as in the screenshot?

@Bluscream
Copy link
Author

Yes and yes, the two existing buttons will only create a pr from fork to source not from source to fork

@Mottie
Copy link
Owner

Mottie commented Jun 18, 2019

Please test out this userscript, let me know if you encounter any issues with weird branch names, or anything:

// ==UserScript==
// @name        GitHub Update Fork
// @version     0.1.1
// @description A userscript that adds a link to update your fork
// @license     MIT
// @author      Rob Garrison
// @namespace   https://github.com/Mottie
// @include     https://github.com/*
// @run-at      document-idle
// @grant       none
// @icon        https://github.githubassets.com/pinned-octocat.svg
// ==/UserScript==
(() => {
  "use strict";

  function getUpstreamBranch(fork, info) {
    const upstreamLink = $("a", fork);
    // Look for "commit behind" or "commits behind"
    if (upstreamLink && info && /commits?\sbehind/.test(info.textContent)) {
      // forked from link text ":user/:repo"
      const user = upstreamLink.textContent.split("/")[0];
      const regexp = new RegExp(`(${user.trim()}:[-\\w.]+)`);
      // The match will include the sentence period because branch names may
      // include a version number, e.g. "user:my-branch-v1.0"
      const branch = (info.textContent.match(regexp) || [])[0];
      return branch ? branch.substring(0, branch.length - 1) : null;
    }
    return null;
  }

  function getUserBranch() {
    // The branch selector may contain a truncated branch name, so use the url
    const path = window.location.pathname;
    const index = path.indexOf("/tree/");
    return index > -1 ? path.substring(index + 6, path.length) : "master";
  }

  function addLink(fork, info) {
    const branch = getUpstreamBranch(fork, info);
    if (branch) {
      const userBranch = getUserBranch();
      const compareLink = $("a[href*='/compare']", info);
      const prLink = $("a[href*='/pull']", info);
      const link = prLink.cloneNode();
      // https://github.com/<FORK>/<REPO>/compare/<BRANCH>...<SOURCE>:<BRANCH>
      link.href = `${compareLink.href}/${userBranch}...${branch}`;
      link.classList.add("ghuf-update-link");
      link.appendChild($("svg", prLink).cloneNode(true));
      link.appendChild(document.createTextNode(" Update fork"));
      prLink.insertAdjacentElement("beforebegin", link);
    }
  }

  function init() {
    const fork = $(".fork-flag");
    const info = $(".branch-infobar");
    if (fork && info) {
      addLink(fork, info);
    }
  }

  function $(str, el = document) {
    return el.querySelector(str);
  }

  document.addEventListener("pjax:end", init);
  init();
})();

@Bluscream
Copy link
Author

It doesn't show the button on https://github.com/Bluscream/VRCModLoader

@Mottie
Copy link
Owner

Mottie commented Jun 19, 2019

Oh, it's only showing when you have "commits behind". I didn't think you'd need to update from upstream if you're "commits ahead"; but it's a comparison, so maybe?

@Bluscream
Copy link
Author

Ah okay, well then i'm going to test it next time i am behind :)

@Mottie
Copy link
Owner

Mottie commented Jun 24, 2019

I've updated the userscript. It wasn't checking for the update on the master branch properly.

@Bluscream
Copy link
Author

It works fine

@Bluscream
Copy link
Author

One thing, could it fill the PR name automatically?

@Mottie
Copy link
Owner

Mottie commented Jul 6, 2019

Update it with what name?

@Bluscream
Copy link
Author

Bluscream commented Jul 6, 2019

Maybe something like "Update fork" or Merge branch <source>/<branch> into <fork>/<branch>

@Mottie
Copy link
Owner

Mottie commented Mar 20, 2020

I finally added this userscript to the repository: https://github.com/Mottie/GitHub-userscripts/wiki/GitHub-update-fork

I never got around to getting it to fill in the PR name automatically; this would require it saving the name across several page loads, and I'm just not confident that it would work well.

@Bluscream
Copy link
Author

Bluscream commented Jun 21, 2023

Maybe this can be interesting as a userscript; Github Compare link/URL generator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants