steveukx/git-js

Git-show not working correctly for images

waskor opened this issue · 1 comments

I am building a GUI for git in Electron. I am trying to display visual differences between image files. However, images from previous commits are not loaded correctly and I cannot display them on the interface. I fetch images from old commits using the git-show API:

const oldImageRaw = await repo.show([
        `HEAD:${path.relative(projectPath, imagePath)}`, 
      ]);
const oldImageBuffer = Buffer.from(oldImageRaw, "binary")

This results in no image being displayed.

I replaced this snippet by running the command directly through the shell using exec from child_process, and it worked. This was the code:

const getImageFromGit = (projectPath, imagePath) => {
  return new Promise((resolve, reject) => {
    const cmd = \`git -C "${projectPath}" show HEAD:${path.relative(projectPath, imagePath)}\`;

    exec(cmd, { encoding: 'buffer', maxBuffer: 1024 * 1024 * 50 }, (error, stdout) => {
      if (error) {
        reject(error);
        return;
      }

      const base64Image = stdout.toString('base64');
      resolve(base64Image);
    });
  });
};

What is the issue? Does simple-git not support images through the git-show api?

Hi, thanks for opening the issue. The default behaviour of all tasks in simple-git is to return a concatenated string from the stdout stream of the underlying git process.

I've added a git.showBuffer which will be released 3.18.0 later today that adds the --binary command to the arguments passed into git and directly returns the buffer with binary data which you'll need to git show on images.