gpt4 book ai didi

Github New line missing end of file: Negative consequences(GitHub新行缺少文件结尾:负面后果)

转载 作者:bug小助手 更新时间:2023-10-25 09:27:19 25 4
gpt4 key购买 nike



We have a Java code pull request with Swagger. It's missing the new line character in end of YAML file. What are the consequences of not having new line character?

我们收到了来自Swagger的Java代码拉取请求。YAML文件末尾缺少换行符。没有新的行字符会有什么后果?


Curious if we should enforce policy at company, too many people are forgetting to do this. Takes lot of time, however researching how it effects our codebase/system.

出于对我们是否应该在公司执行政策的好奇,太多的人忘记了这一点。然而,研究它如何影响我们的代码库/系统需要花费大量的时间。


enter image description here


更多回答
优秀答案推荐

The main consequence is regarding git diff: when you are diffing files, not having a newline at the end can result in misleading diffs. Tools like git will show changes in lines that have not fundamentally changed, just because a newline was added.
Not having a newline can sometimes increase the likelihood of encountering merge conflicts.

主要结果是关于git diff:当您对文件进行差异处理时,末尾没有换行符可能会导致误导性差异。像git这样的工具将显示没有根本更改的行中的更改,只是因为添加了一个换行符。没有换行符有时会增加遇到合并冲突的可能性。


See more at "What's the significance of the "No newline at end of file" log?".

有关详细信息,请参阅“文件末尾没有换行符的意义是什么?”。


Other consequences:

其他后果:



  • Some tools expect a newline at the end of files and may not behave as expected if a newline is missing. That could include text editors, compilers, and others.

  • Certain YAML parsers may expect files to end with a newline and may throw errors or behave unexpectedly if a newline is missing.


Plus, regarding the code style, not having a newline at the end of a file can sometimes make code less readable, as it can result in the last line of code being "joined" with the command prompt in terminal-based text editors.

另外,关于代码样式,文件末尾没有换行符有时会降低代码的可读性,因为在基于终端的文本编辑器中,这会导致最后一行代码与命令提示符“联接”。




You can enforce it:

您可以强制执行该命令:



  • from the client side:

    从客户端:



    • IDE: integrated development environments (IDEs) and text editors have settings that can be configured to automatically add a newline at the end of files upon saving, which can help to enforce this policy without requiring additional effort from developers. It can be a good practice to encourage developers to configure their development environments to automatically add a newline at the end of files to avoid these potential issues.

      IDE:集成开发环境(IDE)和文本编辑器具有可配置为在保存时自动在文件末尾添加换行符的设置,这有助于实施此策略,而无需开发人员进行额外的工作。鼓励开发人员将他们的开发环境配置为在文件末尾自动添加换行符以避免这些潜在问题可能是一种很好的做法。



    • content filter driver "smudge", with a *.yaml filter=ensure_newline in the .gitattributes, and local activation of that filter with git config filter."encure_newline".smudge /path/to/your/ensure-newline.sh + git config filter."encure_newline".clean cat: I prefer it to a pre-commit hook which would have a harder time to be limited to *.yaml file, and would only reject the commit (as opposed to add the missing trailing newline).

      内容筛选器驱动程序“Smodge”,在.git属性中使用*.yaml筛选器=确保_Newline,并使用GIT配置筛选器在本地激活该筛选器。“encure_newline”.smdge/路径/to/Your/Enure-Newline.sh+git配置筛选器。“encure_newline”.lean cat:我更喜欢它而不是预先提交挂钩,因为它更难被限制为*.yaml文件,并且只会拒绝提交(而不是添加丢失的尾随换行符)。






Regarding the last option, the ensure-newline.sh script would be:

对于最后一个选项,确保-newline.sh脚本将是:


#!/bin/bash

# Check if the file ends with a newline, and if not, add one
if [ "$(tail -c1)" != "" ]; then
echo ""
fi

# Pass the file contents through unchanged
cat


  • from the server side

    从服务器端



    • pre-receive hook, to prevent pushing a commit with the wrong content

      预接收挂钩,以防止推送包含错误内容的提交



    • a GitHub Action, preventing any PR to be merged with the wrong content.

      一个GitHub动作,防止任何PR与错误的内容合并。






For the pre-receive hook, the script would be:

对于预接收挂钩,脚本将为:


#!/bin/bash

# Iterate over every ref being pushed
while read oldrev newrev refname; do
# Get the list of files in the push
files_in_push=$(git diff --name-only $oldrev $newrev)

# Check each file in the push
for file in $files_in_push; do
# If the file ends with .yaml, check if it ends with a newline
if [[ $file == *.yaml ]]; then
file_content=$(git show $newrev:$file)

# If the file does not end with a newline, reject the push
if [[ "$file_content" != *"\n" ]]; then
echo "Rejected push: $file does not end with a newline."
exit 1
fi
fi
done
done

# If we reached here, all checks passed
exit 0

For the GitHub Action, the .github/workflows/check_newline.yml would be:

对于GitHub操作,.giroub/workflow/check_newline.yml将为:


name: Check Newline at EOF

on:
pull_request:
paths:
- '**.yaml'

jobs:
check_newline:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Check Newline at EOF
run: |
PR_NUMBER=$(echo $GITHUB_REF | awk 'BEGIN { FS = "/" } ; { print $3 }')
FILES_CHANGED=$(curl -s -X GET -G https://api.github.com/repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER/files | jq -r '.[] | .filename')
for FILE in $FILES_CHANGED; do
if [[ "$FILE" == *.yaml ]]; then
if [ "$(tail -c1 "$FILE")" != "" ]; then
echo "File $FILE does not end with a newline. Please add a newline at the end of this file."
exit 1
fi
fi
done

更多回答

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com