1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-11-17 14:34:49 +01:00

Merge branch 'jc/lockfile' into next

* jc/lockfile:
  ref-log: style fixes.
  refs.c: convert it to use lockfile interface.
  Make index file locking code reusable to others.
  HTTP cleanup
  HTTP cleanup
  git-format-patch: add --output-directory long option again
This commit is contained in:
Junio C Hamano 2006-06-06 14:31:29 -07:00
commit 68a40e5096
18 changed files with 196 additions and 168 deletions

View file

@ -40,8 +40,7 @@ OPTIONS
------- -------
-o|--output-directory <dir>:: -o|--output-directory <dir>::
Use <dir> to store the resulting files, instead of the Use <dir> to store the resulting files, instead of the
current working directory. This option is ignored if current working directory.
--stdout is specified.
-n|--numbered:: -n|--numbered::
Name output in '[PATCH n/m]' format. Name output in '[PATCH n/m]' format.

View file

@ -210,7 +210,7 @@ DIFF_OBJS = \
LIB_OBJS = \ LIB_OBJS = \
blob.o commit.o connect.o csum-file.o cache-tree.o base85.o \ blob.o commit.o connect.o csum-file.o cache-tree.o base85.o \
date.o diff-delta.o entry.o exec_cmd.o ident.o index.o \ date.o diff-delta.o entry.o exec_cmd.o ident.o lockfile.o \
object.o pack-check.o patch-delta.o path.o pkt-line.o \ object.o pack-check.o patch-delta.o path.o pkt-line.o \
quote.o read-cache.o refs.o run-command.o dir.o \ quote.o read-cache.o refs.o run-command.o dir.o \
server-info.o setup.o sha1_file.o sha1_name.o strbuf.o \ server-info.o setup.o sha1_file.o sha1_name.o strbuf.o \
@ -552,7 +552,7 @@ http.o: http.c
$(CC) -o $*.o -c $(ALL_CFLAGS) -DGIT_USER_AGENT='"git/$(GIT_VERSION)"' $< $(CC) -o $*.o -c $(ALL_CFLAGS) -DGIT_USER_AGENT='"git/$(GIT_VERSION)"' $<
ifdef NO_EXPAT ifdef NO_EXPAT
http-fetch.o: http-fetch.c http-fetch.o: http-fetch.c http.h
$(CC) -o $*.o -c $(ALL_CFLAGS) -DNO_EXPAT $< $(CC) -o $*.o -c $(ALL_CFLAGS) -DNO_EXPAT $<
endif endif
@ -576,6 +576,7 @@ git-ssh-push$X: rsh.o
git-imap-send$X: imap-send.o $(LIB_FILE) git-imap-send$X: imap-send.o $(LIB_FILE)
http.o http-fetch.o http-push.o: http.h
git-http-fetch$X: fetch.o http.o http-fetch.o $(LIB_FILE) git-http-fetch$X: fetch.o http.o http-fetch.o $(LIB_FILE)
$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) \ $(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) \
$(LIBS) $(CURL_LIBCURL) $(EXPAT_LIBEXPAT) $(LIBS) $(CURL_LIBCURL) $(EXPAT_LIBEXPAT)

View file

@ -122,7 +122,7 @@ static int add_file_to_index(const char *path, int verbose)
return 0; return 0;
} }
static struct cache_file cache_file; static struct lock_file lock_file;
int cmd_add(int argc, const char **argv, char **envp) int cmd_add(int argc, const char **argv, char **envp)
{ {
@ -134,9 +134,9 @@ int cmd_add(int argc, const char **argv, char **envp)
git_config(git_default_config); git_config(git_default_config);
newfd = hold_index_file_for_update(&cache_file, get_index_file()); newfd = hold_lock_file_for_update(&lock_file, get_index_file());
if (newfd < 0) if (newfd < 0)
die("unable to create new cachefile"); die("unable to create new index file");
if (read_cache() < 0) if (read_cache() < 0)
die("index file corrupt"); die("index file corrupt");
@ -181,7 +181,7 @@ int cmd_add(int argc, const char **argv, char **envp)
if (active_cache_changed) { if (active_cache_changed) {
if (write_cache(newfd, active_cache, active_nr) || if (write_cache(newfd, active_cache, active_nr) ||
commit_index_file(&cache_file)) commit_lock_file(&lock_file))
die("Unable to write new index file"); die("Unable to write new index file");
} }

View file

@ -2072,7 +2072,7 @@ static void write_out_results(struct patch *list, int skipped_patch)
} }
} }
static struct cache_file cache_file; static struct lock_file lock_file;
static struct excludes { static struct excludes {
struct excludes *next; struct excludes *next;
@ -2133,8 +2133,12 @@ static int apply_patch(int fd, const char *filename)
apply = 0; apply = 0;
write_index = check_index && apply; write_index = check_index && apply;
if (write_index && newfd < 0) if (write_index && newfd < 0) {
newfd = hold_index_file_for_update(&cache_file, get_index_file()); newfd = hold_lock_file_for_update(&lock_file,
get_index_file());
if (newfd < 0)
die("unable to create new index file");
}
if (check_index) { if (check_index) {
if (read_cache() < 0) if (read_cache() < 0)
die("unable to read index file"); die("unable to read index file");
@ -2312,8 +2316,8 @@ int cmd_apply(int argc, const char **argv, char **envp)
if (write_index) { if (write_index) {
if (write_cache(newfd, active_cache, active_nr) || if (write_cache(newfd, active_cache, active_nr) ||
commit_index_file(&cache_file)) commit_lock_file(&lock_file))
die("Unable to write new cachefile"); die("Unable to write new index file");
} }
return 0; return 0;

View file

@ -103,7 +103,7 @@ static int git_format_config(const char *var, const char *value)
static FILE *realstdout = NULL; static FILE *realstdout = NULL;
static char *output_directory = NULL; static const char *output_directory = NULL;
static void reopen_stdout(struct commit *commit, int nr, int keep_subject) static void reopen_stdout(struct commit *commit, int nr, int keep_subject)
{ {
@ -206,14 +206,14 @@ int cmd_format_patch(int argc, const char **argv, char **envp)
keep_subject = 1; keep_subject = 1;
rev.total = -1; rev.total = -1;
} }
else if (!strcmp(argv[i], "-o")) { else if (!strcmp(argv[i], "--output-directory") ||
if (argc < 3) !strcmp(argv[i], "-o")) {
die ("Which directory?");
if (mkdir(argv[i + 1], 0777) < 0 && errno != EEXIST)
die("Could not create directory %s",
argv[i + 1]);
output_directory = strdup(argv[i + 1]);
i++; i++;
if (argc <= i)
die("Which directory?");
if (output_directory)
die("Two output directories?");
output_directory = argv[i];
} }
else if (!strcmp(argv[i], "--signoff") || else if (!strcmp(argv[i], "--signoff") ||
!strcmp(argv[i], "-s")) { !strcmp(argv[i], "-s")) {
@ -243,6 +243,14 @@ int cmd_format_patch(int argc, const char **argv, char **envp)
if (argc > 1) if (argc > 1)
die ("unrecognized argument: %s", argv[1]); die ("unrecognized argument: %s", argv[1]);
if (output_directory) {
if (use_stdout)
die("standard output, or directory, which one?");
if (mkdir(output_directory, 0777) < 0 && errno != EEXIST)
die("Could not create directory %s",
output_directory);
}
if (rev.pending_objects && rev.pending_objects->next == NULL) { if (rev.pending_objects && rev.pending_objects->next == NULL) {
rev.pending_objects->item->flags |= UNINTERESTING; rev.pending_objects->item->flags |= UNINTERESTING;
add_head(&rev); add_head(&rev);
@ -293,8 +301,6 @@ int cmd_format_patch(int argc, const char **argv, char **envp)
if (!use_stdout) if (!use_stdout)
fclose(stdout); fclose(stdout);
} }
if (output_directory)
free(output_directory);
free(list); free(list);
return 0; return 0;
} }

View file

@ -877,7 +877,7 @@ static void prime_cache_tree(void)
static const char read_tree_usage[] = "git-read-tree (<sha> | [[-m [--aggressive] | --reset | --prefix=<prefix>] [-u | -i]] <sha1> [<sha2> [<sha3>]])"; static const char read_tree_usage[] = "git-read-tree (<sha> | [[-m [--aggressive] | --reset | --prefix=<prefix>] [-u | -i]] <sha1> [<sha2> [<sha3>]])";
static struct cache_file cache_file; static struct lock_file lock_file;
int cmd_read_tree(int argc, const char **argv, char **envp) int cmd_read_tree(int argc, const char **argv, char **envp)
{ {
@ -888,9 +888,9 @@ int cmd_read_tree(int argc, const char **argv, char **envp)
setup_git_directory(); setup_git_directory();
git_config(git_default_config); git_config(git_default_config);
newfd = hold_index_file_for_update(&cache_file, get_index_file()); newfd = hold_lock_file_for_update(&lock_file, get_index_file());
if (newfd < 0) if (newfd < 0)
die("unable to create new cachefile"); die("unable to create new index file");
git_config(git_default_config); git_config(git_default_config);
@ -1039,7 +1039,7 @@ int cmd_read_tree(int argc, const char **argv, char **envp)
} }
if (write_cache(newfd, active_cache, active_nr) || if (write_cache(newfd, active_cache, active_nr) ||
commit_index_file(&cache_file)) commit_lock_file(&lock_file))
die("unable to write new index file"); die("unable to write new index file");
return 0; return 0;
} }

View file

@ -41,7 +41,7 @@ static int remove_file(const char *name)
return ret; return ret;
} }
static struct cache_file cache_file; static struct lock_file lock_file;
int cmd_rm(int argc, const char **argv, char **envp) int cmd_rm(int argc, const char **argv, char **envp)
{ {
@ -53,7 +53,7 @@ int cmd_rm(int argc, const char **argv, char **envp)
git_config(git_default_config); git_config(git_default_config);
newfd = hold_index_file_for_update(&cache_file, get_index_file()); newfd = hold_lock_file_for_update(&lock_file, get_index_file());
if (newfd < 0) if (newfd < 0)
die("unable to create new index file"); die("unable to create new index file");
@ -144,7 +144,7 @@ int cmd_rm(int argc, const char **argv, char **envp)
if (active_cache_changed) { if (active_cache_changed) {
if (write_cache(newfd, active_cache, active_nr) || if (write_cache(newfd, active_cache, active_nr) ||
commit_index_file(&cache_file)) commit_lock_file(&lock_file))
die("Unable to write new index file"); die("Unable to write new index file");
} }

12
cache.h
View file

@ -167,13 +167,13 @@ extern void fill_stat_cache_info(struct cache_entry *ce, struct stat *st);
#define REFRESH_IGNORE_MISSING 0x0008 /* ignore non-existent */ #define REFRESH_IGNORE_MISSING 0x0008 /* ignore non-existent */
extern int refresh_cache(unsigned int flags); extern int refresh_cache(unsigned int flags);
struct cache_file { struct lock_file {
struct cache_file *next; struct lock_file *next;
char lockfile[PATH_MAX]; char filename[PATH_MAX];
}; };
extern int hold_index_file_for_update(struct cache_file *, const char *path); extern int hold_lock_file_for_update(struct lock_file *, const char *path);
extern int commit_index_file(struct cache_file *); extern int commit_lock_file(struct lock_file *);
extern void rollback_index_file(struct cache_file *); extern void rollback_lock_file(struct lock_file *);
/* Environment bits from configuration mechanism */ /* Environment bits from configuration mechanism */
extern int trust_executable_bit; extern int trust_executable_bit;

View file

@ -168,7 +168,7 @@ static int checkout_all(void)
static const char checkout_cache_usage[] = static const char checkout_cache_usage[] =
"git-checkout-index [-u] [-q] [-a] [-f] [-n] [--stage=[123]|all] [--prefix=<string>] [--temp] [--] <file>..."; "git-checkout-index [-u] [-q] [-a] [-f] [-n] [--stage=[123]|all] [--prefix=<string>] [--temp] [--] <file>...";
static struct cache_file cache_file; static struct lock_file lock_file;
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
@ -211,9 +211,8 @@ int main(int argc, char **argv)
if (!strcmp(arg, "-u") || !strcmp(arg, "--index")) { if (!strcmp(arg, "-u") || !strcmp(arg, "--index")) {
state.refresh_cache = 1; state.refresh_cache = 1;
if (newfd < 0) if (newfd < 0)
newfd = hold_index_file_for_update newfd = hold_lock_file_for_update
(&cache_file, (&lock_file, get_index_file());
get_index_file());
if (newfd < 0) if (newfd < 0)
die("cannot open index.lock file."); die("cannot open index.lock file.");
continue; continue;
@ -262,7 +261,7 @@ int main(int argc, char **argv)
*/ */
if (state.refresh_cache) { if (state.refresh_cache) {
close(newfd); newfd = -1; close(newfd); newfd = -1;
rollback_index_file(&cache_file); rollback_lock_file(&lock_file);
} }
state.refresh_cache = 0; state.refresh_cache = 0;
} }
@ -312,7 +311,7 @@ int main(int argc, char **argv)
if (0 <= newfd && if (0 <= newfd &&
(write_cache(newfd, active_cache, active_nr) || (write_cache(newfd, active_cache, active_nr) ||
commit_index_file(&cache_file))) commit_lock_file(&lock_file)))
die("Unable to write new cachefile"); die("Unable to write new index file");
return 0; return 0;
} }

View file

@ -150,7 +150,8 @@ static int process(struct object *obj)
if (has_sha1_file(obj->sha1)) { if (has_sha1_file(obj->sha1)) {
/* We already have it, so we should scan it now. */ /* We already have it, so we should scan it now. */
obj->flags |= TO_SCAN; obj->flags |= TO_SCAN;
} else { }
else {
if (obj->flags & COMPLETE) if (obj->flags & COMPLETE)
return 0; return 0;
prefetch(obj->sha1); prefetch(obj->sha1);
@ -255,7 +256,8 @@ int pull(char *target)
if (write_ref_log_details) { if (write_ref_log_details) {
msg = xmalloc(strlen(write_ref_log_details) + 12); msg = xmalloc(strlen(write_ref_log_details) + 12);
sprintf(msg, "fetch from %s", write_ref_log_details); sprintf(msg, "fetch from %s", write_ref_log_details);
} else }
else
msg = NULL; msg = NULL;
ret = write_ref_sha1(lock, sha1, msg ? msg : "fetch (unknown)"); ret = write_ref_sha1(lock, sha1, msg ? msg : "fetch (unknown)");
if (msg) if (msg)

View file

@ -186,6 +186,7 @@ static void process_response(void *callback_data)
finish_request(request); finish_request(request);
} }
#ifdef USE_CURL_MULTI
static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb, static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
void *data) void *data)
{ {
@ -349,6 +350,41 @@ static void start_fetch_loose(struct transfer_request *request)
} }
} }
static void start_mkcol(struct transfer_request *request)
{
char *hex = sha1_to_hex(request->obj->sha1);
struct active_request_slot *slot;
char *posn;
request->url = xmalloc(strlen(remote->url) + 13);
strcpy(request->url, remote->url);
posn = request->url + strlen(remote->url);
strcpy(posn, "objects/");
posn += 8;
memcpy(posn, hex, 2);
posn += 2;
strcpy(posn, "/");
slot = get_active_slot();
slot->callback_func = process_response;
slot->callback_data = request;
curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); /* undo PUT setup */
curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MKCOL);
curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
if (start_active_slot(slot)) {
request->slot = slot;
request->state = RUN_MKCOL;
} else {
request->state = ABORTED;
free(request->url);
request->url = NULL;
}
}
#endif
static void start_fetch_packed(struct transfer_request *request) static void start_fetch_packed(struct transfer_request *request)
{ {
char *url; char *url;
@ -438,40 +474,6 @@ static void start_fetch_packed(struct transfer_request *request)
} }
} }
static void start_mkcol(struct transfer_request *request)
{
char *hex = sha1_to_hex(request->obj->sha1);
struct active_request_slot *slot;
char *posn;
request->url = xmalloc(strlen(remote->url) + 13);
strcpy(request->url, remote->url);
posn = request->url + strlen(remote->url);
strcpy(posn, "objects/");
posn += 8;
memcpy(posn, hex, 2);
posn += 2;
strcpy(posn, "/");
slot = get_active_slot();
slot->callback_func = process_response;
slot->callback_data = request;
curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1); /* undo PUT setup */
curl_easy_setopt(slot->curl, CURLOPT_URL, request->url);
curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, request->errorstr);
curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_MKCOL);
curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
if (start_active_slot(slot)) {
request->slot = slot;
request->state = RUN_MKCOL;
} else {
request->state = ABORTED;
free(request->url);
request->url = NULL;
}
}
static void start_put(struct transfer_request *request) static void start_put(struct transfer_request *request)
{ {
char *hex = sha1_to_hex(request->obj->sha1); char *hex = sha1_to_hex(request->obj->sha1);
@ -788,6 +790,7 @@ static void finish_request(struct transfer_request *request)
} }
} }
#ifdef USE_CURL_MULTI
void fill_active_slots(void) void fill_active_slots(void)
{ {
struct transfer_request *request = request_queue_head; struct transfer_request *request = request_queue_head;
@ -821,6 +824,7 @@ void fill_active_slots(void)
slot = slot->next; slot = slot->next;
} }
} }
#endif
static void get_remote_object_list(unsigned char parent); static void get_remote_object_list(unsigned char parent);
@ -851,8 +855,10 @@ static void add_fetch_request(struct object *obj)
request->next = request_queue_head; request->next = request_queue_head;
request_queue_head = request; request_queue_head = request;
#ifdef USE_CURL_MULTI
fill_active_slots(); fill_active_slots();
step_active_slots(); step_active_slots();
#endif
} }
static int add_send_request(struct object *obj, struct remote_lock *lock) static int add_send_request(struct object *obj, struct remote_lock *lock)
@ -889,8 +895,10 @@ static int add_send_request(struct object *obj, struct remote_lock *lock)
request->next = request_queue_head; request->next = request_queue_head;
request_queue_head = request; request_queue_head = request;
#ifdef USE_CURL_MULTI
fill_active_slots(); fill_active_slots();
step_active_slots(); step_active_slots();
#endif
return 1; return 1;
} }
@ -2523,7 +2531,9 @@ int main(int argc, char **argv)
if (objects_to_send) if (objects_to_send)
fprintf(stderr, " sending %d objects\n", fprintf(stderr, " sending %d objects\n",
objects_to_send); objects_to_send);
#ifdef USE_CURL_MULTI
fill_active_slots(); fill_active_slots();
#endif
finish_all_active_slots(); finish_all_active_slots();
/* Update the remote branch if all went well */ /* Update the remote branch if all went well */

5
http.c
View file

@ -287,6 +287,7 @@ void http_cleanup(void)
#endif #endif
curl_global_cleanup(); curl_global_cleanup();
curl_slist_free_all(pragma_header);
} }
struct active_request_slot *get_active_slot(void) struct active_request_slot *get_active_slot(void)
@ -438,11 +439,15 @@ void release_active_slot(struct active_request_slot *slot)
{ {
closedown_active_slot(slot); closedown_active_slot(slot);
if (slot->curl) { if (slot->curl) {
#ifdef USE_CURL_MULTI
curl_multi_remove_handle(curlm, slot->curl); curl_multi_remove_handle(curlm, slot->curl);
#endif
curl_easy_cleanup(slot->curl); curl_easy_cleanup(slot->curl);
slot->curl = NULL; slot->curl = NULL;
} }
#ifdef USE_CURL_MULTI
fill_active_slots(); fill_active_slots();
#endif
} }
static void finish_active_slot(struct active_request_slot *slot) static void finish_active_slot(struct active_request_slot *slot)

57
index.c
View file

@ -1,57 +0,0 @@
/*
* Copyright (c) 2005, Junio C Hamano
*/
#include <signal.h>
#include "cache.h"
static struct cache_file *cache_file_list;
static void remove_lock_file(void)
{
while (cache_file_list) {
if (cache_file_list->lockfile[0])
unlink(cache_file_list->lockfile);
cache_file_list = cache_file_list->next;
}
}
static void remove_lock_file_on_signal(int signo)
{
remove_lock_file();
signal(SIGINT, SIG_DFL);
raise(signo);
}
int hold_index_file_for_update(struct cache_file *cf, const char *path)
{
int fd;
sprintf(cf->lockfile, "%s.lock", path);
fd = open(cf->lockfile, O_RDWR | O_CREAT | O_EXCL, 0666);
if (fd >=0 && !cf->next) {
cf->next = cache_file_list;
cache_file_list = cf;
signal(SIGINT, remove_lock_file_on_signal);
atexit(remove_lock_file);
}
return fd;
}
int commit_index_file(struct cache_file *cf)
{
char indexfile[PATH_MAX];
int i;
strcpy(indexfile, cf->lockfile);
i = strlen(indexfile) - 5; /* .lock */
indexfile[i] = 0;
i = rename(cf->lockfile, indexfile);
cf->lockfile[0] = 0;
return i;
}
void rollback_index_file(struct cache_file *cf)
{
if (cf->lockfile[0])
unlink(cf->lockfile);
cf->lockfile[0] = 0;
}

57
lockfile.c Normal file
View file

@ -0,0 +1,57 @@
/*
* Copyright (c) 2005, Junio C Hamano
*/
#include <signal.h>
#include "cache.h"
static struct lock_file *lock_file_list;
static void remove_lock_file(void)
{
while (lock_file_list) {
if (lock_file_list->filename[0])
unlink(lock_file_list->filename);
lock_file_list = lock_file_list->next;
}
}
static void remove_lock_file_on_signal(int signo)
{
remove_lock_file();
signal(SIGINT, SIG_DFL);
raise(signo);
}
int hold_lock_file_for_update(struct lock_file *lk, const char *path)
{
int fd;
sprintf(lk->filename, "%s.lock", path);
fd = open(lk->filename, O_RDWR | O_CREAT | O_EXCL, 0666);
if (fd >=0 && !lk->next) {
lk->next = lock_file_list;
lock_file_list = lk;
signal(SIGINT, remove_lock_file_on_signal);
atexit(remove_lock_file);
}
return fd;
}
int commit_lock_file(struct lock_file *lk)
{
char result_file[PATH_MAX];
int i;
strcpy(result_file, lk->filename);
i = strlen(result_file) - 5; /* .lock */
result_file[i] = 0;
i = rename(lk->filename, result_file);
lk->filename[0] = 0;
return i;
}
void rollback_lock_file(struct lock_file *lk)
{
if (lk->filename[0])
unlink(lk->filename);
lk->filename[0] = 0;
}

40
refs.c
View file

@ -259,7 +259,7 @@ int check_ref_format(const char *ref)
} }
} }
static struct ref_lock* verify_lock(struct ref_lock *lock, static struct ref_lock *verify_lock(struct ref_lock *lock,
const unsigned char *old_sha1, int mustexist) const unsigned char *old_sha1, int mustexist)
{ {
char buf[40]; char buf[40];
@ -285,7 +285,7 @@ static struct ref_lock* verify_lock(struct ref_lock *lock,
return lock; return lock;
} }
static struct ref_lock* lock_ref_sha1_basic(const char *path, static struct ref_lock *lock_ref_sha1_basic(const char *path,
int plen, int plen,
const unsigned char *old_sha1, int mustexist) const unsigned char *old_sha1, int mustexist)
{ {
@ -301,19 +301,18 @@ static struct ref_lock* lock_ref_sha1_basic(const char *path,
unlock_ref(lock); unlock_ref(lock);
return NULL; return NULL;
} }
lock->lk = xcalloc(1, sizeof(struct lock_file));
lock->ref_file = strdup(path); lock->ref_file = strdup(path);
lock->lock_file = strdup(mkpath("%s.lock", lock->ref_file));
lock->log_file = strdup(git_path("logs/%s", lock->ref_file + plen)); lock->log_file = strdup(git_path("logs/%s", lock->ref_file + plen));
lock->force_write = lstat(lock->ref_file, &st) && errno == ENOENT; lock->force_write = lstat(lock->ref_file, &st) && errno == ENOENT;
if (safe_create_leading_directories(lock->lock_file)) if (safe_create_leading_directories(lock->ref_file))
die("unable to create directory for %s", lock->lock_file); die("unable to create directory for %s", lock->ref_file);
lock->lock_fd = open(lock->lock_file, lock->lock_fd = hold_lock_file_for_update(lock->lk, lock->ref_file);
O_WRONLY | O_CREAT | O_EXCL, 0666);
if (lock->lock_fd < 0) { if (lock->lock_fd < 0) {
error("Couldn't open lock file %s: %s", error("Couldn't open lock file %s: %s",
lock->lock_file, strerror(errno)); lock->lk->filename, strerror(errno));
unlock_ref(lock); unlock_ref(lock);
return NULL; return NULL;
} }
@ -321,7 +320,7 @@ static struct ref_lock* lock_ref_sha1_basic(const char *path,
return old_sha1 ? verify_lock(lock, old_sha1, mustexist) : lock; return old_sha1 ? verify_lock(lock, old_sha1, mustexist) : lock;
} }
struct ref_lock* lock_ref_sha1(const char *ref, struct ref_lock *lock_ref_sha1(const char *ref,
const unsigned char *old_sha1, int mustexist) const unsigned char *old_sha1, int mustexist)
{ {
if (check_ref_format(ref)) if (check_ref_format(ref))
@ -330,23 +329,23 @@ struct ref_lock* lock_ref_sha1(const char *ref,
5 + strlen(ref), old_sha1, mustexist); 5 + strlen(ref), old_sha1, mustexist);
} }
struct ref_lock* lock_any_ref_for_update(const char *ref, struct ref_lock *lock_any_ref_for_update(const char *ref,
const unsigned char *old_sha1, int mustexist) const unsigned char *old_sha1, int mustexist)
{ {
return lock_ref_sha1_basic(git_path("%s", ref), return lock_ref_sha1_basic(git_path("%s", ref),
strlen(ref), old_sha1, mustexist); strlen(ref), old_sha1, mustexist);
} }
void unlock_ref (struct ref_lock *lock) void unlock_ref(struct ref_lock *lock)
{ {
if (lock->lock_fd >= 0) { if (lock->lock_fd >= 0) {
close(lock->lock_fd); close(lock->lock_fd);
unlink(lock->lock_file); /* Do not free lock->lk -- atexit() still looks at them */
if (lock->lk)
rollback_lock_file(lock->lk);
} }
if (lock->ref_file) if (lock->ref_file)
free(lock->ref_file); free(lock->ref_file);
if (lock->lock_file)
free(lock->lock_file);
if (lock->log_file) if (lock->log_file)
free(lock->log_file); free(lock->log_file);
free(lock); free(lock);
@ -385,7 +384,8 @@ static int log_ref_write(struct ref_lock *lock,
sha1_to_hex(sha1), sha1_to_hex(sha1),
comitter, comitter,
msg); msg);
} else { }
else {
maxlen = strlen(comitter) + 2*40 + 4; maxlen = strlen(comitter) + 2*40 + 4;
logrec = xmalloc(maxlen); logrec = xmalloc(maxlen);
len = snprintf(logrec, maxlen, "%s %s %s\n", len = snprintf(logrec, maxlen, "%s %s %s\n",
@ -415,7 +415,7 @@ int write_ref_sha1(struct ref_lock *lock,
if (write(lock->lock_fd, sha1_to_hex(sha1), 40) != 40 || if (write(lock->lock_fd, sha1_to_hex(sha1), 40) != 40 ||
write(lock->lock_fd, &term, 1) != 1 write(lock->lock_fd, &term, 1) != 1
|| close(lock->lock_fd) < 0) { || close(lock->lock_fd) < 0) {
error("Couldn't write %s", lock->lock_file); error("Couldn't write %s", lock->lk->filename);
unlock_ref(lock); unlock_ref(lock);
return -1; return -1;
} }
@ -423,7 +423,7 @@ int write_ref_sha1(struct ref_lock *lock,
unlock_ref(lock); unlock_ref(lock);
return -1; return -1;
} }
if (rename(lock->lock_file, lock->ref_file) < 0) { if (commit_lock_file(lock->lk)) {
error("Couldn't set %s", lock->ref_file); error("Couldn't set %s", lock->ref_file);
unlock_ref(lock); unlock_ref(lock);
return -1; return -1;
@ -478,10 +478,12 @@ int read_ref_at(const char *ref, unsigned long at_time, unsigned char *sha1)
"warning: Log %s has gap after %s.\n", "warning: Log %s has gap after %s.\n",
logfile, show_rfc2822_date(date, tz)); logfile, show_rfc2822_date(date, tz));
} }
} else if (date == at_time) { }
else if (date == at_time) {
if (get_sha1_hex(rec + 41, sha1)) if (get_sha1_hex(rec + 41, sha1))
die("Log %s is corrupt.", logfile); die("Log %s is corrupt.", logfile);
} else { }
else {
if (get_sha1_hex(rec + 41, logged_sha1)) if (get_sha1_hex(rec + 41, logged_sha1))
die("Log %s is corrupt.", logfile); die("Log %s is corrupt.", logfile);
if (memcmp(logged_sha1, sha1, 20)) { if (memcmp(logged_sha1, sha1, 20)) {

8
refs.h
View file

@ -3,8 +3,8 @@
struct ref_lock { struct ref_lock {
char *ref_file; char *ref_file;
char *lock_file;
char *log_file; char *log_file;
struct lock_file *lk;
unsigned char old_sha1[20]; unsigned char old_sha1[20];
int lock_fd; int lock_fd;
int force_write; int force_write;
@ -24,13 +24,13 @@ extern int for_each_remote_ref(int (*fn)(const char *path, const unsigned char *
extern int get_ref_sha1(const char *ref, unsigned char *sha1); extern int get_ref_sha1(const char *ref, unsigned char *sha1);
/** Locks a "refs/" ref returning the lock on success and NULL on failure. **/ /** Locks a "refs/" ref returning the lock on success and NULL on failure. **/
extern struct ref_lock* lock_ref_sha1(const char *ref, const unsigned char *old_sha1, int mustexist); extern struct ref_lock *lock_ref_sha1(const char *ref, const unsigned char *old_sha1, int mustexist);
/** Locks any ref (for 'HEAD' type refs). */ /** Locks any ref (for 'HEAD' type refs). */
extern struct ref_lock* lock_any_ref_for_update(const char *ref, const unsigned char *old_sha1, int mustexist); extern struct ref_lock *lock_any_ref_for_update(const char *ref, const unsigned char *old_sha1, int mustexist);
/** Release any lock taken but not written. **/ /** Release any lock taken but not written. **/
extern void unlock_ref (struct ref_lock *lock); extern void unlock_ref(struct ref_lock *lock);
/** Writes sha1 into the ref specified by the lock. **/ /** Writes sha1 into the ref specified by the lock. **/
extern int write_ref_sha1(struct ref_lock *lock, const unsigned char *sha1, const char *msg); extern int write_ref_sha1(struct ref_lock *lock, const unsigned char *sha1, const char *msg);

View file

@ -186,7 +186,7 @@ static void chmod_path(int flip, const char *path)
die("git-update-index: cannot chmod %cx '%s'", flip, path); die("git-update-index: cannot chmod %cx '%s'", flip, path);
} }
static struct cache_file cache_file; static struct lock_file lock_file;
static void update_one(const char *path, const char *prefix, int prefix_length) static void update_one(const char *path, const char *prefix, int prefix_length)
{ {
@ -489,9 +489,9 @@ int main(int argc, const char **argv)
git_config(git_default_config); git_config(git_default_config);
newfd = hold_index_file_for_update(&cache_file, get_index_file()); newfd = hold_lock_file_for_update(&lock_file, get_index_file());
if (newfd < 0) if (newfd < 0)
die("unable to create new cachefile"); die("unable to create new index file");
entries = read_cache(); entries = read_cache();
if (entries < 0) if (entries < 0)
@ -645,8 +645,8 @@ int main(int argc, const char **argv)
finish: finish:
if (active_cache_changed) { if (active_cache_changed) {
if (write_cache(newfd, active_cache, active_nr) || if (write_cache(newfd, active_cache, active_nr) ||
commit_index_file(&cache_file)) commit_lock_file(&lock_file))
die("Unable to write new cachefile"); die("Unable to write new index file");
} }
return has_errors ? 1 : 0; return has_errors ? 1 : 0;

View file

@ -13,7 +13,7 @@ static char *prefix = NULL;
static const char write_tree_usage[] = static const char write_tree_usage[] =
"git-write-tree [--missing-ok] [--prefix=<prefix>/]"; "git-write-tree [--missing-ok] [--prefix=<prefix>/]";
static struct cache_file cache_file; static struct lock_file lock_file;
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
@ -21,7 +21,7 @@ int main(int argc, char **argv)
setup_git_directory(); setup_git_directory();
newfd = hold_index_file_for_update(&cache_file, get_index_file()); newfd = hold_lock_file_for_update(&lock_file, get_index_file());
entries = read_cache(); entries = read_cache();
while (1 < argc) { while (1 < argc) {
@ -52,7 +52,7 @@ int main(int argc, char **argv)
die("git-write-tree: error building trees"); die("git-write-tree: error building trees");
if (0 <= newfd) { if (0 <= newfd) {
if (!write_cache(newfd, active_cache, active_nr)) if (!write_cache(newfd, active_cache, active_nr))
commit_index_file(&cache_file); commit_lock_file(&lock_file);
} }
/* Not being able to write is fine -- we are only interested /* Not being able to write is fine -- we are only interested
* in updating the cache-tree part, and if the next caller * in updating the cache-tree part, and if the next caller