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