0

I have a C project (ISO 8859-1 encoded) in the STM32CubeMX IDE, and I want to add Git log information in a header file in order to use those in my software (print it in the terminal).

My whole project is ISO 8859-1 encoded, and Git uses the default UTF-8 for commit, so for log too.

The Git command I have is:

git log -n 1 --date=format:"%H:%M %d/%m/%Y" --encoding=ISO-8859-1 --pretty=format:"#ifndef GITCOMMIT_H_%n#define GITCOMMIT_H_%n%n#define AUTHOR_NAME '%an'%n#define COMMIT_HASH '%h'%n#define DATE '%cd'%n%n#endif"  > src\gitcommit.h

But when I compiled I got the error:

../gitcommit.h:1:1: error: stray '\377' in program
    1 | ÿþ# i f n d e f    G I T C O M M I T _ H _

And other errors all along the line.

How can I fix this?

As I am on Windows, when I create the header file with the ">" operator, it is automatically encoded in UTF-16LE.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Clément
  • 53
  • 6
  • Try running it in `git-bash` – LeGEC May 19 '23 at 02:57
  • 2
    What shell are you using? PowerShell? – LeGEC May 19 '23 at 02:58
  • Related (for the source code itself and for a UTF-8 [BOM](https://en.wikipedia.org/wiki/Byte_order_mark)): *[Strange error in code: stray '\239', '\187', and '\191', and in program](https://stackoverflow.com/questions/44626169/strange-error-in-code-stray-239-187-and-191-and-in-program/44626333#44626333)* – Peter Mortensen May 19 '23 at 09:56
  • Related (for the source code itself and [UTF-16BE](https://en.wikipedia.org/wiki/UTF-16#Byte-order_encoding_schemes) instead of [UTF-16LE](https://en.wikipedia.org/wiki/UTF-16#Byte-order_encoding_schemes)): *[How should I use g++'s -finput-charset compiler option correctly in order to compile a non-UTF-8 source file?](https://stackoverflow.com/questions/10345802/how-should-i-use-gs-finput-charset-compiler-option-correctly-in-order-to-com)* – Peter Mortensen May 19 '23 at 09:58
  • Possible duplicate: *['git log' output encoding issues in Windows 10 CLI terminal](https://stackoverflow.com/questions/41139067/git-log-output-encoding-issues-in-windows-10-cli-terminal)* – Peter Mortensen May 19 '23 at 10:09
  • Another option is *[Windows Terminal](https://devblogs.microsoft.com/commandline/introducing-windows-terminal/)*. – Peter Mortensen May 19 '23 at 10:22
  • [Some answers](https://stackoverflow.com/questions/41139067/git-log-output-encoding-issues-in-windows-10-cli-terminal/41416262#41416262) mention the use of [environment variable](https://en.wiktionary.org/wiki/environment_variable#Noun) `LC_ALL`. – Peter Mortensen May 19 '23 at 11:10
  • What version of Windows? There was [a change in June 2019 to Windows 10](https://stackoverflow.com/questions/388490/how-can-i-use-unicode-characters-on-the-windows-command-line/56012091#56012091). – Peter Mortensen May 19 '23 at 11:11
  • Where is the Git command run? In [PowerShell](https://en.wikipedia.org/wiki/PowerShell)? In [cmd.exe](https://en.wikipedia.org/wiki/Cmd.exe)? From inside the IDE? Somewhere else? – Peter Mortensen May 19 '23 at 11:11

2 Answers2

1

Git Bash is a version of Bash, which should not tamper with the output of a command when redirecting its output to a file.

The most straightforward solution is:

  • run your command in Git Bash

PowerShell applies some processing of programs' output without you asking.

The redirection operator >, for example, is not just a "plug the process' standard output to that file"; it calls the Out-File cmdlet which applies some processing on the output of your program before actually writing it to a file.

Check if Out-File for your version of PowerShell supports a suitable encoding: from the documentation for Character encoding in PowerShell. It looks like you need to have PowerShell >= 6.2 to be able to name windows-1252 as an encoding.

So another solution could be:

  • check that your PowerShell version
  • see if you can upgrade to >= 6.2
  • set windows-1252 as the encoding for your shell:
$PSDefaultParameterValues['*:Encoding']

Or just for one run:

... | Out-File -Encoding windows-1252 -FilePath src/git-commit.h
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
LeGEC
  • 46,477
  • 5
  • 57
  • 104
0

In fact, using the Git command I use in PowerShell differs in cmd. In cmd, the output file encoding is UTF-8 which is still not ISO-8951-1, but it was finally OK for my use.

More particularly for STM32CubeIDE, the preprocessor shell can execute a sh script, so I don't have any encoding problem.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Clément
  • 53
  • 6
  • What is ISO-8951-1? Do you mean [ISO 8859-1](https://en.wikipedia.org/wiki/ISO/IEC_8859-1) (it is in the question, incl. revision 1)? – Peter Mortensen Aug 02 '23 at 19:18