cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson#!/usr/bin/ksh -p
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson#
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# CDDL HEADER START
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson#
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# The contents of this file are subject to the terms of the
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# Common Development and Distribution License (the "License").
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# You may not use this file except in compliance with the License.
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson#
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# or http://www.opensolaris.org/os/licensing.
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# See the License for the specific language governing permissions
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# and limitations under the License.
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson#
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# When distributing Covered Code, include this CDDL HEADER in each
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# If applicable, add the following below this CDDL HEADER, with the
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# fields enclosed by brackets "[]" replaced with your own identifying
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# information: Portions Copyright [yyyy] [name of copyright owner]
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson#
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# CDDL HEADER END
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson#
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson#
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# Copyright 2008 Sun Microsystems, Inc. All rights reserved.
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# Use is subject to license terms.
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson#
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# which_scm outputs two strings: one identifying the SCM in use, and
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# the second giving the root directory for the SCM, if known, or just
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# the current working directory if not known.
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# There are three distinct types of SCM systems we can detect. The first
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# type have a control directory per directory (RCS and SCCS), with no other
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# structure. The second type have a control directory in each subdirectory
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# within a tree (CVS and SVN). The last type have a single control
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# directory at the top of the tree (Teamware and Mercurial).
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# If the common CODEMGR_WS variable is set, then we look there for the
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# SCM type and bail out if we can't determine it.
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# If that variable is not set, then we start in the current directory
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# and work our way upwards until we find the top of the tree or we
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# encounter an error.
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# We do handle nested SCM types, and report the innermost one, but if
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# you nest one of the "second type" systems within another instance of
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# itself, we'll keep going upwards and report the top of the nested
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# set of trees.
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# Check for well-known tree-type source code management (SCM) systems.
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelsonfunction primary_type
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson{
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson typeset scmid
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson [ -d "$1/Codemgr_wsdata" ] && scmid="$scmid teamware"
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson [ -d "$1/.hg" ] && scmid="$scmid mercurial"
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson [ -d "$1/CVS" ] && scmid="$scmid cvs"
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson [ -d "$1/.svn" ] && scmid="$scmid subversion"
8bcea973790ad3e762bf78b7c6ad5776e463fd51Richard Lowe [ -d "$1/.git" ] && scmid="$scmid git"
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson echo $scmid
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson}
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelsonif [[ -n "$CODEMGR_WS" ]]; then
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson if [[ ! -d "$CODEMGR_WS" ]]; then
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson print -u2 "which_scm: $CODEMGR_WS is not a directory."
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson exit 1
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson fi
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson set -- $(primary_type "$CODEMGR_WS")
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson if [[ $# != 1 ]]; then
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson set -- unknown
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson fi
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson echo "$1 $CODEMGR_WS"
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson exit 0
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelsonfi
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelsonORIG_CWD=$(pwd)
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelsonif [[ -d RCS ]]; then
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson echo "rcs $ORIG_CWD"
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson exit 0
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelsonfi
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# If it's not Teamware, it could just be local SCCS.
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelsonLOCAL_TYPE=
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson[[ -d SCCS ]] && LOCAL_TYPE="sccs"
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# Scan upwards looking for top of tree.
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelsonDIR=$ORIG_CWD
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelsonCWD_TYPE=$(primary_type "$DIR")
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelsonSCM_TYPE=
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelsonwhile [[ "$DIR" != / ]]; do
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson set -- $(primary_type "$DIR")
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson if [[ $# > 1 ]]; then
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson echo "unknown $ORIG_CWD"
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson exit 0
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson fi
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson SCM_TYPE="$1"
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson # We're done searching if we hit either a change in type or the top
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson # of a "third type" control system.
8bcea973790ad3e762bf78b7c6ad5776e463fd51Richard Lowe if [[ "$SCM_TYPE" != "$CWD_TYPE" || "$SCM_TYPE" == git || \
8bcea973790ad3e762bf78b7c6ad5776e463fd51Richard Lowe "$SCM_TYPE" == mercurial || "$SCM_TYPE" == teamware ]]; then
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson break
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson fi
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson PREVDIR="$DIR"
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson DIR=$(dirname "$DIR")
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelsondone
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# We assume here that the system root directory isn't the root of the SCM.
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# Check for the "second type" of repository. In all cases, we started
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# out in the tree and stepped out on the last iteration, so we want
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# $PREVDIR.
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelsonif [[ "$CWD_TYPE" == cvs || "$CWD_TYPE" == subversion ]]; then
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson echo "$CWD_TYPE $PREVDIR"
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson exit 0
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelsonfi
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# If we still don't know what it is, then check for a local type in the
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson# original directory. If none, then we don't know what it is.
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelsonif [[ -z "$SCM_TYPE" ]]; then
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson if [[ -z "$LOCAL_TYPE" ]]; then
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson SCM_TYPE=unknown
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson else
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson SCM_TYPE=$LOCAL_TYPE
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson DIR=$ORIG_CWD
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson fi
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelsonfi
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelson
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelsonecho "$SCM_TYPE $DIR"
cdf0c1d55d9b3b6beaf994835440dfb01aef5cf0mjnelsonexit 0