283b622013-03-26Martin Nilsson #pike __REAL_VERSION__
92253b2011-11-16Henrik Grubbström (Grubba) 
9486672013-07-20Henrik Grubbström (Grubba) //! This is a module for interacting with the Git //! distributed version control system. //! The git binary to use. //! //! Defaults to @expr{"git"@}, but may be overridden //! to select a different binary.
92253b2011-11-16Henrik Grubbström (Grubba) string git_binary = "git"; //! A normal (non-executable) file. constant MODE_FILE = 0100644; //! A normal, but executable file. constant MODE_EXE = 0100755; //! A symbolic link. constant MODE_SYMLINK = 0120000; //! A gitlink (aka submodule reference). constant MODE_GITLINK = 0160000; //! A subdirectory. constant MODE_DIR = 040000; //! The NULL SHA1. constant NULL_SHA1 = "0000000000000000000000000000000000000000"; //! Start a git process.
9486672013-07-20Henrik Grubbström (Grubba) //! //! @param options //! Options for @[Process.Process()]. //! //! @param git_dir //! Directory containing the Git repository. May be @expr{UNDEFINED@} //! to specify the Git repository for the current directory. //! //! @param command //! Git subcommand to execute. //! //! @param args //! Arguemnts for @[command]. //! //! @returns //! Returns the corresponding @[Process.Process] object.
31882a2016-01-27Henrik Grubbström (Grubba) //! //! @seealso //! @[git()], @[try_git()]
92253b2011-11-16Henrik Grubbström (Grubba) Process.Process low_git(mapping(string:mixed) options, string git_dir, string command, string ... args) {
9486672013-07-20Henrik Grubbström (Grubba)  if (git_dir) { return Process.Process(({ git_binary, "--git-dir", git_dir, command, @args }), options); } return Process.Process(({ git_binary, command, @args }), options);
92253b2011-11-16Henrik Grubbström (Grubba) } //! Run a git command, and get the output.
9486672013-07-20Henrik Grubbström (Grubba) //! //! @param git_dir //! Directory containing the Git repository. May be @expr{UNDEFINED@} //! to specify the Git repository for the current directory. //! //! @param command //! Git subcommand to execute. //! //! @param args //! Arguemnts for @[command]. //! //! @returns //! Returns the output on @tt{stdout@} from running the command
31882a2016-01-27Henrik Grubbström (Grubba) //! on success, and throws an error on failure. //! //! @seealso //! @[try_git()], @[low_git()]
92253b2011-11-16Henrik Grubbström (Grubba) string git(string git_dir, string command, string ... args) {
9486672013-07-20Henrik Grubbström (Grubba)  array(string) cmd; if (git_dir) { cmd = ({ git_binary, "--git-dir", git_dir, command, @args }); } else { cmd = ({ git_binary, command, @args }); }
92253b2011-11-16Henrik Grubbström (Grubba)  mapping(string:string|int) res = Process.run(cmd); if (res->exitcode) { werror("CWD: %O\n", getcwd()); error("Git command '%s' failed with code %d:\n" "%s", cmd*"' '", res->exitcode, res->stderr||""); } return res->stdout||""; }
31882a2016-01-27Henrik Grubbström (Grubba) //! Attempt to run a git command and get its output. //! //! @param git_dir //! Directory containing the Git repository. May be @expr{UNDEFINED@} //! to specify the Git repository for the current directory. //! //! @param command //! Git subcommand to execute. //! //! @param args //! Arguemnts for @[command]. //! //! @returns //! Returns the output on @tt{stdout@} from running the command //! on success, and @expr{0@} (zero) on failure. //! //! @note //! This is a convenience function, and there is no way to get //! the specific cause for failures other than rerunning the //! command with e.g. @[git()]. //! //! @seealso //! @[git()], @[low_git()] string try_git(string git_dir, string command, string ... args) { mixed err = catch { return git(git_dir, command, @args); }; return 0; }
92253b2011-11-16Henrik Grubbström (Grubba) //! Hash algorithm for blobs that is compatible with git. string hash_blob(string data) { Crypto.SHA1 sha1 = Crypto.SHA1(); sha1->update(sprintf("blob %d\0", sizeof(data))); sha1->update(data); return String.string2hex(sha1->digest()); }