Skip to content

Commit 32d462c

Browse files
mhaggergitster
authored andcommitted
pack-refs: merge code from pack-refs.{c,h} into refs.{c,h}
pack-refs.c doesn't contain much code, and the code it does contain is closely related to reference handling. Moreover, there is some duplication between pack_refs() and repack_without_ref(). Therefore, merge pack-refs.c into refs.c and pack-refs.h into refs.h. The code duplication will be addressed in future commits. Signed-off-by: Michael Haggerty <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0c0c0bd commit 32d462c

File tree

7 files changed

+159
-170
lines changed

7 files changed

+159
-170
lines changed

Makefile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,6 @@ LIB_H += notes-cache.h
644644
LIB_H += notes-merge.h
645645
LIB_H += notes.h
646646
LIB_H += object.h
647-
LIB_H += pack-refs.h
648647
LIB_H += pack-revindex.h
649648
LIB_H += pack.h
650649
LIB_H += parse-options.h
@@ -775,7 +774,6 @@ LIB_OBJS += notes-cache.o
775774
LIB_OBJS += notes-merge.o
776775
LIB_OBJS += object.o
777776
LIB_OBJS += pack-check.o
778-
LIB_OBJS += pack-refs.o
779777
LIB_OBJS += pack-revindex.o
780778
LIB_OBJS += pack-write.o
781779
LIB_OBJS += pager.o

builtin/clone.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include "transport.h"
1919
#include "strbuf.h"
2020
#include "dir.h"
21-
#include "pack-refs.h"
2221
#include "sigchain.h"
2322
#include "branch.h"
2423
#include "remote.h"

builtin/pack-refs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "builtin.h"
22
#include "parse-options.h"
3-
#include "pack-refs.h"
3+
#include "refs.h"
44

55
static char const * const pack_refs_usage[] = {
66
N_("git pack-refs [options]"),

pack-refs.c

Lines changed: 0 additions & 148 deletions
This file was deleted.

pack-refs.h

Lines changed: 0 additions & 18 deletions
This file was deleted.

refs.c

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1984,6 +1984,150 @@ static void write_packed_entry(int fd, char *refname, unsigned char *sha1,
19841984
}
19851985
}
19861986

1987+
struct ref_to_prune {
1988+
struct ref_to_prune *next;
1989+
unsigned char sha1[20];
1990+
char name[FLEX_ARRAY];
1991+
};
1992+
1993+
struct pack_refs_cb_data {
1994+
unsigned int flags;
1995+
struct ref_to_prune *ref_to_prune;
1996+
FILE *refs_file;
1997+
};
1998+
1999+
static int do_not_prune(int flags)
2000+
{
2001+
/* If it is already packed or if it is a symref,
2002+
* do not prune it.
2003+
*/
2004+
return (flags & (REF_ISSYMREF|REF_ISPACKED));
2005+
}
2006+
2007+
static int pack_one_ref(const char *path, const unsigned char *sha1,
2008+
int flags, void *cb_data)
2009+
{
2010+
struct pack_refs_cb_data *cb = cb_data;
2011+
struct object *o;
2012+
int is_tag_ref;
2013+
2014+
/* Do not pack the symbolic refs */
2015+
if ((flags & REF_ISSYMREF))
2016+
return 0;
2017+
is_tag_ref = !prefixcmp(path, "refs/tags/");
2018+
2019+
/* ALWAYS pack refs that were already packed or are tags */
2020+
if (!(cb->flags & PACK_REFS_ALL) && !is_tag_ref && !(flags & REF_ISPACKED))
2021+
return 0;
2022+
2023+
fprintf(cb->refs_file, "%s %s\n", sha1_to_hex(sha1), path);
2024+
2025+
o = parse_object_or_die(sha1, path);
2026+
if (o->type == OBJ_TAG) {
2027+
o = deref_tag(o, path, 0);
2028+
if (o)
2029+
fprintf(cb->refs_file, "^%s\n",
2030+
sha1_to_hex(o->sha1));
2031+
}
2032+
2033+
if ((cb->flags & PACK_REFS_PRUNE) && !do_not_prune(flags)) {
2034+
int namelen = strlen(path) + 1;
2035+
struct ref_to_prune *n = xcalloc(1, sizeof(*n) + namelen);
2036+
hashcpy(n->sha1, sha1);
2037+
strcpy(n->name, path);
2038+
n->next = cb->ref_to_prune;
2039+
cb->ref_to_prune = n;
2040+
}
2041+
return 0;
2042+
}
2043+
2044+
/*
2045+
* Remove empty parents, but spare refs/ and immediate subdirs.
2046+
* Note: munges *name.
2047+
*/
2048+
static void try_remove_empty_parents(char *name)
2049+
{
2050+
char *p, *q;
2051+
int i;
2052+
p = name;
2053+
for (i = 0; i < 2; i++) { /* refs/{heads,tags,...}/ */
2054+
while (*p && *p != '/')
2055+
p++;
2056+
/* tolerate duplicate slashes; see check_refname_format() */
2057+
while (*p == '/')
2058+
p++;
2059+
}
2060+
for (q = p; *q; q++)
2061+
;
2062+
while (1) {
2063+
while (q > p && *q != '/')
2064+
q--;
2065+
while (q > p && *(q-1) == '/')
2066+
q--;
2067+
if (q == p)
2068+
break;
2069+
*q = '\0';
2070+
if (rmdir(git_path("%s", name)))
2071+
break;
2072+
}
2073+
}
2074+
2075+
/* make sure nobody touched the ref, and unlink */
2076+
static void prune_ref(struct ref_to_prune *r)
2077+
{
2078+
struct ref_lock *lock = lock_ref_sha1(r->name + 5, r->sha1);
2079+
2080+
if (lock) {
2081+
unlink_or_warn(git_path("%s", r->name));
2082+
unlock_ref(lock);
2083+
try_remove_empty_parents(r->name);
2084+
}
2085+
}
2086+
2087+
static void prune_refs(struct ref_to_prune *r)
2088+
{
2089+
while (r) {
2090+
prune_ref(r);
2091+
r = r->next;
2092+
}
2093+
}
2094+
2095+
static struct lock_file packed;
2096+
2097+
int pack_refs(unsigned int flags)
2098+
{
2099+
int fd;
2100+
struct pack_refs_cb_data cbdata;
2101+
2102+
memset(&cbdata, 0, sizeof(cbdata));
2103+
cbdata.flags = flags;
2104+
2105+
fd = hold_lock_file_for_update(&packed, git_path("packed-refs"),
2106+
LOCK_DIE_ON_ERROR);
2107+
cbdata.refs_file = fdopen(fd, "w");
2108+
if (!cbdata.refs_file)
2109+
die_errno("unable to create ref-pack file structure");
2110+
2111+
/* perhaps other traits later as well */
2112+
fprintf(cbdata.refs_file, "# pack-refs with: peeled fully-peeled \n");
2113+
2114+
for_each_ref(pack_one_ref, &cbdata);
2115+
if (ferror(cbdata.refs_file))
2116+
die("failed to write ref-pack file");
2117+
if (fflush(cbdata.refs_file) || fsync(fd) || fclose(cbdata.refs_file))
2118+
die_errno("failed to write ref-pack file");
2119+
/*
2120+
* Since the lock file was fdopen()'ed and then fclose()'ed above,
2121+
* assign -1 to the lock file descriptor so that commit_lock_file()
2122+
* won't try to close() it.
2123+
*/
2124+
packed.fd = -1;
2125+
if (commit_lock_file(&packed) < 0)
2126+
die_errno("unable to overwrite old ref-pack file");
2127+
prune_refs(cbdata.ref_to_prune);
2128+
return 0;
2129+
}
2130+
19872131
static int repack_ref_fn(struct ref_entry *entry, void *cb_data)
19882132
{
19892133
int *fd = cb_data;

refs.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,20 @@ extern void warn_dangling_symref(FILE *fp, const char *msg_fmt, const char *refn
7272
*/
7373
extern void add_packed_ref(const char *refname, const unsigned char *sha1);
7474

75+
/*
76+
* Flags for controlling behaviour of pack_refs()
77+
* PACK_REFS_PRUNE: Prune loose refs after packing
78+
* PACK_REFS_ALL: Pack _all_ refs, not just tags and already packed refs
79+
*/
80+
#define PACK_REFS_PRUNE 0x0001
81+
#define PACK_REFS_ALL 0x0002
82+
83+
/*
84+
* Write a packed-refs file for the current repository.
85+
* flags: Combination of the above PACK_REFS_* flags.
86+
*/
87+
int pack_refs(unsigned int flags);
88+
7589
extern int ref_exists(const char *);
7690

7791
/*

0 commit comments

Comments
 (0)