THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

400 lines
9.1KB

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