Skip to content

Commit

Permalink
ubuntu .gitattributes workaround
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-williams committed Dec 26, 2024
1 parent e098b5c commit 37759fa
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions git-diff-dvc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,37 @@ init_tmppath() {

prefix0=a
prefix1=b
tmppath0=$(init_tmppath "$path0" $prefix0)
tmppath1=$(init_tmppath "$path1" $prefix1)
relpath0=$(init_tmppath "$path0" $prefix0)
relpath1=$(init_tmppath "$path1" $prefix1)

# We essentially want to run:
#
# ```bash
# GIT_DIR="$orig_dir/.git" GIT_WORK_TREE="$orig_dir" git diff --no-index --ext-diff "$relpath0" "$relpath1"
# ```
#
# - `$GIT_DIR` picks up configs from the original repo.
# - `$GIT_WORK_TREE` should pick up `.gitattributes` (in the root of the Git repo, not contained in `.git` / `$GIT_DIR`).
#
# However, on Ubuntu, something about setting the latter results in the correct diff driver not being selected (even though e.g. `git check-attr diff $relpath1` will detect it, with the env vars above set). On MacOS it seems to work as expected.
#
# As a workaround, we manually ask `check-attr diff` for each file's "type", and write them to `.gitattributes` in the current directory (`$tmpdir`).
#
# TODO: would creating `$tmpdir` in the Git worktree make this unnecessary?

get_attr_type() {
if [ -z "$1" ]; then return 0; fi
if [ "$1" == /dev/null ]; then return 0; fi
# if [ "${1:0:1}" == "/" ]; then return 0; fi
type="$(GIT_WORK_TREE="$orig_dir" git check-attr diff "$1" | sed -E 's/.*: diff: (.*)/\1/')"
if [ "$type" != "unset" ] && [ "$type" != "unspecified" ]; then
echo "$1 diff=$type" >> .gitattributes
fi
}

get_attr_type "$relpath0"
get_attr_type "$relpath1"

set +e
GIT_DIR="$orig_dir/.git" git diff --no-index --ext-diff "$tmppath0" "$tmppath1"
GIT_DIR="$orig_dir/.git" git diff --no-index --ext-diff "$relpath0" "$relpath1"
exit 0

0 comments on commit 37759fa

Please # to comment.