mononoke: conflict_markers hook

Summary:
Hooks that makes sure that there are no conflict markers in file contents.
This hook is bypassable.

Reviewed By: purplefox

Differential Revision: D10260230

fbshipit-source-id: b9d69e757f18ed3f4f889a01032ef7360cba6867
This commit is contained in:
Stanislau Hlebik 2018-10-11 05:41:09 -07:00 committed by Facebook Github Bot
parent d5a8790a56
commit 382c1e8d31
3 changed files with 208 additions and 0 deletions

View File

@ -0,0 +1,26 @@
local allowed_suffixes = {".*%.rst$", ".*%.markdown$", ".*%.md$", ".*%.rdoc$"}
hook = function (ctx)
for _, suffix in ipairs(allowed_suffixes) do
if ctx.file.path:match(suffix) then
return true
end
end
local content = ctx.file.content()
-- Consider that file is binary if it has \0
-- And do not check binary files
if content:find('\0') then
return true
end
local error_msg = ("Conflict markers were found in file '%s'"):format(ctx.file.path)
if content:match("^>>>>>>> ") then
return false, error_msg
elseif content:match("^<<<<<<< ") then
return false, error_msg
elseif content:match("^=======$") then
return false, error_msg
end
return true
end

View File

@ -317,3 +317,67 @@ server=False
reponame=repo
EOF
}
# Does all the setup necessary for hook tests
function hook_test_setup() {
. "$TESTDIR"/library.sh
setup_hg_config_repo
cd "$TESTTMP/mononoke-config" || exit 1
cat >> repos/repo/server.toml <<CONFIG
[[bookmarks]]
name="master_bookmark"
CONFIG
HOOK_FILE="$1"
HOOK_NAME="$2"
HOOK_TYPE="$3"
shift 3
BYPASS=""
if [[ $# -gt 0 ]]; then
BYPASS="$1"
fi
mkdir -p common/hooks
cp "$HOOK_FILE" common/hooks/"$HOOK_NAME".lua
register_hook common/hooks/"$HOOK_NAME".lua "$HOOK_TYPE" "$BYPASS"
commit_and_blobimport_config_repo
setup_common_hg_configs
cd "$TESTTMP" || exit 1
cat >> "$HGRCPATH" <<EOF
[ui]
ssh="$DUMMYSSH"
EOF
hg init repo-hg
cd repo-hg || exit 1
setup_hg_server
hg debugdrawdag <<EOF
C
|
B
|
A
EOF
hg bookmark master_bookmark -r tip
cd ..
blobimport rocksdb repo-hg/.hg repo
mononoke
wait_for_mononoke "$TESTTMP"/repo
hgclone_treemanifest ssh://user@dummy/repo-hg repo2 --noupdate --config extensions.remotenames= -q
cd repo2 || exit 1
setup_hg_client
cat >> .hg/hgrc <<EOF
[extensions]
pushrebase =
remotenames =
EOF
}

View File

@ -0,0 +1,118 @@
$ . $TESTDIR/library.sh
$ hook_test_setup $TESTDIR/hooks/conflict_markers.lua conflict_markers PerAddedOrModifiedFile "bypass_commit_string=\"@ignore-conflict-markers\""
Negative testing
$ markers_good=('<<<<<<<'
> '<<<<<<<<<<'
> '>>>>>>>'
> '<<<<<<<'
> '>>>>>>>>>>'
> '====='
> '===============')
$ hg up -q 0
$ i=0
$ for input in "${markers_good[@]}"; do
> i=$((i+1))
> printf "$input" > "file$i"
> done
$ hg ci -Aqm 1
$ hgmn push -r . --to master_bookmark
remote: * DEBG Session with Mononoke started with uuid: * (glob)
pushing rev 069fca863ff8 to destination ssh://user@dummy/repo bookmark master_bookmark
searching for changes
adding changesets
adding manifests
adding file changes
added 1 changesets with 0 changes to 0 files
server ignored bookmark master_bookmark update
remote: * DEBG Session with Mononoke started with uuid: * (glob)
Positive testing
$ hg up -q 0
$ echo '>>>>>>> 123' > 1 && hg add 1 && hg ci -m 1
$ hgmn push -r . --to master_bookmark
remote: * DEBG Session with Mononoke started with uuid: * (glob)
pushing rev 17a746afd78e to destination ssh://user@dummy/repo bookmark master_bookmark
searching for changes
remote: * ERRO Command failed, remote: true, error: hooks failed: (glob)
remote: conflict_markers: Conflict markers were found in file '1', root_cause: ErrorMessage {
remote: msg: "hooks failed:\nconflict_markers: Conflict markers were found in file \'1\'"
remote: }, backtrace: , session_uuid: * (glob)
abort: stream ended unexpectedly (got 0 bytes, expected 4)
[255]
$ hg up -q 0
$ echo '<<<<<<< 123' > 1 && hg add 1 && hg ci -m 1
$ hgmn push -r . --to master_bookmark
remote: * DEBG Session with Mononoke started with uuid: * (glob)
pushing rev 948f2ceaf570 to destination ssh://user@dummy/repo bookmark master_bookmark
searching for changes
remote: * ERRO Command failed, remote: true, error: hooks failed: (glob)
remote: conflict_markers: Conflict markers were found in file '1', root_cause: ErrorMessage {
remote: msg: "hooks failed:\nconflict_markers: Conflict markers were found in file \'1\'"
remote: }, backtrace: , session_uuid: * (glob)
abort: stream ended unexpectedly (got 0 bytes, expected 4)
[255]
$ hg up -q 0
$ printf '=======' > 1 && hg add 1 && hg ci -m 1
$ hgmn push -r . --to master_bookmark
remote: * DEBG Session with Mononoke started with uuid: * (glob)
pushing rev 19aec9624bdb to destination ssh://user@dummy/repo bookmark master_bookmark
searching for changes
remote: * ERRO Command failed, remote: true, error: hooks failed: (glob)
remote: conflict_markers: Conflict markers were found in file '1', root_cause: ErrorMessage {
remote: msg: "hooks failed:\nconflict_markers: Conflict markers were found in file \'1\'"
remote: }, backtrace: , session_uuid: * (glob)
abort: stream ended unexpectedly (got 0 bytes, expected 4)
[255]
Negative testing
Files with bad markers should be accepted with these suffixes
$ hg up -q 0
$ suffixes=('.md' '.markdown' '.rdoc' '.rst')
$ for suffix in "${suffixes[@]}"; do
$ echo ">>>>>>> " > "file$suffix"
$ done
$ hg ci -Aqm 'markdowns'
$ hgmn push -r . --to master_bookmark
remote: * DEBG Session with Mononoke started with uuid: * (glob)
pushing rev ced9269b0dde to destination ssh://user@dummy/repo bookmark master_bookmark
searching for changes
adding changesets
adding manifests
adding file changes
added 1 changesets with 0 changes to 0 files
server ignored bookmark master_bookmark update
Negative Testing
Files with bad markers should be accepted if they are binary.
File is considered binary if it contains \0
$ hg up -q 0
$ echo -e ">>>>>>> \0" > file
$ hg ci -Aqm binary
$ hgmn push -r . --to master_bookmark
remote: * DEBG Session with Mononoke started with uuid: * (glob)
pushing rev e913daf3ef9f to destination ssh://user@dummy/repo bookmark master_bookmark
searching for changes
adding changesets
adding manifests
adding file changes
added 1 changesets with 0 changes to 0 files
server ignored bookmark master_bookmark update
Test bypass
$ hg up -q 0
$ echo -e ">>>>>>> " > largefile
$ hg ci -Aqm '@ignore-conflict-markers'
$ hgmn push -r . --to master_bookmark
remote: * DEBG Session with Mononoke started with uuid: * (glob)
pushing rev a45fdf76c250 to destination ssh://user@dummy/repo bookmark master_bookmark
searching for changes
adding changesets
adding manifests
adding file changes
added 1 changesets with 0 changes to 0 files
server ignored bookmark master_bookmark update