Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Cannot write to a process
Forum Updated to NodeBB v4.3 + New Features

Cannot write to a process

Scheduled Pinned Locked Moved Solved General and Desktop
20 Posts 3 Posters 1.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    smallC
    wrote on last edited by smallC
    #1

    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

    1 Reply Last reply
    0
    • S smallC

      @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.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #14

      @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.

      S 1 Reply Last reply
      1
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        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.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        1
        • S Offline
          S Offline
          smallC
          wrote on last edited by
          #3

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

          1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #4

            @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.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            S 1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              @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.

              S Offline
              S Offline
              smallC
              wrote on last edited by
              #5

              @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)

              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #6

                @smallC said in Cannot write to a process:

                How to do it? (Using QProcess of course)

                See QProcess::start()

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                S 1 Reply Last reply
                0
                • Christian EhrlicherC Christian Ehrlicher

                  @smallC said in Cannot write to a process:

                  How to do it? (Using QProcess of course)

                  See QProcess::start()

                  S Offline
                  S Offline
                  smallC
                  wrote on last edited by smallC
                  #7

                  @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.

                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #8

                    @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.

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    S 1 Reply Last reply
                    0
                    • Christian EhrlicherC Christian Ehrlicher

                      @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.

                      S Offline
                      S Offline
                      smallC
                      wrote on last edited by smallC
                      #9

                      @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.

                      1 Reply Last reply
                      0
                      • Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #10

                        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.

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        S 1 Reply Last reply
                        0
                        • Christian EhrlicherC Christian Ehrlicher

                          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.

                          S Offline
                          S Offline
                          smallC
                          wrote on last edited by smallC
                          #11

                          @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?

                          Christian EhrlicherC 1 Reply Last reply
                          0
                          • S smallC

                            @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?

                            Christian EhrlicherC Offline
                            Christian EhrlicherC Offline
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on last edited by
                            #12

                            @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.

                            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                            Visit the Qt Academy at https://academy.qt.io/catalog

                            S 1 Reply Last reply
                            0
                            • Christian EhrlicherC Christian Ehrlicher

                              @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.

                              S Offline
                              S Offline
                              smallC
                              wrote on last edited by
                              #13

                              @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.

                              JonBJ 1 Reply Last reply
                              0
                              • S smallC

                                @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.

                                JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on last edited by JonB
                                #14

                                @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.

                                S 1 Reply Last reply
                                1
                                • Christian EhrlicherC Offline
                                  Christian EhrlicherC Offline
                                  Christian Ehrlicher
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #15

                                  @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()

                                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                  Visit the Qt Academy at https://academy.qt.io/catalog

                                  S 1 Reply Last reply
                                  0
                                  • Christian EhrlicherC Christian Ehrlicher

                                    @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()

                                    S Offline
                                    S Offline
                                    smallC
                                    wrote on last edited by smallC
                                    #16

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

                                    1 Reply Last reply
                                    0
                                    • JonBJ JonB

                                      @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.

                                      S Offline
                                      S Offline
                                      smallC
                                      wrote on last edited by
                                      #17

                                      @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.

                                      JonBJ 1 Reply Last reply
                                      1
                                      • S smallC

                                        @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.

                                        JonBJ Offline
                                        JonBJ Offline
                                        JonB
                                        wrote on last edited by JonB
                                        #18

                                        @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?

                                        S 1 Reply Last reply
                                        1
                                        • JonBJ JonB

                                          @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?

                                          S Offline
                                          S Offline
                                          smallC
                                          wrote on last edited by
                                          #19

                                          @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.

                                          JonBJ 1 Reply Last reply
                                          1
                                          • S smallC

                                            @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.

                                            JonBJ Offline
                                            JonBJ Offline
                                            JonB
                                            wrote on last edited by
                                            #20

                                            @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.

                                            1 Reply Last reply
                                            2

                                            • Login

                                            • Login or register to search.
                                            • First post
                                              Last post
                                            0
                                            • Categories
                                            • Recent
                                            • Tags
                                            • Popular
                                            • Users
                                            • Groups
                                            • Search
                                            • Get Qt Extensions
                                            • Unsolved