Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Cannot write to a process



  • Hi, I'm trying to communicate with git process using QProcess but unfortunately I cannot write anything to it.

    QProcess process;
    const QString git_process_path{"/usr/bin/git"};
      
       process.setProgram(git_process_path);
       process.start();
       if (process.waitForStarted(-1)) {
    const QByteArray command = "git status\n";
    const auto written = process.write(command);
           process.waitForFinished(-1);
           process.waitForReadyRead();
    const QString output{process.readAllStandardOutput()};
    }
    

    Here, in the output variable I'm getting the typical output from the git instead of git status:
    "usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]..."

    Why? And how to fix it so I can actually get in response the status info?
    Thank you



  • @smallC
    You are running a command of "/usr/bin/git", and then sending the string "git status\n" to its standard input. That makes no sense at all, what would git begin to do with a command starting with git... even if it worked this way?

    I don't know what you claim it's doing under Windows. I don't want to get into a debate about that. Here you must be under Linux ("/usr/bin/git"), so if you don't agree and think what you had should work I invite you to man git and find where you says it will read commands from stdin as you say rather than accept status as an argument which is what I/ @Christian-Ehrlicher say.

    Since you need to run git status you need to pass status as an argument on the command-line to git. You need:

    process.start(git_process_path, QStringList() << "status");
    // or, if you prefer
    process.setProgram(git_process_path);
    process.setArguments(QStringList() << "status");
    process.start();
    

    You can then read from its standard output. However, you really should also read from its standard error too, at present you are leaving anything it might write to stderr to vanish into the blue. Separately from that, for production code at least, you should be checking for error return code.


  • Lifetime Qt Champion

    Your code does not compile and I'm pretty sure git has no interactive mode - you simpy have to call 'git status' in your QProcess.



  • Hi, fixed the code. Now it compiles.
    This code works as intended on Windows.


  • Lifetime Qt Champion

    @smallC said in Cannot write to a process:

    This code works as intended on Windows.

    As I already said - git has no interactive mode - neither on windows nor on linux. You have to call 'git status' and nothing more.



  • @Christian-Ehrlicher
    And as I've stated, this code behaves correctly on Windows. On Windows in the output field I'll get output from git status command, here on linux I'm getting the default prompt from git informing me how to use it.

    So perhaps, I'll ask differently:

    You have to call 'git status' and nothing more.

    How to do it? (Using QProcess of course)


  • Lifetime Qt Champion

    @smallC said in Cannot write to a process:

    How to do it? (Using QProcess of course)

    See QProcess::start()



  • @Christian-Ehrlicher
    Am afraid you got it wrong.
    I'm not trying to send arguments to the git process during start up of this process.
    I'm trying to write through write channel to that git process AFTER the process had already started.
    This is something different to passing arguments to a process when we start that process from command line.
    What I'm trying to do is to communicate with that process via stdin stdout channels.


  • Lifetime Qt Champion

    @smallC said in Cannot write to a process:

    I'm trying to write through write channel to that git process AFTER the process had already started.

    Again: git has no interactive mode so you can't send anything to it when the process runs.



  • @Christian-Ehrlicher
    I can. I do it on Windows with the exact code I've posted.
    On Windows this code works as intended:
    I'm starting git process via QProcess with start() method.
    Then, when the process started I communicate with that process via write and readAllStandardOutput methods of QProcess.
    So when I do:
    process.write("git status");
    I'm getting git status output as if I were to type it in the console, so when I execute this:
    auto output = process. readAllStandardOutput();
    in this^^^ output field I'm getting output from git.


  • Lifetime Qt Champion

    Even on windows it works only by accident. When you click on git.exe on windows there will be no command prompt which stays open - so no interaction possible.



  • @Christian-Ehrlicher You're joking. You must be. Did you ever hear about communication between processes? For example writing to other process stdin and reading from such process stdout?


  • Lifetime Qt Champion

    @smallC said in Cannot write to a process:

    For example writing to other process stdin and reading from such process stdout?

    I did, but git does not do it (don't know how often I have to repeat it) - git is not interactive. Simply call 'git status' and you're done.



  • @Christian-Ehrlicher
    Please post the code you want me to execute in order to get reply from git to the "git status" command.
    I've tried the start command with program and params list and it doesn't work.



  • @smallC
    You are running a command of "/usr/bin/git", and then sending the string "git status\n" to its standard input. That makes no sense at all, what would git begin to do with a command starting with git... even if it worked this way?

    I don't know what you claim it's doing under Windows. I don't want to get into a debate about that. Here you must be under Linux ("/usr/bin/git"), so if you don't agree and think what you had should work I invite you to man git and find where you says it will read commands from stdin as you say rather than accept status as an argument which is what I/ @Christian-Ehrlicher say.

    Since you need to run git status you need to pass status as an argument on the command-line to git. You need:

    process.start(git_process_path, QStringList() << "status");
    // or, if you prefer
    process.setProgram(git_process_path);
    process.setArguments(QStringList() << "status");
    process.start();
    

    You can then read from its standard output. However, you really should also read from its standard error too, at present you are leaving anything it might write to stderr to vanish into the blue. Separately from that, for production code at least, you should be checking for error return code.


  • Lifetime Qt Champion

    @smallC said in Cannot write to a process:

    Please post the code you want me to execute in order to get reply from git to the "git status" command.
    I've tried the start command with program and params list and it doesn't work.

    Please post your code - I won't write code for you. But basically it's just QProcess::start(), QProcess::waitForFinished() (not recommended, use signals/slots) and the QProcess::readAllStandardError()/QProcess::readAllStandardOutput()



  • @Christian-Ehrlicher
    I did post my code. In OP.



  • @JonB
    Hi thanks,
    That indeed worked. My mistake was that I was thinking of /usr/bin/git in terms of git executable on Windows.
    Thanks for your help.



  • @smallC
    I think your original way worked under Windows because of the git implementation there, only. I look at https://gitforwindows.org/ and I see it talking about

    Git BASH

    Git for Windows provides a BASH emulation used to run Git from the command line. *NIX users should feel right at home, as the BASH emulation behaves just like the "git" command in LINUX and UNIX environments.

    and the screenshot at https://gitforwindows.org/img/gw1.png. I think they are saying in their Windows git that a command of git alone enters a "shell", kind of emulating Linux bash, which allows you to type things like git status into it, and it stays there and executes. Perhaps not quite, but something like that anyway. That is not a facility of git under Linux. probably why your stuff worked under Windows and not Linux?



  • @JonB
    Yes, in Windows it opens a bash/cmd and you can write/read from it. It works differently on Linux. I wasn't aware of that. I thought that the /usr/bin/git<<this git, is an equivalent to Windows git.exe. That's why the confusion.
    Thanks for your help.



  • @smallC
    Indeed. FWIW, if you do want to support Windows too I think the Linux command-line-arguments-only will work there too, but not the other way round, though you'd have to verify.


Log in to reply