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