尝试自动化 git,仅提交名为 "T"

Trying to automate git, commit only called "T"

我一直在努力减轻将 FRC java 代码上传到 github 的工作量。在此,我制作了这个程序:

int _tmain(int argc, _TCHAR* argv[])
{
    printf("Adding files to commit.\n");
    system("git add *");

    _TCHAR* commit = argv[1];
    printf("Committing changes\n");
    char* buffer = new char[300];
    sprintf(buffer, "git commit -m '%s' *", commit);
    system(buffer);

    printf("Status:\n");
    system("git status");

    printf("Uploading...\n");
    system("git push origin master");

    printf("Done!");

    return 0;
}

问题是当我传递参数 "Test" 时,提交被命名为“'T'”。我该如何解决这个问题?

我怀疑您将程序构建为 Unicode 程序,因此所有 TCHAR 类型的变量都是 Unicode。当您将 commit 传递给 sprintf() 时,不会对参数进行类型检查,因为它是可变参数的一部分。在 Unicode 字符串中,"Test" 将有一个值为 'T' 的字节,后跟一个值为 0 的字节 - 就 sprintf() 而言结束字符串。

我建议放弃 TCHAR 类型,只为这个简单的程序使用 char 类型。如果你不想这样做,那就全力以赴,使用基于 TCHAR 的函数,包括 _tprintf(),而不是将 TCHAR 函数与普通的旧 char 函数混合使用。