Skip to content

Commit c8dd277

Browse files
spearceJunio C Hamano
authored andcommitted
Refactor run_update_hook to be more useful
This is a simple refactoring of run_update_hook to allow the function to be passed the name of the hook it runs and also to build the argument list from a list of struct commands, rather than just one struct command. The refactoring is to support new pre-receive and post-receive hooks that will be given the entire list of struct commands, rather than just one struct command. These new hooks will follow in another patch. Signed-off-by: Shawn O. Pearce <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3e6e152 commit c8dd277

File tree

1 file changed

+44
-19
lines changed

1 file changed

+44
-19
lines changed

receive-pack.c

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,45 @@ struct command {
6767

6868
static struct command *commands;
6969

70-
static char update_hook[] = "hooks/update";
70+
static const char update_hook[] = "hooks/update";
7171

72-
static int run_update_hook(const char *refname,
73-
char *old_hex, char *new_hex)
72+
static int run_hook(const char *hook_name,
73+
struct command *first_cmd,
74+
int single)
7475
{
75-
int code;
76+
struct command *cmd;
77+
int argc, code;
78+
const char **argv;
79+
80+
for (argc = 0, cmd = first_cmd; cmd; cmd = cmd->next) {
81+
if (!cmd->error_string)
82+
argc += 3;
83+
if (single)
84+
break;
85+
}
7686

77-
if (access(update_hook, X_OK) < 0)
87+
if (!argc || access(hook_name, X_OK) < 0)
7888
return 0;
79-
code = run_command_opt(RUN_COMMAND_NO_STDIN
80-
| RUN_COMMAND_STDOUT_TO_STDERR,
81-
update_hook, refname, old_hex, new_hex, NULL);
89+
90+
argv = xmalloc(sizeof(*argv) * (2 + argc));
91+
argv[0] = hook_name;
92+
for (argc = 1, cmd = first_cmd; cmd; cmd = cmd->next) {
93+
if (!cmd->error_string) {
94+
argv[argc++] = xstrdup(cmd->ref_name);
95+
argv[argc++] = xstrdup(sha1_to_hex(cmd->old_sha1));
96+
argv[argc++] = xstrdup(sha1_to_hex(cmd->new_sha1));
97+
}
98+
if (single)
99+
break;
100+
}
101+
argv[argc] = NULL;
102+
103+
code = run_command_v_opt(argv,
104+
RUN_COMMAND_NO_STDIN | RUN_COMMAND_STDOUT_TO_STDERR);
105+
while (--argc > 0)
106+
free((char*)argv[argc]);
107+
free(argv);
108+
82109
switch (code) {
83110
case 0:
84111
return 0;
@@ -91,11 +118,11 @@ static int run_update_hook(const char *refname,
91118
case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
92119
return error("waitpid is confused");
93120
case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
94-
return error("%s died of signal", update_hook);
121+
return error("%s died of signal", hook_name);
95122
case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
96-
return error("%s died strangely", update_hook);
123+
return error("%s died strangely", hook_name);
97124
default:
98-
error("%s exited with error code %d", update_hook, -code);
125+
error("%s exited with error code %d", hook_name, -code);
99126
return -code;
100127
}
101128
}
@@ -105,7 +132,6 @@ static int update(struct command *cmd)
105132
const char *name = cmd->ref_name;
106133
unsigned char *old_sha1 = cmd->old_sha1;
107134
unsigned char *new_sha1 = cmd->new_sha1;
108-
char new_hex[41], old_hex[41];
109135
struct ref_lock *lock;
110136

111137
cmd->error_string = NULL;
@@ -115,13 +141,10 @@ static int update(struct command *cmd)
115141
name);
116142
}
117143

118-
strcpy(new_hex, sha1_to_hex(new_sha1));
119-
strcpy(old_hex, sha1_to_hex(old_sha1));
120-
121144
if (!is_null_sha1(new_sha1) && !has_sha1_file(new_sha1)) {
122145
cmd->error_string = "bad pack";
123146
return error("unpack should have generated %s, "
124-
"but I can't find it!", new_hex);
147+
"but I can't find it!", sha1_to_hex(new_sha1));
125148
}
126149
if (deny_non_fast_forwards && !is_null_sha1(new_sha1) &&
127150
!is_null_sha1(old_sha1) &&
@@ -140,7 +163,7 @@ static int update(struct command *cmd)
140163
return error("denying non-fast forward;"
141164
" you should pull first");
142165
}
143-
if (run_update_hook(name, old_hex, new_hex)) {
166+
if (run_hook(update_hook, cmd, 1)) {
144167
cmd->error_string = "hook declined";
145168
return error("hook declined to update %s", name);
146169
}
@@ -150,7 +173,8 @@ static int update(struct command *cmd)
150173
cmd->error_string = "failed to delete";
151174
return error("failed to delete %s", name);
152175
}
153-
fprintf(stderr, "%s: %s -> deleted\n", name, old_hex);
176+
fprintf(stderr, "%s: %s -> deleted\n", name,
177+
sha1_to_hex(old_sha1));
154178
}
155179
else {
156180
lock = lock_any_ref_for_update(name, old_sha1);
@@ -162,7 +186,8 @@ static int update(struct command *cmd)
162186
cmd->error_string = "failed to write";
163187
return -1; /* error() already called */
164188
}
165-
fprintf(stderr, "%s: %s -> %s\n", name, old_hex, new_hex);
189+
fprintf(stderr, "%s: %s -> %s\n", name,
190+
sha1_to_hex(old_sha1), sha1_to_hex(new_sha1));
166191
}
167192
return 0;
168193
}

0 commit comments

Comments
 (0)