Skip to content

Commit 5bf518c

Browse files
committed
Unstage selected lines of a staged addition as a modification of the index entry
A partial selection on a creation diff leaves other lines in place, and git refuses to reverse part of a creation patch ("new file depends on old contents"). For line operations, rewrite the header as a modification of the same path with the old side starting at line 1, so the selected lines are removed from the index entry and nothing else moves. Selecting every line keeps the creation header so the entry is removed outright, the same as the hunk-level button.
1 parent 8fa0a35 commit 5bf518c

1 file changed

Lines changed: 24 additions & 1 deletion

File tree

‎Resources/html/views/commit/commit.js‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,12 +381,35 @@ var stageLines = function(reverse) {
381381
patch += l+"\n";
382382
}
383383
}
384-
patch = diffHeader + '\n' + "@@ -" + start_old.toString() + "," + count[0].toString() +
384+
var header = diffHeader;
385+
if (start_old == 0 && count[0] > 0) {
386+
// A partial selection on an addition leaves other lines in place, and git
387+
// refuses a partial reverse of a creation patch ("new file depends on old
388+
// contents"). Present it as a modification of the index entry instead:
389+
// no "new file mode", both sides named, old side starting at line 1.
390+
// Selecting every line keeps the creation header so the entry is removed
391+
// outright, the same as the hunk-level button
392+
header = modificationHeader(diffHeader);
393+
start_old = 1;
394+
}
395+
patch = header + '\n' + "@@ -" + start_old.toString() + "," + count[0].toString() +
385396
" +" + start_new.toString() + "," + count[1].toString() + " @@\n"+patch;
386397

387398
addHunkText(patch,reverse);
388399
}
389400

401+
/* Turn the header of a creation diff into the header of a modification of
402+
* the same path. The names come from the "diff --git A B" line, which git
403+
* writes with both sides the same length, quoted or not, so there is no
404+
* need to guess where a path with spaces ends */
405+
var modificationHeader = function(header)
406+
{
407+
var names = header.split("\n")[0].replace(/^diff --git /, "");
408+
var half = (names.length - 1) / 2;
409+
var a = names.substr(0, half), b = names.substr(half + 1);
410+
return "diff --git " + a + " " + b + "\n--- " + a + "\n+++ " + b;
411+
}
412+
390413
/* Compute the selection before actually making it. Return as object
391414
* with 2-element array "bounds", and "good", which indicates if the
392415
* selection contains add/del lines. */

0 commit comments

Comments
 (0)