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