Revert "Make it possible to set up libgit directly (instead of from the environment)"
[gitweb.git] / perl / Git.xs
index d4608eb1cb198f0c5426d90270e202d441c7da82..2bbec4365f9c244a7a905df702393bca591d9d67 100644 (file)
 #include "perl.h"
 #include "XSUB.h"
 
-#include "ppport.h"
+
+static char *
+report_xs(const char *prefix, const char *err, va_list params)
+{
+       static char buf[4096];
+       strcpy(buf, prefix);
+       vsnprintf(buf + strlen(prefix), 4096 - strlen(prefix), err, params);
+       return buf;
+}
+
+static void NORETURN
+die_xs(const char *err, va_list params)
+{
+       char *str;
+       str = report_xs("fatal: ", err, params);
+       croak(str);
+}
+
+static void
+error_xs(const char *err, va_list params)
+{
+       char *str;
+       str = report_xs("error: ", err, params);
+       warn(str);
+}
 
 
 MODULE = Git           PACKAGE = Git
 
 PROTOTYPES: DISABLE
 
+
+BOOT:
+{
+       set_error_routine(error_xs);
+       set_die_routine(die_xs);
+}
+
+
 # /* TODO: xs_call_gate(). See Git.pm. */
 
 
-const char *
+char *
 xs_version()
 CODE:
 {
@@ -33,11 +65,11 @@ OUTPUT:
        RETVAL
 
 
-const char *
+char *
 xs_exec_path()
 CODE:
 {
-       RETVAL = git_exec_path();
+       RETVAL = (char *)git_exec_path();
 }
 OUTPUT:
        RETVAL
@@ -66,42 +98,36 @@ CODE:
 }
 
 char *
-xs_hash_object(file, type = "blob")
-       SV *file;
+xs_hash_object_pipe(type, fd)
+       char *type;
+       int fd;
+CODE:
+{
+       unsigned char sha1[20];
+
+       if (index_pipe(sha1, fd, type, 0))
+               croak("Unable to hash given filehandle");
+       RETVAL = sha1_to_hex(sha1);
+}
+OUTPUT:
+       RETVAL
+
+char *
+xs_hash_object_file(type, path)
        char *type;
+       char *path;
 CODE:
 {
        unsigned char sha1[20];
+       int fd = open(path, O_RDONLY);
+       struct stat st;
+
+       if (fd < 0 ||
+           fstat(fd, &st) < 0 ||
+           index_fd(sha1, fd, &st, 0, type))
+               croak("Unable to hash %s", path);
+       close(fd);
 
-       if (SvTYPE(file) == SVt_RV)
-               file = SvRV(file);
-
-       if (SvTYPE(file) == SVt_PVGV) {
-               /* Filehandle */
-               PerlIO *pio;
-
-               pio = IoIFP(sv_2io(file));
-               if (!pio)
-                       croak("You passed me something weird - a dir glob?");
-               /* XXX: I just hope PerlIO didn't read anything from it yet.
-                * --pasky */
-               if (index_pipe(sha1, PerlIO_fileno(pio), type, 0))
-                       croak("Unable to hash given filehandle");
-               /* Avoid any nasty surprises. */
-               PerlIO_close(pio);
-
-       } else {
-               /* String */
-               char *path = SvPV_nolen(file);
-               int fd = open(path, O_RDONLY);
-               struct stat st;
-
-               if (fd < 0 ||
-                   fstat(fd, &st) < 0 ||
-                   index_fd(sha1, fd, &st, 0, type))
-                       croak("Unable to hash %s", path);
-               close(fd);
-       }
        RETVAL = sha1_to_hex(sha1);
 }
 OUTPUT: