@matheusbsbs / matheus-bagliano / commits / 3159c8dc2d
Fork 0

Teach receive-pack about ref-log

This converts receive-pack to use the standard ref locking code instead of its own. As a side effect, it automatically records the "push" event to ref-log if enabled. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Sep 27, 2006 at 02:40 UTC 3159c8dc2da4c88dcecabb95404fe88edc5441e4
1 file changed +15 -73
receive-pack.c
+15 -73
@@ -41,34 +41,6 @@ struct command {
41
42 static struct command *commands;
43
44 -static int is_all_zeroes(const char *hex)
45 -{
46 - int i;
47 - for (i = 0; i < 40; i++)
48 - if (*hex++ != '0')
49 - return 0;
50 - return 1;
51 -}
52 -
53 -static int verify_old_ref(const char *name, char *hex_contents)
54 -{
55 - int fd, ret;
56 - char buffer[60];
57 -
58 - if (is_all_zeroes(hex_contents))
59 - return 0;
60 - fd = open(name, O_RDONLY);
61 - if (fd < 0)
62 - return -1;
63 - ret = read(fd, buffer, 40);
64 - close(fd);
65 - if (ret != 40)
66 - return -1;
67 - if (memcmp(buffer, hex_contents, 40))
68 - return -1;
69 - return 0;
70 -}
71 -
44 static char update_hook[] = "hooks/update";
45
46 static int run_update_hook(const char *refname,
@@ -105,8 +77,8 @@ static int update(struct command *cmd)
77 const char *name = cmd->ref_name;
78 unsigned char *old_sha1 = cmd->old_sha1;
79 unsigned char *new_sha1 = cmd->new_sha1;
108 - char new_hex[60], *old_hex, *lock_name;
109 - int newfd, namelen, written;
80 + char new_hex[41], old_hex[41];
81 + struct ref_lock *lock;
82
83 cmd->error_string = NULL;
84 if (!strncmp(name, "refs/", 5) && check_ref_format(name + 5)) {
@@ -115,59 +87,27 @@ static int update(struct command *cmd)
87 name);
88 }
89
118 - namelen = strlen(name);
119 - lock_name = xmalloc(namelen + 10);
120 - memcpy(lock_name, name, namelen);
121 - memcpy(lock_name + namelen, ".lock", 6);
122 -
90 strcpy(new_hex, sha1_to_hex(new_sha1));
124 - old_hex = sha1_to_hex(old_sha1);
91 + strcpy(old_hex, sha1_to_hex(old_sha1));
92 if (!has_sha1_file(new_sha1)) {
93 cmd->error_string = "bad pack";
94 return error("unpack should have generated %s, "
95 "but I can't find it!", new_hex);
96 }
130 - safe_create_leading_directories(lock_name);
131 -
132 - newfd = open(lock_name, O_CREAT | O_EXCL | O_WRONLY, 0666);
133 - if (newfd < 0) {
134 - cmd->error_string = "can't lock";
135 - return error("unable to create %s (%s)",
136 - lock_name, strerror(errno));
137 - }
138 -
139 - /* Write the ref with an ending '\n' */
140 - new_hex[40] = '\n';
141 - new_hex[41] = 0;
142 - written = write(newfd, new_hex, 41);
143 - /* Remove the '\n' again */
144 - new_hex[40] = 0;
145 -
146 - close(newfd);
147 - if (written != 41) {
148 - unlink(lock_name);
149 - cmd->error_string = "can't write";
150 - return error("unable to write %s", lock_name);
151 - }
152 - if (verify_old_ref(name, old_hex) < 0) {
153 - unlink(lock_name);
154 - cmd->error_string = "raced";
155 - return error("%s changed during push", name);
156 - }
97 if (run_update_hook(name, old_hex, new_hex)) {
158 - unlink(lock_name);
98 cmd->error_string = "hook declined";
99 return error("hook declined to update %s", name);
100 }
162 - else if (rename(lock_name, name) < 0) {
163 - unlink(lock_name);
164 - cmd->error_string = "can't rename";
165 - return error("unable to replace %s", name);
166 - }
167 - else {
168 - fprintf(stderr, "%s: %s -> %s\n", name, old_hex, new_hex);
169 - return 0;
101 +
102 + lock = lock_any_ref_for_update(name, old_sha1);
103 + if (!lock) {
104 + cmd->error_string = "failed to lock";
105 + return error("failed to lock %s", name);
106 }
107 + write_ref_sha1(lock, new_sha1, "push");
108 +
109 + fprintf(stderr, "%s: %s -> %s\n", name, old_hex, new_hex);
110 + return 0;
111 }
112
113 static char update_post_hook[] = "hooks/post-update";
@@ -318,9 +258,11 @@ int main(int argc, char **argv)
258 if (!dir)
259 usage(receive_pack_usage);
260
321 - if(!enter_repo(dir, 0))
261 + if (!enter_repo(dir, 0))
262 die("'%s': unable to chdir or not a git archive", dir);
263
264 + git_config(git_default_config);
265 +
266 write_head_info();
267
268 /* EOF */