summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-12-02 15:29:06 +0000
committertv <tv@krebsco.de>2026-12-02 16:39:05 +0100
commit3baa291b244d65e020c8ba6ce94de8d5cca97213 (patch)
tree68d432b82acc71c65b12077d6cf42a92075c8e8c
parent41f57f6969e889e39a7a2aeabbc890ef2cafbc07 (diff)
git-preview: merge with git merge-tree
The merge happens in the object store, without a worktree, a checkout or a commit. That fixes a conflict stopping the script before it removed its worktree, and the removal of .git/worktrees/NAME failing outside the repository's top directory and in a linked worktree. A conflict now shows in the diff, as conflict markers. merge-tree knows only git's default strategy, so GIT_PREVIEW_MERGE_STRATEGY goes. The commit is checked first, since merge-tree exits with 1, as for a conflict, for a name it can't resolve.
-rw-r--r--pkgs/simple/git-preview/default.nix26
1 files changed, 11 insertions, 15 deletions
diff --git a/pkgs/simple/git-preview/default.nix b/pkgs/simple/git-preview/default.nix
index da152507..f0ed4dfd 100644
--- a/pkgs/simple/git-preview/default.nix
+++ b/pkgs/simple/git-preview/default.nix
@@ -2,19 +2,15 @@
writeDashBin "git-preview" ''
set -efu
- head_commit=$(${git}/bin/git log -1 --format=%H)
- merge_commit=$1; shift
- merge_message='Merge for git-preview'
- preview_dir=$(${coreutils}/bin/mktemp --tmpdir -d git-preview.XXXXXXXX)
- preview_name=$(${coreutils}/bin/basename "$preview_dir")
- ${git}/bin/git worktree add --detach -f "$preview_dir" 2>/dev/null
- ${git}/bin/git -C "$preview_dir" checkout -q "$head_commit"
- ${git}/bin/git -C "$preview_dir" merge \
- ''${GIT_PREVIEW_MERGE_STRATEGY+-s "$GIT_PREVIEW_MERGE_STRATEGY"} \
- -m "$merge_message" \
- -q \
- "$merge_commit"
- ${git}/bin/git -C "$preview_dir" diff "$head_commit.." "$@"
- ${coreutils}/bin/rm -fR "$preview_dir"
- ${coreutils}/bin/rm -R .git/worktrees/"$preview_name"
+ # Checked here: merge-tree fails with exit status 1 for a name it can't
+ # resolve, too, which would pass for a conflict below.
+ merge_commit=$(${git}/bin/git rev-parse --verify "$1^{commit}"); shift
+ # Merges in the object store, without a worktree: exit status 1 means
+ # conflicts, which the tree then holds as conflict markers.
+ output=$(${git}/bin/git merge-tree --write-tree HEAD "$merge_commit") || {
+ status=$?
+ test "$status" = 1 || exit "$status"
+ }
+ tree=$(printf '%s\n' "$output" | ${coreutils}/bin/head -n 1)
+ ${git}/bin/git diff HEAD "$tree" "$@"
''
[cgit] Unable to lock slot /tmp/cgit/55100000.lock: Permission denied (13)