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