PageRenderTime 25ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/usr/src/tools/scripts/which_scm.sh

https://github.com/richlowe/illumos-gate
Shell | 131 lines | 60 code | 18 blank | 53 comment | 27 complexity | 35398d4b1270faa548ad004e34d21955 MD5 | raw file
  1. #!/usr/bin/ksh -p
  2. #
  3. # CDDL HEADER START
  4. #
  5. # The contents of this file are subject to the terms of the
  6. # Common Development and Distribution License (the "License").
  7. # You may not use this file except in compliance with the License.
  8. #
  9. # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  10. # or http://www.opensolaris.org/os/licensing.
  11. # See the License for the specific language governing permissions
  12. # and limitations under the License.
  13. #
  14. # When distributing Covered Code, include this CDDL HEADER in each
  15. # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  16. # If applicable, add the following below this CDDL HEADER, with the
  17. # fields enclosed by brackets "[]" replaced with your own identifying
  18. # information: Portions Copyright [yyyy] [name of copyright owner]
  19. #
  20. # CDDL HEADER END
  21. #
  22. #
  23. # Copyright 2008 Sun Microsystems, Inc. All rights reserved.
  24. # Use is subject to license terms.
  25. #
  26. # which_scm outputs two strings: one identifying the SCM in use, and
  27. # the second giving the root directory for the SCM, if known, or just
  28. # the current working directory if not known.
  29. # There are three distinct types of SCM systems we can detect. The first
  30. # type have a control directory per directory (RCS and SCCS), with no other
  31. # structure. The second type have a control directory in each subdirectory
  32. # within a tree (CVS and SVN). The last type have a single control
  33. # directory at the top of the tree (Teamware and Mercurial).
  34. # If the common CODEMGR_WS variable is set, then we look there for the
  35. # SCM type and bail out if we can't determine it.
  36. # If that variable is not set, then we start in the current directory
  37. # and work our way upwards until we find the top of the tree or we
  38. # encounter an error.
  39. # We do handle nested SCM types, and report the innermost one, but if
  40. # you nest one of the "second type" systems within another instance of
  41. # itself, we'll keep going upwards and report the top of the nested
  42. # set of trees.
  43. # Check for well-known tree-type source code management (SCM) systems.
  44. function primary_type
  45. {
  46. typeset scmid
  47. [ -d "$1/Codemgr_wsdata" ] && scmid="$scmid teamware"
  48. [ -d "$1/.hg" ] && scmid="$scmid mercurial"
  49. [ -d "$1/CVS" ] && scmid="$scmid cvs"
  50. [ -d "$1/.svn" ] && scmid="$scmid subversion"
  51. [ -d "$1/.git" ] && scmid="$scmid git"
  52. echo $scmid
  53. }
  54. if [[ -n "$CODEMGR_WS" ]]; then
  55. if [[ ! -d "$CODEMGR_WS" ]]; then
  56. print -u2 "which_scm: $CODEMGR_WS is not a directory."
  57. exit 1
  58. fi
  59. set -- $(primary_type "$CODEMGR_WS")
  60. if [[ $# != 1 ]]; then
  61. set -- unknown
  62. fi
  63. echo "$1 $CODEMGR_WS"
  64. exit 0
  65. fi
  66. ORIG_CWD=$(pwd)
  67. if [[ -d RCS ]]; then
  68. echo "rcs $ORIG_CWD"
  69. exit 0
  70. fi
  71. # If it's not Teamware, it could just be local SCCS.
  72. LOCAL_TYPE=
  73. [[ -d SCCS ]] && LOCAL_TYPE="sccs"
  74. # Scan upwards looking for top of tree.
  75. DIR=$ORIG_CWD
  76. CWD_TYPE=$(primary_type "$DIR")
  77. SCM_TYPE=
  78. while [[ "$DIR" != / ]]; do
  79. set -- $(primary_type "$DIR")
  80. if [[ $# > 1 ]]; then
  81. echo "unknown $ORIG_CWD"
  82. exit 0
  83. fi
  84. SCM_TYPE="$1"
  85. # We're done searching if we hit either a change in type or the top
  86. # of a "third type" control system.
  87. if [[ "$SCM_TYPE" != "$CWD_TYPE" || "$SCM_TYPE" == git || \
  88. "$SCM_TYPE" == mercurial || "$SCM_TYPE" == teamware ]]; then
  89. break
  90. fi
  91. PREVDIR="$DIR"
  92. DIR=$(dirname "$DIR")
  93. done
  94. # We assume here that the system root directory isn't the root of the SCM.
  95. # Check for the "second type" of repository. In all cases, we started
  96. # out in the tree and stepped out on the last iteration, so we want
  97. # $PREVDIR.
  98. if [[ "$CWD_TYPE" == cvs || "$CWD_TYPE" == subversion ]]; then
  99. echo "$CWD_TYPE $PREVDIR"
  100. exit 0
  101. fi
  102. # If we still don't know what it is, then check for a local type in the
  103. # original directory. If none, then we don't know what it is.
  104. if [[ -z "$SCM_TYPE" ]]; then
  105. if [[ -z "$LOCAL_TYPE" ]]; then
  106. SCM_TYPE=unknown
  107. else
  108. SCM_TYPE=$LOCAL_TYPE
  109. DIR=$ORIG_CWD
  110. fi
  111. fi
  112. echo "$SCM_TYPE $DIR"
  113. exit 0