logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

git-sh-setup (8398B)


  1. # This shell scriplet is meant to be included by other shell scripts
  2. # to set up some variables pointing at the normal git directories and
  3. # a few helper shell functions.
  4. # Having this variable in your environment would break scripts because
  5. # you would cause "cd" to be taken to unexpected places. If you
  6. # like CDPATH, define it for your interactive shell sessions without
  7. # exporting it.
  8. # But we protect ourselves from such a user mistake nevertheless.
  9. unset CDPATH
  10. # Similarly for IFS, but some shells (e.g. FreeBSD 7.2) are buggy and
  11. # do not equate an unset IFS with IFS with the default, so here is
  12. # an explicit SP HT LF.
  13. IFS='
  14. '
  15. git_broken_path_fix () {
  16. case ":$PATH:" in
  17. *:$1:*) : ok ;;
  18. *)
  19. PATH=$(
  20. SANE_TOOL_PATH="$1"
  21. IFS=: path= sep=
  22. set x $PATH
  23. shift
  24. for elem
  25. do
  26. case "$SANE_TOOL_PATH:$elem" in
  27. (?*:/bin | ?*:/usr/bin)
  28. path="$path$sep$SANE_TOOL_PATH"
  29. sep=:
  30. SANE_TOOL_PATH=
  31. esac
  32. path="$path$sep$elem"
  33. sep=:
  34. done
  35. echo "$path"
  36. )
  37. ;;
  38. esac
  39. }
  40. # Source git-sh-i18n for gettext support.
  41. . "$(git --exec-path)/git-sh-i18n"
  42. die () {
  43. die_with_status 1 "$@"
  44. }
  45. die_with_status () {
  46. status=$1
  47. shift
  48. printf >&2 '%s\n' "$*"
  49. exit "$status"
  50. }
  51. if test -n "$OPTIONS_SPEC"; then
  52. usage() {
  53. "$0" -h
  54. exit 1
  55. }
  56. parseopt_extra=
  57. [ -n "$OPTIONS_KEEPDASHDASH" ] &&
  58. parseopt_extra="--keep-dashdash"
  59. [ -n "$OPTIONS_STUCKLONG" ] &&
  60. parseopt_extra="$parseopt_extra --stuck-long"
  61. eval "$(
  62. echo "$OPTIONS_SPEC" |
  63. git rev-parse --parseopt $parseopt_extra -- "$@" ||
  64. echo exit $?
  65. )"
  66. else
  67. dashless=$(basename -- "$0" | sed -e 's/-/ /')
  68. usage() {
  69. die "$(eval_gettext "usage: \$dashless \$USAGE")"
  70. }
  71. if [ -z "$LONG_USAGE" ]
  72. then
  73. LONG_USAGE="$(eval_gettext "usage: \$dashless \$USAGE")"
  74. else
  75. LONG_USAGE="$(eval_gettext "usage: \$dashless \$USAGE
  76. $LONG_USAGE")"
  77. fi
  78. case "$1" in
  79. -h)
  80. echo "$LONG_USAGE"
  81. exit
  82. esac
  83. fi
  84. # Set the name of the end-user facing command in the reflog when the
  85. # script may update refs. When GIT_REFLOG_ACTION is already set, this
  86. # will not overwrite it, so that a scripted Porcelain (e.g. "git
  87. # rebase") can set it to its own name (e.g. "rebase") and then call
  88. # another scripted Porcelain (e.g. "git am") and a call to this
  89. # function in the latter will keep the name of the end-user facing
  90. # program (e.g. "rebase") in GIT_REFLOG_ACTION, ensuring whatever it
  91. # does will be record as actions done as part of the end-user facing
  92. # operation (e.g. "rebase").
  93. #
  94. # NOTE NOTE NOTE: consequently, after assigning a specific message to
  95. # GIT_REFLOG_ACTION when calling a "git" command to record a custom
  96. # reflog message, do not leave that custom value in GIT_REFLOG_ACTION,
  97. # after you are done. Other callers of "git" commands that rely on
  98. # writing the default "program name" in reflog expect the variable to
  99. # contain the value set by this function.
  100. #
  101. # To use a custom reflog message, do either one of these three:
  102. #
  103. # (a) use a single-shot export form:
  104. # GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: preparing frotz" \
  105. # git command-that-updates-a-ref
  106. #
  107. # (b) save the original away and restore:
  108. # SAVED_ACTION=$GIT_REFLOG_ACTION
  109. # GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: preparing frotz"
  110. # git command-that-updates-a-ref
  111. # GIT_REFLOG_ACITON=$SAVED_ACTION
  112. #
  113. # (c) assign the variable in a subshell:
  114. # (
  115. # GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: preparing frotz"
  116. # git command-that-updates-a-ref
  117. # )
  118. set_reflog_action() {
  119. if [ -z "${GIT_REFLOG_ACTION:+set}" ]
  120. then
  121. GIT_REFLOG_ACTION="$*"
  122. export GIT_REFLOG_ACTION
  123. fi
  124. }
  125. git_editor() {
  126. if test -z "${GIT_EDITOR:+set}"
  127. then
  128. GIT_EDITOR="$(git var GIT_EDITOR)" || return $?
  129. fi
  130. eval "$GIT_EDITOR" '"$@"'
  131. }
  132. git_pager() {
  133. if test -t 1
  134. then
  135. GIT_PAGER=$(git var GIT_PAGER)
  136. else
  137. GIT_PAGER=cat
  138. fi
  139. for vardef in LESS=FRX LV=-c
  140. do
  141. var=${vardef%%=*}
  142. eval ": \"\${$vardef}\" && export $var"
  143. done
  144. eval "$GIT_PAGER" '"$@"'
  145. }
  146. is_bare_repository () {
  147. git rev-parse --is-bare-repository
  148. }
  149. cd_to_toplevel () {
  150. cdup=$(git rev-parse --show-toplevel) &&
  151. cd "$cdup" || {
  152. gettextln "Cannot chdir to \$cdup, the toplevel of the working tree" >&2
  153. exit 1
  154. }
  155. }
  156. require_work_tree_exists () {
  157. if test "z$(git rev-parse --is-bare-repository)" != zfalse
  158. then
  159. program_name=$0
  160. die "$(eval_gettext "fatal: \$program_name cannot be used without a working tree.")"
  161. fi
  162. }
  163. require_work_tree () {
  164. test "$(git rev-parse --is-inside-work-tree 2>/dev/null)" = true || {
  165. program_name=$0
  166. die "$(eval_gettext "fatal: \$program_name cannot be used without a working tree.")"
  167. }
  168. }
  169. require_clean_work_tree () {
  170. git rev-parse --verify HEAD >/dev/null || exit 1
  171. git update-index -q --ignore-submodules --refresh
  172. err=0
  173. if ! git diff-files --quiet --ignore-submodules
  174. then
  175. action=$1
  176. case "$action" in
  177. "rewrite branches")
  178. gettextln "Cannot rewrite branches: You have unstaged changes." >&2
  179. ;;
  180. *)
  181. eval_gettextln "Cannot \$action: You have unstaged changes." >&2
  182. ;;
  183. esac
  184. err=1
  185. fi
  186. if ! git diff-index --cached --quiet --ignore-submodules HEAD --
  187. then
  188. if test $err = 0
  189. then
  190. action=$1
  191. eval_gettextln "Cannot \$action: Your index contains uncommitted changes." >&2
  192. else
  193. gettextln "Additionally, your index contains uncommitted changes." >&2
  194. fi
  195. err=1
  196. fi
  197. if test $err = 1
  198. then
  199. test -n "$2" && echo "$2" >&2
  200. exit 1
  201. fi
  202. }
  203. # Generate a sed script to parse identities from a commit.
  204. #
  205. # Reads the commit from stdin, which should be in raw format (e.g., from
  206. # cat-file or "--pretty=raw").
  207. #
  208. # The first argument specifies the ident line to parse (e.g., "author"), and
  209. # the second specifies the environment variable to put it in (e.g., "AUTHOR"
  210. # for "GIT_AUTHOR_*"). Multiple pairs can be given to parse author and
  211. # committer.
  212. pick_ident_script () {
  213. while test $# -gt 0
  214. do
  215. lid=$1; shift
  216. uid=$1; shift
  217. printf '%s' "
  218. /^$lid /{
  219. s/'/'\\\\''/g
  220. h
  221. s/^$lid "'\([^<]*\) <[^>]*> .*$/\1/'"
  222. s/.*/GIT_${uid}_NAME='&'/p
  223. g
  224. s/^$lid "'[^<]* <\([^>]*\)> .*$/\1/'"
  225. s/.*/GIT_${uid}_EMAIL='&'/p
  226. g
  227. s/^$lid "'[^<]* <[^>]*> \(.*\)$/@\1/'"
  228. s/.*/GIT_${uid}_DATE='&'/p
  229. }
  230. "
  231. done
  232. echo '/^$/q'
  233. }
  234. # Create a pick-script as above and feed it to sed. Stdout is suitable for
  235. # feeding to eval.
  236. parse_ident_from_commit () {
  237. LANG=C LC_ALL=C sed -ne "$(pick_ident_script "$@")"
  238. }
  239. # Parse the author from a commit given as an argument. Stdout is suitable for
  240. # feeding to eval to set the usual GIT_* ident variables.
  241. get_author_ident_from_commit () {
  242. encoding=$(git config i18n.commitencoding || echo UTF-8)
  243. git show -s --pretty=raw --encoding="$encoding" "$1" -- |
  244. parse_ident_from_commit author AUTHOR
  245. }
  246. # Generate a virtual base file for a two-file merge. Uses git apply to
  247. # remove lines from $1 that are not in $2, leaving only common lines.
  248. create_virtual_base() {
  249. sz0=$(wc -c <"$1")
  250. diff -u -La/"$1" -Lb/"$1" "$1" "$2" | git apply --no-add
  251. sz1=$(wc -c <"$1")
  252. # If we do not have enough common material, it is not
  253. # worth trying two-file merge using common subsections.
  254. expr $sz0 \< $sz1 \* 2 >/dev/null || : >"$1"
  255. }
  256. # Platform specific tweaks to work around some commands
  257. case $(uname -s) in
  258. *MINGW*)
  259. # Windows has its own (incompatible) sort and find
  260. sort () {
  261. /usr/bin/sort "$@"
  262. }
  263. find () {
  264. /usr/bin/find "$@"
  265. }
  266. # git sees Windows-style pwd
  267. pwd () {
  268. builtin pwd -W
  269. }
  270. is_absolute_path () {
  271. case "$1" in
  272. [/\\]* | [A-Za-z]:*)
  273. return 0 ;;
  274. esac
  275. return 1
  276. }
  277. ;;
  278. *)
  279. is_absolute_path () {
  280. case "$1" in
  281. /*)
  282. return 0 ;;
  283. esac
  284. return 1
  285. }
  286. esac
  287. # Make sure we are in a valid repository of a vintage we understand,
  288. # if we require to be in a git repository.
  289. git_dir_init () {
  290. GIT_DIR=$(git rev-parse --git-dir) || exit
  291. if [ -z "$SUBDIRECTORY_OK" ]
  292. then
  293. test -z "$(git rev-parse --show-cdup)" || {
  294. exit=$?
  295. gettextln "You need to run this command from the toplevel of the working tree." >&2
  296. exit $exit
  297. }
  298. fi
  299. test -n "$GIT_DIR" && GIT_DIR=$(cd "$GIT_DIR" && pwd) || {
  300. gettextln "Unable to determine absolute path of git directory" >&2
  301. exit 1
  302. }
  303. : "${GIT_OBJECT_DIRECTORY="$(git rev-parse --git-path objects)"}"
  304. }
  305. if test -z "$NONGIT_OK"
  306. then
  307. git_dir_init
  308. fi
  309. peel_committish () {
  310. case "$1" in
  311. :/*)
  312. peeltmp=$(git rev-parse --verify "$1") &&
  313. git rev-parse --verify "${peeltmp}^0"
  314. ;;
  315. *)
  316. git rev-parse --verify "${1}^0"
  317. ;;
  318. esac
  319. }