8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / tools / scripts / which_scm.sh
blobc8a4b1e4297b871689392eafd68dc7cd264c004f
1 #!/usr/bin/ksh -p
3 # CDDL HEADER START
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.
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.
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]
20 # CDDL HEADER END
24 # Copyright 2008 Sun Microsystems, Inc. All rights reserved.
25 # Use is subject to license terms.
28 # which_scm outputs two strings: one identifying the SCM in use, and
29 # the second giving the root directory for the SCM, if known, or just
30 # the current working directory if not known.
32 # There are three distinct types of SCM systems we can detect. The first
33 # type have a control directory per directory (RCS and SCCS), with no other
34 # structure. The second type have a control directory in each subdirectory
35 # within a tree (CVS and SVN). The last type have a single control
36 # directory at the top of the tree (Teamware and Mercurial).
38 # If the common CODEMGR_WS variable is set, then we look there for the
39 # SCM type and bail out if we can't determine it.
41 # If that variable is not set, then we start in the current directory
42 # and work our way upwards until we find the top of the tree or we
43 # encounter an error.
45 # We do handle nested SCM types, and report the innermost one, but if
46 # you nest one of the "second type" systems within another instance of
47 # itself, we'll keep going upwards and report the top of the nested
48 # set of trees.
51 # Check for well-known tree-type source code management (SCM) systems.
52 function primary_type
54 typeset scmid
56 [ -d "$1/Codemgr_wsdata" ] && scmid="$scmid teamware"
57 [ -d "$1/.hg" ] && scmid="$scmid mercurial"
58 [ -d "$1/CVS" ] && scmid="$scmid cvs"
59 [ -d "$1/.svn" ] && scmid="$scmid subversion"
60 [ -d "$1/.git" ] && scmid="$scmid git"
61 echo $scmid
64 if [[ -n "$CODEMGR_WS" ]]; then
65 if [[ ! -d "$CODEMGR_WS" ]]; then
66 print -u2 "which_scm: $CODEMGR_WS is not a directory."
67 exit 1
69 set -- $(primary_type "$CODEMGR_WS")
70 if [[ $# != 1 ]]; then
71 set -- unknown
73 echo "$1 $CODEMGR_WS"
74 exit 0
77 ORIG_CWD=$(pwd)
79 if [[ -d RCS ]]; then
80 echo "rcs $ORIG_CWD"
81 exit 0
84 # If it's not Teamware, it could just be local SCCS.
85 LOCAL_TYPE=
86 [[ -d SCCS ]] && LOCAL_TYPE="sccs"
88 # Scan upwards looking for top of tree.
89 DIR=$ORIG_CWD
90 CWD_TYPE=$(primary_type "$DIR")
91 SCM_TYPE=
92 while [[ "$DIR" != / ]]; do
93 set -- $(primary_type "$DIR")
94 if [[ $# > 1 ]]; then
95 echo "unknown $ORIG_CWD"
96 exit 0
98 SCM_TYPE="$1"
99 # We're done searching if we hit either a change in type or the top
100 # of a "third type" control system.
101 if [[ "$SCM_TYPE" != "$CWD_TYPE" || "$SCM_TYPE" == git || \
102 "$SCM_TYPE" == mercurial || "$SCM_TYPE" == teamware ]]; then
103 break
105 PREVDIR="$DIR"
106 DIR=$(dirname "$DIR")
107 done
109 # We assume here that the system root directory isn't the root of the SCM.
111 # Check for the "second type" of repository. In all cases, we started
112 # out in the tree and stepped out on the last iteration, so we want
113 # $PREVDIR.
114 if [[ "$CWD_TYPE" == cvs || "$CWD_TYPE" == subversion ]]; then
115 echo "$CWD_TYPE $PREVDIR"
116 exit 0
119 # If we still don't know what it is, then check for a local type in the
120 # original directory. If none, then we don't know what it is.
121 if [[ -z "$SCM_TYPE" ]]; then
122 if [[ -z "$LOCAL_TYPE" ]]; then
123 SCM_TYPE=unknown
124 else
125 SCM_TYPE=$LOCAL_TYPE
126 DIR=$ORIG_CWD
130 echo "$SCM_TYPE $DIR"
131 exit 0