Skip to content

Commit 6c331f4

Browse files
authored
Merge pull request #316 from intersystems/branchNameWithSlash
Checkout branch name with slash via Web UI no longer fails
2 parents cc73a52 + c7221b7 commit 6c331f4

File tree

3 files changed

+87
-36
lines changed

3 files changed

+87
-36
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313
- Deletion of files in locked environment is now suppressed (#302)
1414
- Failed to import file VS Code popup no longer shows up after overwriting file on server once (#264)
1515
- Don't automatically stage files added to source control (#303)
16+
- Checkout of branches whose names contain slashes via Web UI no longer fails (#295)
1617

1718
## [2.3.0] - 2023-12-06
1819

git-webui/release/share/git-webui/webui/js/git-webui.js

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -323,15 +323,15 @@ webui.SideBarView = function(mainView, noEventHandlers) {
323323
var cardDiv = $('<div class="card custom-card">').appendTo(accordionDiv)[0];
324324
if (id.indexOf("local-branches") > -1) {
325325
// parses the output of git branch --verbose --verbose
326-
var matches = /^\*?\s*([\w-]+)\s+([^\s]+)\s+(\[.*\])?.*/.exec(ref);
326+
var matches = /^\*?\s*([\w-\/]+)\s+([^\s]+)\s+(\[.*\])?.*/.exec(ref);
327327
var branchInfo = {
328328
"branch_name": matches[1],
329329
"hash": matches[2],
330330
"remote": matches[3]
331331
}
332332
var refname = branchInfo.branch_name;
333333
var canPush = (branchInfo.remote === undefined) || (branchInfo.remote.indexOf("ahead") > -1) // either no upstream or ahead of upstream
334-
var itemId = refname + idPostfix;
334+
var itemId = refname.replaceAll('/', '-') + idPostfix;
335335
var cardHeader = $('<div class="card-header" id="heading-' + itemId + '">').appendTo(cardDiv);
336336
var button = $('<button class="btn btn-sm btn-default btn-branch text-left" type="button" data-toggle="collapse" data-target="#collapse-' + itemId + '" aria-expanded="true" aria-controls="collapse-' + itemId + '">'
337337
+ refname
@@ -479,16 +479,12 @@ webui.SideBarView = function(mainView, noEventHandlers) {
479479
webui.git("fetch --prune",updateSideBar);
480480
}
481481

482-
self.checkoutBranch = function(e) {
483-
e.preventDefault();
484-
$("#confirm-branch-checkout").remove();
485482

486-
var refName = $(this).parent().parent().parent().siblings(
487-
".card-header").children("button").html();
483+
self.checkoutBranch = function(branchType, refName) {
484+
$("#confirm-branch-checkout").remove();
488485

489486
var remoteName = refName.split("/")[0];
490-
var branchName = refName.split("/")[1];
491-
487+
var branchName = refName.split("/").slice(1).join("/");
492488
var flag = 0;
493489
webui.git("status -u --porcelain", function(data) {
494490
$.get("api/uncommitted", function (uncommitted) {
@@ -534,12 +530,18 @@ webui.SideBarView = function(mainView, noEventHandlers) {
534530
$("#confirm-branch-checkout").on('click', '#confirm-checkout', function(e){
535531
e.preventDefault();
536532
var refName = $("#confirm-branch-checkout pre")[0].innerHTML;
537-
var remoteName = refName.split("/")[0];
538-
var branchName = refName.split("/")[1];
539533

540-
if(branchName){
534+
if(branchType === "remote"){
535+
var remoteName = refName.split("/")[0];
536+
var branchName = refName.split("/")[1];
541537
webui.git("fetch "+remoteName+" "+branchName);
542-
webui.git("checkout -b " +branchName + " " + refName, updateSideBar);
538+
webui.git("branch -l "+branchName, function(existingBranch) {
539+
if (existingBranch.length > 0) {
540+
webui.git("checkout " +branchName, updateSideBar);
541+
} else {
542+
webui.git("checkout -b " +branchName + " " + refName, updateSideBar);
543+
}
544+
});
543545
}
544546
else{
545547
webui.git("checkout " + refName, updateSideBar, "", "", webui.showSuccess);
@@ -552,11 +554,16 @@ webui.SideBarView = function(mainView, noEventHandlers) {
552554
});
553555
}
554556
else{
555-
if(branchName){
557+
if(branchType === "remote"){
556558
webui.git("fetch "+remoteName+" "+branchName);
557-
webui.git("checkout -b " +branchName + " " + refName, updateSideBar);
559+
webui.git("branch -l "+branchName, function(existingBranch) {
560+
if (existingBranch.length > 0) {
561+
webui.git("checkout " +branchName, updateSideBar);
562+
} else {
563+
webui.git("checkout -b " +branchName + " " + refName, updateSideBar);
564+
}
565+
});
558566
}
559-
560567
else{
561568
webui.git("checkout " + refName, updateSideBar, "", "", webui.showSuccess);
562569
}
@@ -565,6 +572,24 @@ webui.SideBarView = function(mainView, noEventHandlers) {
565572
});
566573
}
567574

575+
self.checkoutLocalBranch = function(e) {
576+
e.preventDefault();
577+
578+
var refName = $(this).parent().parent().parent().siblings(
579+
".card-header").children("button").html();
580+
581+
self.checkoutBranch("local", refName);
582+
}
583+
584+
self.checkoutRemoteBranch = function(e) {
585+
e.preventDefault();
586+
587+
var refName = $(this).parent().parent().parent().siblings(
588+
".card-header").children("button").html();
589+
590+
self.checkoutBranch("remote", refName);
591+
}
592+
568593
self.deleteLocalBranch = function(e) {
569594
e.preventDefault();
570595

@@ -779,9 +804,9 @@ webui.SideBarView = function(mainView, noEventHandlers) {
779804
self.fetchSection($("#sidebar-tags", self.element)[0], "Tags", "tags", "tag");
780805

781806
if(!noEventHandlers){
782-
$(document).on('click', '.btn-checkout-local-branch', self.checkoutBranch);
807+
$(document).on('click', '.btn-checkout-local-branch', self.checkoutLocalBranch);
783808
$(document).on('click', '.btn-push-branch', self.pushBranch);
784-
$(document).on('click', '.btn-checkout-remote-branch', self.checkoutBranch);
809+
$(document).on('click', '.btn-checkout-remote-branch', self.checkoutRemoteBranch);
785810

786811
$(document).on('click', '.btn-delete-branch', self.deleteLocalBranch);
787812

git-webui/src/share/git-webui/webui/js/git-webui.js

Lines changed: 43 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -323,15 +323,15 @@ webui.SideBarView = function(mainView, noEventHandlers) {
323323
var cardDiv = $('<div class="card custom-card">').appendTo(accordionDiv)[0];
324324
if (id.indexOf("local-branches") > -1) {
325325
// parses the output of git branch --verbose --verbose
326-
var matches = /^\*?\s*([\w-]+)\s+([^\s]+)\s+(\[.*\])?.*/.exec(ref);
326+
var matches = /^\*?\s*([\w-\/]+)\s+([^\s]+)\s+(\[.*\])?.*/.exec(ref);
327327
var branchInfo = {
328328
"branch_name": matches[1],
329329
"hash": matches[2],
330330
"remote": matches[3]
331331
}
332332
var refname = branchInfo.branch_name;
333333
var canPush = (branchInfo.remote === undefined) || (branchInfo.remote.indexOf("ahead") > -1) // either no upstream or ahead of upstream
334-
var itemId = refname + idPostfix;
334+
var itemId = refname.replaceAll('/', '-') + idPostfix;
335335
var cardHeader = $('<div class="card-header" id="heading-' + itemId + '">').appendTo(cardDiv);
336336
var button = $('<button class="btn btn-sm btn-default btn-branch text-left" type="button" data-toggle="collapse" data-target="#collapse-' + itemId + '" aria-expanded="true" aria-controls="collapse-' + itemId + '">'
337337
+ refname
@@ -479,16 +479,12 @@ webui.SideBarView = function(mainView, noEventHandlers) {
479479
webui.git("fetch --prune",updateSideBar);
480480
}
481481

482-
self.checkoutBranch = function(e) {
483-
e.preventDefault();
484-
$("#confirm-branch-checkout").remove();
485482

486-
var refName = $(this).parent().parent().parent().siblings(
487-
".card-header").children("button").html();
483+
self.checkoutBranch = function(branchType, refName) {
484+
$("#confirm-branch-checkout").remove();
488485

489486
var remoteName = refName.split("/")[0];
490-
var branchName = refName.split("/")[1];
491-
487+
var branchName = refName.split("/").slice(1).join("/");
492488
var flag = 0;
493489
webui.git("status -u --porcelain", function(data) {
494490
$.get("api/uncommitted", function (uncommitted) {
@@ -534,12 +530,18 @@ webui.SideBarView = function(mainView, noEventHandlers) {
534530
$("#confirm-branch-checkout").on('click', '#confirm-checkout', function(e){
535531
e.preventDefault();
536532
var refName = $("#confirm-branch-checkout pre")[0].innerHTML;
537-
var remoteName = refName.split("/")[0];
538-
var branchName = refName.split("/")[1];
539533

540-
if(branchName){
534+
if(branchType === "remote"){
535+
var remoteName = refName.split("/")[0];
536+
var branchName = refName.split("/")[1];
541537
webui.git("fetch "+remoteName+" "+branchName);
542-
webui.git("checkout -b " +branchName + " " + refName, updateSideBar);
538+
webui.git("branch -l "+branchName, function(existingBranch) {
539+
if (existingBranch.length > 0) {
540+
webui.git("checkout " +branchName, updateSideBar);
541+
} else {
542+
webui.git("checkout -b " +branchName + " " + refName, updateSideBar);
543+
}
544+
});
543545
}
544546
else{
545547
webui.git("checkout " + refName, updateSideBar, "", "", webui.showSuccess);
@@ -552,11 +554,16 @@ webui.SideBarView = function(mainView, noEventHandlers) {
552554
});
553555
}
554556
else{
555-
if(branchName){
557+
if(branchType === "remote"){
556558
webui.git("fetch "+remoteName+" "+branchName);
557-
webui.git("checkout -b " +branchName + " " + refName, updateSideBar);
559+
webui.git("branch -l "+branchName, function(existingBranch) {
560+
if (existingBranch.length > 0) {
561+
webui.git("checkout " +branchName, updateSideBar);
562+
} else {
563+
webui.git("checkout -b " +branchName + " " + refName, updateSideBar);
564+
}
565+
});
558566
}
559-
560567
else{
561568
webui.git("checkout " + refName, updateSideBar, "", "", webui.showSuccess);
562569
}
@@ -565,6 +572,24 @@ webui.SideBarView = function(mainView, noEventHandlers) {
565572
});
566573
}
567574

575+
self.checkoutLocalBranch = function(e) {
576+
e.preventDefault();
577+
578+
var refName = $(this).parent().parent().parent().siblings(
579+
".card-header").children("button").html();
580+
581+
self.checkoutBranch("local", refName);
582+
}
583+
584+
self.checkoutRemoteBranch = function(e) {
585+
e.preventDefault();
586+
587+
var refName = $(this).parent().parent().parent().siblings(
588+
".card-header").children("button").html();
589+
590+
self.checkoutBranch("remote", refName);
591+
}
592+
568593
self.deleteLocalBranch = function(e) {
569594
e.preventDefault();
570595

@@ -779,9 +804,9 @@ webui.SideBarView = function(mainView, noEventHandlers) {
779804
self.fetchSection($("#sidebar-tags", self.element)[0], "Tags", "tags", "tag");
780805

781806
if(!noEventHandlers){
782-
$(document).on('click', '.btn-checkout-local-branch', self.checkoutBranch);
807+
$(document).on('click', '.btn-checkout-local-branch', self.checkoutLocalBranch);
783808
$(document).on('click', '.btn-push-branch', self.pushBranch);
784-
$(document).on('click', '.btn-checkout-remote-branch', self.checkoutBranch);
809+
$(document).on('click', '.btn-checkout-remote-branch', self.checkoutRemoteBranch);
785810

786811
$(document).on('click', '.btn-delete-branch', self.deleteLocalBranch);
787812

0 commit comments

Comments
 (0)