exit 1
}
- # cat_file "topic:file"
- # Like `git cat-file blob $1`, but topics '(i)' and '(w)' means index and worktree
+# Make sure we are in the worktree, not under .git; die otherwise
+ensure_git_repo_or_die()
+{
+ local is_inside_repo is_inside_git_dir
+ is_inside_repo=1
+ is_inside_git_dir=$(git rev-parse --is-inside-git-dir 2>/dev/null) ||
+ is_inside_repo=0
+
+ case "$is_inside_repo/$is_inside_git_dir" in
+ 0*) die "Cannot run outside of a Git repository.";;
+ 1/true) die "Cannot run from inside \`.git\` hierarchy, please switch to work-tree.";;
+ esac
+}
+
+ # cat_file TOPIC:PATH [FROM]
+ # cat the file PATH from branch TOPIC when FROM is empty.
+ # FROM can be -i or -w, than the file will be from the index or worktree,
+ # respectively. The caller should than ensure that HEAD is TOPIC, to make sense.
cat_file()
{
- arg="$1"
- case "$arg" in
- '(w):'*)
- arg=$(echo "$arg" | tail --bytes=+5)
- cat "$arg"
- return
+ path="$1"
+ case "${2-}" in
+ -w)
+ cat "$root_dir/${path#*:}"
;;
- '(i):'*)
+ -i)
# ':file' means cat from index
- arg=$(echo "$arg" | tail --bytes=+5)
- git cat-file blob ":$arg"
+ git cat-file blob ":${path#*:}"
+ ;;
+ '')
+ git cat-file blob "$path"
;;
*)
- git cat-file blob "$arg"
+ die "Wrong argument to cat_file: '$2'"
+ ;;
esac
}