Make it possible to set up libgit directly (instead of from the environment)
[git] / perl / Git.xs
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. */
6
7 /* libgit interface */
8 #include "../cache.h"
9 #include "../exec_cmd.h"
10
11 /* XS and Perl interface */
12 #include "EXTERN.h"
13 #include "perl.h"
14 #include "XSUB.h"
15
16
17 static char *
18 report_xs(const char *prefix, const char *err, va_list params)
19 {
20         static char buf[4096];
21         strcpy(buf, prefix);
22         vsnprintf(buf + strlen(prefix), 4096 - strlen(prefix), err, params);
23         return buf;
24 }
25
26 static void NORETURN
27 die_xs(const char *err, va_list params)
28 {
29         char *str;
30         str = report_xs("fatal: ", err, params);
31         croak(str);
32 }
33
34 static void
35 error_xs(const char *err, va_list params)
36 {
37         char *str;
38         str = report_xs("error: ", err, params);
39         warn(str);
40 }
41
42
43 MODULE = Git            PACKAGE = Git
44
45 PROTOTYPES: DISABLE
46
47
48 BOOT:
49 {
50         set_error_routine(error_xs);
51         set_die_routine(die_xs);
52 }
53
54
55 void
56 xs__call_gate(repoid, git_dir)
57         long repoid;
58         char *git_dir;
59 CODE:
60 {
61         static long last_repoid;
62         if (repoid != last_repoid) {
63                 setup_git(git_dir,
64                           getenv(DB_ENVIRONMENT),
65                           getenv(INDEX_ENVIRONMENT),
66                           getenv(GRAFT_ENVIRONMENT));
67                 last_repoid = repoid;
68         }
69 }
70
71
72 char *
73 xs_version()
74 CODE:
75 {
76         RETVAL = GIT_VERSION;
77 }
78 OUTPUT:
79         RETVAL
80
81
82 char *
83 xs_exec_path()
84 CODE:
85 {
86         RETVAL = (char *)git_exec_path();
87 }
88 OUTPUT:
89         RETVAL
90
91
92 void
93 xs__execv_git_cmd(...)
94 CODE:
95 {
96         const char **argv;
97         int i;
98
99         argv = malloc(sizeof(const char *) * (items + 1));
100         if (!argv)
101                 croak("malloc failed");
102         for (i = 0; i < items; i++)
103                 argv[i] = strdup(SvPV_nolen(ST(i)));
104         argv[i] = NULL;
105
106         execv_git_cmd(argv);
107
108         for (i = 0; i < items; i++)
109                 if (argv[i])
110                         free((char *) argv[i]);
111         free((char **) argv);
112 }
113
114 char *
115 xs_hash_object_pipe(type, fd)
116         char *type;
117         int fd;
118 CODE:
119 {
120         unsigned char sha1[20];
121
122         if (index_pipe(sha1, fd, type, 0))
123                 croak("Unable to hash given filehandle");
124         RETVAL = sha1_to_hex(sha1);
125 }
126 OUTPUT:
127         RETVAL
128
129 char *
130 xs_hash_object_file(type, path)
131         char *type;
132         char *path;
133 CODE:
134 {
135         unsigned char sha1[20];
136         int fd = open(path, O_RDONLY);
137         struct stat st;
138
139         if (fd < 0 ||
140             fstat(fd, &st) < 0 ||
141             index_fd(sha1, fd, &st, 0, type))
142                 croak("Unable to hash %s", path);
143         close(fd);
144
145         RETVAL = sha1_to_hex(sha1);
146 }
147 OUTPUT:
148         RETVAL