PR Branch Buttons

Insert Buttons to copy branch-names as well as a button that generates a git-checkout script that checks out the origin branch tracking the target branch

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name        PR Branch Buttons
// @namespace   Violentmonkey Scripts
// @match       https://*bitbucket*/projects/*/repos/*/pull-requests/*
// @grant       none
// @version     1.0
// @author      Dovid Weisz
// @description Insert Buttons to copy branch-names as well as a button that generates a git-checkout script that checks out the origin branch tracking the target branch
// @license     MIT
// ==/UserScript==
function addButton(elem){
  const content = elem.querySelector(".branch-lozenge-content, .ref-lozenge-content")
  const button = document.createElement("button");
  button.innerHTML = "Copy";
  button.onclick = copy;
  button.style.marginRight = "8px";
  button.style.fontSize = "9px";

  const input = document.createElement("input");
  input.type = "text";
  input.value = content.innerText;
  input.style.position = "absolute";
  input.style.left = "-99999em";

  function copy(){
    input.select();
    document.execCommand("copy");
  }

  elem.prepend(input);
  content.prepend(button);


}
function makeButtons(){
  const branchLozenges = document.querySelectorAll(".branch-lozenge, .ref-lozenge");
  if(branchLozenges.length > 0){
    const [from, to] = Array.from(branchLozenges).map(elem =>
      elem.querySelector(".branch-lozenge-content, .ref-lozenge-content").innerText
    );

    const command = `git fetch && git checkout ${from} && git branch -u origin/${to}`;

    const button = document.createElement("button");
    button.innerHTML = "Checkout";
    button.onclick = copy;
    button.style.marginRight = "8px";
    button.style.fontSize = "9px";

    const input = document.createElement("input");
    input.type = "text";
    input.value = command;
    input.style.position = "absolute";
    input.style.left = "-99999em";

    function copy(){
      input.select();
      document.execCommand("copy");
    }

    const metadataContainer = document.querySelector(".pull-request-metadata");

    metadataContainer.append(input);
    metadataContainer.append(button);


    for(let lozenge of branchLozenges){
      addButton(lozenge);
    }
    return true;
  }
  return false;
}

const contentPannel = document.querySelector("#content");
if(contentPannel){
  if(!makeButtons()) {
    const observer = new MutationObserver(() => {
      if(makeButtons()){
        observer.disconnect();
      }
    });
    observer.observe(contentPannel, { childList: true, subtree: true });
  }
}else{
  console.error("Content pannel not found.");
}