@@ -67,18 +67,45 @@ struct command {
67
67
68
68
static struct command * commands ;
69
69
70
- static char update_hook [] = "hooks/update" ;
70
+ static const char update_hook [] = "hooks/update" ;
71
71
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 )
74
75
{
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
+ }
76
86
77
- if (access (update_hook , X_OK ) < 0 )
87
+ if (! argc || access (hook_name , X_OK ) < 0 )
78
88
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
+
82
109
switch (code ) {
83
110
case 0 :
84
111
return 0 ;
@@ -91,11 +118,11 @@ static int run_update_hook(const char *refname,
91
118
case - ERR_RUN_COMMAND_WAITPID_WRONG_PID :
92
119
return error ("waitpid is confused" );
93
120
case - ERR_RUN_COMMAND_WAITPID_SIGNAL :
94
- return error ("%s died of signal" , update_hook );
121
+ return error ("%s died of signal" , hook_name );
95
122
case - ERR_RUN_COMMAND_WAITPID_NOEXIT :
96
- return error ("%s died strangely" , update_hook );
123
+ return error ("%s died strangely" , hook_name );
97
124
default :
98
- error ("%s exited with error code %d" , update_hook , - code );
125
+ error ("%s exited with error code %d" , hook_name , - code );
99
126
return - code ;
100
127
}
101
128
}
@@ -105,7 +132,6 @@ static int update(struct command *cmd)
105
132
const char * name = cmd -> ref_name ;
106
133
unsigned char * old_sha1 = cmd -> old_sha1 ;
107
134
unsigned char * new_sha1 = cmd -> new_sha1 ;
108
- char new_hex [41 ], old_hex [41 ];
109
135
struct ref_lock * lock ;
110
136
111
137
cmd -> error_string = NULL ;
@@ -115,13 +141,10 @@ static int update(struct command *cmd)
115
141
name );
116
142
}
117
143
118
- strcpy (new_hex , sha1_to_hex (new_sha1 ));
119
- strcpy (old_hex , sha1_to_hex (old_sha1 ));
120
-
121
144
if (!is_null_sha1 (new_sha1 ) && !has_sha1_file (new_sha1 )) {
122
145
cmd -> error_string = "bad pack" ;
123
146
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 ) );
125
148
}
126
149
if (deny_non_fast_forwards && !is_null_sha1 (new_sha1 ) &&
127
150
!is_null_sha1 (old_sha1 ) &&
@@ -140,7 +163,7 @@ static int update(struct command *cmd)
140
163
return error ("denying non-fast forward;"
141
164
" you should pull first" );
142
165
}
143
- if (run_update_hook ( name , old_hex , new_hex )) {
166
+ if (run_hook ( update_hook , cmd , 1 )) {
144
167
cmd -> error_string = "hook declined" ;
145
168
return error ("hook declined to update %s" , name );
146
169
}
@@ -150,7 +173,8 @@ static int update(struct command *cmd)
150
173
cmd -> error_string = "failed to delete" ;
151
174
return error ("failed to delete %s" , name );
152
175
}
153
- fprintf (stderr , "%s: %s -> deleted\n" , name , old_hex );
176
+ fprintf (stderr , "%s: %s -> deleted\n" , name ,
177
+ sha1_to_hex (old_sha1 ));
154
178
}
155
179
else {
156
180
lock = lock_any_ref_for_update (name , old_sha1 );
@@ -162,7 +186,8 @@ static int update(struct command *cmd)
162
186
cmd -> error_string = "failed to write" ;
163
187
return -1 ; /* error() already called */
164
188
}
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 ));
166
191
}
167
192
return 0 ;
168
193
}
0 commit comments