<commit_before>#/bin/bash
. t/test-lib.sh
set -e

echo "1..7"

branch=testbr
repo=tmp/test/repo
files=tmp/test/files
export GIT_AUTHOR_DATE=2000-01-01T00:00:00
export GIT_COMMITTER_DATE=2001-01-01T00:00:00

git="git --git-dir=$repo/.git --work-tree=$repo"
gitlog="$git log --pretty=oneline --color=never"

make_test_repo() {
    rm -rf tmp/test
    mkdir -p $repo/subdir $files/subdir
    echo foo > $repo/one
    echo foo > $repo/subdir/two
    $git init -q
    $git add -A
    $git commit -q $date -m 'commit 1'
}

##### 1

start_test 'Check we fail when branch does not exist'
make_test_repo
test_equal "Invalid ref: refs/heads/testbr
128" \
    "$($git commit-filetree 2>&1 $branch $files; echo $?)"
end_test

##### 2

start_test 'Check we fail when untracked files are in the working copy.'
make_test_repo
$git branch $branch
touch $repo/untracked
test_equal 'Cannot commit with untracked files in working copy.
1' \
    "$($git commit-filetree 2>&1 $branch $files; echo $?)"
end_test

##### 3

start_test 'Check we fail when working copy is dirty'
make_test_repo
$git branch $branch
touch $repo/three
$git add three
test_equal 'Cannot commit with uncommited files in working copy.
1' \
    "$($git commit-filetree 2>&1 $branch $files; echo $?)"
end_test

##### 4

start_test 'Check commit'

make_test_repo
$git branch $branch
test_equal "737b0f4 (HEAD, $branch, master) commit 1" \
            "$($gitlog)"

echo bar > $files/one
echo bar > $files/subdir/two
$git commit-filetree $branch $files

test_equal "003e598 ($branch) Build from source commit 737b0f4.
737b0f4 (HEAD, master) commit 1" \
            "$($gitlog $branch)"

end_test

##### 5

start_test 'Check commit with refs/heads/branchname'

make_test_repo
$git branch $branch

echo bar > $files/one
echo bar > $files/subdir/two
$git commit-filetree refs/heads/$branch $files

test_equal "003e598 ($branch) Build from source commit 737b0f4.
737b0f4 (HEAD, master) commit 1" \
            "$($gitlog $branch)"

end_test

##### 6

start_test 'Check commit when script is run standalone'

make_test_repo
$git branch $branch

echo bar > $files/one
echo bar > $files/subdir/two
(cd $repo && ../../../git-commit-filetree $branch ../files)

test_equal "003e598 ($branch) Build from source commit 737b0f4.
737b0f4 (HEAD, master) commit 1" \
            "$($gitlog $branch)"

end_test

##### 7

start_test 'Check we do not commit if it would be an empty commit.'
make_test_repo
$git branch $branch
echo bar > $files/one
echo bar > $files/subdir/two
$git commit-filetree $branch $files
$git commit-filetree $branch $files
$git commit-filetree $branch $files
test_equal "003e598 ($branch) Build from source commit 737b0f4.
737b0f4 (HEAD, master) commit 1" \
            "$($gitlog $branch)"

end_test<commit_msg>t/t020-test-commit.t: explicitly specify log format.<commit_after>#/bin/bash
. t/test-lib.sh
set -e

echo "1..7"

branch=testbr
repo=tmp/test/repo
files=tmp/test/files
export GIT_AUTHOR_DATE=2000-01-01T00:00:00
export GIT_COMMITTER_DATE=2001-01-01T00:00:00

git="git --git-dir=$repo/.git --work-tree=$repo"
gitlog="$git log --color=never --format=%h%d_%s"

make_test_repo() {
    rm -rf tmp/test
    mkdir -p $repo/subdir $files/subdir
    echo foo > $repo/one
    echo foo > $repo/subdir/two
    $git init -q
    $git add -A
    $git commit -q $date -m 'commit 1'
}

##### 1

start_test 'Check we fail when branch does not exist'
make_test_repo
test_equal "Invalid ref: refs/heads/testbr
128" \
    "$($git commit-filetree 2>&1 $branch $files; echo $?)"
end_test

##### 2

start_test 'Check we fail when untracked files are in the working copy.'
make_test_repo
$git branch $branch
touch $repo/untracked
test_equal 'Cannot commit with untracked files in working copy.
1' \
    "$($git commit-filetree 2>&1 $branch $files; echo $?)"
end_test

##### 3

start_test 'Check we fail when working copy is dirty'
make_test_repo
$git branch $branch
touch $repo/three
$git add three
test_equal 'Cannot commit with uncommited files in working copy.
1' \
    "$($git commit-filetree 2>&1 $branch $files; echo $?)"
end_test

##### 4

start_test 'Check commit'

make_test_repo
$git branch $branch
test_equal "737b0f4 (HEAD, $branch, master)_commit 1" \
            "$($gitlog)"

echo bar > $files/one
echo bar > $files/subdir/two
$git commit-filetree $branch $files

test_equal "003e598 ($branch)_Build from source commit 737b0f4.
737b0f4 (HEAD, master)_commit 1" \
            "$($gitlog $branch)"

end_test

##### 5

start_test 'Check commit with refs/heads/branchname'

make_test_repo
$git branch $branch

echo bar > $files/one
echo bar > $files/subdir/two
$git commit-filetree refs/heads/$branch $files

test_equal "003e598 ($branch)_Build from source commit 737b0f4.
737b0f4 (HEAD, master)_commit 1" \
            "$($gitlog $branch)"

end_test

##### 6

start_test 'Check commit when script is run standalone'

make_test_repo
$git branch $branch

echo bar > $files/one
echo bar > $files/subdir/two
(cd $repo && ../../../git-commit-filetree $branch ../files)

test_equal "003e598 ($branch)_Build from source commit 737b0f4.
737b0f4 (HEAD, master)_commit 1" \
            "$($gitlog $branch)"

end_test

##### 7

start_test 'Check we do not commit if it would be an empty commit.'
make_test_repo
$git branch $branch
echo bar > $files/one
echo bar > $files/subdir/two
$git commit-filetree $branch $files
$git commit-filetree $branch $files
$git commit-filetree $branch $files
test_equal "003e598 ($branch)_Build from source commit 737b0f4.
737b0f4 (HEAD, master)_commit 1" \
            "$($gitlog $branch)"

end_test
{'fraction_non_alphanumeric': 0.1318858783420061, 'fraction_numerical': 0.044679705724026554, 'mean_word_length': 3.8596338273757627, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2717824', 'n_tokens_mistral': 2148, 'n_tokens_neox': 1996, 'n_words': 622}