Git.pm: Call external commands using execv_git_cmd()
[git/debian.git] / perl / Git.xs
blob6478f9c77f4ead953c0f1fdbf9d9c41732529215
1 /* By carefully stacking #includes here (even if WE don't really need them)
2  * we strive to make the thing actually compile. Git header files aren't very
3  * nice. Perl headers are one of the signs of the coming apocalypse. */
4 #include <ctype.h>
5 /* Ok, it hasn't been so bad so far. */
7 /* libgit interface */
8 #include "../cache.h"
9 #include "../exec_cmd.h"
11 /* XS and Perl interface */
12 #include "EXTERN.h"
13 #include "perl.h"
14 #include "XSUB.h"
16 #include "ppport.h"
19 MODULE = Git            PACKAGE = Git
21 PROTOTYPES: DISABLE
23 # /* TODO: xs_call_gate(). See Git.pm. */
26 const char *
27 xs_exec_path()
28 CODE:
30         RETVAL = git_exec_path();
32 OUTPUT:
33         RETVAL
36 void
37 xs__execv_git_cmd(...)
38 CODE:
40         const char **argv;
41         int i;
43         argv = malloc(sizeof(const char *) * (items + 1));
44         if (!argv)
45                 croak("malloc failed");
46         for (i = 0; i < items; i++)
47                 argv[i] = strdup(SvPV_nolen(ST(i)));
48         argv[i] = NULL;
50         execv_git_cmd(argv);
52         for (i = 0; i < items; i++)
53                 if (argv[i])
54                         free((char *) argv[i]);
55         free((char **) argv);
58 char *
59 xs_hash_object(file, type = "blob")
60         SV *file;
61         char *type;
62 CODE:
64         unsigned char sha1[20];
66         if (SvTYPE(file) == SVt_RV)
67                 file = SvRV(file);
69         if (SvTYPE(file) == SVt_PVGV) {
70                 /* Filehandle */
71                 PerlIO *pio;
73                 pio = IoIFP(sv_2io(file));
74                 if (!pio)
75                         croak("You passed me something weird - a dir glob?");
76                 /* XXX: I just hope PerlIO didn't read anything from it yet.
77                  * --pasky */
78                 if (index_pipe(sha1, PerlIO_fileno(pio), type, 0))
79                         croak("Unable to hash given filehandle");
80                 /* Avoid any nasty surprises. */
81                 PerlIO_close(pio);
83         } else {
84                 /* String */
85                 char *path = SvPV_nolen(file);
86                 int fd = open(path, O_RDONLY);
87                 struct stat st;
89                 if (fd < 0 ||
90                     fstat(fd, &st) < 0 ||
91                     index_fd(sha1, fd, &st, 0, type))
92                         croak("Unable to hash %s", path);
93                 close(fd);
94         }
95         RETVAL = sha1_to_hex(sha1);
97 OUTPUT:
98         RETVAL