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