Code Monkey home page Code Monkey logo

Comments (7)

chippyash avatar chippyash commented on July 20, 2024 1

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.

from git-php.

Taitava avatar Taitava commented on July 20, 2024 1

Thank you for this one too! :)

from git-php.

Taitava avatar Taitava commented on July 20, 2024

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? :)

from git-php.

Taitava avatar Taitava commented on July 20, 2024

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?

from git-php.

janpecha avatar janpecha commented on July 20, 2024

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.

from git-php.

Taitava avatar Taitava commented on July 20, 2024

Ok, thanks for clarification. :)

from git-php.

janpecha avatar janpecha commented on July 20, 2024

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

from git-php.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.