-
-
Notifications
You must be signed in to change notification settings - Fork 617
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
178 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
-- | ||
-- git_integration.lua | ||
-- | ||
-- Code for git integration. | ||
-- | ||
-- Copyright (c) 2024 Jason Perkins and the Premake project | ||
-- | ||
|
||
local p = premake | ||
local api = p.api | ||
p.git_integration = {} | ||
local m = p.git_integration | ||
|
||
----------------------------------------------------------------------------- | ||
-- | ||
-- Register gitintegration. | ||
-- | ||
----------------------------------------------------------------------------- | ||
|
||
api.register { | ||
name = "gitintegration", | ||
scope = "global", | ||
kind = "string", | ||
allowed = { | ||
"Off", | ||
"Always", | ||
"OnNewFiles" | ||
} | ||
} | ||
|
||
--- | ||
-- Find root directory (directory containing '.git' directory). | ||
--- | ||
local function find_git_root(current_path) | ||
current_path = path.getabsolute(current_path or _MAIN_SCRIPT_DIR) | ||
|
||
while not os.isdir(path.join(current_path, ".git")) do | ||
current_path = path.getdirectory(current_path) | ||
if current_path == "" then | ||
error("No git root path") | ||
end | ||
end | ||
return current_path | ||
end | ||
|
||
--- | ||
-- Write git post-checkout content | ||
--- | ||
local function print_git_post_checkout_hooks(root_path, mode) | ||
local args = {} | ||
for _, arg in ipairs(_ARGV) do | ||
if not (arg:startswith("--file") or arg:startswith("/file")) then | ||
table.insert(args, arg); | ||
end | ||
end | ||
|
||
local indent = '' | ||
p.outln('#!/bin/sh') | ||
if mode == 'OnNewFiles' then | ||
p.outln('count=`git diff --compact-summary $1 $2 | grep -E "( \\(new\\)| \\(gone\\)|premake)" | wc -l`') | ||
p.outln('if [ $count -ne 0 ]') | ||
p.outln('then') | ||
indent = ' ' | ||
end | ||
p.outln(indent .. p.esc(path.getrelative(root_path, _PREMAKE_COMMAND)) .. ' --file=' .. p.esc(path.getrelative(root_path, _MAIN_SCRIPT)) .. ' ' .. table.concat(p.esc(args), ' ')) | ||
if mode == 'OnNewFiles' then | ||
p.outln('fi') | ||
end | ||
p.outln('') | ||
end | ||
|
||
--- | ||
-- Generate .git/hooks/post-checkout according to mode | ||
--- | ||
function m.gitHookInstallation() | ||
local git_integration_mode = p.api.rootContainer().gitintegration or "Off" | ||
if git_integration_mode == "Off" then | ||
return | ||
end | ||
local root_path = find_git_root() | ||
|
||
local content = p.capture(function () print_git_post_checkout_hooks(root_path, git_integration_mode) end) | ||
local res, err = os.writefile_ifnotequal(content, path.join(root_path, '.git', 'hooks', 'post-checkout')) | ||
|
||
if (res == 0) then -- file not modified | ||
return | ||
elseif (res < 0) then | ||
error(err, 0) | ||
elseif (res > 0) then -- file modified | ||
printf("Generated %s...", path.getrelative(os.getcwd(), path.join(root_path, '.git', 'hooks', 'post-checkout'))) | ||
return | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
Enable git integration to run premake on checkout. | ||
|
||
```lua | ||
gitintegration ("value") | ||
``` | ||
|
||
### Parameters ### | ||
|
||
| Action | Description | | ||
|-----------------|----------------------------------------------------------------------------------| | ||
| Off | Disable git integration. | | ||
| Always | Run premake on checkout. | | ||
| OnNewFiles | Run premake only when files are added/removed or if premake script has changed. | | ||
|
||
### Applies To ### | ||
|
||
Global scope. | ||
|
||
### Availability ### | ||
|
||
Premake 5.0.0 beta 3 or later. | ||
|
||
### Examples ### | ||
|
||
Regenerate autoversion.h with git tag when checkout to another branch. | ||
|
||
```lua | ||
gitintegration "Always" | ||
|
||
local locationDir = _OPTIONS["to"] | ||
|
||
local function autoversion_h() | ||
local git_tag, errorCode = os.outputof("git describe --tag --always") | ||
if errorCode == 0 then | ||
print("git description: ", git_tag) | ||
local content = io.readfile("src/autoversion.h.in") | ||
content = content:gsub("${GIT_DESC}", git_tag) | ||
|
||
os.mkdir(locationDir) | ||
local f, err = os.writefile_ifnotequal(content, path.join(locationDir, "autoversion.h")) | ||
|
||
if (f == 0) then -- file not modified | ||
elseif (f < 0) then | ||
error(err, 0) | ||
return false | ||
elseif (f > 0) then | ||
print("Generated autoversion.h...") | ||
end | ||
|
||
return true | ||
else | ||
print("`git describe --tag` failed with error code", errorCode, git_tag) | ||
return false | ||
end | ||
end | ||
|
||
local have_autoversion_h = autoversion_h() | ||
|
||
workspace "MyProject" | ||
location(locationDir) | ||
|
||
if have_autoversion_h then | ||
includedirs { locationDir } -- for generated file (autoversion.h) | ||
end | ||
-- [..] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters