From 229415444ec509f41ae338e3cdd3b8da0db4530e Mon Sep 17 00:00:00 2001 From: Jack Bird Date: Sat, 15 Jul 2023 20:56:07 +0100 Subject: [PATCH] Added -d and --delete to git push to delete remote branches --- src/js/git/commands.js | 41 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/src/js/git/commands.js b/src/js/git/commands.js index 49e3675c..56e46e98 100644 --- a/src/js/git/commands.js +++ b/src/js/git/commands.js @@ -3,6 +3,7 @@ var intl = require('../intl'); var Graph = require('../graph'); var Errors = require('../util/errors'); +const { compact } = require('underscore'); var CommandProcessError = Errors.CommandProcessError; var GitError = Errors.GitError; var Warning = Errors.Warning; @@ -744,7 +745,9 @@ var commandConfig = { push: { regex: /^git +push($|\s)/, options: [ - '--force' + '--force', + '--delete', + '-d' ], execute: function(engine, command) { if (!engine.hasOrigin()) { @@ -758,14 +761,46 @@ var commandConfig = { var source; var sourceObj; var commandOptions = command.getOptionsMap(); + var isDelete = commandOptions['-d'] || commandOptions['--delete']; // git push is pretty complex in terms of // the arguments it wants as well... get ready! var generalArgs = command.getGeneralArgs(); + + // put the commandOption of delete back in the generalArgs + // as it is a flag option + if(isDelete) { + let option = commandOptions['-d'] || commandOptions['--delete']; + generalArgs = option[0] === 'origin' + ? option.concat(generalArgs) + : generalArgs.concat(option); + } + command.twoArgsForOrigin(generalArgs); assertOriginSpecified(generalArgs); - var firstArg = generalArgs[1]; + + if(isDelete) { + if(!firstArg) { + throw new GitError({ + msg: intl.todo( + '--delete doesn\'t make sense without any refs' + ) + }); + } + + if(isColonRefspec(firstArg)) { + throw new GitError({ + msg: intl.todo( + '--delete only accepts plain target ref names' + ) + }); + } + + // transform delete target ref to delete colon refspec + firstArg = ":"+firstArg; + } + if (firstArg && isColonRefspec(firstArg)) { var refspecParts = firstArg.split(':'); source = refspecParts[0]; @@ -773,7 +808,7 @@ var commandConfig = { if (source === "" && !engine.origin.resolveID(destination)) { throw new GitError({ msg: intl.todo( - 'cannot delete branch ' + options.destination + ' which doesnt exist' + 'cannot delete branch ' + options.destination + ' which doesn\'t exist' ) }); }