xref: /titanic_44/usr/src/tools/scripts/which_scm.sh (revision 49b225e1cfa7bbf7738d4df0a03f18e3283426eb)
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.
54function 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	echo $scmid
63}
64
65if [[ -n "$CODEMGR_WS" ]]; then
66	if [[ ! -d "$CODEMGR_WS" ]]; then
67		print -u2 "which_scm: $CODEMGR_WS is not a directory."
68		exit 1
69	fi
70	set -- $(primary_type "$CODEMGR_WS")
71	if [[ $# != 1 ]]; then
72		set -- unknown
73	fi
74	echo "$1 $CODEMGR_WS"
75	exit 0
76fi
77
78ORIG_CWD=$(pwd)
79
80if [[ -d RCS ]]; then
81	echo "rcs $ORIG_CWD"
82	exit 0
83fi
84
85# If it's not Teamware, it could just be local SCCS.
86LOCAL_TYPE=
87[[ -d SCCS ]] && LOCAL_TYPE="sccs"
88
89# Scan upwards looking for top of tree.
90DIR=$ORIG_CWD
91CWD_TYPE=$(primary_type "$DIR")
92SCM_TYPE=
93while [[ "$DIR" != / ]]; do
94	set -- $(primary_type "$DIR")
95	if [[ $# > 1 ]]; then
96		echo "unknown $ORIG_CWD"
97		exit 0
98	fi
99	SCM_TYPE="$1"
100	# We're done searching if we hit either a change in type or the top
101	# of a "third type" control system.
102	if [[ "$SCM_TYPE" != "$CWD_TYPE" || "$SCM_TYPE" == mercurial || \
103	    "$SCM_TYPE" == teamware ]]; then
104		break
105	fi
106	PREVDIR="$DIR"
107	DIR=$(dirname "$DIR")
108done
109
110# We assume here that the system root directory isn't the root of the SCM.
111
112# Check for the "second type" of repository.  In all cases, we started
113# out in the tree and stepped out on the last iteration, so we want
114# $PREVDIR.
115if [[ "$CWD_TYPE" == cvs || "$CWD_TYPE" == subversion ]]; then
116	echo "$CWD_TYPE $PREVDIR"
117	exit 0
118fi
119
120# If we still don't know what it is, then check for a local type in the
121# original directory.  If none, then we don't know what it is.
122if [[ -z "$SCM_TYPE" ]]; then
123	if [[ -z "$LOCAL_TYPE" ]]; then
124		SCM_TYPE=unknown
125	else
126		SCM_TYPE=$LOCAL_TYPE
127		DIR=$ORIG_CWD
128	fi
129fi
130
131echo "$SCM_TYPE $DIR"
132exit 0
133