Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/git2/errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ typedef enum {
GIT_EMISMATCH = -33, /**< Hashsum mismatch in object */
GIT_EINDEXDIRTY = -34, /**< Unsaved changes in the index would be overwritten */
GIT_EAPPLYFAIL = -35, /**< Patch application failed */
GIT_EMISSING = -36, /**< Requested object is missing but promised */
} git_error_code;

/**
Expand Down
11 changes: 11 additions & 0 deletions src/odb.c
Original file line number Diff line number Diff line change
Expand Up @@ -1727,6 +1727,17 @@ int git_odb__error_notfound(
return GIT_ENOTFOUND;
}

int git_odb__error_missing(
const char *message, const git_oid *oid, size_t oid_len)
{
char oid_str[GIT_OID_HEXSZ + 1];
git_oid_tostr(oid_str, oid_len+1, oid);
git_error_set(GIT_ERROR_ODB, "object missing/promised - %s (%.*s)",
message, (int) oid_len, oid_str);

return GIT_EMISSING;
}

static int error_null_oid(int error, const char *message)
{
git_error_set(GIT_ERROR_ODB, "odb: %s: null OID cannot exist", message);
Expand Down
6 changes: 6 additions & 0 deletions src/odb.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ int git_odb__error_mismatch(
int git_odb__error_notfound(
const char *message, const git_oid *oid, size_t oid_len);

/*
* Generate a GIT_EMISSING error for the ODB.
*/
int git_odb__error_missing(
const char *message, const git_oid *oid, size_t oid_len);

/*
* Generate a GIT_EAMBIGUOUS error for the ODB.
*/
Expand Down
21 changes: 16 additions & 5 deletions src/odb_pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ static int pack_entry_find(struct git_pack_entry *e, struct pack_backend *backen
struct git_pack_file *last_found = backend->last_found, *p;
git_midx_entry midx_entry;
size_t i;
bool has_promisor = false;

if (backend->midx &&
git_midx_entry_find(&midx_entry, backend->midx, oid, GIT_OID_HEXSZ) == 0 &&
Expand All @@ -281,9 +282,15 @@ static int pack_entry_find(struct git_pack_entry *e, struct pack_backend *backen
return 0;
}

if (last_found &&
git_pack_entry_find(e, last_found, oid, GIT_OID_HEXSZ) == 0)
return 0;
if (last_found) {
if (git_pack_entry_find(e, last_found, oid, GIT_OID_HEXSZ) == 0)
return 0;

if (last_found->pack_promisor) {
// if an object is referred to from a promisor pack it's "promised"
has_promisor = true;
}
}

git_vector_foreach(&backend->packs, i, p) {
if (p == last_found)
Expand All @@ -295,8 +302,12 @@ static int pack_entry_find(struct git_pack_entry *e, struct pack_backend *backen
}
}

return git_odb__error_notfound(
"failed to find pack entry", oid, GIT_OID_HEXSZ);
if (has_promisor)
return git_odb__error_missing(
"pack entry is promised", oid, GIT_OID_HEXSZ);
else
return git_odb__error_notfound(
"failed to find pack entry", oid, GIT_OID_HEXSZ);
}

static int pack_entry_find_prefix(
Expand Down
23 changes: 21 additions & 2 deletions src/pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* a Linking Exception. For full terms see the included COPYING file.
*/

#include <string.h>

#include "pack.h"

#include "delta.h"
Expand Down Expand Up @@ -1171,6 +1173,8 @@ int git_packfile_alloc(struct git_pack_file **pack_out, const char *path)
struct stat st;
struct git_pack_file *p;
size_t path_len = path ? strlen(path) : 0, alloc_len;
size_t root_len;
char promisor_path[GIT_PATH_MAX];

*pack_out = NULL;

Expand All @@ -1190,15 +1194,30 @@ int git_packfile_alloc(struct git_pack_file **pack_out, const char *path)
* the index looks sane.
*/
if (git__suffixcmp(path, ".idx") == 0) {
size_t root_len = path_len - strlen(".idx");

root_len = path_len - strlen(".idx");
if (!git_disable_pack_keep_file_checks) {
memcpy(p->pack_name + root_len, ".keep", sizeof(".keep"));
if (git_path_exists(p->pack_name) == true)
p->pack_keep = 1;
}

memcpy(p->pack_name + root_len, ".pack", sizeof(".pack"));
} else {
root_len = path_len - strlen(".pack");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In line 1181 we ensure that path_len is at least strlen(".idx"). If it were strlen(".idx"), here we would set root_len to (size_t)-1.

I think that it's unlikely that there's a call path that would call us with just .idx as the path but some sort of guard seems like it should be in order.

}

/*
* Check for a .promisor file.
*/
memcpy(promisor_path, p->pack_name, root_len);
if (root_len + strlen(".promisor") > GIT_PATH_MAX) {
git_error_set(GIT_ERROR_OS, "failed to check packfile promisor");
git__free(p);
return -1;
}
memcpy(promisor_path + root_len, ".promisor", sizeof(".promisor"));
if (!p_stat(promisor_path, &st) && S_ISREG(st.st_mode)) {
p->pack_promisor = 1;
}

if (p_stat(p->pack_name, &st) < 0 || !S_ISREG(st.st_mode)) {
Expand Down
2 changes: 1 addition & 1 deletion src/pack.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ struct git_pack_file {

int index_version;
git_time_t mtime;
unsigned pack_local:1, pack_keep:1, has_cache:1;
unsigned pack_local:1, pack_keep:1, has_cache:1, pack_promisor:1;
git_oidmap *idx_cache;
git_oid **oids;

Expand Down