2

I'm trying to set up a macro to set up git repo for version control, so that I don't have to resort to manual terminal commands every time. Commit & push scripts are ready and working, it's initialising a new git repo that gives me grief:

The idea is that a script will perform 4 functions:

  1. Initialise a git repo at the same directory your file is saved
  2. Clone a remote repo (I have a base repo with scripts/packages I want present into all tex projects I create)
  3. Create a new remote repo (using Node GH) and assign it as the origin (basically re-creating the ability to fork a repo from yourself via command line)
  4. Commit and push all files to the new remote repo.

Here's my code:

%SCRIPT
buildManager.runCommand("git init", editor.fileName());
buildManager.runCommand("gh re --new " + editor.fileName() + 
  " --type private", editor.fileName());
buildManager.runCommand("git clone https://github.com/example/example.git " 
  + editor.fileName(), editor.fileName());
buildManager.runCommand("git remote set-url origin https://github.com/example/" 
  + editor.fileName(), editor.fileName());
buildManager.runCommand("git push origin master", editor.fileName());
buildManager.runCommand("git push --all", editor.fileName())

The problem is I think TeXStudio doesn't perform the commands in order of appearance, which results in some commands failing (sample output at the bottom). Is there a way to define the order the commands are performed? Alternatively, can I do the same by calling a bash script (where I can definitely define the order) and pass the working dir through the macro?


Log output:

Process started: git push --all
fatal: Not a git repository (or any of the parent directories): .git
Process exited with error(s)    
fatal: Not a git repository (or any of the parent directories): .git
Process exited with error(s)
fatal: Not a git repository (or any of the parent directories): .git
Process exited with error(s)
fatal: could not create work tree dir '/NewProject': Permission denied
/.git: Permission denied
Process exited with error(s)
Process exited with error(s)
Process exited normally
2

buildmanager.runCommand() does not wait for the termination of the command. If you need that control, you should use system() instead. It returns a process object which you then can control (for details see the manual).

In your particular case, it would still be a bit tedious because you want to have the command executed in the directory of the file. As of now (TXS 2.10.8) the working directory of system() is the working directory of TeXstudio. So you either have to start TeXstudio from the shell in the desired directory or you have to wrap the git call in a shell script that changes the directory first.

Future releases will allow an optional second argument to system() for providing a working directory.

filename = editor.fileName()
workdir = filename.substring(0, filename.lastIndexOf("/"))
proc = system("git init", workdir)
proc.waitForFinished()
...
  • Thanks for the clarification, I'll look forward to the additional arguments in future releases. I did end up enclosing it in a shell script in the end, passing two arguments from input dialogs for file name and working dir. I'll post the workaround below as an answer, just for reference. – Nikko Mar 27 '16 at 14:24
1

This is a work-around until TeXStudio allows for arguments to its system() command (see Tim Hoffmann's answer for details).

What follows works basically like this: A shell script changes the working dir to the desired path, uses Node GH to create a remote repo with the desired name and clones it to the working dir. The working dir and repo name are passed to the script as arguments by two input dialogs.

%SCRIPT
choisedialog = UniversalInputDialog()
choisedialog.setWindowTitle("Repo Name")
choisedialog.add(editor.fileName(), "New Repository:", "name")
if (choisedialog.exec() != null) {
    dialog = new UniversalInputDialog()
    dialog.setWindowTitle("Working Dir")
    dialog.add(editor.fileName(), "Dir path:", "dir")
    if (dialog.exec() != null) {
        repoName = choisedialog.get("name")
        workDir = dialog.get("dir")
        system("sh /Users/nikko/git/scripts/createRepo.sh " + repoName + " \"" + workDir + "\"")
        }
}

createRepo.sh:

#!/bin/bash
#make sure you clone the generic repo on an empty folder,
#otherwise git will complain.

file="$1"
dir="$2"

cd "$dir"
gh re --new "$file" --type private  \
&& git clone https://github.com/user/genericRepoName.git . \
&& git remote set-url origin https://github.com/user/"$file".git \
&& git push origin master  \
&& git push --all

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.