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.

197 lines
4.3KB

  1. #!/bin/sh
  2. #
  3. # This program launch a web browser on the html page
  4. # describing a git command.
  5. #
  6. # Copyright (c) 2007 Christian Couder
  7. # Copyright (c) 2006 Theodore Y. Ts'o
  8. #
  9. # This file is heavily stolen from git-mergetool.sh, by
  10. # Theodore Y. Ts'o (thanks) that is:
  11. #
  12. # Copyright (c) 2006 Theodore Y. Ts'o
  13. #
  14. # This file is licensed under the GPL v2, or a later version
  15. # at the discretion of Junio C Hamano or any other official
  16. # git maintainer.
  17. #
  18. USAGE='[--browser=browser|--tool=browser] [--config=conf.var] url/file ...'
  19. # This must be capable of running outside of git directory, so
  20. # the vanilla git-sh-setup should not be used.
  21. NONGIT_OK=Yes
  22. . git-sh-setup
  23. valid_custom_tool()
  24. {
  25. browser_cmd="$(git config "browser.$1.cmd")"
  26. test -n "$browser_cmd"
  27. }
  28. valid_tool() {
  29. case "$1" in
  30. firefox | iceweasel | seamonkey | iceape | \
  31. chrome | google-chrome | chromium | chromium-browser | \
  32. konqueror | opera | w3m | elinks | links | lynx | dillo | open | \
  33. start | cygstart | xdg-open)
  34. ;; # happy
  35. *)
  36. valid_custom_tool "$1" || return 1
  37. ;;
  38. esac
  39. }
  40. init_browser_path() {
  41. browser_path=$(git config "browser.$1.path")
  42. if test -z "$browser_path" &&
  43. test "$1" = chromium &&
  44. type chromium-browser >/dev/null 2>&1
  45. then
  46. browser_path=chromium-browser
  47. fi
  48. : ${browser_path:="$1"}
  49. }
  50. while test $# != 0
  51. do
  52. case "$1" in
  53. -b|--browser*|-t|--tool*)
  54. case "$#,$1" in
  55. *,*=*)
  56. browser=$(expr "z$1" : 'z-[^=]*=\(.*\)')
  57. ;;
  58. 1,*)
  59. usage ;;
  60. *)
  61. browser="$2"
  62. shift ;;
  63. esac
  64. ;;
  65. -c|--config*)
  66. case "$#,$1" in
  67. *,*=*)
  68. conf=$(expr "z$1" : 'z-[^=]*=\(.*\)')
  69. ;;
  70. 1,*)
  71. usage ;;
  72. *)
  73. conf="$2"
  74. shift ;;
  75. esac
  76. ;;
  77. --)
  78. break
  79. ;;
  80. -*)
  81. usage
  82. ;;
  83. *)
  84. break
  85. ;;
  86. esac
  87. shift
  88. done
  89. test $# = 0 && usage
  90. if test -z "$browser"
  91. then
  92. for opt in "$conf" "web.browser"
  93. do
  94. test -z "$opt" && continue
  95. browser="$(git config $opt)"
  96. test -z "$browser" || break
  97. done
  98. if test -n "$browser" && ! valid_tool "$browser"; then
  99. echo >&2 "git config option $opt set to unknown browser: $browser"
  100. echo >&2 "Resetting to default..."
  101. unset browser
  102. fi
  103. fi
  104. if test -z "$browser" ; then
  105. if test -n "$DISPLAY"; then
  106. browser_candidates="firefox iceweasel google-chrome chrome chromium chromium-browser konqueror opera seamonkey iceape w3m elinks links lynx dillo xdg-open"
  107. if test "$KDE_FULL_SESSION" = "true"; then
  108. browser_candidates="konqueror $browser_candidates"
  109. fi
  110. else
  111. browser_candidates="w3m elinks links lynx"
  112. fi
  113. # SECURITYSESSIONID indicates an OS X GUI login session
  114. if test -n "$SECURITYSESSIONID" || test -n "$TERM_PROGRAM"
  115. then
  116. browser_candidates="open $browser_candidates"
  117. fi
  118. # /bin/start indicates MinGW
  119. if test -x /bin/start; then
  120. browser_candidates="start $browser_candidates"
  121. fi
  122. # /usr/bin/cygstart indicates Cygwin
  123. if test -x /usr/bin/cygstart; then
  124. browser_candidates="cygstart $browser_candidates"
  125. fi
  126. for i in $browser_candidates; do
  127. init_browser_path $i
  128. if type "$browser_path" > /dev/null 2>&1; then
  129. browser=$i
  130. break
  131. fi
  132. done
  133. test -z "$browser" && die "No known browser available."
  134. else
  135. valid_tool "$browser" || die "Unknown browser '$browser'."
  136. init_browser_path "$browser"
  137. if test -z "$browser_cmd" && ! type "$browser_path" > /dev/null 2>&1; then
  138. die "The browser $browser is not available as '$browser_path'."
  139. fi
  140. fi
  141. case "$browser" in
  142. firefox|iceweasel|seamonkey|iceape)
  143. # Check version because firefox < 2.0 does not support "-new-tab".
  144. vers=$(expr "$($browser_path -version)" : '.* \([0-9][0-9]*\)\..*')
  145. NEWTAB='-new-tab'
  146. test "$vers" -lt 2 && NEWTAB=''
  147. "$browser_path" $NEWTAB "$@" &
  148. ;;
  149. google-chrome|chrome|chromium|chromium-browser)
  150. # No need to specify newTab. It's default in chromium
  151. "$browser_path" "$@" &
  152. ;;
  153. konqueror)
  154. case "$(basename "$browser_path")" in
  155. konqueror)
  156. # It's simpler to use kfmclient to open a new tab in konqueror.
  157. browser_path="$(echo "$browser_path" | sed -e 's/konqueror$/kfmclient/')"
  158. type "$browser_path" > /dev/null 2>&1 || die "No '$browser_path' found."
  159. "$browser_path" newTab "$@" &
  160. ;;
  161. kfmclient)
  162. "$browser_path" newTab "$@" &
  163. ;;
  164. *)
  165. "$browser_path" "$@" &
  166. ;;
  167. esac
  168. ;;
  169. w3m|elinks|links|lynx|open|cygstart|xdg-open)
  170. "$browser_path" "$@"
  171. ;;
  172. start)
  173. exec "$browser_path" '"web-browse"' "$@"
  174. ;;
  175. opera|dillo)
  176. "$browser_path" "$@" &
  177. ;;
  178. *)
  179. if test -n "$browser_cmd"; then
  180. ( eval "$browser_cmd \"\$@\"" )
  181. fi
  182. ;;
  183. esac