@matheusbsbs / matheus-bagliano / commits / acd3b9eca8
Fork 0

Enhance hold_lock_file_for_{update,append}() API

This changes the "die_on_error" boolean parameter to a mere "flags", and changes the existing callers of hold_lock_file_for_update/append() functions to pass LOCK_DIE_ON_ERROR. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Oct 17, 2008 at 15:44 UTC acd3b9eca82e38950f94e4708b528b7dae09a7c8
11 files changed +39 -23
builtin-commit.c
+2 -1
@@ -320,7 +320,8 @@ static char *prepare_index(int argc, const char **argv, const char *prefix)
320 die("unable to write new_index file");
321
322 fd = hold_lock_file_for_update(&false_lock,
323 - git_path("next-index-%d", getpid()), 1);
323 + git_path("next-index-%d", getpid()),
324 + LOCK_DIE_ON_ERROR);
325
326 create_base_index();
327 add_remove_files(&partial);
builtin-fetch-pack.c
+2 -1
@@ -813,7 +813,8 @@ struct ref *fetch_pack(struct fetch_pack_args *my_args,
813 )
814 die("shallow file was changed during fetch");
815
816 - fd = hold_lock_file_for_update(&lock, shallow, 1);
816 + fd = hold_lock_file_for_update(&lock, shallow,
817 + LOCK_DIE_ON_ERROR);
818 if (!write_shallow_commits(fd, 0)) {
819 unlink(shallow);
820 rollback_lock_file(&lock);
builtin-revert.c
+2 -1
@@ -338,7 +338,8 @@ static int revert_or_cherry_pick(int argc, const char **argv)
338 * reverse of it if we are revert.
339 */
340
341 - msg_fd = hold_lock_file_for_update(&msg_file, defmsg, 1);
341 + msg_fd = hold_lock_file_for_update(&msg_file, defmsg,
342 + LOCK_DIE_ON_ERROR);
343
344 encoding = get_encoding(message);
345 if (!encoding)
bundle.c
+2 -1
@@ -186,7 +186,8 @@ int create_bundle(struct bundle_header *header, const char *path,
186 if (bundle_to_stdout)
187 bundle_fd = 1;
188 else
189 - bundle_fd = hold_lock_file_for_update(&lock, path, 1);
189 + bundle_fd = hold_lock_file_for_update(&lock, path,
190 + LOCK_DIE_ON_ERROR);
191
192 /* write signature */
193 write_or_die(bundle_fd, bundle_signature, strlen(bundle_signature));
cache.h
+2
@@ -411,6 +411,8 @@ struct lock_file {
411 char on_list;
412 char filename[PATH_MAX];
413 };
414 +#define LOCK_DIE_ON_ERROR 1
415 +#define LOCK_NODEREF 2
416 extern int hold_lock_file_for_update(struct lock_file *, const char *path, int);
417 extern int hold_lock_file_for_append(struct lock_file *, const char *path, int);
418 extern int commit_lock_file(struct lock_file *);
lockfile.c
+17 -12
@@ -121,15 +121,17 @@ static char *resolve_symlink(char *p, size_t s)
121 }
122
123
124 -static int lock_file(struct lock_file *lk, const char *path)
124 +static int lock_file(struct lock_file *lk, const char *path, int flags)
125 {
126 - if (strlen(path) >= sizeof(lk->filename)) return -1;
126 + if (strlen(path) >= sizeof(lk->filename))
127 + return -1;
128 strcpy(lk->filename, path);
129 /*
130 * subtract 5 from size to make sure there's room for adding
131 * ".lock" for the lock file name
132 */
132 - resolve_symlink(lk->filename, sizeof(lk->filename)-5);
133 + if (!(flags & LOCK_NODEREF))
134 + resolve_symlink(lk->filename, sizeof(lk->filename)-5);
135 strcat(lk->filename, ".lock");
136 lk->fd = open(lk->filename, O_RDWR | O_CREAT | O_EXCL, 0666);
137 if (0 <= lk->fd) {
@@ -155,21 +157,21 @@ static int lock_file(struct lock_file *lk, const char *path)
157 return lk->fd;
158 }
159
158 -int hold_lock_file_for_update(struct lock_file *lk, const char *path, int die_on_error)
160 +int hold_lock_file_for_update(struct lock_file *lk, const char *path, int flags)
161 {
160 - int fd = lock_file(lk, path);
161 - if (fd < 0 && die_on_error)
162 + int fd = lock_file(lk, path, flags);
163 + if (fd < 0 && (flags & LOCK_DIE_ON_ERROR))
164 die("unable to create '%s.lock': %s", path, strerror(errno));
165 return fd;
166 }
167
166 -int hold_lock_file_for_append(struct lock_file *lk, const char *path, int die_on_error)
168 +int hold_lock_file_for_append(struct lock_file *lk, const char *path, int flags)
169 {
170 int fd, orig_fd;
171
170 - fd = lock_file(lk, path);
172 + fd = lock_file(lk, path, flags);
173 if (fd < 0) {
172 - if (die_on_error)
174 + if (flags & LOCK_DIE_ON_ERROR)
175 die("unable to create '%s.lock': %s", path, strerror(errno));
176 return fd;
177 }
@@ -177,13 +179,13 @@ int hold_lock_file_for_append(struct lock_file *lk, const char *path, int die_on
179 orig_fd = open(path, O_RDONLY);
180 if (orig_fd < 0) {
181 if (errno != ENOENT) {
180 - if (die_on_error)
182 + if (flags & LOCK_DIE_ON_ERROR)
183 die("cannot open '%s' for copying", path);
184 close(fd);
185 return error("cannot open '%s' for copying", path);
186 }
187 } else if (copy_fd(orig_fd, fd)) {
186 - if (die_on_error)
188 + if (flags & LOCK_DIE_ON_ERROR)
189 exit(128);
190 close(fd);
191 return -1;
@@ -215,7 +217,10 @@ int commit_lock_file(struct lock_file *lk)
217
218 int hold_locked_index(struct lock_file *lk, int die_on_error)
219 {
218 - return hold_lock_file_for_update(lk, get_index_file(), die_on_error);
220 + return hold_lock_file_for_update(lk, get_index_file(),
221 + die_on_error
222 + ? LOCK_DIE_ON_ERROR
223 + : 0);
224 }
225
226 void set_alternate_index_output(const char *name)
pack-refs.c
+2 -1
@@ -89,7 +89,8 @@ int pack_refs(unsigned int flags)
89 memset(&cbdata, 0, sizeof(cbdata));
90 cbdata.flags = flags;
91
92 - fd = hold_lock_file_for_update(&packed, git_path("packed-refs"), 1);
92 + fd = hold_lock_file_for_update(&packed, git_path("packed-refs"),
93 + LOCK_DIE_ON_ERROR);
94 cbdata.refs_file = fdopen(fd, "w");
95 if (!cbdata.refs_file)
96 die("unable to create ref-pack file structure (%s)",
refs.c
+6 -3
@@ -790,7 +790,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *ref, const unsigned char
790 struct ref_lock *lock;
791 struct stat st;
792 int last_errno = 0;
793 - int type;
793 + int type, lflags;
794 int mustexist = (old_sha1 && !is_null_sha1(old_sha1));
795
796 lock = xcalloc(1, sizeof(struct ref_lock));
@@ -830,8 +830,11 @@ static struct ref_lock *lock_ref_sha1_basic(const char *ref, const unsigned char
830
831 lock->lk = xcalloc(1, sizeof(struct lock_file));
832
833 - if (flags & REF_NODEREF)
833 + lflags = LOCK_DIE_ON_ERROR;
834 + if (flags & REF_NODEREF) {
835 ref = orig_ref;
836 + lflags |= LOCK_NODEREF;
837 + }
838 lock->ref_name = xstrdup(ref);
839 lock->orig_ref_name = xstrdup(orig_ref);
840 ref_file = git_path("%s", ref);
@@ -845,8 +848,8 @@ static struct ref_lock *lock_ref_sha1_basic(const char *ref, const unsigned char
848 error("unable to create directory for %s", ref_file);
849 goto error_return;
850 }
848 - lock->lock_fd = hold_lock_file_for_update(lock->lk, ref_file, 1);
851
852 + lock->lock_fd = hold_lock_file_for_update(lock->lk, ref_file, lflags);
853 return old_sha1 ? verify_lock(lock, old_sha1, mustexist) : lock;
854
855 error_return:
rerere.c
+2 -1
@@ -346,7 +346,8 @@ int setup_rerere(struct string_list *merge_rr)
346 return -1;
347
348 merge_rr_path = xstrdup(git_path("MERGE_RR"));
349 - fd = hold_lock_file_for_update(&write_lock, merge_rr_path, 1);
349 + fd = hold_lock_file_for_update(&write_lock, merge_rr_path,
350 + LOCK_DIE_ON_ERROR);
351 read_rr(merge_rr);
352 return fd;
353 }
sha1_file.c
+1 -1
@@ -385,7 +385,7 @@ static void read_info_alternates(const char * relative_base, int depth)
385 void add_to_alternates_file(const char *reference)
386 {
387 struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
388 - int fd = hold_lock_file_for_append(lock, git_path("objects/info/alternates"), 1);
388 + int fd = hold_lock_file_for_append(lock, git_path("objects/info/alternates"), LOCK_DIE_ON_ERROR);
389 char *alt = mkpath("%s/objects\n", reference);
390 write_or_die(fd, alt, strlen(alt));
391 if (commit_lock_file(lock))
t/t7201-co.sh
+1 -1
@@ -339,7 +339,7 @@ test_expect_success 'checkout w/--track from non-branch HEAD fails' '
339 test "z$(git rev-parse master^0)" = "z$(git rev-parse HEAD)"
340 '
341
342 -test_expect_failure 'detach a symbolic link HEAD' '
342 +test_expect_success 'detach a symbolic link HEAD' '
343 git checkout master &&
344 git config --bool core.prefersymlinkrefs yes &&
345 git checkout side &&