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

Consistency between executing commands #49

Closed
Taitava opened this issue Jan 13, 2020 · 7 comments
Closed

Consistency between executing commands #49

Taitava opened this issue Jan 13, 2020 · 7 comments
Labels
Milestone

Comments

@Taitava
Copy link

Taitava commented Jan 13, 2020

Hi,

I'm having three questions related to consistent command execution. I will post them in this same issue, but in different comments.

1. GitRepository::run() vs. exec()

I'd like to ask about the different styles of executing git commands in this library. Some methods of GitRepository use GitRepository::run() to execute commands, while others use the exec() function directly. Am I correct that this is just because different developers were not familiar with how to execute commands in a consistent way (run())? If so, can I make a PR that changes all commands to be run via run()?

Methods that use run():

  • createTag()
  • removeTag()
  • renameTag()
  • merge()
  • createBranch()
  • removeBranch()
  • checkout()
  • removeFile()
  • addFile()
  • addAllChanges()
  • renameFile()
  • commit()
  • hasChanges()
  • pull()
  • push()
  • fetch()
  • addRemote()
  • renameRemote()
  • removeRemote()
  • setRemoteUrl()

Methods that use exec() directly:

  • getLastCommitId()
  • getCommitMessage()
  • getCommitData()

Well, actually now that I created this list of methods, I noticed that there's only three methods calling exec() directly, but then again I still think that it would be cleaner to refactor them to use run() instead.

@Taitava
Copy link
Author

Taitava commented Jan 13, 2020

2. GitRepository::run() vs GitRepository::execute()

Can you tell me about the differences between run() and execute()? When should execute() be used instead of run()? It seems to me that run() is an internal method not designed to be called outside of GitRepository, which is logical as run() does not call begin() and end() methods to switch the current working directory, which should be done every time a command is run.

execute() does call begin() and end(). And it takes different parameters than run(). execute() must be passed the command without the git prefix, and a command is defined as an array of strings. Examples:

  • To execute git status, you would call execute(['status']) or run('git status').
  • To execute git commit -m 'Hello world!', you would call execute(['commit', '-m' => 'Hello world!']) or run('git commit', ['-m' => 'Hello world!']).

Why the methods do not use similar parameters?

Then one thing is the code inside the methods. Both methods are very similar on what they do, but the code is partly repeated:

protected function run($cmd/*, $options = NULL*/)
{
$args = func_get_args();
$cmd = self::processCommand($args);
exec($cmd . ' 2>&1', $output, $ret);
if($ret !== 0)
{
throw new GitException("Command '$cmd' failed (exit-code $ret).", $ret);
}
return $this;
}

public function execute($cmd)
{
if (!is_array($cmd)) {
$cmd = array($cmd);
}
array_unshift($cmd, 'git');
$cmd = self::processCommand($cmd);
$this->begin();
exec($cmd . ' 2>&1', $output, $ret);
$this->end();
if($ret !== 0)
{
throw new GitException("Command '$cmd' failed (exit-code $ret).", $ret);
}
return $output;
}

Shouldn't execute() call run() instead of calling processCommand() and exec()? It seems to me that it would be more DRY :).

Is there any other differences between run() and execute() that I haven't noticed? :)

@Taitava
Copy link
Author

Taitava commented Jan 13, 2020

3. Every command method must pass the main command "git" to run()

For example run("git commit", ...):

public function commit($message, $params = NULL)
{
if(!is_array($params))
{
$params = array();
}
return $this->begin()
->run("git commit", $params, array(
'-m' => $message,
))
->end();
}

To me it seems that the main command git is repeated unnecessarily. It's not easy to change the main command if one would need to. For example, if git is not defined in the shell user's $PATH, a developer might want to prefix the main command with a path, for example /path/to/git. Or maybe even use a bash script as a wrapper around git to do some customizations (i.e. log all commands that were ran).

Could we change run() so that it wouldn't require the caller to prefix the command with "git "? run() is a protected function, so maybe this will cause some backwards compatibility issues? But not as bad as it would do if the method was public? Maybe this change could go to version 4.0?

@janpecha
Copy link
Contributor

janpecha commented Jan 15, 2020

Hello! Thanks for summary. I know about it, I plan solve it in version 4.0 (see #18 and #39), but I'm currently very busy.

GitRepository::run() vs. exec()

I think exec() is used in places where we need to work with output of command, because run() method cannot return output.

GitRepository::run() vs GitRepository::execute()

The run() method is internal and I plan remove it in version 4.x. The execute() method is public interface for running of commands by users, it can return output.

@janpecha janpecha added the todo label Jan 15, 2020
@janpecha janpecha added this to the Version 4.0 milestone Jan 15, 2020
@Taitava
Copy link
Author

Taitava commented Jan 15, 2020

Ok, thanks for clarification. :)

@chippyash
Copy link

I think the confusion of methods underlays the issue: #52

The clone() method uses proc_open() instead of exec.

This then, is the third method of executing a command.

Ideally, all execution of the git command should be via the same 'runner' method which captures output and returns it. The caller method can decide whether it wants to process the output or not.

Consider creating a CmdResult object and return that from the 'runner' method. The object, a simple data object can hold the command result code + any output etc.

@janpecha
Copy link
Contributor

Fixed 72f177e. Now we use only proc_open().

@Taitava
Copy link
Author

Taitava commented Apr 30, 2021

Thank you for this one too! :)

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

No branches or pull requests

3 participants