Darcs-git: a few notes for Git hackers
Hi,
Here are a few notes about Git that should probably be taken into account by people working on Git itself or on Git wrappers. The notes apply to Linus' Git-0.6, which is the code I'm using in Darcs-git; some of them might no longer be applicable to Darcs.
1. Darcs-git uses the fact that Git updates are atomic when reading from a Git repository. Darcs-git almost writes to Git repositories atomically, with one exception: it performs a non-atomic read/update/write cycle on .git/HEAD.
For that reason, I'm taking a high-level lock on .git repositories whenever I write them. The lockfile is ``.git/lock''. I haven't thought about whether Darcs can be easily coerced into accessing Git repos atomically; have people writing Git wrappers found the need for a global lock?
2. The files git.h and git.c in Darcs-git are a simple ``libgit'' that contains just enough functionality for Darcs-git; they use the functionality of sha1_file.c and read_cache.c from Git-0.6.
I've found a few problems with the interfaces in these files:
- the global variables sha1_file_directory, active_cache, active_nr and active_alloc are not marked ``extern'' in cache.h. This breaks linkers that don't grok common symbols, such as the one in GHCi (silly GHCi).
- the function write_sha1_file takes the metadata and the data in a contiguous buffer, which is a problem when the data has been allocated by a higher layer. I'm currently working around the problem by memcpy-ing everything into a temp buffer, but that's obviously not a good thing. I don't care whether write_sha1_file is changed to use a writev-like interface, or to take the metadata explicitly (as in char *type, unsigned long length).
- there is no (usable) function to write a tree; there's the code in write_tree.c, but it's not generally useful. See the function ``git_write_tree_done'' in git.c for the type of interface I'm thinking of.
- there's no way to have multiple simultaneous caches, short of hacking at the values of Git's global variables by hand.
As I'd rather not maintain my own version of Git, I'd be mighty grateful if some friendly Git hacker could fix the above.
Juliusz
Dear diary, on Mon, May 09, 2005 at 08:01:25PM CEST, I got a letter where Juliusz Chroboczek <[email protected]> told me that...
Quoted 10 lines
1. Darcs-git uses the fact that Git updates are atomic when reading from a Git repository. Darcs-git almost writes to Git repositories atomically, with one exception: it performs a non-atomic read/update/write cycle on .git/HEAD.
For that reason, I'm taking a high-level lock on .git repositories whenever I write them. The lockfile is ``.git/lock''. I haven't thought about whether Darcs can be easily coerced into accessing Git repos atomically; have people writing Git wrappers found the need for a global lock?
FWIW, Cogito does not lock at all yet - this is one of the things which should be fixed soon.
Quoted 2 lines
- there's no way to have multiple simultaneous caches, short of hacking at the values of Git's global variables by hand.
See the Brad Robert's patches of Apr 21. I've decided not to apply them since it appears a lot has changed since then and it would be some pain; but they may be a worthy starting point for a more up-to-date patch.
Signature · 3 lines
Petr "Pasky" Baudis Stuff: http://pasky.or.cz/ C++: an octopus made by nailing extra legs onto a dog. -- Steve Taylor
Ahoj,
Quoted 2 lines
FWIW, Cogito does not lock at all yet - this is one of the things which should be fixed soon.
I see. Let me know if you decide to use a different name for the lock file so I can switch to using the same one as yours.
Julek
Quoted 10 lines · Juliusz Chroboczek
Ahoj,
Quoted 2 lines
FWIW, Cogito does not lock at all yet - this is one of the things which should be fixed soon.
I see. Let me know if you decide to use a different name for the lock file so I can switch to using the same one as yours.
Are you using flock(), or some other contraption that breaks if a process dies unexpectedly?
-hpa
Quoted 5 lines
Quoted 2 lines
I see. Let me know if you decide to use a different name for the lock file so I can switch to using the same one as yours.
Are you using
flock(), or some other contraption that breaks if a process dies unexpectedly?
No, I'm using a file that is created by the NFS-safe equivalent of open(O_CREAT | O_EXCL). This is what Darcs has been doing basically forever.
Darcs usually doesn't die unexpectedly -- it's a Haskell program, so bugs usually manifest themselves with an exception being thrown allowing Darcs to clean-up after itself.
The one exception is when Darcs gets killed by the OOM killer (which, as you doubtless know, doesn't give any advance warning to a process, thus making it impossible for a process to deal with it gracefully). In such cases, manual intervention is necessary anyway -- a file could have been written half-way.
Juliusz
Quoted 15 lines · Juliusz Chroboczek
Quoted 6 lines
Quoted 2 lines
I see. Let me know if you decide to use a different name for the lock file so I can switch to using the same one as yours.
Are you using
flock(), or some other contraption that breaks if a process dies unexpectedly?No, I'm using a file that is created by the NFS-safe equivalent of
open(O_CREAT | O_EXCL). This is what Darcs has been doing basically forever.Darcs usually doesn't die unexpectedly -- it's a Haskell program, so bugs usually manifest themselves with an exception being thrown allowing Darcs to clean-up after itself.
Quoted 6 lines
The one exception is when Darcs gets killed by the OOM killer (which, as you doubtless know, doesn't give any advance warning to a process, thus making it impossible for a process to deal with it gracefully). In such cases, manual intervention is necessary anyway -- a file could have been written half-way.
In the case of git, it should not be necessary even then; there might be a broken file in the repository but nothing would reference it so it shouldn't have any effect. Functionally speaking, operations on the git repository are in themselves atomic.
-hpa
Quoted 7 lines
Quoted 2 lines
In such cases, manual intervention is necessary anyway -- a file could have been written half-way.
In the case of git, it should not be necessary even then; there might be a broken file in the repository but nothing would reference it so it shouldn't have any effect. Functionally speaking, operations on the git repository are in themselves atomic.
Yes, you're right.
I still prefer using a lockfile to flock -- NFS-safety is important for us. And experience with the Darcs user base (who are probably less Unix-savvy then the Git userbase) shows that they have no problem doing
$ ps $ rm _darcs/lock $ darcs check
when Darcs complains about a stray lockfile.
Juliusz
Quoted 9 lines
Quoted 2 lines
- there's no way to have multiple simultaneous caches, short of hacking at the values of Git's global variables by hand.
See the Brad Robert's patches of Apr 21. I've decided not to apply them since it appears a lot has changed since then and it would be some pain; but they may be a worthy starting point for a more up-to-date patch.
--
Petr "Pasky" Baudis
Since there's interest, I'll pull tip of your tree and re-do them. I haven't bothered todate since no one seemed interested. Do you want them piece meal like I did last time or just one big diff?
Later,
Brad
Dear diary, on Tue, May 10, 2005 at 12:50:33AM CEST, I got a letter where Brad Roberts <[email protected]> told me that...
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines
- there's no way to have multiple simultaneous caches, short of hacking at the values of Git's global variables by hand.
See the Brad Robert's patches of Apr 21. I've decided not to apply them since it appears a lot has changed since then and it would be some pain; but they may be a worthy starting point for a more up-to-date patch.
Quoted 6 lines
Quoted 2 lines
--
Petr "Pasky" BaudisSince there's interest, I'll pull tip of your tree and re-do them. I haven't bothered todate since no one seemed interested. Do you want them piece meal like I did last time or just one big diff?
Piece meal would be excellent.
Signature · 3 lines
Petr "Pasky" Baudis Stuff: http://pasky.or.cz/ C++: an octopus made by nailing extra legs onto a dog. -- Steve Taylor
I'd rather see it done against the tip of the Linus tree.
Quoted 17 lines · Brad Roberts
Date: Mon, 9 May 2005 15:50:33 -0700 (PDT)
From: Brad Roberts <[email protected]>
To: Petr Baudis <[email protected]>
Cc: Juliusz Chroboczek <[email protected]>,
Git Mailing List <[email protected]>, [email protected]
Subject: Re: Darcs-git: a few notes for Git hackersQuoted 9 lines
Quoted 2 lines
- there's no way to have multiple simultaneous caches, short of hacking at the values of Git's global variables by hand.
See the Brad Robert's patches of Apr 21. I've decided not to apply them since it appears a lot has changed since then and it would be some pain; but they may be a worthy starting point for a more up-to-date patch.
--
Petr "Pasky" Baudis
Quoted 6 lines
Since there's interest, I'll pull tip of your tree and re-do them. I haven't bothered todate since no one seemed interested. Do you want them piece meal like I did last time or just one big diff?
Later,
Brad
I wasn't able to finish redoing these against linus tip, but I got most of it done (patches 1-14 of the original 19):
http://gameboy2.puremagic.com:8090/
rsync://gameboy2.puremagic.com/git/
The second, third, and forth to last changes need a careful review, they're direct applications of the original patches which were lightly tested during the first round and nothing other than compile tested in this round.
I suspect the remaining parts of the original patch series will go in fairly smoothly. If no one gets to them before tonight I'll finish it up after work.
Later,
Brad
The commit comments:
!-------------------------------------------------------------flip-
- remove the no-longer-true comment about the cache being in native byte order
- move the cache_header struct into read-cache.c since it's in internal detail of the cache, not a publicly accessed element
!-------------------------------------------------------------flip-
Drop the active_cache and active_nr parameters to write_cache
!-------------------------------------------------------------flip-
- Introduce set_cache_entry(ce, pos)
- Migrate update-cache.c, the only place that does a active_cache[pos] = ce to use it
- Migrate all the same style code in read-cache.c to use it also, except for read_cache itself which is setting up the initial active_cache entries
TODO: rewrite the code that deal with pointers into the active_cache array such as read-tree.c's merging code.
!-------------------------------------------------------------flip-
Introduce get_cache_entry(int pos) and use it for all trivial calls like:
ce = active_cache[pos]
TODO: rework the non-trivial active_cache manipulations
!-------------------------------------------------------------flip-
remove active_cache_changed from cache.h
!-------------------------------------------------------------flip-
Introduce get_num_cache_entries() and migrate all the trivial callers to it
!-------------------------------------------------------------flip-
Remove active_alloc from cache.h
!-------------------------------------------------------------flip-
Restructure the diff algorythm to use indexes rather than pointer math. The resulting code is probably a little less efficient but abstracts the data structure.
!-------------------------------------------------------------flip-
Restructure the write tree algorythm to use indexes rather than moving the base pointer and reducing the num entries and start using the cache abstraction apis.
!-------------------------------------------------------------flip-
Move from pointer math to indexes and use the abstractions
!-------------------------------------------------------------flip-
- convert the last caller from touching active_cache directly
- drop active_cache and active_nr from cache.h
!-------------------------------------------------------------flip-
Quoted 13 lines · Juliusz Chroboczek
Hi,
Here are a few notes about Git that should probably be taken into account by people working on Git itself or on Git wrappers. The notes apply to Linus' Git-0.6, which is the code I'm using in Darcs-git; some of them might no longer be applicable to Darcs.
1. Darcs-git uses the fact that Git updates are atomic when reading from a Git repository. Darcs-git almost writes to Git repositories atomically, with one exception: it performs a non-atomic read/update/write cycle on .git/HEAD.
Quoted 5 lines
For that reason, I'm taking a high-level lock on .git repositories whenever I write them. The lockfile is ``.git/lock''. I haven't thought about whether Darcs can be easily coerced into accessing Git repos atomically; have people writing Git wrappers found the need for a global lock?
I think most things are using the O_CREAT | O_EXCL write to a file and then rename or link/unlink to the desired location. I have some code to do this with refs/*/* as well, and I think people have generally settled on symlinking HEAD to something in refs/heads/. So it shouldn't be necessary to lock the whole repository, unless you're doing some operation like swapping two heads.
Quoted 10 lines
2. The files
git.handgit.cin Darcs-git are a simple ``libgit'' that contains just enough functionality for Darcs-git; they use the functionality ofsha1_file.candread_cache.cfrom Git-0.6.I've found a few problems with the interfaces in these files:
- the global variables
sha1_file_directory,active_cache,active_nrandactive_allocare not marked ``extern'' incache.h. This breaks linkers that don't grok common symbols, such as the one in GHCi (silly GHCi).
Should be trivial to fix.
Quoted 7 lines
- the function
write_sha1_filetakes the metadata and the data in a contiguous buffer, which is a problem when the data has been allocated by a higher layer. I'm currently working around the problem by memcpy-ing everything into a temp buffer, but that's obviously not a good thing. I don't care whetherwrite_sha1_fileis changed to use a writev-like interface, or to take the metadata explicitly (as in char *type, unsigned long length).
I've got some patches to make new functions of the write_sha1_file sort easier to write cleanly (for making git-*-pull clean); it wouldn't be too hard to have an open/write/close set.
Quoted 4 lines
- there is no (usable) function to write a tree; there's the code in
write_tree.c, but it's not generally useful. See the function ``git_write_tree_done'' ingit.cfor the type of interface I'm thinking of.
I'm working on making this cleaner. Are you wanting to write a tree from something other than a cache?
I can post my patches, but Linus is on vacation, so they couldn't go into the mainline until Friday or so anyway.
-Daniel
*This .sig left intentionally blank*
Dear diary, on Sun, May 15, 2005 at 04:04:25AM CEST, I got a letter where Brad Roberts <[email protected]> told me that...
Quoted 15 lines
Quoted 15 lines
I wasn't able to finish redoing these against linus tip, but I got most of it done (patches 1-14 of the original 19):
http://gameboy2.puremagic.com:8090/
rsync://gameboy2.puremagic.com/git/The second, third, and forth to last changes need a careful review, they're direct applications of the original patches which were lightly tested during the first round and nothing other than compile tested in this round.
I suspect the remaining parts of the original patch series will go in fairly smoothly. If no one gets to them before tonight I'll finish it up after work.
Quoted 8 lines
Quoted 2 lines
Later,
BradI've completed the re-merge, and moved to tip of git-pb.git rather than tip of git.git. Unfortunatly that merge was also somewhat intrusive and my individual diffs along the way are somewhat useless now. The entire history is available about the above locations still. Attached is the full diff vs git-pb @
902b92e00e491a60d55c4b2bce122903b8347f34.
I've merged some of the minor stuff for now.
Quoted 2 lines
2) Should the index changing areas be constructing a new index instead of shuffling bits within the current index?
When I have a big cache (the only time it matters), I do usually only relatively small changes to it, so...
Quoted 1 line
3) The vocabulary and code is inconsistent between cache and index.
Yes...
Quoted 1 line
4)
read-cache.cdoes much more than reading.
and yes. And cache.h is full of crap. Perhaps we could move read-cache.c to cache.c?
I'd imagine the plan of attack to continue by changing active_cache to be struct cache, then making it local.
Signature · 3 lines
Petr "Pasky" Baudis Stuff: http://pasky.or.cz/ C++: an octopus made by nailing extra legs onto a dog. -- Steve Taylor
Quoted 27 lines
Date: Sun, 15 May 2005 13:48:47 +0200
From: Petr Baudis <[email protected]>
To: Brad Roberts <[email protected]>
Cc: Juliusz Chroboczek <[email protected]>,
[email protected]
Subject: Re: Darcs-git: a few notes for Git hackersDear diary, on Sun, May 15, 2005 at 04:04:25AM CEST, I got a letter where Brad Roberts <[email protected]> told me that...
Quoted 18 lines
Quoted 17 lines
I wasn't able to finish redoing these against linus tip, but I got most of it done (patches 1-14 of the original 19):
http://gameboy2.puremagic.com:8090/
rsync://gameboy2.puremagic.com/git/The second, third, and forth to last changes need a careful review, they're direct applications of the original patches which were lightly tested during the first round and nothing other than compile tested in this round.
I suspect the remaining parts of the original patch series will go in fairly smoothly. If no one gets to them before tonight I'll finish it up after work.
Later,
Brad
Quoted 7 lines
Quoted 5 lines
I've completed the re-merge, and moved to tip of git-pb.git rather than tip of git.git. Unfortunatly that merge was also somewhat intrusive and my individual diffs along the way are somewhat useless now. The entire history is available about the above locations still. Attached is the full diff vs git-pb @
902b92e00e491a60d55c4b2bce122903b8347f34.I've merged some of the minor stuff for now.
Cool, though there appears to have been some objections. :)
Quoted 5 lines
Quoted 2 lines
2) Should the index changing areas be constructing a new index instead of shuffling bits within the current index?
When I have a big cache (the only time it matters), I do usually only relatively small changes to it, so...
The entire index is bit shuffled around even if nothing changed. At least today, size and amount changed doesn't matter.
Quoted 8 lines
Quoted 1 line
3) The vocabulary and code is inconsistent between cache and index.
Yes...
Quoted 1 line
4)
read-cache.cdoes much more than reading.and yes. And
cache.his full of crap. Perhaps we could moveread-cache.ctocache.c?
At least parts of it, probably yes.
Quoted 2 lines
I'd imagine the plan of attack to continue by changing
active_cacheto be struct cache, then making it local.
Which is what the rest of that patch does.
Thanks for looking at this.
Later,
Brad
Dear diary, on Sun, May 15, 2005 at 09:06:27PM CEST, I got a letter where Brad Roberts <[email protected]> told me that...
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
2) Should the index changing areas be constructing a new index instead of shuffling bits within the current index?
When I have a big cache (the only time it matters), I do usually only relatively small changes to it, so...
The entire index is bit shuffled around even if nothing changed. At least today, size and amount changed doesn't matter.
At the very least, it probably shouldn't get shuffled around if there are no changes whatsoever.
Quoted 4 lines
Quoted 2 lines
I'd imagine the plan of attack to continue by changing
active_cacheto be struct cache, then making it local.Which is what the rest of that patch does.
Well, the important word there was "then". :-) I think the patch is too big, do you think it would be possible to separate it to those two stages? (I could do it on my own when I get enough time, but who knows when that happens... ;-)
Thanks,
Signature · 3 lines
Petr "Pasky" Baudis Stuff: http://pasky.or.cz/ C++: an octopus made by nailing extra legs onto a dog. -- Steve Taylor
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
"BR" == Brad Roberts <[email protected]> writes:
I've merged some of the minor stuff for now.
BR> Cool, though there appears to have been some objections. :)
I do not have any problem with what the _patch_ does at all. I had more trouble in the process of how the patch appeared in git-pb tree, and I still do.
Please consider the revert request retracted. Request to forewarn people in the mailing list still stands.
Dear diary, on Sun, May 15, 2005 at 09:25:06PM CEST, I got a letter where Junio C Hamano <[email protected]> told me that...
Quoted 4 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
"BR" == Brad Roberts <[email protected]> writes:
I've merged some of the minor stuff for now.
Quoted 8 lines
BR> Cool, though there appears to have been some objections. :)
I do not have any problem with what the _patch_ does at all. I had more trouble in the process of how the patch appeared in git-pb tree, and I still do.
Please consider the revert request retracted. Request to forewarn people in the mailing list still stands.
Well, it isn't like this was some huge large-scale change; the diff is quite small. Do you want me to announce _any_ identifier renames in advance on the mailing list? Or where should be the threshold? These were three renames of not-so-frequently used identifiers.
Signature · 3 lines
Petr "Pasky" Baudis Stuff: http://pasky.or.cz/ C++: an octopus made by nailing extra legs onto a dog. -- Steve Taylor
Quoted 20 lines
Date: Sun, 15 May 2005 21:48:03 +0200
From: Petr Baudis <[email protected]>
To: Junio C Hamano <[email protected]>
Cc: Brad Roberts <[email protected]>,
Juliusz Chroboczek <[email protected]>, [email protected]
Subject: Re: Darcs-git: a few notes for Git hackersDear diary, on Sun, May 15, 2005 at 09:25:06PM CEST, I got a letter where Junio C Hamano <[email protected]> told me that...
Quoted 10 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
"BR" == Brad Roberts <[email protected]> writes:
I've merged some of the minor stuff for now.
BR> Cool, though there appears to have been some objections. :)
I do not have any problem with what the _patch_ does at all. I had more trouble in the process of how the patch appeared in git-pb tree, and I still do.
Quoted 7 lines
Quoted 2 lines
Please consider the revert request retracted. Request to forewarn people in the mailing list still stands.
Well, it isn't like this was some huge large-scale change; the diff is quite small. Do you want me to announce _any_ identifier renames in advance on the mailing list? Or where should be the threshold? These were three renames of not-so-frequently used identifiers.
Additionally, all of these changes were posted originally on 4/22, this was just a bring-forward of them as requested.
Later,
Brad
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
"BR" == Brad Roberts <[email protected]> writes:
BR> Additionally, all of these changes were posted originally on 4/22, this BR> was just a bring-forward of them as requested.
I admit I overreacted without first seeing the extent of damage. Sorry I made too big a fuss about this.
The 4/22 one I found very good and expected it to be in good shape when finished. I simply did not expect that to be taken piecemeal like Petr did. I overreacted, thinking he did so _without_ considering much about possible conflicts.
The request to _continue_ using good judgements when to forewarn people still stands ;-). In this case, Petr used good judgement. My apologies to both of you if my knee-jerk reaction caused you bad feelings.
Dear diary, on Sun, May 15, 2005 at 10:36:54PM CEST, I got a letter where Junio C Hamano <[email protected]> told me that...
Quoted 4 lines
The request to _continue_ using good judgements when to forewarn people still stands ;-). In this case, Petr used good judgement. My apologies to both of you if my knee-jerk reaction caused you bad feelings.
No problem here. I'm of course going to consult the mailing list on any larger-scale code changes.
Signature · 3 lines
Petr "Pasky" Baudis Stuff: http://pasky.or.cz/ C++: an octopus made by nailing extra legs onto a dog. -- Steve Taylor
Resending, I left off the mailing list on the to list last time.
Quoted 65 lines
Quoted 11 lines
I wasn't able to finish redoing these against linus tip, but I got most of it done (patches 1-14 of the original 19):
http://gameboy2.puremagic.com:8090/
rsync://gameboy2.puremagic.com/git/The second, third, and forth to last changes need a careful review, they're direct applications of the original patches which were lightly tested during the first round and nothing other than compile tested in this round.
Quoted 6 lines
I suspect the remaining parts of the original patch series will go in fairly smoothly. If no one gets to them before tonight I'll finish it up after work.
Later,
BradI've completed the re-merge, and moved to tip of git-pb.git rather than tip of git.git. Unfortunatly that merge was also somewhat intrusive and my individual diffs along the way are somewhat useless now. The entire history is available about the above locations still. Attached is the full diff vs git-pb @
902b92e00e491a60d55c4b2bce122903b8347f34.The unit tests that are being added are a wonderful thing, thanks so much for doing them Junio.
These changes feel rough to me still. Some areas to discuss / think about:
1) The hunks that change the merge routines from pointer based to index based could have been left much less intruded upon by adding a
get_cache_entry_array(cache)api.2) Should the index changing areas be constructing a new index instead of shuffling bits within the current index?
3) The vocabulary and code is inconsistent between cache and index.
4)
read-cache.cdoes much more than reading.5) Like before, cleaning up memory for the cache is a rarity, preferring to let the end of the process garbage collect everything.
cache.h | 52 ++++++------- check-files.c | 12 +-- checkout-cache.c | 26 +++--- diff-cache.c | 60 ++++++++------- diff-files.c | 31 ++++--- diff-helper.c | 6 - diff-tree.c | 50 ++++++------ diff.c | 36 +++++---- diff.h | 11 +- fsck-cache.c | 6 - local-pull.c | 2 ls-files.c | 47 ++++++----- merge-cache.c | 29 +++---- read-cache.c | 217 ++++++++++++++++++++++++++++++++++--------------------- read-tree.c | 65 +++++++++------- sha1_file.c | 4 - tree.c | 15 ++- update-cache.c | 45 +++++------ write-tree.c | 29 ++++--- 19 files changed, 416 insertions(+), 327 deletions(-)
Quoted 3 lines
Signed-off-by Brad Roberts <[email protected]>
cache.h (mode:100644) +26 -26
@@ -40,19 +40,8 @@ /* * Basic data structures for the directory cache - * - * NOTE NOTE NOTE! This is all in the native CPU byte format. It's - * not even trying to be portable. It's trying to be efficient. It's - * just a cache, after all. */ -#define CACHE_SIGNATURE 0x44495243 /* "DIRC" */ -struct cache_header { - unsigned int hdr_signature; - unsigned int hdr_version; - unsigned int hdr_entries; -}; - /* * The "cache_time" is just the low 32 bits of the * time. It doesn't matter if it overflows - we only
@@ -89,6 +78,9 @@ #define CE_STAGEMASK (0x3000) #define CE_STAGESHIFT 12 +extern int ce_match_stat(struct cache_entry *ce, struct stat *st); +extern int ce_same_name(struct cache_entry *a, struct cache_entry *b); + #define create_ce_flags(len, stage) htons((len) | ((stage) << CE_STAGESHIFT)) #define ce_namelen(ce) (CE_NAMEMASK & ntohs((ce)->ce_flags)) #define ce_size(ce) cache_entry_size(ce_namelen(ce))
@@ -104,9 +96,6 @@ #define cache_entry_size(len) ((offsetof(struct cache_entry,name) + (len) + 8) & ~7) -extern struct cache_entry **active_cache; -extern unsigned int active_nr, active_alloc, active_cache_changed; - #define GIT_DIR_ENVIRONMENT "GIT_DIR" #define DEFAULT_GIT_DIR_ENVIRONMENT ".git" #define DB_ENVIRONMENT "GIT_OBJECT_DIRECTORY"
@@ -120,17 +109,19 @@ #define alloc_nr(x) (((x)+16)*3/2) /* Initialize and use the cache information */ -extern int read_cache(void); -extern int write_cache(int newfd, struct cache_entry **cache, int entries); -extern int cache_name_pos(const char *name, int namelen); +extern struct cache *new_cache(void); +extern struct cache *read_cache(void); +extern int write_cache(struct cache *cache, int newfd); +extern void free_cache(struct cache *cache); +extern int cache_name_pos(struct cache *cache, const char *name, int namelen); #define ADD_CACHE_OK_TO_ADD 1 /* Ok to add */ #define ADD_CACHE_OK_TO_REPLACE 2 /* Ok to replace file/directory */ -extern int add_cache_entry(struct cache_entry *ce, int option); -extern int remove_entry_at(int pos); -extern int remove_file_from_cache(char *path); -extern int same_name(struct cache_entry *a, struct cache_entry *b); -extern int cache_match_stat(struct cache_entry *ce, struct stat *st); -extern int index_fd(unsigned char *sha1, int fd, struct stat *st); +extern int add_cache_entry(struct cache *cache, struct cache_entry *ce, int option); +extern int remove_file_from_cache(struct cache *cache, char *path); +extern int get_num_cache_entries(struct cache *cache); +extern struct cache_entry *get_cache_entry(struct cache *cache, int pos); +extern void set_cache_entry(struct cache *cache, struct cache_entry *ce, int pos); +extern int remove_cache_entry_at(struct cache *cache, int pos); #define MTIME_CHANGED 0x0001 #define CTIME_CHANGED 0x0002
@@ -148,11 +139,12 @@ extern void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned long *size); extern void * read_sha1_file(const unsigned char *sha1, char *type, unsigned long *size); extern int write_sha1_file(char *buf, unsigned long len, const char *type, unsigned char *return_sha1); +extern int index_fd(unsigned char *sha1, int fd, struct stat *st); extern int check_sha1_signature(unsigned char *sha1, void *buf, unsigned long size, const char *type); /* Read a tree into the cache */ -extern int read_tree(void *buffer, unsigned long size, int stage); +extern int read_tree(struct cache *cache, void *buffer, unsigned long size, int stage); extern int write_sha1_from_fd(const unsigned char *sha1, int fd);
@@ -179,7 +171,7 @@ void parse_date(char *date, char *buf, int bufsize); void datestamp(char *buf, int bufsize); -static inline void *xmalloc(int size) +static inline void *xmalloc(size_t size) { void *ret = malloc(size); if (!ret)
@@ -187,7 +179,7 @@ return ret; } -static inline void *xrealloc(void *ptr, int size) +static inline void *xrealloc(void *ptr, size_t size) { void *ret = realloc(ptr, size); if (!ret)
@@ -195,4 +187,12 @@ return ret; } +static inline void *xcalloc(size_t nmemb, size_t size) +{ + void *ret = calloc(nmemb, size); + if (!ret) + die("Out of memory, calloc failed"); + return ret; +} + #endif /* CACHE_H */
check-files.c (mode:100644) +6 -6
@@ -8,7 +8,7 @@ */ #include "cache.h" -static void check_file(const char *path) +static void check_file(struct cache *cache, const char *path) { int fd = open(path, O_RDONLY); struct cache_entry *ce;
@@ -23,15 +23,15 @@ } /* Exists but is not in the cache is not fine */ - pos = cache_name_pos(path, strlen(path)); + pos = cache_name_pos(cache, path, strlen(path)); if (pos < 0) die("preparing to update existing file '%s' not in cache", path); - ce = active_cache[pos]; + ce = get_cache_entry(cache, pos); if (lstat(path, &st) < 0) die("lstat(%s): %s", path, strerror(errno)); - changed = cache_match_stat(ce, &st); + changed = ce_match_stat(ce, &st); if (changed) die("preparing to update file '%s' not uptodate in cache", path); }
@@ -39,9 +39,9 @@ int main(int argc, char **argv) { int i; + struct cache *cache = read_cache(); - read_cache(); for (i = 1; i < argc ; i++) - check_file(argv[i]); + check_file(cache, argv[i]); return 0; }
checkout-cache.c (mode:100644) +13 -13
@@ -167,7 +167,7 @@ strcpy(path + len, ce->name); if (!lstat(path, &st)) { - unsigned changed = cache_match_stat(ce, &st); + unsigned changed = ce_match_stat(ce, &st); if (!changed) return 0; if (!force) {
@@ -188,30 +188,30 @@ return write_entry(ce, path); } -static int checkout_file(const char *name, const char *base_dir) +static int checkout_file(struct cache *cache, const char *name, const char *base_dir) { - int pos = cache_name_pos(name, strlen(name)); + int pos = cache_name_pos(cache, name, strlen(name)); if (pos < 0) { if (!quiet) { pos = -pos - 1; fprintf(stderr, "checkout-cache: %s is %s.\n", name, - (pos < active_nr && - !strcmp(active_cache[pos]->name, name)) ? + (pos < get_num_cache_entries(cache) && + !strcmp(get_cache_entry(cache, pos)->name, name)) ? "unmerged" : "not in the cache"); } return -1; } - return checkout_entry(active_cache[pos], base_dir); + return checkout_entry(get_cache_entry(cache, pos), base_dir); } -static int checkout_all(const char *base_dir) +static int checkout_all(struct cache *cache, const char *base_dir) { int i; - for (i = 0; i < active_nr ; i++) { - struct cache_entry *ce = active_cache[i]; + for (i = 0; i < get_num_cache_entries(cache) ; i++) { + struct cache_entry *ce = get_cache_entry(cache, i); if (ce_stage(ce)) continue; if (checkout_entry(ce, base_dir) < 0)
@@ -225,15 +225,15 @@ int i, force_filename = 0; const char *base_dir = ""; - if (read_cache() < 0) { + struct cache *cache = read_cache(); + if (!cache) die("invalid cache"); - } for (i = 1; i < argc; i++) { const char *arg = argv[i]; if (!force_filename) { if (!strcmp(arg, "-a")) { - checkout_all(base_dir); + checkout_all(cache, base_dir); continue; } if (!strcmp(arg, "--")) {
@@ -257,7 +257,7 @@ continue; } } - checkout_file(arg, base_dir); + checkout_file(cache, arg, base_dir); } return 0; }
diff-cache.c (mode:100644) +32 -28
163 lines not on this page · raw message
diff-files.c (mode:100644) +17 -14
@@ -28,18 +28,18 @@ return 0; } -static void show_unmerge(const char *path) +static void show_unmerge(struct cache *cache, const char *path) { if (generate_patch) - diff_unmerge(path); + diff_unmerge(cache, path); else printf("U %s%c", path, line_termination); } -static void show_file(int pfx, struct cache_entry *ce) +static void show_file(struct cache *cache, int pfx, struct cache_entry *ce) { if (generate_patch) - diff_addremove(pfx, ntohl(ce->ce_mode), ce->sha1, + diff_addremove(cache, pfx, ntohl(ce->ce_mode), ce->sha1, ce->name, NULL); else printf("%c%06o\t%s\t%s\t%s%c",
@@ -47,7 +47,7 @@ sha1_to_hex(ce->sha1), ce->name, line_termination); } -static void show_modified(int oldmode, int mode, +static void show_modified(struct cache *cache, int oldmode, int mode, const char *old_sha1, const char *sha1, char *path) {
@@ -55,7 +55,7 @@ strcpy(old_sha1_hex, sha1_to_hex(old_sha1)); if (generate_patch) - diff_change(oldmode, mode, old_sha1, sha1, path, NULL); + diff_change(cache, oldmode, mode, old_sha1, sha1, path, NULL); else printf("*%06o->%06o\tblob\t%s->%s\t%s%c", oldmode, mode, old_sha1_hex, sha1_to_hex(sha1), path,
@@ -65,7 +65,8 @@ int main(int argc, char **argv) { static const char null_sha1[20] = { 0, }; - int entries = read_cache(); + struct cache *cache = read_cache(); + int entries; int i; while (1 < argc && argv[1][0] == '-') {
@@ -87,15 +88,17 @@ /* At this point, if argc == 1, then we are doing everything. * Otherwise argv[1] .. argv[argc-1] have the explicit paths. */ - if (entries < 0) { + if (!cache) { perror("read_cache"); exit(1); } + entries = get_num_cache_entries(cache); + for (i = 0; i < entries; i++) { struct stat st; unsigned int oldmode, mode; - struct cache_entry *ce = active_cache[i]; + struct cache_entry *ce = get_cache_entry(cache, i); int changed; if (1 < argc &&
@@ -103,9 +106,9 @@ continue; if (ce_stage(ce)) { - show_unmerge(ce->name); + show_unmerge(cache, ce->name); while (i < entries && - !strcmp(ce->name, active_cache[i]->name)) + !strcmp(ce->name, get_cache_entry(cache, i)->name)) i++; i--; /* compensate for loop control increments */ continue;
@@ -118,10 +121,10 @@ } if (silent) continue; - show_file('-', ce); + show_file(cache, '-', ce); continue; } - changed = cache_match_stat(ce, &st); + changed = ce_match_stat(ce, &st); if (!changed) continue;
@@ -129,7 +132,7 @@ mode = (S_ISLNK(st.st_mode) ? S_IFLNK : S_IFREG | ce_permissions(st.st_mode)); - show_modified(oldmode, mode, ce->sha1, null_sha1, + show_modified(cache, oldmode, mode, ce->sha1, null_sha1, ce->name); } return 0;
diff-helper.c (mode:100644) +3 -3
@@ -56,7 +56,7 @@ switch (*cp++) { case 'U': if (!cnt || matches_pathspec(cp + 1, spec, cnt)) - diff_unmerge(cp + 1); + diff_unmerge(NULL, cp + 1); return 0; case '+': old.file_valid = 0;
@@ -102,9 +102,9 @@ } if (!cnt || matches_pathspec(path, spec, cnt)) { if (reverse) - run_external_diff(path, &new, &old); + run_external_diff(NULL, path, &new, &old); else - run_external_diff(path, &old, &new); + run_external_diff(NULL, path, &old, &new); } return 0; }
diff-tree.c (mode:100644) +25 -25
173 lines not on this page · raw message
diff.c (mode:100644) +20 -16
@@ -147,7 +147,7 @@ * the work tree has that object contents, return true, so that * prepare_temp_file() does not have to inflate and extract. */ -static int work_tree_matches(const char *name, const unsigned char *sha1) +static int work_tree_matches(struct cache *cache, const char *name, const unsigned char *sha1) { struct cache_entry *ce; struct stat st;
@@ -165,17 +165,17 @@ * by diff-cache --cached, which does read the cache before * calling us. */ - if (!active_cache) + if (!get_num_cache_entries(cache)) return 0; len = strlen(name); - pos = cache_name_pos(name, len); + pos = cache_name_pos(cache, name, len); if (pos < 0) return 0; - ce = active_cache[pos]; + ce = get_cache_entry(cache, pos); if ((lstat(name, &st) < 0) || !S_ISREG(st.st_mode) || - cache_match_stat(ce, &st) || + ce_match_stat(ce, &st) || memcmp(sha1, ce->sha1, 20)) return 0; return 1;
@@ -202,7 +202,8 @@ sprintf(temp->mode, "%06o", mode); } -static void prepare_temp_file(const char *name, +static void prepare_temp_file(struct cache *cache, + const char *name, struct diff_tempfile *temp, struct diff_spec *one) {
@@ -222,7 +223,7 @@ if (one->sha1_valid && (!memcmp(one->blob_sha1, null_sha1, sizeof(null_sha1)) || - work_tree_matches(name, one->blob_sha1))) + work_tree_matches(cache, name, one->blob_sha1))) use_work_tree = 1; if (!one->sha1_valid || use_work_tree) {
@@ -293,7 +294,8 @@ * infile2 infile2-sha1 infile2-mode. * */ -void run_external_diff(const char *name, +void run_external_diff(struct cache *cache, + const char *name, struct diff_spec *one, struct diff_spec *two) {
@@ -303,8 +305,8 @@ static int atexit_asked = 0; if (one && two) { - prepare_temp_file(name, &temp[0], one); - prepare_temp_file(name, &temp[1], two); + prepare_temp_file(cache, name, &temp[0], one); + prepare_temp_file(cache, name, &temp[1], two); if (! atexit_asked && (temp[0].name == temp[0].tmp_path || temp[1].name == temp[1].tmp_path)) {
@@ -357,7 +359,8 @@ remove_tempfile(); } -void diff_addremove(int addremove, unsigned mode, +void diff_addremove(struct cache *cache, + int addremove, unsigned mode, const unsigned char *sha1, const char *base, const char *path) {
@@ -379,10 +382,11 @@ strcpy(concatpath, base); strcat(concatpath, path); } - run_external_diff(path ? concatpath : base, one, two); + run_external_diff(cache, path ? concatpath : base, one, two); } -void diff_change(unsigned old_mode, unsigned new_mode, +void diff_change(struct cache *cache, + unsigned old_mode, unsigned new_mode, const unsigned char *old_sha1, const unsigned char *new_sha1, const char *base, const char *path) {
@@ -400,10 +404,10 @@ strcpy(concatpath, base); strcat(concatpath, path); } - run_external_diff(path ? concatpath : base, &spec[0], &spec[1]); + run_external_diff(cache, path ? concatpath : base, &spec[0], &spec[1]); } -void diff_unmerge(const char *path) +void diff_unmerge(struct cache *cache, const char *path) { - run_external_diff(path, NULL, NULL); + run_external_diff(cache, path, NULL, NULL); }
diff.h (mode:100644) +7 -4
@@ -4,18 +4,20 @@ #ifndef DIFF_H #define DIFF_H -extern void diff_addremove(int addremove, +extern void diff_addremove(struct cache *cache, + int addremove, unsigned mode, const unsigned char *sha1, const char *base, const char *path); -extern void diff_change(unsigned mode1, unsigned mode2, +extern void diff_change(struct cache *cache, + unsigned mode1, unsigned mode2, const unsigned char *sha1, const unsigned char *sha2, const char *base, const char *path); -extern void diff_unmerge(const char *path); +extern void diff_unmerge(struct cache *cache, const char *path); /* These are for diff-helper */
@@ -31,7 +33,8 @@ unsigned file_valid : 1; /* if false the file does not even exist */ }; -extern void run_external_diff(const char *name, +extern void run_external_diff(struct cache *cache, + const char *name, struct diff_spec *, struct diff_spec *); #endif /* DIFF_H */
fsck-cache.c (mode:100644) +3 -3
@@ -356,9 +356,9 @@ if (keep_cache_objects) { int i; - read_cache(); - for (i = 0; i < active_nr; i++) { - struct blob *blob = lookup_blob(active_cache[i]->sha1); + struct cache *cache = read_cache(); + for (i = 0; i < get_num_cache_entries(cache); i++) { + struct blob *blob = lookup_blob(get_cache_entry(cache, i)->sha1); struct object *obj; if (!blob) continue;
local-pull.c (mode:100644) +1 -1
@@ -61,7 +61,7 @@ } map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, ifd, 0); close(ifd); - if (-1 == (int)(long)map) { + if (MAP_FAILED == map) { fprintf(stderr, "cannot mmap %s\n", filename); return -1; }
ls-files.c (mode:100644) +24 -23
136 lines not on this page · raw message
merge-cache.c (mode:100644) +15 -14
@@ -34,11 +34,11 @@ } } -static int merge_entry(int pos, const char *path) +static int merge_entry(struct cache *cache, int pos, const char *path) { int found; - if (pos >= active_nr) + if (pos >= get_num_cache_entries(cache)) die("merge-cache: %s not in the cache", path); arguments[0] = pgm; arguments[1] = "";
@@ -52,7 +52,7 @@ do { static char hexbuf[4][60]; static char ownbuf[4][60]; - struct cache_entry *ce = active_cache[pos]; + struct cache_entry *ce = get_cache_entry(cache, pos); int stage = ce_stage(ce); if (strcmp(ce->name, path))
@@ -62,44 +62,45 @@ sprintf(ownbuf[stage], "%o", ntohl(ce->ce_mode) & (~S_IFMT)); arguments[stage] = hexbuf[stage]; arguments[stage + 4] = ownbuf[stage]; - } while (++pos < active_nr); + } while (++pos < get_num_cache_entries(cache)); if (!found) die("merge-cache: %s not in the cache", path); run_program(); return found; } -static void merge_file(const char *path) +static void merge_file(struct cache *cache, const char *path) { - int pos = cache_name_pos(path, strlen(path)); + int pos = cache_name_pos(cache, path, strlen(path)); /* * If it already exists in the cache as stage0, it's * already merged and there is nothing to do. */ if (pos < 0) - merge_entry(-pos-1, path); + merge_entry(cache, -pos-1, path); } -static void merge_all(void) +static void merge_all(struct cache *cache) { int i; - for (i = 0; i < active_nr; i++) { - struct cache_entry *ce = active_cache[i]; + for (i = 0; i < get_num_cache_entries(cache); i++) { + struct cache_entry *ce = get_cache_entry(cache, i); if (!ce_stage(ce)) continue; - i += merge_entry(i, ce->name)-1; + i += merge_entry(cache, i, ce->name)-1; } } int main(int argc, char **argv) { int i, force_file = 0; + struct cache *cache; if (argc < 3) usage("merge-cache [-o] <merge-program> (-a | <filename>*)"); - read_cache(); + cache = read_cache(); i = 1; if (!strcmp(argv[1], "-o")) {
@@ -115,12 +116,12 @@ continue; } if (!strcmp(arg, "-a")) { - merge_all(); + merge_all(cache); continue; } die("merge-cache: unknown option %s", arg); } - merge_file(arg); + merge_file(cache, arg); } if (err) die("merge program failed");
read-cache.c (mode:100644) +138 -79
360 lines not on this page · raw message
read-tree.c (mode:100644) +39 -26
157 lines not on this page · raw message
sha1_file.c (mode:100644) +2 -2
@@ -302,7 +302,7 @@ } map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); close(fd); - if (-1 == (int)(long)map) + if (MAP_FAILED == map) return NULL; *size = st.st_size; return map;
@@ -587,7 +587,7 @@ if (size) buf = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0); close(fd); - if ((int)(long)buf == -1) + if (buf == MAP_FAILED) return -1; ret = write_sha1_file(buf, size, "blob", sha1);
tree.c (mode:100644) +8 -7
@@ -5,7 +5,8 @@ const char *tree_type = "tree"; -static int read_one_entry(unsigned char *sha1, const char *base, int baselen, const char *pathname, unsigned mode, int stage) +static int read_one_entry(struct cache *cache, unsigned char *sha1, const char *base, + int baselen, const char *pathname, unsigned mode, int stage) { int len = strlen(pathname); unsigned int size = cache_entry_size(baselen + len);
@@ -18,10 +19,10 @@ memcpy(ce->name, base, baselen); memcpy(ce->name + baselen, pathname, len+1); memcpy(ce->sha1, sha1, 20); - return add_cache_entry(ce, ADD_CACHE_OK_TO_ADD); + return add_cache_entry(cache, ce, ADD_CACHE_OK_TO_ADD); } -static int read_tree_recursive(void *buffer, unsigned long size, +static int read_tree_recursive(struct cache* cache, void *buffer, unsigned long size, const char *base, int baselen, int stage) { while (size) {
@@ -53,7 +54,7 @@ memcpy(newbase, base, baselen); memcpy(newbase + baselen, path, pathlen); newbase[baselen + pathlen] = '/'; - retval = read_tree_recursive(eltbuf, eltsize, + retval = read_tree_recursive(cache, eltbuf, eltsize, newbase, baselen + pathlen + 1, stage); free(eltbuf);
@@ -62,15 +63,15 @@ return -1; continue; } - if (read_one_entry(sha1, base, baselen, path, mode, stage) < 0) + if (read_one_entry(cache, sha1, base, baselen, path, mode, stage) < 0) return -1; } return 0; } -int read_tree(void *buffer, unsigned long size, int stage) +int read_tree(struct cache *cache, void *buffer, unsigned long size, int stage) { - return read_tree_recursive(buffer, size, "", 0, stage); + return read_tree_recursive(cache, buffer, size, "", 0, stage); } struct tree *lookup_tree(unsigned char *sha1)
update-cache.c (mode:100644) +21 -24
141 lines not on this page · raw message
write-tree.c (mode:100644) +16 -13
@@ -17,7 +17,7 @@ return ret; } -static int write_tree(struct cache_entry **cachep, int maxentries, const char *base, int baselen, unsigned char *returnsha1) +static int write_tree(struct cache *cache, int start_pos, const char *base, int baselen, unsigned char *returnsha1) { unsigned char subdir_sha1[20]; unsigned long size, offset;
@@ -30,8 +30,8 @@ offset = 0; nr = 0; - while (nr < maxentries) { - struct cache_entry *ce = cachep[nr]; + while ((start_pos + nr) < get_num_cache_entries(cache)) { + struct cache_entry *ce = get_cache_entry(cache, start_pos + nr); const char *pathname = ce->name, *filename, *dirname; int pathlen = ce_namelen(ce), entrylen; unsigned char *sha1;
@@ -41,16 +41,13 @@ if (baselen >= pathlen || memcmp(base, pathname, baselen)) break; - sha1 = ce->sha1; - mode = ntohl(ce->ce_mode); - /* Do we have _further_ subdirectories? */ filename = pathname + baselen; dirname = strchr(filename, '/'); if (dirname) { int subdir_written; - subdir_written = write_tree(cachep + nr, maxentries - nr, pathname, dirname-pathname+1, subdir_sha1); + subdir_written = write_tree(cache, start_pos + nr, pathname, dirname-pathname+1, subdir_sha1); nr += subdir_written; /* Now we need to write out the directory entry into this tree.. */
@@ -60,6 +57,9 @@ /* ..but the directory entry doesn't count towards the total count */ nr--; sha1 = subdir_sha1; + } else { + sha1 = ce->sha1; + mode = ntohl(ce->ce_mode); } if (check_valid_sha1(sha1) < 0)
@@ -85,16 +85,19 @@ int main(int argc, char **argv) { int i, funny; - int entries = read_cache(); + struct cache *cache = read_cache(); + int entries; unsigned char sha1[20]; - if (entries < 0) + if (!cache) die("write-tree: error reading cache"); + entries = get_num_cache_entries(cache); + /* Verify that the tree is merged */ funny = 0; for (i = 0; i < entries; i++) { - struct cache_entry *ce = active_cache[i]; + struct cache_entry *ce = get_cache_entry(cache, i); if (ntohs(ce->ce_flags) & ~CE_NAMEMASK) { if (10 < ++funny) { fprintf(stderr, "...\n");
@@ -116,8 +119,8 @@ * the cache is sorted. Also path can appear only once, * which means conflicting one would immediately follow. */ - const char *this_name = active_cache[i]->name; - const char *next_name = active_cache[i+1]->name; + const char *this_name = get_cache_entry(cache, i)->name; + const char *next_name = get_cache_entry(cache, i+1)->name; int this_len = strlen(this_name); if (this_len < strlen(next_name) && strncmp(this_name, next_name, this_len) == 0 &&
@@ -134,7 +137,7 @@ die("write-tree: not able to write tree"); /* Ok, write it out */ - if (write_tree(active_cache, entries, "", 0, sha1) != entries) + if (write_tree(cache, 0, "", 0, sha1) != entries) die("write-tree: internal error"); printf("%s\n", sha1_to_hex(sha1)); return 0;