Skip to content

Commit d0740d9

Browse files
spearceJunio C Hamano
authored and
Junio C Hamano
committed
Log ref updates made by fetch.
If a ref is changed by http-fetch, local-fetch or ssh-fetch record the change and the remote URL/name in the log for the ref. This requires loading the config file to check logAllRefUpdates. Also fixed a bug in the ref lock generation; the log file name was not being produced right due to a bad prefix length. Signed-off-by: Shawn O. Pearce <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 732232a commit d0740d9

File tree

6 files changed

+27
-4
lines changed

6 files changed

+27
-4
lines changed

fetch.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "refs.h"
99

1010
const char *write_ref = NULL;
11+
const char *write_ref_log_details = NULL;
1112

1213
const unsigned char *current_ref = NULL;
1314

@@ -206,13 +207,17 @@ int pull(char *target)
206207
{
207208
struct ref_lock *lock;
208209
unsigned char sha1[20];
210+
char *msg;
211+
int ret;
209212

210213
save_commit_buffer = 0;
211214
track_object_refs = 0;
212215
if (write_ref) {
213216
lock = lock_ref_sha1(write_ref, current_ref, 1);
214-
if (!lock)
217+
if (!lock) {
218+
error("Can't lock ref %s", write_ref);
215219
return -1;
220+
}
216221
}
217222

218223
if (!get_recover) {
@@ -234,7 +239,15 @@ int pull(char *target)
234239
}
235240

236241
if (write_ref) {
237-
return write_ref_sha1(lock, sha1, "git fetch");
242+
if (write_ref_log_details) {
243+
msg = xmalloc(strlen(write_ref_log_details) + 12);
244+
sprintf(msg, "fetch from %s", write_ref_log_details);
245+
} else
246+
msg = NULL;
247+
ret = write_ref_sha1(lock, sha1, msg ? msg : "fetch (unknown)");
248+
if (msg)
249+
free(msg);
250+
return ret;
238251
}
239252
return 0;
240253
}

fetch.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ extern int fetch_ref(char *ref, unsigned char *sha1);
2525
/* If set, the ref filename to write the target value to. */
2626
extern const char *write_ref;
2727

28+
/* If set additional text will appear in the ref log. */
29+
extern const char *write_ref_log_details;
30+
2831
/* If set, the hash that the current value of write_ref must be. */
2932
extern const unsigned char *current_ref;
3033

http-fetch.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,6 +1223,7 @@ int main(int argc, char **argv)
12231223
int rc = 0;
12241224

12251225
setup_git_directory();
1226+
git_config(git_default_config);
12261227

12271228
while (arg < argc && argv[arg][0] == '-') {
12281229
if (argv[arg][1] == 't') {
@@ -1249,6 +1250,7 @@ int main(int argc, char **argv)
12491250
}
12501251
commit_id = argv[arg];
12511252
url = argv[arg + 1];
1253+
write_ref_log_details = url;
12521254

12531255
http_init();
12541256

local-fetch.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ int main(int argc, char **argv)
208208
int arg = 1;
209209

210210
setup_git_directory();
211+
git_config(git_default_config);
211212

212213
while (arg < argc && argv[arg][0] == '-') {
213214
if (argv[arg][1] == 't')
@@ -239,6 +240,7 @@ int main(int argc, char **argv)
239240
usage(local_pull_usage);
240241
commit_id = argv[arg];
241242
path = argv[arg + 1];
243+
write_ref_log_details = path;
242244

243245
if (pull(commit_id))
244246
return 1;

refs.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ static int do_for_each_ref(const char *base, int (*fn)(const char *path, const u
142142
namelen = strlen(de->d_name);
143143
if (namelen > 255)
144144
continue;
145+
if (namelen>5 && !strcmp(de->d_name+namelen-5,".lock"))
146+
continue;
145147
memcpy(path + baselen, de->d_name, namelen+1);
146148
if (stat(git_path("%s", path), &st) < 0)
147149
continue;
@@ -296,7 +298,6 @@ static struct ref_lock* lock_ref_sha1_basic(const char *path,
296298
plen = strlen(path) - plen;
297299
path = resolve_ref(path, lock->old_sha1, mustexist);
298300
if (!path) {
299-
error("Can't read ref %s", path);
300301
unlock_ref(lock);
301302
return NULL;
302303
}
@@ -326,7 +327,7 @@ struct ref_lock* lock_ref_sha1(const char *ref,
326327
if (check_ref_format(ref))
327328
return NULL;
328329
return lock_ref_sha1_basic(git_path("refs/%s", ref),
329-
strlen(ref), old_sha1, mustexist);
330+
5 + strlen(ref), old_sha1, mustexist);
330331
}
331332

332333
struct ref_lock* lock_any_ref_for_update(const char *ref,

ssh-fetch.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ int main(int argc, char **argv)
132132
if (!prog) prog = "git-ssh-upload";
133133

134134
setup_git_directory();
135+
git_config(git_default_config);
135136

136137
while (arg < argc && argv[arg][0] == '-') {
137138
if (argv[arg][1] == 't') {
@@ -158,6 +159,7 @@ int main(int argc, char **argv)
158159
}
159160
commit_id = argv[arg];
160161
url = argv[arg + 1];
162+
write_ref_log_details = url;
161163

162164
if (setup_connection(&fd_in, &fd_out, prog, url, arg, argv + 1))
163165
return 1;

0 commit comments

Comments
 (0)