xref: /titanic_51/usr/src/tools/scripts/which_scm.sh (revision 8bcea973790ad3e762bf78b7c6ad5776e463fd51)
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
28# which_scm outputs two strings: one identifying the SCM in use, and
29# the second giving the root directory for the SCM, if known, or just
30# the current working directory if not known.
31
32# There are three distinct types of SCM systems we can detect.  The first
33# type have a control directory per directory (RCS and SCCS), with no other
34# structure.  The second type have a control directory in each subdirectory
35# within a tree (CVS and SVN).  The last type have a single control
36# directory at the top of the tree (Teamware and Mercurial).
37
38# If the common CODEMGR_WS variable is set, then we look there for the
39# SCM type and bail out if we can't determine it.
40
41# If that variable is not set, then we start in the current directory
42# and work our way upwards until we find the top of the tree or we
43# encounter an error.
44
45# We do handle nested SCM types, and report the innermost one, but if
46# you nest one of the "second type" systems within another instance of
47# itself, we'll keep going upwards and report the top of the nested
48# set of trees.
49
50
51# Check for well-known tree-type source code management (SCM) systems.
52function primary_type
53{
54	typeset scmid
55
56	[ -d "$1/Codemgr_wsdata" ] && scmid="$scmid teamware"
57	[ -d "$1/.hg" ] && scmid="$scmid mercurial"
58	[ -d "$1/CVS" ] && scmid="$scmid cvs"
59	[ -d "$1/.svn" ] && scmid="$scmid subversion"
60	[ -d "$1/.git" ] && scmid="$scmid git"
61	echo $scmid
62}
63
64if [[ -n "$CODEMGR_WS" ]]; then
65	if [[ ! -d "$CODEMGR_WS" ]]; then
66		print -u2 "which_scm: $CODEMGR_WS is not a directory."
67		exit 1
68	fi
69	set -- $(primary_type "$CODEMGR_WS")
70	if [[ $# != 1 ]]; then
71		set -- unknown
72	fi
73	echo "$1 $CODEMGR_WS"
74	exit 0
75fi
76
77ORIG_CWD=$(pwd)
78
79if [[ -d RCS ]]; then
80	echo "rcs $ORIG_CWD"
81	exit 0
82fi
83
84# If it's not Teamware, it could just be local SCCS.
85LOCAL_TYPE=
86[[ -d SCCS ]] && LOCAL_TYPE="sccs"
87
88# Scan upwards looking for top of tree.
89DIR=$ORIG_CWD
90CWD_TYPE=$(primary_type "$DIR")
91SCM_TYPE=
92while [[ "$DIR" != / ]]; do
93	set -- $(primary_type "$DIR")
94	if [[ $# > 1 ]]; then
95		echo "unknown $ORIG_CWD"
96		exit 0
97	fi
98	SCM_TYPE="$1"
99	# We're done searching if we hit either a change in type or the top
100	# of a "third type" control system.
101	if [[ "$SCM_TYPE" != "$CWD_TYPE" || "$SCM_TYPE" == git || \
102	    "$SCM_TYPE" == mercurial || "$SCM_TYPE" == teamware ]]; then
103		break
104	fi
105	PREVDIR="$DIR"
106	DIR=$(dirname "$DIR")
107done
108
109# We assume here that the system root directory isn't the root of the SCM.
110
111# Check for the "second type" of repository.  In all cases, we started
112# out in the tree and stepped out on the last iteration, so we want
113# $PREVDIR.
114if [[ "$CWD_TYPE" == cvs || "$CWD_TYPE" == subversion ]]; then
115	echo "$CWD_TYPE $PREVDIR"
116	exit 0
117fi
118
119# If we still don't know what it is, then check for a local type in the
120# original directory.  If none, then we don't know what it is.
121if [[ -z "$SCM_TYPE" ]]; then
122	if [[ -z "$LOCAL_TYPE" ]]; then
123		SCM_TYPE=unknown
124	else
125		SCM_TYPE=$LOCAL_TYPE
126		DIR=$ORIG_CWD
127	fi
128fi
129
130echo "$SCM_TYPE $DIR"
131exit 0
132