mirror of
https://github.com/ChuckBuilds/LEDMatrix.git
synced 2026-08-08 04:08:06 +00:00
fix(web): make the update button work on branches without tracking (#443)
Reported from a pi whose checkout sat on a local branch:
git pull failed (returncode=1): There is no tracking information for
the current branch. Please specify which branch you want to rebase
against.
The Tools tab reported that as "Update failed; check logs for details",
which tells the user nothing they can act on, and the underlying git
message never reached the UI at all.
A branch with no upstream is easy to end up on — checking one out by
name, restoring a backup, or following a guide that names a branch — and
until now it left the update button permanently broken with no way out
except SSH.
resolve_pull_command() now decides how to pull:
- upstream set -> git pull --rebase, as before
- no upstream, origin/<branch> -> git pull --rebase origin <branch>,
then attach tracking so the next update is a plain pull
- no upstream, no remote branch -> an error naming the branch and
pointing at Switch branch
- detached HEAD -> says so, rather than failing obscurely
That resolution happens BEFORE the stash. Previously the handler stashed
local changes and then discovered it could not pull, putting the user's
work away for an update that was never going to run.
Failures now surface git's own message instead of "check logs".
Adds a branch picker to the Tools tab, backed by GET
/system/git-branches (local + remote-only) and a checkout_branch action.
Switching attaches tracking, so Pull Latest works afterwards. Branch
names are validated against a strict pattern before reaching a subprocess
argument list.
Local edits block a checkout, as they should. Rather than a truncated
one-line error, the response carries git's full list of blocking files
and a can_retry_with_stash flag; the UI then offers "Stash and switch" as
an explicit choice. Stashing is never done unasked — putting someone's
edits away without consent is worse than refusing the switch.
Verified on the pi that produced the report: on its untracked 'audit'
branch the update now returns the actionable message, git-info reports
upstream='' and can_pull=false, and an injected branch name is rejected.
27 tests build real git repositories and cover each path, including the
stash route that could not be exercised safely on the device.
This commit is contained in:
@@ -31,6 +31,24 @@
|
||||
</div>
|
||||
|
||||
<div class="space-y-4">
|
||||
<!-- Switch branch -->
|
||||
<div class="flex items-start justify-between gap-4">
|
||||
<div class="min-w-0">
|
||||
<p class="text-sm font-medium text-gray-900">Branch</p>
|
||||
<p class="text-xs text-gray-500 mt-0.5">Choose which branch this pi follows. Switching attaches tracking, so Pull Latest works afterwards.</p>
|
||||
</div>
|
||||
<div class="shrink-0 flex items-center gap-2">
|
||||
<select id="branch-select" class="text-sm border border-gray-300 rounded-md px-2 py-2 bg-white max-w-[14rem]">
|
||||
<option value="">Loading branches…</option>
|
||||
</select>
|
||||
<button id="btn-checkout-branch" onclick="checkoutBranch(false)"
|
||||
class="inline-flex items-center px-3 py-2 border border-gray-300 text-sm font-medium rounded-md text-gray-700 bg-white hover:bg-gray-50">
|
||||
<i class="fas fa-code-branch mr-2"></i>Switch
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div id="result-checkout-branch" class="hidden"></div>
|
||||
|
||||
<!-- Pull latest -->
|
||||
<div class="flex items-start justify-between gap-4">
|
||||
<div>
|
||||
@@ -467,6 +485,14 @@
|
||||
</div>`;
|
||||
}
|
||||
|
||||
if (d.upstream) {
|
||||
html += `<p class="text-xs text-gray-500 mt-1"><i class="fas fa-link mr-1"></i>tracking <span class="font-mono">${escHtml(d.upstream)}</span></p>`;
|
||||
} else if (d.can_pull) {
|
||||
html += `<p class="text-xs text-blue-700 mt-1"><i class="fas fa-info-circle mr-1"></i>No upstream set; Pull Latest will use <span class="font-mono">origin/${escHtml(d.branch || '')}</span> and set it.</p>`;
|
||||
} else {
|
||||
html += `<p class="text-xs text-amber-700 mt-1"><i class="fas fa-triangle-exclamation mr-1"></i>No upstream and no matching branch on origin — Pull Latest cannot run. Switch to a branch that exists on the remote.</p>`;
|
||||
}
|
||||
|
||||
if (d.remote_url) {
|
||||
html += `<p class="text-xs text-gray-400 mt-1"><i class="fas fa-cloud mr-1"></i>${escHtml(d.remote_url)}</p>`;
|
||||
}
|
||||
@@ -479,6 +505,84 @@
|
||||
});
|
||||
}
|
||||
|
||||
// ── branch picker ─────────────────────────────────────────────────────
|
||||
// A pi can end up on a branch with no tracking information (checked out by
|
||||
// name, restored from a backup), where `git pull` refuses to run. Being
|
||||
// able to see and change the branch from here beats needing SSH.
|
||||
|
||||
function loadBranches() {
|
||||
const sel = document.getElementById('branch-select');
|
||||
if (!sel) return;
|
||||
|
||||
fetch('/api/v3/system/git-branches')
|
||||
.then(r => r.ok ? r.json() : r.json().then(d => Promise.reject(d.message || `HTTP ${r.status}`)))
|
||||
.then(d => {
|
||||
if (d.status === 'error') {
|
||||
sel.innerHTML = `<option value="">${escHtml(d.message || 'unavailable')}</option>`;
|
||||
sel.disabled = true;
|
||||
return;
|
||||
}
|
||||
sel.innerHTML = '';
|
||||
const add = (name, suffix) => {
|
||||
const o = document.createElement('option');
|
||||
o.value = name;
|
||||
o.textContent = name + (suffix || '');
|
||||
if (name === d.current) o.selected = true;
|
||||
sel.appendChild(o);
|
||||
};
|
||||
(d.local || []).forEach(b => add(b, b === d.current ? ' (current)' : ''));
|
||||
// Remote-only branches are checked out on demand.
|
||||
(d.remote_only || []).forEach(b => add(b, ' (remote)'));
|
||||
if (!sel.options.length) add('', 'no branches found');
|
||||
})
|
||||
.catch(err => {
|
||||
sel.innerHTML = `<option value="">${escHtml(String(err))}</option>`;
|
||||
sel.disabled = true;
|
||||
});
|
||||
}
|
||||
|
||||
window.checkoutBranch = function(stash) {
|
||||
const sel = document.getElementById('branch-select');
|
||||
const branch = sel && sel.value;
|
||||
if (!branch) return;
|
||||
|
||||
setBusy('btn-checkout-branch', true);
|
||||
const el = document.getElementById('result-checkout-branch');
|
||||
if (el) el.classList.add('hidden');
|
||||
|
||||
fetch('/api/v3/system/action', {
|
||||
method: 'POST',
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: JSON.stringify({action: 'checkout_branch', branch: branch, stash: !!stash})
|
||||
})
|
||||
.then(r => r.json().catch(() => ({status: 'error', message: `HTTP ${r.status}`})))
|
||||
.then(d => {
|
||||
const ok = d.status === 'success';
|
||||
// Show git's own list of blocking files, then offer the single
|
||||
// action that clears it. Stashing is never done unasked.
|
||||
showResult('result-checkout-branch', ok, d.message || '', d.detail || '');
|
||||
if (!ok && d.can_retry_with_stash && el) {
|
||||
const retry = document.createElement('div');
|
||||
retry.className = 'mt-2 flex items-center gap-2';
|
||||
const label = document.createElement('span');
|
||||
label.className = 'text-xs text-gray-700';
|
||||
label.textContent = 'Stash these changes and switch anyway?';
|
||||
const btn = document.createElement('button');
|
||||
btn.className = 'inline-flex items-center px-2 py-1 border border-gray-300 text-xs font-medium rounded text-gray-700 bg-white hover:bg-gray-50';
|
||||
btn.textContent = 'Stash and switch';
|
||||
btn.onclick = function() { window.checkoutBranch(true); };
|
||||
retry.appendChild(label);
|
||||
retry.appendChild(btn);
|
||||
el.appendChild(retry);
|
||||
}
|
||||
// Both panels describe the checkout, so refresh them together.
|
||||
loadGitInfo();
|
||||
loadBranches();
|
||||
})
|
||||
.catch(err => showResult('result-checkout-branch', false, String(err)))
|
||||
.finally(() => setBusy('btn-checkout-branch', false));
|
||||
};
|
||||
|
||||
// ── power supply diagnostics panel ────────────────────────────────────────
|
||||
// Reuses the same SSE stream (window.statsSource, set up in base.html)
|
||||
// that already drives the header badge/banner and Overview card, instead
|
||||
@@ -810,6 +914,7 @@
|
||||
|
||||
// Load on first render; HTMX will have already swapped us in by this point.
|
||||
loadGitInfo();
|
||||
loadBranches();
|
||||
|
||||
// Plugin health: initial load + periodic refresh. Guard against duplicate
|
||||
// timers if this partial is re-swapped in by HTMX; the handler re-resolves
|
||||
|
||||
Reference in New Issue
Block a user