Skip to content

Commit d2214be

Browse files
committed
Add support for SHA-256 repositories
Use variable extensions.objectformat, if available, to determine the length of the hash id in the current repository.
1 parent c60d029 commit d2214be

27 files changed

+310
-41
lines changed

doc/manual.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@ following variables.
203203
|%(repo:is-inside-work-tree)
204204
|Whether Tig is running inside a work tree,
205205
either `true` or `false`.
206+
|%(repo:object-format) |The hash algorithm used for the repository, e.g. `sha1`
207+
or `sha256`.
206208
|=============================================================================
207209

208210
Example user-defined commands:

include/tig/main.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include "tig/util.h"
2020

2121
struct commit {
22-
char id[SIZEOF_REV]; /* SHA1 ID. */
22+
char id[SIZEOF_REV]; /* Hash ID. */
2323
const struct ident *author; /* Author of the commit. */
2424
struct time time; /* Date from the author ident. */
2525
struct graph_canvas graph; /* Ancestry chain graphics. */

include/tig/parse.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,17 @@ bool parse_chunk_header(struct chunk_header *header, const char *line);
3131
bool parse_chunk_lineno(unsigned long *lineno, const char *chunk, int marker);
3232

3333
struct blame_commit {
34-
char id[SIZEOF_REV]; /* SHA1 ID. */
34+
char id[SIZEOF_REV]; /* Hash ID. */
3535
char title[128]; /* First line of the commit message. */
3636
const struct ident *author; /* Author of the commit. */
3737
struct time time; /* Date from the author ident. */
3838
const char *filename; /* Name of file. */
39-
char parent_id[SIZEOF_REV]; /* Parent/previous SHA1 ID. */
39+
char parent_id[SIZEOF_REV]; /* Parent/previous hash ID. */
4040
const char *parent_filename; /* Parent/previous name of file. */
4141
};
4242

4343
struct blame_header {
44-
char id[SIZEOF_REV]; /* SHA1 ID. */
44+
char id[SIZEOF_REV]; /* Hash ID. */
4545
size_t orig_lineno;
4646
size_t lineno;
4747
size_t group;

include/tig/refdb.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,20 @@
1717
#include "tig/tig.h"
1818
#include "tig/types.h"
1919
#include "tig/util.h"
20+
#include "tig/repo.h"
2021

2122
struct argv_env;
2223

2324
struct ref {
2425
struct ref *next;
2526
enum reference_type type;
26-
char id[SIZEOF_REV]; /* Commit SHA1 ID */
27+
char id[SIZEOF_REV]; /* Commit hash ID */
2728
unsigned int valid:1; /* Is the ref still valid? */
2829
char name[1]; /* Ref name; tag or head names are shortened. */
2930
};
3031

3132
#define is_initial_commit() (!get_ref_head())
32-
#define is_head_commit(rev) (!strcmp((rev), "HEAD") || (get_ref_head() && !strncmp(rev, get_ref_head()->id, SIZEOF_REV - 1)))
33+
#define is_head_commit(rev) (!strcmp((rev), "HEAD") || (get_ref_head() && !strncmp(rev, get_ref_head()->id, REPO_INFO_SIZEOF_REV - 1)))
3334
#define ref_is_tag(ref) ((ref)->type == REFERENCE_TAG || (ref)->type == REFERENCE_LOCAL_TAG)
3435
#define ref_is_remote(ref) ((ref)->type == REFERENCE_REMOTE || (ref)->type == REFERENCE_TRACKED_REMOTE)
3536

include/tig/repo.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,21 @@
1616

1717
#include "tig/tig.h"
1818

19+
typedef enum {
20+
REPO_INFO_SHA1 = 40,
21+
REPO_INFO_SHA256 = 64
22+
} repo_object_format;
23+
24+
/* Holds a hash ID and an ending NUL. */
25+
#define REPO_INFO_SIZEOF_REV (repo.object_format + 1)
26+
1927
typedef char repo_ref[SIZEOF_REF];
2028
typedef char repo_rev[SIZEOF_REV];
2129
typedef char repo_str[SIZEOF_STR];
2230

31+
/* Leave object_format in first position. */
2332
#define REPO_INFO(_) \
33+
_(repo_object_format, object_format) \
2434
_(repo_ref, head) \
2535
_(repo_rev, head_id) \
2636
_(repo_ref, remote) \

include/tig/string.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ void string_ncopy_do(char *dst, size_t dstlen, const char *src, size_t srclen);
8585
void string_copy_rev(char *dst, const char *src);
8686
void string_copy_rev_from_commit_line(char *dst, const char *src);
8787

88-
#define string_rev_is_null(rev) !strncmp(rev, NULL_ID, STRING_SIZE(NULL_ID))
88+
#define string_rev_is_null(rev) !strncmp(rev, NULL_ID, REPO_INFO_SIZEOF_REV - 1)
8989

9090
#define string_add(dst, from, src) \
9191
string_ncopy_do(dst + (from), sizeof(dst) - (from), src, sizeof(src))

include/tig/tig.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@
148148

149149
#define SIZEOF_STR 1024 /* Default string size. */
150150
#define SIZEOF_MED_STR 8192 /* Medium string size. */
151-
#define SIZEOF_REF 256 /* Size of symbolic or SHA1 ID. */
152-
#define SIZEOF_REV 41 /* Holds a SHA-1 and an ending NUL. */
151+
#define SIZEOF_REF 256 /* Size of symbolic or hash ID. */
152+
#define SIZEOF_REV 65 /* Holds a SHA-1 or SHA-256 and an ending NUL. */
153153

154154
/* This color name can be used to refer to the default term colors. */
155155
#define COLOR_DEFAULT (-1)
@@ -164,7 +164,7 @@
164164
#define MIN_VIEW_WIDTH 4
165165
#define VSPLIT_SCALE 0.5
166166

167-
#define NULL_ID "0000000000000000000000000000000000000000"
167+
#define NULL_ID "0000000000000000000000000000000000000000000000000000000000000000"
168168

169169
#define S_ISGITLINK(mode) (((mode) & S_IFMT) == 0160000)
170170

src/argv.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,14 @@ bool_formatter(struct format_context *format, struct format_var *var)
415415
return string_format_from(format->buf, &format->bufpos, "%s", value ? "true" : "false");
416416
}
417417

418+
static bool
419+
repo_object_format_formatter(struct format_context *format, struct format_var *var)
420+
{
421+
repo_object_format value = *(repo_object_format *)var->value_ref;
422+
423+
return string_format_from(format->buf, &format->bufpos, "%s", value == REPO_INFO_SHA256 ? "sha256" : "sha1");
424+
}
425+
418426
static bool
419427
repo_str_formatter(struct format_context *format, struct format_var *var)
420428
{

src/blame.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
*/
2929

3030
struct blame_history_state {
31-
char id[SIZEOF_REV]; /* SHA1 ID. */
31+
char id[SIZEOF_REV]; /* Hash ID. */
3232
const char *filename; /* Name of file. */
3333
};
3434

@@ -187,15 +187,15 @@ get_blame_commit(struct view *view, const char *id)
187187
if (!blame->commit)
188188
continue;
189189

190-
if (!strncmp(blame->commit->id, id, SIZEOF_REV - 1))
190+
if (!strncmp(blame->commit->id, id, REPO_INFO_SIZEOF_REV - 1))
191191
return blame->commit;
192192
}
193193

194194
{
195195
struct blame_commit *commit = calloc(1, sizeof(*commit));
196196

197197
if (commit)
198-
string_ncopy(commit->id, id, SIZEOF_REV);
198+
string_ncopy(commit->id, id, REPO_INFO_SIZEOF_REV);
199199
return commit;
200200
}
201201
}

src/draw.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "tig/graph.h"
1616
#include "tig/draw.h"
1717
#include "tig/options.h"
18+
#include "tig/repo.h"
1819
#include "compat/hashtab.h"
1920

2021
static const enum line_type palette_colors[] = {
@@ -267,7 +268,7 @@ draw_id(struct view *view, struct view_column *column, const char *id)
267268
return false;
268269

269270
if (column->opt.id.color && id) {
270-
hashval_t color = iterative_hash(id, SIZEOF_REV - 1, 0);
271+
hashval_t color = iterative_hash(id, REPO_INFO_SIZEOF_REV - 1, 0);
271272

272273
type = palette_colors[color % ARRAY_SIZE(palette_colors)];
273274
}

0 commit comments

Comments
 (0)