question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Issue with .raw method and git cherry-pick

See original GitHub issue

Hi, I found this solution in an old closed issue:


There isn’t currently development on supporting cherry-pick, but you can use the library’s raw method to still run your command at the moment:

const git = require('simple-git');
git().raw(['cherry-pick', '--strategy=recursive', '-X', 'theirs', sha], function (err, data) {
  err && console.error(err);
  err || console.log(data);
}); 

_Originally posted by @steveukx in https://github.com/steveukx/git-js/issues/270#issuecomment-390886146_


Unfortunately, I’m having issues with this approach (working in typescript). My compiler throws this error:

src/main.ts:206:7 - error TS2769: No overload matches this call.
  The last overload gave the following error.
    Argument of type 'string[]' is not assignable to parameter of type 'string'.

206       ['cherry-pick', commitHash],
          ~~~~~~~~~~~~~~~~~~~~~~~~~~~

  node_modules/simple-git/typings/simple-git.d.ts:470:4
    470    raw(a: string, b: string, callback?: types.SimpleGitTaskCallback<string>): Response<string>;
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    The last overload is declared here.


Found 1 error.

This is my code (all the presumably necessary bits), changed it a little bit, but even the same code you suggested above throws the same error:

import fsSync from 'fs';
import simpleGit, { SimpleGit, SimpleGitOptions } from 'simple-git';

const gitOptions: SimpleGitOptions = {
  baseDir: `${process.cwd()}/docs`,
  binary: 'git',
  maxConcurrentProcesses: 6,
  config: []
};

const git = gitSetup(gitOptions);

function gitSetup (options: SimpleGitOptions): SimpleGit {
  if (!fsSync.existsSync('./docs/.git')) {
    fsSync.mkdirSync('./docs', { recursive: true });
  }
  return simpleGit(options);
}

// ...

async function publish (filePath: string): Promise<void> {
  // get commit hash
  const commitHash = (await git.log({ file: `./docs/${filePath}` })).latest.hash;

  // publish
  console.log('Publishing ...');
  const status = await git
    .checkout('publish-test')
    .pull()
    .raw(
      ['cherry-pick', commitHash],
      (err, data) => {
        if (err) {
          console.error(err);
        } else {
          console.log(data);
        }
      }
    )
    .push('origin', 'publish-test');
  console.log(status);
  console.log('Publishing completed.');
}

I even found this overload in the simple-git.d.ts, I wonder why this doesn’t work out:

raw(commands: string | string[] | types.TaskOptions, callback?: types.SimpleGitTaskCallback<string>): Response<string>;

Can you help with this issue?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
steveukxcommented, Aug 13, 2021

Hi, you should now be able to upgrade to 2.43.0 to use your existing code without the need to explicitly set types.

If you have any other problems, please do open a new issue. Thanks.

0reactions
steveukxcommented, Aug 9, 2021

Hi, thank you for all the additional detail on this.

There is a problem with the callback types - I’m using an interface with multiple call signatures rather than a union type of function types, so Parameters<SimpleGitTaskCallback<string>> currently always comes back as just [Error] rather than [null, string] | [Error].

I will arrange a fix for this, but in the meantime you can unblock yourself by explicitly adding types to your code, eg:

const status = await git
    .checkout('publish-test')
    .pull()
    .raw(['cherry-pick', commitHash], (...[err, data]: [Error] | [null, string]) => {
        if (err) console.error(err);
        if (data) console.log(data);
    })
   .push('origin', 'publish-test');
Read more comments on GitHub >

github_iconTop Results From Across the Web

Git Cherry-Picking: Handling urgent hotfixes | Git Workflows
Java/Spring courses & guideshttps://www.marcobehler.com▻ Newsletterhttps://bit.ly/2K0Ao4F▻ YouTube subhttps://bit.ly/2lVExgmWhat are you ...
Read more >
Cannot cherry-pick commit changing not-existing file
My understanding is that a commit represents a full snapshot of working directory. So cherry-picking a commit containing file that currently ...
Read more >
Cherry-pick changes - GitLab Docs
In Git, cherry-picking is taking a single commit from one branch and adding it as the latest commit on another branch. The rest...
Read more >
git-cherry-pick Documentation - Git
If a commit being cherry picked duplicates a commit already in the current history, it will become empty. By default these redundant commits...
Read more >
Git Cherry Pick | Atlassian Git Tutorial
Git cherry -pick is a useful tool when merging several branches together but not always a best practice. Learn when, how and where...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found