Git Post-Receive Hook不一致地添加更改

时间:2012-07-03 19:14:32

标签: php git branch githooks git-post-receive

我正在尝试编写一个post-receive钩子,它会将所有新分支拉入bitbucket帐户。然后,如果推送这些更改的人是特定开发站点的指定用户,则应自动检出该分支。

这是代码:

$payload = stripslashes(($_POST['payload']) ? $_POST['payload'] : $_GET['payload']);
$obj = json_decode($payload);

$commits = $obj->commits;
$head_commit = $commits[0];

for ($i=0; $i<sizeof($commits); $i++) {
    $commit = $commits[$i];
    $head_date = strtotime($head_commit->timestamp);
    $comm_date = strtotime($commit->timestamp);

    if ($comm_date > $head_date) {
        $head_commit = $commit;
    }
}

$branch = $head_commit->branch;
$author = $head_commit->raw_author;

$msgs = array();                                
$ini = parse_ini_file('pull_settings.ini',true);
define ("CURRENT_SITE",$_SERVER["SERVER_NAME"]);

foreach ($ini as $server) {
    if (CURRENT_SITE==$server["site_name"]) {
        if ($server["type"]=="master") {
            $users = $server["users"];
            if (in_array($author,$users) && $branch == "master") {
                exec("git pull",$msgs);
            } else $users = array();
        }
        if ($server["type"]=="dev") {
            exec("git stash",$msgs);
            exec("git fetch --all",$msgs);
            $users = $server["users"];
            if (in_array($author,$users)) {
                exec("git checkout -f $branch",$msgs);
                exec("git merge origin/$branch",$msgs);
            } else $users = array();
        }
    }
}

$payload = stripslashes(($_POST['payload']) ? $_POST['payload'] : $_GET['payload']); $obj = json_decode($payload); $commits = $obj->commits; $head_commit = $commits[0]; for ($i=0; $i<sizeof($commits); $i++) { $commit = $commits[$i]; $head_date = strtotime($head_commit->timestamp); $comm_date = strtotime($commit->timestamp); if ($comm_date > $head_date) { $head_commit = $commit; } } $branch = $head_commit->branch; $author = $head_commit->raw_author; $msgs = array(); $ini = parse_ini_file('pull_settings.ini',true); define ("CURRENT_SITE",$_SERVER["SERVER_NAME"]); foreach ($ini as $server) { if (CURRENT_SITE==$server["site_name"]) { if ($server["type"]=="master") { $users = $server["users"]; if (in_array($author,$users) && $branch == "master") { exec("git pull",$msgs); } else $users = array(); } if ($server["type"]=="dev") { exec("git stash",$msgs); exec("git fetch --all",$msgs); $users = $server["users"]; if (in_array($author,$users)) { exec("git checkout -f $branch",$msgs); exec("git merge origin/$branch",$msgs); } else $users = array(); } } }

问题在于:

它并不总是有效!有时它不会检查分支,即使它应该。此外,有时它会在检出新分支时留下文件。这导致git说修改了一大堆文件,这些文件不在它所在的分支内。

请帮忙!非常困难。

由于

1 个答案:

答案 0 :(得分:0)

git stash将保存已跟踪文件的修改。如果您有新文件,则不会保存它们。这不可能是你的问题之一吗?您有新的未跟踪文件,然后由您的git merge添加。