]> git.madduck.net Git - code/topgit-ng.git/blob - tg-datastore-pcommits.inc

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

import first prototype
[code/topgit-ng.git] / tg-datastore-pcommits.inc
1 _get_empty_treeref()
2 {
3   git mktree </dev/null
4 }
5
6 _make_pcommit()
7 {
8   local timestamp; timestamp=$(date +'%s %z')
9   git hash-object -t commit -w --stdin <<-_commit_data
10         tree $(_get_empty_treeref)
11         author TopGit <> $timestamp
12         committer "$author"
13         x-topgit-data-node yes
14
15         TopGit data node
16
17         This commit is used internally by TopGit. Please just ignore it.
18
19         TopGit data follow:
20         $(cat)
21         _commit_data
22 }
23
24 _append_parentref_to_HEAD()
25 {
26   local parent; parent="$1"
27   local ref; ref=$(git cat-file commit HEAD |
28                      sed -e "/^parent/aparent ${parent}" |
29                      git hash-object -t commit -w --stdin)
30   git update-ref HEAD $ref
31 }
32
33 _remove_parentref_from_HEAD()
34 {
35   local parent; parent="$1"
36   local ref; ref=$(git cat-file commit HEAD |
37                      grep -v "^parent ${parent}" |
38                      git hash-object -t commit -w --stdin)
39   git update-ref HEAD $ref
40 }
41
42 # returns 0 if $1 is a valid TopGit pcommit
43 _is_pcommit()
44 {
45   local pcommit; pcommit="$1"
46   git cat-file commit $pcommit | grep -q '^x-topgit-data-node yes$'
47 }
48
49 # returns the ref of the pcommit attached to $1
50 # returns 1 when none is found
51 _find_pcommit()
52 {
53   local commit; commit="$1"
54   local parentnr; parentnr=1
55
56   while :; do
57     ref=$(tg_ds_resolve_commit "${commit}^${parentnr}") || break
58     _is_pcommit "$ref" && printf "$ref\n" && return 0
59     parentnr=$(($parentnr + 1))
60   done
61   return 1
62 }
63
64 # expects data on stdin and adds to HEAD
65 tg_ds_add_data()
66 {
67   local author; author=$(git cat-file commit HEAD | sed -rne 's,^author ,,p')
68   _append_parentref_to_HEAD $(_make_pcommit "$author")
69 }
70
71 # removes data from HEAD
72 tg_ds_remove_data()
73 {
74   local pcommit; pcommit=$(_find_pcommit HEAD) || return   # no error
75   _remove_parentref_from_HEAD $pcommit
76 }
77
78 # lists data on a given commit
79 tg_ds_list_data()
80 {
81   local commit; commit="${1:-HEAD}"
82   ref=$(tg_ds_resolve_commit $commit) || die "tg_ds_list_data: invalid commit: $commit"
83   local pcommit; pcommit=$(_find_pcommit $commit) || return 1
84   git cat-file commit $pcommit | sed -e '0,/^TopGit data follow:$/d'
85 }