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