#!/bin/sh
#
# Regression tester for SRC.
#

# Set the umask for a bit of defensiveness
umask 0077

# Needs to not be a subdirectory of here, or git gets confused.  Use
# -u so that src's initialization will be tested.
SANDBOX=$(mktemp -u /tmp/.src_XXXXXXXX)

# Set the PATH to include the current directory, so the repository
# head version of src can always be tested.
PATH="$(pwd)":$PATH

trap "rm -fr $SANDBOX" 0 1 2 15

check() {
    case $? in
	0) echo "srctest: $1 succeeded";;
	*) echo "srctest: $1 failed"; exit 1;;
    esac
}

mkdir $SANDBOX
check "scratch directory creation"

cd $SANDBOX

TESTOPTS="-T $*"

cat >testfile1 <<EOF
Now is the time
EOF
cp testfile1 testrev1

src $TESTOPTS commit -m "First comment" testfile1 >/dev/null
check "commit with -m option"

src $TESTOPTS cat testfile1 | diff -u testfile1 - >/dev/null
check "content check after first commit"

cat >testfile1 <<EOF
Now is the time
for all good men
EOF
cp testfile1 testrev2

cat >testfile4 <<EOF
@@ -1 +1,2 @@
 Now is the time
+for all good men
EOF

# (read; read; cat) removes header lines containing variable date information
src $TESTOPTS diff testfile1 | (read discard; read discard; cat) >testfile5
diff -u testfile4 testfile5
check "nonempty diff"

sleep 1	# Force commit to have different timestamp
echo "Second comment" | src $TESTOPTS commit - testfile1 >/dev/null
check "commit with -"

src $TESTOPTS cat testfile1 | diff -u testfile1 -
check "content check after second commit"

cat >testfile2 <<EOF
= testfile1 ============================================================
2     * 1970-01-02T00:01:00Z Second comment
1     - 1970-01-02T00:00:00Z First comment
EOF

src $TESTOPTS list testfile1 >testfile3
diff -u testfile2 testfile3
check "list test"

cat >testfile2 <<EOF
= testfile1 ============================================================
2    | 1970-01-02T00:01:00Z | trunk
Second comment
------------------------------------------------------------------------
1    | 1970-01-02T00:00:00Z | trunk
First comment
------------------------------------------------------------------------
EOF

src $TESTOPTS log testfile1 >testfile3
diff -u testfile2 testfile3
check "2-commit log test"

cat >testfile1 <<EOF
Now is the time
for all good men
to come to the aid of foo
EOF
cp testfile1 testrev3

sleep 1	# Force commit to have different timestamp
echo "'Third' comment" >testfile4
src $TESTOPTS commit -f testfile4 testfile1 >/dev/null
check "commit with -f"

cat >testfile4 <<EOF
= testfile1 ============================================================
3    | 1970-01-02T00:02:00Z | trunk
'Third' comment
------------------------------------------------------------------------
2    | 1970-01-02T00:01:00Z | trunk
Second comment
------------------------------------------------------------------------
1    | 1970-01-02T00:00:00Z | trunk
First comment
------------------------------------------------------------------------
EOF

src $TESTOPTS log testfile1 >testfile5
diff -u testfile4 testfile5
check "3-commit log test"

cat >testfile6 <<EOF
= testfile1 ============================================================
src: testfile1 has no 5 revision
EOF

src $TESTOPTS list 5 testfile1 >testfile7 2>&1
diff -u testfile6 testfile7
check "oob revision spec check"

for rev in 1 2 3
do
    src $TESTOPTS checkout $rev testfile1 >/dev/null
    check "revision $rev checkout test"
    diff -u testrev${rev} testfile1
    check "revision $rev content test"
done

src $TESTOPTS tag create sampletag
check "tag set to default tip revision"

cat >testfile8 <<EOF
= testfile1 ==========================================================
   3	sampletag
EOF
src $TESTOPTS tag list >testfile9
diff -u testfile8 testfile9
check "tag list check"

src $TESTOPTS tag create basetag 1
check "tag set to revision 1"

cat >testfile10 <<EOF
= testfile1 ==========================================================
   1	basetag
   3	sampletag
EOF
src $TESTOPTS tag -l >testfile11
diff -u testfile10 testfile11
check "second tag list check"

src $TESTOPTS tag delete sampletag
check "tag deletion: sampletag"

cat >testfile12 <<EOF
= testfile1 ==========================================================
   1	basetag
EOF
src $TESTOPTS tag >testfile13
diff -u testfile12 testfile13
check "tag list check after deletion"

# Alas, we have to do this or the fast-export regression will fail.
# We don't know how to be perfectly canonical about tags yet.
src $TESTOPTS tag delete basetag
check "tag deletion: basetag"

mkdir foo
src $TESTOPTS fast-export testfile1 >testfile14
cat testfile14 | (cd foo >/dev/null; git init --quiet; git fast-import --quiet)
(cd foo >/dev/null; git fast-export --all) >testfile15
diff -u testfile14 testfile15
check "fast-export"
rm -fr foo

# There was a point at which fast-export was broken,
# but the test was passing because an empty file
# produced a repo that git dumped as an emoty file!  
grep jrh testfile15 >/dev/null
check "fast-export content check"

src $TESTOPTS move testfile1 newname1
check "move command"
if [ -e newname1 -a -e .src/newname1,v ]
then
    echo "srctest: content move succeeded"
else
    echo "src: content move failed"
    exit 1
fi

src $TESTOPTS copy newname1 newname2
check "copy command"
if [ -e newname2 -a -e .src/newname2,v ]
then
    echo "srctest: content copy succeeded"
else
    echo "src: content copy failed"
    exit 1
fi

cat >testfile16<<EOF
newname1
newname2
EOF
src $TESTOPTS ls | diff -u testfile16 -
check "ls"

src $TESTOPTS commit -m "Another sample" testfile16
src $TESTOPTS status testfile16 | grep "^=" >/dev/null
check "= status check after first commit"

echo "Add a second line" >>testfile16
src $TESTOPTS status testfile16 | grep "^M" >/dev/null
check "M status check after modification"

rm testfile16
src $TESTOPTS status testfile16 | grep "^!" >/dev/null
check "! status check after deletion"

src $TESTOPTS checkout testfile16
src $TESTOPTS status testfile16 | grep "^=" >/dev/null
check "= status check after restoration"

rm -f .src/testfile16,v
src $TESTOPTS status testfile16 | grep "^?" >/dev/null
check "? status check after master deletion"

src $TESTOPTS amend -m "Amended comment" newname1
check "amend"

src $TESTOPTS list 3 newname1 | grep "Amended comment" >/dev/null
check "amended comment content"

# Introduce a test history with some branchiness, so
# we can test traversal across branch joins. Also has tags.
cat >.src/sample,v <<EOF
head	1.4;
branch	1.3.1.4.1;
access;
symbols
	GLARB:1.4
	GORP:1.3.1.4
	sample:1.3.1.4.0.1
	muggle:1.3.0.1;
locks
	esr:1.3.1.4.1.5;
comment	@# @;
expand	@b@;


1.4
date	2014.11.11.03.26.53;	author esr;	state Exp;
branches;
next	1.3;

1.3
date	2014.11.10.23.48.49;	author esr;	state Exp;
branches
	1.3.1.1;
next	1.2;

1.2
date	2014.11.10.23.48.11;	author esr;	state Exp;
branches;
next	1.1;

1.1
date	2014.11.10.23.47.08;	author esr;	state Exp;
branches;
next	;

1.3.1.1
date	2014.11.11.04.23.47;	author esr;	state Exp;
branches;
next	1.3.1.2;

1.3.1.2
date	2014.11.11.06.39.47;	author esr;	state Exp;
branches;
next	1.3.1.3;

1.3.1.3
date	2014.11.11.06.48.00;	author esr;	state Exp;
branches;
next	1.3.1.4;

1.3.1.4
date	2014.11.12.03.36.45;	author esr;	state Exp;
branches
	1.3.1.4.1.1;
next	;

1.3.1.4.1.1
date	2014.11.13.04.04.55;	author esr;	state Exp;
branches;
next	1.3.1.4.1.2;

1.3.1.4.1.2
date	2014.11.13.04.23.04;	author esr;	state Exp;
branches;
next	1.3.1.4.1.3;

1.3.1.4.1.3
date	2014.11.13.04.26.23;	author esr;	state Exp;
branches;
next	1.3.1.4.1.4;

1.3.1.4.1.4
date	2014.11.13.04.30.39;	author esr;	state Exp;
branches;
next	1.3.1.4.1.5;

1.3.1.4.1.5
date	2014.11.13.04.31.02;	author esr;	state Exp;
branches;
next	;


desc
@@


1.4
log
@On a branch?
@
text
@Now is the time
For all good men.
To come to the aid of foo.
This should be branch text.
@


1.3
log
@Third comment.
@
text
@d4 1
@


1.3.1.1
log
@Totally unfubared.
@
text
@a3 2

Again, this should be branch text.
@


1.3.1.2
log
@Still utterly fubar
@
text
@d5 1
a5 1
Yet again, this should be branch text.
@


1.3.1.3
log
@Totally chenille.
@
text
@a5 2

Revision 7.
@


1.3.1.4
log
@Utterly fubar
@
text
@d4 4
a7 2
This should be branch text.
Try a comment with a hyphen.
@


1.3.1.4.1.1
log
@All fixed up.
@
text
@a5 1
Test of commit comments.
@


1.3.1.4.1.2
log
@It' all good.
@
text
@a6 1
Glotch.
@


1.3.1.4.1.3
log
@No good.
@
text
@a7 1
Random modification.
@


1.3.1.4.1.4
log
@That's good.
@
text
@a4 1
jjjj
d7 1
@


1.3.1.4.1.5
log
@I see you
@
text
@d5 1
a5 1
jjjjjjjjkkk
@


1.2
log
@Second comment.
@
text
@d3 1
@


1.1
log
@First comment.
@
text
@d2 1
@
EOF

cat >sample.seqlog <<EOF
= sample ===============================================================
6    | 1970-01-02T00:05:00Z | muggle
Still utterly fubar
------------------------------------------------------------------------
5    | 1970-01-02T00:04:00Z | muggle
Totally unfubared.
------------------------------------------------------------------------
4    | 1970-01-02T00:03:00Z | trunk
On a branch?
------------------------------------------------------------------------
3    | 1970-01-02T00:02:00Z | trunk
Third comment.
------------------------------------------------------------------------
2    | 1970-01-02T00:01:00Z | trunk
Second comment.
------------------------------------------------------------------------
EOF

src $TESTOPTS log 6-2 sample >tryit
diff -u tryit sample.seqlog
check "traversal by -"

cat >sample.branchlog <<EOF
= sample ===============================================================
6    | 1970-01-02T00:05:00Z | muggle
Still utterly fubar
------------------------------------------------------------------------
5    | 1970-01-02T00:04:00Z | muggle
Totally unfubared.
------------------------------------------------------------------------
3    | 1970-01-02T00:02:00Z | trunk
Third comment.
------------------------------------------------------------------------
2    | 1970-01-02T00:01:00Z | trunk
Second comment.
------------------------------------------------------------------------
EOF

src $TESTOPTS log 6..2 sample >tryit
diff -u tryit sample.branchlog
check "traversal by .."

cat >sample.taglog <<EOF
= sample ===============================================================
4     - 1970-01-02T00:03:00Z On a branch?
EOF

src $TESTOPTS list @GLARB sample >tryit
diff -u tryit sample.taglog
check "named-tag lookup with @"

src $TESTOPTS rename tag GLARB GROTTY sample
check "tag renaming"

cat >changes <<EOF
= sample =============================================================
   8	GORP
   4	GROTTY
EOF

src $TESTOPTS tag -l sample >tryit
diff -u tryit changes
check "tag list after rename"

cat >branchlist <<EOF
= sample =============================================================
   0	trunk
   3	muggle
   8	sample
EOF

src $TESTOPTS branch -l sample >tryit
check "branch list"

src $TESTOPTS branch -l sample >tryit
diff -u tryit branchlist
check "branch list content"

cat >branchlog <<EOF
= sample ===============================================================
4     - 1970-01-02T00:03:00Z On a branch?
EOF

src $TESTOPTS list @trunk sample >branchlog
check "branch name resolution"

src $TESTOPTS list @trunk sample >tryit
diff -u tryit branchlog
check "branch name resolved content"

cat >statuslog <<EOF
=	newname1
=	newname2
EOF

src $TESTOPTS status newname1 newname2 >tryit
diff -u statuslog tryit
check "status before ignore"

echo "newname1" >.srcignore

cat >statuslog <<EOF
I	newname1
=	newname2
EOF

src $TESTOPTS status newname1 newname2 >tryit
diff -u statuslog tryit
check "status after ignore"

src $TESTOPTS branch delete trunk sample
check "branch delete"

cat >newlist <<EOF
= sample =============================================================
   0	trunk
   3	muggle
   7	sample
EOF

src $TESTOPTS branch -l sample >tryit
diff -u newlist tryit
check "branch list check after delete"

cat >newlog <<EOF
= sample ===============================================================
12   | 1970-01-02T00:11:00Z | sample
I see you
------------------------------------------------------------------------
11   | 1970-01-02T00:10:00Z | sample
That's good.
------------------------------------------------------------------------
10   | 1970-01-02T00:09:00Z | sample
No good.
------------------------------------------------------------------------
9    | 1970-01-02T00:08:00Z | sample
It' all good.
------------------------------------------------------------------------
8    | 1970-01-02T00:07:00Z | sample
All fixed up.
------------------------------------------------------------------------
7    | 1970-01-02T00:06:00Z | muggle
Utterly fubar
------------------------------------------------------------------------
6    | 1970-01-02T00:05:00Z | muggle
Totally chenille.
------------------------------------------------------------------------
5    | 1970-01-02T00:04:00Z | muggle
Still utterly fubar
------------------------------------------------------------------------
4    | 1970-01-02T00:03:00Z | muggle
Totally unfubared.
------------------------------------------------------------------------
3    | 1970-01-02T00:02:00Z | trunk
Third comment.
------------------------------------------------------------------------
2    | 1970-01-02T00:01:00Z | trunk
Second comment.
------------------------------------------------------------------------
1    | 1970-01-02T00:00:00Z | trunk
First comment.
------------------------------------------------------------------------
EOF

src $TESTOPTS log sample >tryit 
diff -u newlog tryit
check "content after branch deletion"

echo "Random content for numeric file" >23
src $TESTOPTS commit -m "I don't know why you say goodbye" -- 23 >/dev/null
check "commit of file with numeric name"

cat >randomcontent <<EOF
Random content for numeric file
EOF

src $TESTOPTS cat -- 23 >tryit
diff -u randomcontent tryit
check "cat of file with numeric name"

echo "Should not be executable" >exectest
src $TESTOPTS commit -m "First comment on exectest" exectest >/dev/null
check "first commit of exectest"
chmod a+x exectest
echo "Should be executable" >exectest
src $TESTOPTS commit -m "Second comment on exectest" exectest >/dev/null
check "second commit of exectest"
ls -l exectest | grep 'x' >/dev/null
check "propagation of exec bit"

echo "srctest: all tests succeeded"

rm -fr $SANDBOX

# end
