【发布时间】:2019-03-18 10:44:37
【问题描述】:
我正在 Windows 上使用 nodeJS 编写 post-commit 挂钩脚本。以下代码调用最后一条提交消息:
#!/bin/env node
require('child_process').exec('git log -1 --pretty=%B', function(err, commit) {
console.log(commit); // prints message in command prompt with 2 empty lines
var messages = commit.split(' '); // the delimiter is irrelevant
console.log(messages); // prints the array and shows '\n\n' at the end of last element
console.log(messages[messages.length - 1]); // yet this prints the last element WITHOUT '\n\n'
});
为什么有 2 条新线?我阅读了 Unix 和非 Unix 系统如何处理 CR 和 LF。 git config core.autocrlf 也有一点,但我认为这不是问题。
【问题讨论】:
-
您能描述一下您的用例吗?我对此很感兴趣,我可以扩展我的pypi.org/project/hooks4git 应用程序以内置这种调用。所以你只需要在 .ini 文件中说明你想显示最后的日志。
-
回复有点晚,抱歉。我的目标是获取提交消息,对其进行格式化并将其存储在文本文件中。我的问题是我最终得到了不需要的新行。此外,例如,Windows 的 Notepad 对待 CR 和 LF 的方式与 Notepad++ 不同。 IE。存储的提交消息出现在同一行。 Notepad++ 在新行上显示每条消息。
-
我明白了。理想情况下,您需要规范化 CRLF ......对我来说,Windows 是“错误的”,所以我保留所有类似 linux 的东西。不确定这是否会为您带来另一个问题。