2

I am trying to merge branches into a main branch programmatically, using a node script. The first thing I tried was from Execute a command line binary with Node.js, which worked fine for executing terminal commands. But I encountered difficulties with changing the working directory. I wanted to change the working directory to the git repository, then run git commands from there. Executing cd ~/repo or cd /home/me/repo did not change the working directory at all and I could not find any way to change it other than using cd.

I then tried executing the git commands from the script's directory but pointed at my repo. I tried git --git-dir and git --work-tree but neither of those commands worked. The error was the same: fatal: Not a git repository (or any of the parent directories): .git I'm guessing this is because the directory where the script is running from is not a git repo.

So, I either want to send git commands to a directory other than the one I am running the script from, or I want a way to change the script's working directory. Preferably the latter. My full code and output is below:

import JiraData from './jiradata';
const jiraData = new JiraData();
const { exec } = require('child_process');

(async function () {
    const cards = await jiraData.getCardsInTest();
    let commands = ['cd /home/me/repo', 'pwd'];
    let proceeding = true;
    // for (const card of cards) {
    //     commands.push(`git merge origin/${card} --commit --no-edit`);
    // }

    for (const command of commands) {
        if (proceeding) {
            console.log(`Executing command "${command}"`);
            exec(command, (err, stdout, stderr) => {
                if (err) {
                    proceeding = false;
                    console.log(stderr);
                    return;
                }
                console.log(stdout);
            })
        }
    }
})();

Output:

> ci@1.0.0 start /home/me/CI
> babel-node index.js --presets es2015,stage-2

Executing command "cd /home/me/repo"
Executing command "pwd"
/home/me/CI
Al Fahad
  • 2,378
  • 5
  • 28
  • 37
Uncle Gus
  • 61
  • 1
  • 7
  • Try `git --git-dir=/home/me/repo/.git --work-tree=/home/me/repo merge foo`. Don't use `~` to replace `/home/me` in the path. – ElpieKay Oct 17 '17 at 02:56
  • Any particular reason why you're doing this as a child process, rather than using the npm git package? It's about a million times easier when using that... https://www.npmjs.com/package/nodegit – dvsoukup Oct 17 '17 at 03:39

2 Answers2

1

Try instead with:

git -C /path/to/git/repo your_git_command

That will execute the git command in the context of your git repo /path/to/git/repo

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thank you, I'll try that. I did already find a solution for this case, but it might be worth simplifying it to use your suggestion. – Uncle Gus Oct 18 '17 at 03:28
1

I got around this problem by using ShellJS. ShellJS has a method .cd() which changes the directory and seems to stick.

Uncle Gus
  • 61
  • 1
  • 7