Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'git commit' command should run in a new terminal #39

Open
alessandro-newzoo opened this issue Mar 29, 2022 · 7 comments
Open

'git commit' command should run in a new terminal #39

alessandro-newzoo opened this issue Mar 29, 2022 · 7 comments

Comments

@alessandro-newzoo
Copy link

Hello!
I still need to figure out how to use this, but thanks a lot for your hard work!

When I run the extension's command, git commit runs in the terminal I'm currently using, which might cause issues if something like Gulp is running.

Besides potential issues, git commit will not actually commit since the terminal is running a task, hence won't take any input.
It will just print it and that's it:

image

So I suggest you have the extension command to open a new terminal tab, and run the command in it.
You can use vscode.window.createTerminal for that 🙂

@jmaicaaan
Copy link
Owner

Hi @alessandro-newzoo thanks for checking! 👍 I've experienced this too but I can't seem to have a proper solution if there are multiple tabs and you just lost the "focus".

In your use case, I understand that creating another terminal will solve it. But I usually have multiple terminals like 1 terminal for the React server, and 1 terminal for another process. Then another 1 for commiting my changes.

From a user perspective, I'm not sure if I would like to open multiple terminals for my 5 files changed... What do you think about that?

Can VSCode find the available terminal where it can do the command? 🤔 🤔 🤔

The alternative working solution I've found here is to copy the commit on the clipboard so that it doesn't get lost

@solid-pixel
Copy link

hey, I'm the issue reporter, from my personal account!

Yeah I also usually use multiple terminals, but always keep gulp in the foreground as I need to see if LESS throws any errors when I save the files.

Now that I think about it, why use the terminal at all for your extension? You can run a shell command with NodeJS's child_process without going through the terminal. Would save you a lot of headache, would be cleaner and won't cause issues like the one I reported :)
Using the terminal would be very unreliable, you have no idea in what directory the user currently is, and might send commits to the wrong repo, or no repo at all.

If you wanna go without the terminal, add this at the top of your file:

const cp = require('child_process');

And then this to replace your function that writes to the terminal:

	// Get this value from the commit message:
	let commitText = 'test'; 

	// Replace this path with a config where the user can specify the directory
        // containing the repo they want to work with
	let folderPath = "C:\\Users\\Alessandro\\Project";

	let command = `git commit -m "${commitText}"`;
	let opts = {
		"cwd": folderPath
	}

	// console.log() is for you to debug, you can either remove it, or send "stdout" to an output channel
        // or a notification with "vscode.window.showInformationMessage". I personally wouldn't show anything
	cp.exec(command, opts, function(err, stdout, stderr) {
		console.log('stdout: ' + stdout);
		console.log('stderr: ' + stderr);
		if (err) {
			console.log('error: ' + err);
		}
	});

If you check your Debug Console, you'll see the git command has run in the specified directory, and didn't need to open a terminal.

The only challenge for you to solve is the folderPath variable, which you still have with the terminal approach anyway.

Based on my research, there are mainly 3 ways to go about it:

1 - You let the user enter the path in a setting, like I said above.

2 - vscode.window.activeTextEditor

// This outputs the absolute path of the open file in the editor
let folderPath = vscode.window.activeTextEditor?.document.uri.fsPath;

// Issue: other things are considered editors too, so if you're focused for example 
// on the Output view, it won't know the path of the file anymore

3 - vscode.workspace.workspaceFolders

// This outputs an array of all the workspace folders in the open workspace. This is very solid but has some issues too.
let folderPath = vscode.workspace.workspaceFolders?.map(folder => folder.uri.path);

// Issue #1: if people have more than one workspace folder in the workspace, 
// the array returns them all. How do you know which one is the right one?
// Could work around this with some prompt in the QuickPick I guess, that appears only
// if they have more than 1 workspace folder and lets them click on the correct one

// Issue #2: a workspace folder doesn't necessarily contain a repository, 
// perhaps this could be solved with the workaround mentioned in Issue #1

Hope this helps 😄

@solid-pixel
Copy link

solid-pixel commented Mar 29, 2022

Forgot to add, if you really wanna go the terminal way, then you can create a new terminal for the git command to run, and add ; exit at the end of the git command to quit the new terminal once done:

let command = `git commit -m "${commitText}"; exit`

@jmaicaaan
Copy link
Owner

jmaicaaan commented Mar 30, 2022

@solid-pixel - Thanks again for your time! The reason I have gone with the terminal approach and not with the "automatic commit behind the scene" approach is that I want the user to "see" or "verify" what is going to be committed.

It seems like there are users who are okay not seeing or verifying it already and find the process redundant (I guess).

I'm thinking maybe we can extend the current setting "Auto Commit" which paste it into the terminal and automatically enters it without you manually doing it.

image

What do you think? Would you be okay enabling the "Auto Commit" setting and then it would spin up a node process at the background or terminal then close it without you seeing the constructed commit? 😄

@solid-pixel
Copy link

hey sorry for the late reply, I've been busy finishing my extension!

I understand why you want the user to see what happens, that makes sense, but still, relying on the terminal seems quite unreliable for the reasons explained above - perhaps you can use the status bar, notification messages, or the quick pick (command palette) to ask for confirmation? Or at the very least instruct the user to open a terminal in the right directory before committing.

To answer your question, yeah I would be ok I think, because I commit all the time and having to confirm each commit would disrupt my flow a bit, but that is just my personal opinion and use case, and your idea of showing what's going on might apply to a lot of other people :)

Either way, I've decided I don't want my commits to contain emojis, so I'm not using the extension anymore sorry :D

But still happy to provide my input if it helps other users out there 👍

@jmaicaaan
Copy link
Owner

Thanks for your feedback! I am considering your input :)

Either way, I've decided I don't want my commits to contain emojis, so I'm not using the extension anymore sorry :D

No problem! In case you haven't checked it, you can remove the emojis and customize the "format" of the commit message 😉

Good luck with your extension! Hope you can share it with me too once it's done!

@solid-pixel
Copy link

oh nice, I'll give it a try when I have a moment, and no worries happy to help!

Here's my (first) extension, very niche use case though so I doubt you'll need it :D it was made out of necessity for myself, then decided to share it: https://marketplace.visualstudio.com/items?itemName=AlessandroBenassi.cloudflare-devtools

I left you a 5 stars review :)

Take care!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants