Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capture full output of `git clone`?

Tags:

git

shell

unix

pipe

When I run git clone like usual, I see this:

$ git clone https://github.com/bensmithett/webpack-css-example
Cloning into 'webpack-css-example'...
remote: Counting objects: 179, done.
remote: Total 179 (delta 0), reused 0 (delta 0), pack-reused 179
Receiving objects: 100% (179/179), 24.07 KiB | 0 bytes/s, done.
Resolving deltas: 100% (79/79), done.
Checking connectivity... done

However, when I try to redirect that to a file (or store it in a shell variable) I only see this:

Cloning into 'webpack-css-example'...

Here is what I tried:

$ git clone https://github.com/bensmithett/webpack-css-example 2>&1 | tee out.log
$ cat out.log
Cloning into 'sample-data'...

I tried it in Node.js as well, and it does the same thing:

const fs = require('fs');
const child = spawn('git clone https://github.com/bensmithett/webpack-css-example');
child.stdout.on('data', function(data){
  console.log(String(data));
});
child.stderr.on('data', function(data){
  console.log(String(data));
});
// Cloning into 'webpack-css-example'...

Why is all the remote: etc. stuff not getting piped to either stdin/stderr? Is there any way to capture the output? If not, what is happening that makes it so the output is displayed in the terminal, yet it is not being passed through either stdout or stderr?

like image 286
Lance Avatar asked Jun 07 '16 01:06

Lance


People also ask

Does git clone copy the entire repository?

The git clone command copies an existing Git repository. This is sort of like SVN checkout, except the “working copy” is a full-fledged Git repository—it has its own history, manages its own files, and is a completely isolated environment from the original repository.

What does depth 1 do in git clone?

Developers should be aware that the depth 1 clone operation only pulls down one branch. If the remote repository contains other branches, they won't be able to check them out locally without a pathspec error. After a git clone depth 1 operation, attempts to checkout an alternate branch will trigger a pathspec error.

How do you check which branch I have cloned?

You successfully cloned a Git repository into a specific folder on your server. In this case, you cloned the master branch from your Git remote repository. You can check the current branch cloned by running the “git branch” command.


1 Answers

By default Git will display the cloning progress only when the standard error stream is directed to a terminal. Since you're redirecting it to the pipe, the output stream is no longer attached to the terminal. So in order to capture the output, you need to add --progress parameter to force the progress status, e.g.

git clone --progress https://github.com/foo/bar 2> out.log

or, in order to store the output in a shell variable

out=$(git clone --progress https://github.com/foo/bar 2>&1)

See: man git-clone

--progress

Progress status is reported on the standard error stream by default when it is attached to a terminal, unless -q is specified. This flag forces progress status even if the standard error stream is not directed to a terminal.


To force the terminal in any other way, you'll have to preload some library to force isatty() to return always true (see: man isatty). This function is used by git across the source code.

like image 113
kenorb Avatar answered Oct 06 '22 01:10

kenorb