Skip to content
This repository was archived by the owner on Dec 31, 2025. It is now read-only.

Commit 98c397f

Browse files
authored
Merge pull request #53 from guitarrapc/feature/sourcecaching
feat: Support Source caching
2 parents 170ebbe + b0bfd0b commit 98c397f

File tree

4 files changed

+108
-37
lines changed

4 files changed

+108
-37
lines changed

.circleci/test-deploy.yml

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,18 @@ orbs:
44
orb-tools: circleci/orb-tools@11.5
55

66
filters: &filters
7+
branches:
8+
only: /.*/
79
tags:
810
only: /.*/
911

1012
jobs:
13+
normal-checkout:
14+
docker:
15+
- image: cimg/base:stable
16+
steps:
17+
- checkout
18+
- run: git tag --list
1119
# Define one or more jobs which will utilize your orb's commands and parameters to validate your changes.
1220
integration-test-checkout:
1321
docker:
@@ -19,13 +27,24 @@ jobs:
1927
- image: cimg/base:stable
2028
steps:
2129
- git-shallow-clone/checkout_advanced
30+
integration-test-checkout_advanced_alpine:
31+
docker:
32+
- image: circleci/redis:6.2.1-alpine
33+
steps:
34+
- run: apk update && apk add openssh git # openssh is required
35+
- git-shallow-clone/checkout_advanced
2236
integration-test-checkout_advanced_fetchoptions:
2337
docker:
2438
- image: cimg/base:stable
2539
steps:
2640
- git-shallow-clone/checkout_advanced:
2741
clone_options: "--depth 100 --single-branch"
2842
fetch_options: "--depth 5"
43+
integration-test-checkout_advanced_macos:
44+
macos:
45+
xcode: 15.0.0 # see: https://circleci.com/docs/using-macos/
46+
steps:
47+
- git-shallow-clone/checkout_advanced
2948
integration-test-checkout_advanced_notags:
3049
docker:
3150
- image: cimg/base:stable
@@ -48,6 +67,20 @@ jobs:
4867
exit 1
4968
fi
5069
fi
70+
integration-test-checkout_advanced_sourcecaching:
71+
docker:
72+
- image: cimg/base:stable
73+
steps:
74+
- restore_cache:
75+
keys:
76+
- &source-cache source-v1-checkoutadvanced-{{ .Branch }}-{{ .Revision }}
77+
- source-v1-checkoutadvanced-{{ .Branch }}-
78+
- source-v1-checkoutadvanced-
79+
- git-shallow-clone/checkout_advanced
80+
- save_cache:
81+
key: *source-cache
82+
paths:
83+
- ".git"
5184
integration-test-checkout_advanced_tags:
5285
docker:
5386
- image: cimg/base:stable
@@ -63,7 +96,7 @@ jobs:
6396
if [ $count -eq 0 ]; then exit 1; fi
6497
integration-test-checkout_alpine:
6598
docker:
66-
- image: circleci/redis:alpine3.13
99+
- image: circleci/redis:6.2.1-alpine
67100
steps:
68101
- run: apk update && apk add openssh git # openssh is required
69102
- git-shallow-clone/checkout
@@ -94,7 +127,7 @@ jobs:
94127
keyscan_github: true
95128
integration-test-checkout_macos:
96129
macos:
97-
xcode: 13.0.0
130+
xcode: 15.0.0 # see: https://circleci.com/docs/using-macos/
98131
steps:
99132
- git-shallow-clone/checkout
100133
integration-test-checkout_notags:
@@ -124,6 +157,20 @@ jobs:
124157
steps:
125158
- git-shallow-clone/checkout:
126159
path: src
160+
integration-test-checkout_sourcechching:
161+
docker:
162+
- image: cimg/base:stable
163+
steps:
164+
- restore_cache:
165+
keys:
166+
- &source-cache source-v1-checkout-{{ .Branch }}-{{ .Revision }}
167+
- source-v1-checkout-{{ .Branch }}-
168+
- source-v1-checkout-
169+
- git-shallow-clone/checkout
170+
- save_cache:
171+
key: *source-cache
172+
paths:
173+
- ".git"
127174
integration-test-checkout_tags:
128175
docker:
129176
- image: cimg/base:stable
@@ -156,14 +203,22 @@ workflows:
156203
# jobs and commands to ensure they behave as expected. or, run other
157204
# integration tests of your choosing
158205
# Run any integration tests defined within the `jobs` key.
206+
- normal-checkout:
207+
filters: *filters
159208
- integration-test-checkout:
160209
filters: *filters
161210
- integration-test-checkout_advanced:
162211
filters: *filters
212+
- integration-test-checkout_advanced_alpine:
213+
filters: *filters
163214
- integration-test-checkout_advanced_fetchoptions:
164215
filters: *filters
216+
# - integration-test-checkout_advanced_macos:
217+
# filters: *filters
165218
- integration-test-checkout_advanced_notags:
166219
filters: *filters
220+
- integration-test-checkout_advanced_sourcecaching:
221+
filters: *filters
167222
- integration-test-checkout_advanced_tags:
168223
filters: *filters
169224
- integration-test-checkout_alpine:
@@ -182,6 +237,8 @@ workflows:
182237
filters: *filters
183238
- integration-test-checkout_path:
184239
filters: *filters
240+
- integration-test-checkout_sourcechching:
241+
filters: *filters
185242
- integration-test-checkout_tags:
186243
filters: *filters
187244
- integration-test-api-dispatch:
@@ -197,7 +254,9 @@ workflows:
197254
- orb-tools/pack
198255
- integration-test-checkout
199256
- integration-test-checkout_advanced
257+
- integration-test-checkout_advanced_alpine
200258
- integration-test-checkout_advanced_fetchoptions
259+
#- integration-test-checkout_advanced_macos
201260
- integration-test-checkout_advanced_notags
202261
- integration-test-checkout_advanced_tags
203262
- integration-test-checkout_alpine

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
## git-shallow-clone-orb
2-
[![CircleCI](https://circleci.com/gh/guitarrapc/git-shallow-clone-orb.svg?style=svg)](https://circleci.com/gh/guitarrapc/git-shallow-clone-orb) ![Orb Version Badge](https://badges.circleci.com/orbs/guitarrapc/git-shallow-clone.svg)
1+
[![CircleCI](https://dl.circleci.com/status-badge/img/gh/guitarrapc/git-shallow-clone-orb/tree/main.svg?style=svg)](https://dl.circleci.com/status-badge/redirect/gh/guitarrapc/git-shallow-clone-orb/tree/main)
2+
![Orb Version Badge](https://badges.circleci.com/orbs/guitarrapc/git-shallow-clone.svg)
33

4+
## Git-shallow-clone-orb
45

56
## Usage
67

src/commands/checkout.yml

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -113,31 +113,36 @@ steps:
113113
fetch_tag_args="--no-tags"
114114
fi
115115
116-
# checkout
117-
git clone ${clone_tag_args} --depth << parameters.depth >> $CIRCLE_REPOSITORY_URL "<< parameters.path >>"
118-
cd "<< parameters.path >>"
116+
# Checkout. SourceCaching? or not.
117+
if [ -e "$HOME/project/<< parameters.path >>/.git" ]; then
118+
echo 'Fetching into existing repository'
119+
existing_repo='true'
120+
cd "$HOME/project/<< parameters.path >>"
121+
git remote set-url origin "$CIRCLE_REPOSITORY_URL" || true
122+
else
123+
echo 'Cloning git repository'
124+
existing_repo='false'
125+
mkdir -p "$HOME/project/<< parameters.path >>"
126+
cd "$HOME/project/<< parameters.path >>"
127+
git clone ${clone_tag_args} --depth << parameters.depth >> $CIRCLE_REPOSITORY_URL .
128+
fi
119129
120-
# Fetch remote and check the commit ID of the checked out code (sh version of bash `elif [[ "$CIRCLE_BRANCH" =~ ^pull\/* ]]`)
130+
# NOTE: Original checkout fetch only if SourceCaching, but we fetch always for depth selection.
131+
echo 'Fetching from remote repository'
121132
if [ -n "$CIRCLE_TAG" ]; then
122-
# tag
123-
git fetch ${fetch_tag_args} --depth << parameters.fetch_depth >> --force origin "+refs/tags/${CIRCLE_TAG}:refs/tags/${CIRCLE_TAG}"
124-
elif [[ $(echo $CIRCLE_BRANCH | grep -E ^pull\/[0-9]+$) ]]; then
125-
# pull request
126-
git fetch ${fetch_tag_args} --depth << parameters.fetch_depth >> --force origin "${CIRCLE_BRANCH}/head:remotes/origin/${CIRCLE_BRANCH}"
133+
git fetch ${fetch_tag_args} --depth << parameters.fetch_depth >> --force --tags origin "+refs/tags/${CIRCLE_TAG}:refs/tags/${CIRCLE_TAG}"
127134
else
128-
# others
129-
git fetch ${fetch_tag_args} --depth=<< parameters.fetch_depth >> --force origin "$CIRCLE_BRANCH:remotes/origin/$CIRCLE_BRANCH"
135+
git fetch ${fetch_tag_args} --depth << parameters.fetch_depth >> --force origin +refs/heads/${CIRCLE_BRANCH}:refs/remotes/origin/${CIRCLE_BRANCH}
130136
fi
131137
132138
# Check the commit ID of the checked out code
133139
if [ -n "$CIRCLE_TAG" ]; then
134140
echo 'Checking out tag'
135-
git reset --hard "$CIRCLE_SHA1"
136-
git checkout -q "$CIRCLE_TAG"
141+
git checkout --force "$CIRCLE_TAG"
142+
git reset --hard "$CIRCLE_SHA1" # move to triggered SHA
137143
elif [ -n "$CIRCLE_BRANCH" ] && [ "$CIRCLE_BRANCH" != 'HEAD' ]; then
138144
echo 'Checking out branch'
139-
git reset --hard "$CIRCLE_SHA1"
140-
git checkout -q -B "$CIRCLE_BRANCH"
145+
git checkout --force -B "$CIRCLE_BRANCH"
146+
git reset --hard "$CIRCLE_SHA1" # move to triggered SHA
147+
git --no-pager log --no-color -n 1 --format='HEAD is now at %h %s'
141148
fi
142-
143-
git reset --hard "$CIRCLE_SHA1"

src/commands/checkout_advanced.yml

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -100,30 +100,36 @@ steps:
100100
git config --global url."ssh://git@github.com".insteadOf "https://github.com" || true
101101
git config --global gc.auto 0 || true
102102
103-
# checkout
104-
git clone << parameters.clone_options >> $CIRCLE_REPOSITORY_URL "<< parameters.path >>"
105-
cd "<< parameters.path >>"
103+
# Checkout. SourceCaching? or not.
104+
if [ -e "$HOME/project/<< parameters.path >>/.git" ]; then
105+
echo 'Fetching into existing repository'
106+
existing_repo='true'
107+
cd "$HOME/project/<< parameters.path >>"
108+
git remote set-url origin "$CIRCLE_REPOSITORY_URL" || true
109+
else
110+
echo 'Cloning git repository'
111+
existing_repo='false'
112+
mkdir -p "$HOME/project/<< parameters.path >>"
113+
cd "$HOME/project/<< parameters.path >>"
114+
git clone << parameters.clone_options >> $CIRCLE_REPOSITORY_URL .
115+
fi
106116
107-
# Fetch remote and check the commit ID of the checked out code (sh version of bash `elif [[ "$CIRCLE_BRANCH" =~ ^pull\/* ]]`)
117+
# NOTE: Original checkout fetch only if SourceCaching, but we fetch always for depth selection.
118+
echo 'Fetching from remote repository'
108119
if [ -n "$CIRCLE_TAG" ]; then
109-
# tag
110-
git fetch << parameters.tag_fetch_options >> << parameters.fetch_options >> --force origin "+refs/tags/${CIRCLE_TAG}:refs/tags/${CIRCLE_TAG}"
111-
elif [[ $(echo $CIRCLE_BRANCH | grep -E ^pull\/[0-9]+$) ]]; then
112-
git fetch << parameters.fetch_options >> --force origin "${CIRCLE_BRANCH}/head:remotes/origin/${CIRCLE_BRANCH}"
120+
git fetch << parameters.tag_fetch_options >> << parameters.fetch_options >> --force --tags origin "+refs/tags/${CIRCLE_TAG}:refs/tags/${CIRCLE_TAG}"
113121
else
114-
# others
115-
git fetch << parameters.fetch_options >> --force origin "${CIRCLE_BRANCH}:remotes/origin/${CIRCLE_BRANCH}"
122+
git fetch << parameters.fetch_options >> --force origin +refs/heads/${CIRCLE_BRANCH}:refs/remotes/origin/${CIRCLE_BRANCH}
116123
fi
117124
118125
# Check the commit ID of the checked out code
119126
if [ -n "$CIRCLE_TAG" ]; then
120127
echo 'Checking out tag'
121-
git reset --hard "$CIRCLE_SHA1"
122-
git checkout -q "$CIRCLE_TAG"
128+
git checkout --force "$CIRCLE_TAG"
129+
git reset --hard "$CIRCLE_SHA1" # move to triggered SHA
123130
elif [ -n "$CIRCLE_BRANCH" ] && [ "$CIRCLE_BRANCH" != 'HEAD' ]; then
124131
echo 'Checking out branch'
125-
git reset --hard "$CIRCLE_SHA1"
126-
git checkout -q -B "$CIRCLE_BRANCH"
132+
git checkout --force -B "$CIRCLE_BRANCH"
133+
git reset --hard "$CIRCLE_SHA1" # move to triggered SHA
134+
git --no-pager log --no-color -n 1 --format='HEAD is now at %h %s'
127135
fi
128-
129-
git reset --hard "$CIRCLE_SHA1"

0 commit comments

Comments
 (0)