【初心者向け】SwaggerとAWS SAMを使ってWebAPIを簡単に作ってみた

AWS SAMとSwaggerを使って、WebAPIを簡単に作ってみました。初心者向けの内容です。 Swaggerを使うことで、ドキュメント内容とWebAPI仕様の一致が期待できます(齟齬がない)。














サーバーレス開発部の藤井元貴です。インフルエンザに怯えてます。(予防接種はしてる)

API Gatewayでは、WebAPIの作成にSwaggerを使用できます。 Swaggerを使うことで、ドキュメント内容とWebAPI仕様の一致が期待できます(齟齬がない)。

私にとってSwaggerは初めて使うため、ひとまず下記を試してみました。

  • Swaggerを使ってWebAPIを作る
  • AWS SAMを使う

まずはシンプルに試すため、最低限の内容です。 例えば、開発環境と本番環境(devとprod)のように分けていません。

おすすめ

  • SwaggerでAPI Gatewayを定義したい人
  • AWS SAMを使いたい人
  • サーバーレスなWebAPIに興味がある人

環境

項目バージョン
macOSHigh Sierra 10.13.6
AWS CLIaws-cli/1.16.89 Python/3.6.1 Darwin/17.7.0 botocore/1.12.79
AWS SAM0.10.0
Docker for Mac18.09.1
Python3.6

全体概要

WebAPIを作成します。裏側にはLambdaを配置するサーバーレスな構成です。

API Gatewayの定義にSwaggerを使用し、デプロイにAWS SAMを使用します。

WebAPIの仕様

Swaggerを使う前に、まずはWebAPIの仕様を考えます。

PathMethod概要
/versionGETLambdaのPythonバージョンを取得する

応答パラメータの例は下記とします。

{
  "python": "3.6.2"
}

AWS SAMプロジェクトの準備

下記コマンドでプロジェクト一式を作成します。

sam init --runtime python3.6 --name SwaggerSample

Swagger Editor

導入

こちらを参考に導入します。Dockerは便利ですね。

API定義

Swagger Editorで下記を作成しました。この内容をswagger.yamlとして、AWS SAMプロジェクトフォルダに保存します。

swagger.yaml

swagger: "2.0"
info:
  description: "SwaggerとAPI Gatewayのサンプルです。"
  version: "1.0.0"
  title: "Swagger Sample"
basePath: "/Prod"
tags:
  - name: "Version"
schemes:
  - "https"
paths:
  /version:
    get:
      tags:
        - "Version"
      summary: "Pythonバージョン取得"
      description: "Lambdaで動いているPythonのバージョンを取得します。"
      consumes:
        - "application/json"
      produces:
        - "application/json"
      responses:
        200:
          description: "successful operation"
          schema:
            $ref: "#/definitions/Version"
      x-amazon-apigateway-integration:
        uri:
          Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${PythonVersionFunction.Arn}/invocations
        passthroughBehavior: when_no_templates
        httpMethod: POST
        type: aws_proxy
definitions:
  Version:
    type: "object"
    required:
      - "python"
    properties:
      python:
        type: "string"

API Gatewayで必要となるx-amazon-apigateway-integrationを記載しています。詳細はこちらへ。

なお、下記のようなドキュメントになります。参考まで。

AWS SAM

Lambda関数

Lambda関数のコードは下記です。Pythonバージョンの文字列を返却します。

app.py

import sys
import json


def lambda_handler(event, context):
    version = f'{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}'

    return {
        "statusCode": 200,
        "body": json.dumps(
            {"python": version},
        ),
    }

templateファイル

AWS SAMのtemplate.yamlは下記です。

template.yaml

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
    SwaggerSample

    Sample SAM Template for SwaggerSample

Globals:
    Function:
        Timeout: 3

Resources:
    PythonVersionApi:
        Type: AWS::Serverless::Api
        Properties:
            StageName: Prod
            DefinitionBody:
                Fn::Transform:
                    Name: AWS::Include
                    Parameters:
                        Location: s3://cm-fujii.genki-sam-test-bucket/swagger.yaml

    PythonVersionFunction:
        Type: AWS::Serverless::Function
        Properties:
            CodeUri: hello_world/
            Handler: app.lambda_handler
            Runtime: python3.6
            Events:
                HelloWorld:
                    Type: Api
                    Properties:
                        Path: /version
                        Method: get
                        RestApiId: !Ref PythonVersionApi

Outputs:
  PythonVersionApiUrl:
      Description: "API Gateway endpoint URL for Prod stage for Python Version Function"
      Value: !Sub "https://${PythonVersionApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/version"

Type: AWS::Serverless::ApiでAPIを明示的に定義し、S3に格納してあるyamlファイル(swagger.yaml)を指定します。

S3

コード等を格納するためのS3バケットを作成します。作成済みの場合は飛ばします。

aws s3 mb s3://cm-fujii.genki-sam-test-bucket

Swaggerファイルを格納

SwaggerファイルをS3バケットに格納します。

aws s3 cp swagger.yaml s3://cm-fujii.genki-sam-test-bucket/swagger.yaml

ビルド

下記コマンドでビルドします。

sam build

動作確認(ローカル)

まずはAPIを準備します。

sam local start-api

続いてcurlでAPIを叩きます。

$ curl http://localhost:3000/version
{"python": "3.6.1"}

OKですね!

package

続いてコード一式をS3バケットにアップロードします。

sam package \
    --output-template-file packaged.yaml \
    --s3-bucket cm-fujii.genki-sam-test-bucket

deploy

最後にデプロイします。

sam deploy \
    --template-file packaged.yaml \
    --stack-name SwaggerSample \
    --capabilities CAPABILITY_IAM

動作確認

作成したWebAPIのエンドポイントを確認します。

Web画面ポチポチでも良いですが、せっかくなのでコマンドを使います。

$ aws cloudformation describe-stacks --stack-name SwaggerSample --query 'Stacks[].Outputs'
[
    [
        {
            "OutputKey": "PythonVersionApiUrl",
            "OutputValue": "https://hogehoge.execute-api.ap-northeast-1.amazonaws.com/Prod/version",
            "Description": "API Gateway endpoint URL for Prod stage for Python Version Function"
        }
    ]
]

では、作成したWebAPIを叩いてみましょう!

$ curl https://hogehoge.execute-api.ap-northeast-1.amazonaws.com/Prod/version
{"python": "3.6.8"}

Python3系で最新の3.6.8が使用されていました。

ハマったこと

Swaggerの記載で下記にハマりました。

  • definitionsexampleを記載すると、sam deployが失敗する
  • WebAPIのMethodがGETでも、x-amazon-apigateway-integrationhttpMethodはPOSTを指定する

どちらもAWS(AWS SAM、API Gateway、Lambda)との連携部分です。辛かった……。

Swagger Editorですべて頑張らずに、AWSのWeb画面でAPI Gatewayをポチポチ作成したあと、Swagger形式でエクスポートするのも有効です。むしろこのほうが効率良さそう。

さいごに

SwaggerとAWSの組み合わせは、使い倒すほどハマる点が増えそうに感じましたが、Infrastructure as Codeは良いですね!! 楽しいです!


'DevOps > swagger' 카테고리의 다른 글

openapi의 yaml파일 작성법  (0) 2020.01.15

https://swagger.io/docs/specification/basic-structure/


Basic Structure

You can write OpenAPI definitions in YAML or JSON. In this guide, we use only YAML examples but JSON works equally well. A sample OpenAPI 3.0 definition written in YAML looks like:

  1. openapi: 3.0.0
  2. info:
  3. title: Sample API
  4. description: Optional multiline or single-line description in [CommonMark](http://commonmark.org/help/) or HTML.
  5. version: 0.1.9
  6. servers:
  7. - url: http://api.example.com/v1
  8. description: Optional server description, e.g. Main (production) server
  9. - url: http://staging-api.example.com
  10. description: Optional server description, e.g. Internal staging server for testing
  11. paths:
  12. /users:
  13. get:
  14. summary: Returns a list of users.
  15. description: Optional extended description in CommonMark or HTML.
  16. responses:
  17. '200': # status code
  18. description: A JSON array of user names
  19. content:
  20. application/json:
  21. schema:
  22. type: array
  23. items:
  24. type: string

All keyword names are case-sensitive.

Metadata

Every API definition must include the version of the OpenAPI Specification that this definition is based on:

  1. openapi: 3.0.0

The OpenAPI version defines the overall structure of an API definition – what you can document and how you document it. OpenAPI 3.0 uses semantic versioning with a three-part version number. The available versions are 3.0.03.0.1, and 3.0.2; they are functionally the same.

The info section contains API information: titledescription (optional), version:

  1. info:
  2. title: Sample API
  3. description: Optional multiline or single-line description in [CommonMark](http://commonmark.org/help/) or HTML.
  4. version: 0.1.9

title is your API name. description is extended information about your API. It can be multiline and supports the CommonMark dialect of Markdown for rich text representation. HTML is supported to the extent provided by CommonMark (see HTML Blocks in CommonMark 0.27 Specification). version is an arbitrary string that specifies the version of your API (do not confuse it with file revision or the openapi version). You can use semantic versioning like major.minor.patch, or an arbitrary string like 1.0-beta or 2017-07-25info also supports other keywords for contact information, license, terms of service, and other details.

Reference: Info Object.

Servers

The servers section specifies the API server and base URL. You can define one or several servers, such as production and sandbox.

  1. servers:
  2. - url: http://api.example.com/v1
  3. description: Optional server description, e.g. Main (production) server
  4. - url: http://staging-api.example.com
  5. description: Optional server description, e.g. Internal staging server for testing

All API paths are relative to the server URL. In the example above, /users means http://api.example.com/v1/users or http://staging-api.example.com/users, depending on the server used. For more information, see API Server and Base Path.

Paths

The paths section defines individual endpoints (paths) in your API, and the HTTP methods (operations) supported by these endpoints. For example, GET /users can be described as:

  1. paths:
  2. /users:
  3. get:
  4. summary: Returns a list of users.
  5. description: Optional extended description in CommonMark or HTML
  6. responses:
  7. '200':
  8. description: A JSON array of user names
  9. content:
  10. application/json:
  11. schema:
  12. type: array
  13. items:
  14. type: string

An operation definition includes parameters, request body (if any), possible response status codes (such as 200 OK or 404 Not Found) and response contents. For more information, see Paths and Operations.

Parameters

Operations can have parameters passed via URL path (/users/{userId}), query string (/users?role=admin), headers (X-CustomHeader: Value) or cookies (Cookie: debug=0). You can define the parameter data types, format, whether they are required or optional, and other details:

  1. paths:
  2. /user/{userId}:
  3. get:
  4. summary: Returns a user by ID.
  5. parameters:
  6. - name: userId
  7. in: path
  8. required: true
  9. description: Parameter description in CommonMark or HTML.
  10. schema:
  11. type : integer
  12. format: int64
  13. minimum: 1
  14. responses:
  15. '200':
  16. description: OK

For more information, see Describing Parameters.

Request Body

If an operation sends a request body, use the requestBody keyword to describe the body content and media type.

  1. paths:
  2. /users:
  3. post:
  4. summary: Creates a user.
  5. requestBody:
  6. required: true
  7. content:
  8. application/json:
  9. schema:
  10. type: object
  11. properties:
  12. username:
  13. type: string
  14. responses:
  15. '201':
  16. description: Created

For more information, see Describing Request Body.

Responses

For each operation, you can define possible status codes, such as 200 OK or 404 Not Found, and the response body schema. Schemas can be defined inline or referenced via $ref. You can also provide example responses for different content types:

  1. paths:
  2. /user/{userId}:
  3. get:
  4. summary: Returns a user by ID.
  5. parameters:
  6. - name: userId
  7. in: path
  8. required: true
  9. description: The ID of the user to return.
  10. schema:
  11. type: integer
  12. format: int64
  13. minimum: 1
  14. responses:
  15. '200':
  16. description: A user object.
  17. content:
  18. application/json:
  19. schema:
  20. type: object
  21. properties:
  22. id:
  23. type: integer
  24. format: int64
  25. example: 4
  26. name:
  27. type: string
  28. example: Jessica Smith
  29. '400':
  30. description: The specified user ID is invalid (not a number).
  31. '404':
  32. description: A user with the specified ID was not found.
  33. default:
  34. description: Unexpected error

Note that the response HTTP status codes must be enclosed in quotes: "200" (OpenAPI 2.0 did not require this). For more information, see Describing Responses.

Input and Output Models

The global components/schemas section lets you define common data structures used in your API. They can be referenced via $ref whenever a schema is required – in parameters, request bodies, and response bodies. For example, this JSON object:

  1. {
  2. "id": 4,
  3. "name": "Arthur Dent"
  4. }

can be represented as:

  1. components:
  2. schemas:
  3. User:
  4. properties:
  5. id:
  6. type: integer
  7. name:
  8. type: string
  9. # Both properties are required
  10. required:
  11. - id
  12. - name

and then referenced in the request body schema and response body schema as follows:

  1. paths:
  2. /users/{userId}:
  3. get:
  4. summary: Returns a user by ID.
  5. parameters:
  6. - in: path
  7. name: userId
  8. required: true
  9. type: integer
  10. responses:
  11. '200':
  12. description: OK
  13. content:
  14. application/json:
  15. schema:
  16. $ref: '#/components/schemas/User'
  17. /users:
  18. post:
  19. summary: Creates a new user.
  20. requestBody:
  21. required: true
  22. content:
  23. application/json:
  24. schema:
  25. $ref: '#/components/schemas/User'
  26. responses:
  27. '201':
  28. description: Created

Authentication

The securitySchemes and security keywords are used to describe the authentication methods used in your API.

  1. components:
  2. securitySchemes:
  3. BasicAuth:
  4. type: http
  5. scheme: basic
  6. security:
  7. - BasicAuth: []

Supported authentication methods are:

For more information, see Authentication.

Full Specification

The full OpenAPI 3.0 Specification is available on GitHub: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md

http://schacon.github.io/git/git-diff.html

 

git-diff(1) Manual Page

NAME

git-diff - Show changes between commits, commit and working tree, etc

SYNOPSIS

git diff [options] [<commit>] [--] [<path>…] git diff [options] --cached [<commit>] [--] [<path>…] git diff [options] <commit> <commit> [--] [<path>…] git diff [options] [--no-index] [--] <path> <path>

DESCRIPTION

Show changes between the working tree and the index or a tree, changes between the index and a tree, changes between two trees, or changes between two files on disk.

git diff [--options] [--] [<path>…]

This form is to view the changes you made relative to the index (staging area for the next commit). In other words, the differences are what you could tell git to further add to the index but you still haven’t. You can stage these changes by using git-add(1).

If exactly two paths are given and at least one points outside the current repository, git diff will compare the two files / directories. This behavior can be forced by --no-index.

git diff [--options] --cached [<commit>] [--] [<path>…]

This form is to view the changes you staged for the next commit relative to the named <commit>. Typically you would want comparison with the latest commit, so if you do not give <commit>, it defaults to HEAD. If HEAD does not exist (e.g. unborned branches) and <commit> is not given, it shows all staged changes. --staged is a synonym of --cached.

git diff [--options] <commit> [--] [<path>…]

This form is to view the changes you have in your working tree relative to the named <commit>. You can use HEAD to compare it with the latest commit, or a branch name to compare with the tip of a different branch.

git diff [--options] <commit> <commit> [--] [<path>…]

This is to view the changes between two arbitrary <commit>.

git diff [--options] <commit>..<commit> [--] [<path>…]

This is synonymous to the previous form. If <commit> on one side is omitted, it will have the same effect as using HEAD instead.

git diff [--options] <commit>...<commit> [--] [<path>…]

This form is to view the changes on the branch containing and up to the second <commit>, starting at a common ancestor of both <commit>. "git diff A...B" is equivalent to "git diff $(git-merge-base A B) B". You can omit any one of <commit>, which has the same effect as using HEAD instead.

Just in case if you are doing something exotic, it should be noted that all of the <commit> in the above description, except in the last two forms that use ".." notations, can be any <tree>. The third form (git diff <commit> <commit>) can also be used to compare two <blob> objects.

For a more complete list of ways to spell <commit>, see "SPECIFYING REVISIONS" section in gitrevisions(7). However, "diff" is about comparing two endpoints, not ranges, and the range notations ("<commit>..<commit>" and "<commit>...<commit>") do not mean a range as defined in the "SPECIFYING RANGES" section in gitrevisions(7).

OPTIONS

-p-u--patch

Generate patch (see section on generating patches). This is the default.

-U<n>--unified=<n>

Generate diffs with <n> lines of context instead of the usual three. Implies -p.

--raw

Generate the raw format.

--patch-with-raw

Synonym for -p --raw.

--patience

Generate a diff using the "patience diff" algorithm.

--stat[=<width>[,<name-width>[,<count>]]]

Generate a diffstat. You can override the default output width for 80-column terminal by --stat=<width>. The width of the filename part can be controlled by giving another width to it separated by a comma. By giving a third parameter <count>, you can limit the output to the first <count> lines, followed by  if there are more.

These parameters can also be set individually with --stat-width=<width>, --stat-name-width=<name-width> and --stat-count=<count>.

--numstat

Similar to --stat, but shows number of added and deleted lines in decimal notation and pathname without abbreviation, to make it more machine friendly. For binary files, outputs two - instead of saying 0 0.

--shortstat

Output only the last line of the --stat format containing total number of modified files, as well as number of added and deleted lines.

--dirstat[=<param1,param2,…>]

Output the distribution of relative amount of changes for each sub-directory. The behavior of --dirstat can be customized by passing it a comma separated list of parameters. The defaults are controlled by the diff.dirstat configuration variable (see git-config(1)). The following parameters are available:

changes

Compute the dirstat numbers by counting the lines that have been removed from the source, or added to the destination. This ignores the amount of pure code movements within a file. In other words, rearranging lines in a file is not counted as much as other changes. This is the default behavior when no parameter is given.

lines

Compute the dirstat numbers by doing the regular line-based diff analysis, and summing the removed/added line counts. (For binary files, count 64-byte chunks instead, since binary files have no natural concept of lines). This is a more expensive --dirstat behavior than the changes behavior, but it does count rearranged lines within a file as much as other changes. The resulting output is consistent with what you get from the other --*stat options.

files

Compute the dirstat numbers by counting the number of files changed. Each changed file counts equally in the dirstat analysis. This is the computationally cheapest --dirstat behavior, since it does not have to look at the file contents at all.

cumulative

Count changes in a child directory for the parent directory as well. Note that when using cumulative, the sum of the percentages reported may exceed 100%. The default (non-cumulative) behavior can be specified with the noncumulative parameter.

<limit>

An integer parameter specifies a cut-off percent (3% by default). Directories contributing less than this percentage of the changes are not shown in the output.

Example: The following will count changed files, while ignoring directories with less than 10% of the total amount of changed files, and accumulating child directory counts in the parent directories: --dirstat=files,10,cumulative.

--summary

Output a condensed summary of extended header information such as creations, renames and mode changes.

--patch-with-stat

Synonym for -p --stat.

-z

When --raw, --numstat, --name-only or --name-status has been given, do not munge pathnames and use NULs as output field terminators.

Without this option, each pathname output will have TAB, LF, double quotes, and backslash characters replaced with \t, \n, \", and \\, respectively, and the pathname will be enclosed in double quotes if any of those replacements occurred.

--name-only

Show only names of changed files.

--name-status

Show only names and status of changed files. See the description of the --diff-filter option on what the status letters mean.

--submodule[=<format>]

Chose the output format for submodule differences. <format> can be one of short and log. short just shows pairs of commit names, this format is used when this option is not given. log is the default value for this option and lists the commits in that commit range like the summary option of git-submodule(1) does.

--color[=<when>]

Show colored diff. The value must be always (the default for <when>), never, or auto. The default value is never. It can be changed by the color.ui and color.diff configuration settings.

--no-color

Turn off colored diff. This can be used to override configuration settings. It is the same as --color=never.

--word-diff[=<mode>]

Show a word diff, using the <mode> to delimit changed words. By default, words are delimited by whitespace; see --word-diff-regex below. The <mode> defaults to plain, and must be one of:

color

Highlight changed words using only colors. Implies --color.

plain

Show words as [-removed-] and {added}. Makes no attempts to escape the delimiters if they appear in the input, so the output may be ambiguous.

porcelain

Use a special line-based format intended for script consumption. Added/removed/unchanged runs are printed in the usual unified diff format, starting with a +/-/` ` character at the beginning of the line and extending to the end of the line. Newlines in the input are represented by a tilde ~ on a line of its own.

none

Disable word diff again.

Note that despite the name of the first mode, color is used to highlight the changed parts in all modes if enabled.

--word-diff-regex=<regex>

Use <regex> to decide what a word is, instead of considering runs of non-whitespace to be a word. Also implies --word-diff unless it was already enabled.

Every non-overlapping match of the <regex> is considered a word. Anything between these matches is considered whitespace and ignored(!) for the purposes of finding differences. You may want to append |[^[:space:]] to your regular expression to make sure that it matches all non-whitespace characters. A match that contains a newline is silently truncated(!) at the newline.

The regex can also be set via a diff driver or configuration option, see gitattributes(1) or git-config(1). Giving it explicitly overrides any diff driver or configuration setting. Diff drivers override configuration settings.

--color-words[=<regex>]

Equivalent to --word-diff=color plus (if a regex was specified) --word-diff-regex=<regex>.

--no-renames

Turn off rename detection, even when the configuration file gives the default to do so.

--check

Warn if changes introduce whitespace errors. What are considered whitespace errors is controlled by core.whitespace configuration. By default, trailing whitespaces (including lines that solely consist of whitespaces) and a space character that is immediately followed by a tab character inside the initial indent of the line are considered whitespace errors. Exits with non-zero status if problems are found. Not compatible with --exit-code.

--full-index

Instead of the first handful of characters, show the full pre- and post-image blob object names on the "index" line when generating patch format output.

--binary

In addition to --full-index, output a binary diff that can be applied with git-apply.

--abbrev[=<n>]

Instead of showing the full 40-byte hexadecimal object name in diff-raw format output and diff-tree header lines, show only a partial prefix. This is independent of the --full-index option above, which controls the diff-patch output format. Non default number of digits can be specified with --abbrev=<n>.

-B[<n>][/<m>]--break-rewrites[=[<n>][/<m>]]

Break complete rewrite changes into pairs of delete and create. This serves two purposes:

It affects the way a change that amounts to a total rewrite of a file not as a series of deletion and insertion mixed together with a very few lines that happen to match textually as the context, but as a single deletion of everything old followed by a single insertion of everything new, and the number m controls this aspect of the -B option (defaults to 60%). -B/70% specifies that less than 30% of the original should remain in the result for git to consider it a total rewrite (i.e. otherwise the resulting patch will be a series of deletion and insertion mixed together with context lines).

When used with -M, a totally-rewritten file is also considered as the source of a rename (usually -M only considers a file that disappeared as the source of a rename), and the number n controls this aspect of the -B option (defaults to 50%). -B20% specifies that a change with addition and deletion compared to 20% or more of the file’s size are eligible for being picked up as a possible source of a rename to another file.

-M[<n>]--find-renames[=<n>]

Detect renames. If n is specified, it is a threshold on the similarity index (i.e. amount of addition/deletions compared to the file’s size). For example, -M90% means git should consider a delete/add pair to be a rename if more than 90% of the file hasn’t changed.

-C[<n>]--find-copies[=<n>]

Detect copies as well as renames. See also --find-copies-harder. If n is specified, it has the same meaning as for -M<n>.

--find-copies-harder

For performance reasons, by default, -C option finds copies only if the original file of the copy was modified in the same changeset. This flag makes the command inspect unmodified files as candidates for the source of copy. This is a very expensive operation for large projects, so use it with caution. Giving more than one -C option has the same effect.

-D--irreversible-delete

Omit the preimage for deletes, i.e. print only the header but not the diff between the preimage and /dev/null. The resulting patch is not meant to be applied with patch nor git apply; this is solely for people who want to just concentrate on reviewing the text after the change. In addition, the output obviously lack enough information to apply such a patch in reverse, even manually, hence the name of the option.

When used together with -B, omit also the preimage in the deletion part of a delete/create pair.

-l<num>

The -M and -C options require O(n^2) processing time where n is the number of potential rename/copy targets. This option prevents rename/copy detection from running if the number of rename/copy targets exceeds the specified number.

--diff-filter=[(A|C|D|M|R|T|U|X|B)…[*]]

Select only files that are Added (A), Copied (C), Deleted (D), Modified (M), Renamed (R), have their type (i.e. regular file, symlink, submodule, …) changed (T), are Unmerged (U), are Unknown (X), or have had their pairing Broken (B). Any combination of the filter characters (including none) can be used. When * (All-or-none) is added to the combination, all paths are selected if there is any file that matches other criteria in the comparison; if there is no file that matches other criteria, nothing is selected.

-S<string>

Look for differences that introduce or remove an instance of <string>. Note that this is different than the string simply appearing in diff output; see the pickaxe entry in gitdiffcore(7) for more details.

-G<regex>

Look for differences whose added or removed line matches the given <regex>.

--pickaxe-all

When -S or -G finds a change, show all the changes in that changeset, not just the files that contain the change in <string>.

--pickaxe-regex

Make the <string> not a plain string but an extended POSIX regex to match.

-O<orderfile>

Output the patch in the order specified in the <orderfile>, which has one shell glob pattern per line.

-R

Swap two inputs; that is, show differences from index or on-disk file to tree contents.

--relative[=<path>]

When run from a subdirectory of the project, it can be told to exclude changes outside the directory and show pathnames relative to it with this option. When you are not in a subdirectory (e.g. in a bare repository), you can name which subdirectory to make the output relative to by giving a <path> as an argument.

-a--text

Treat all files as text.

--ignore-space-at-eol

Ignore changes in whitespace at EOL.

-b--ignore-space-change

Ignore changes in amount of whitespace. This ignores whitespace at line end, and considers all other sequences of one or more whitespace characters to be equivalent.

-w--ignore-all-space

Ignore whitespace when comparing lines. This ignores differences even if one line has whitespace where the other line has none.

--inter-hunk-context=<lines>

Show the context between diff hunks, up to the specified number of lines, thereby fusing hunks that are close to each other.

--exit-code

Make the program exit with codes similar to diff(1). That is, it exits with 1 if there were differences and 0 means no differences.

--quiet

Disable all output of the program. Implies --exit-code.

--ext-diff

Allow an external diff helper to be executed. If you set an external diff driver with gitattributes(5), you need to use this option with git-log(1) and friends.

--no-ext-diff

Disallow external diff drivers.

--textconv--no-textconv

Allow (or disallow) external text conversion filters to be run when comparing binary files. See gitattributes(5) for details. Because textconv filters are typically a one-way conversion, the resulting diff is suitable for human consumption, but cannot be applied. For this reason, textconv filters are enabled by default only for git-diff(1) and git-log(1), but not for git-format-patch(1) or diff plumbing commands.

--ignore-submodules[=<when>]

Ignore changes to submodules in the diff generation. <when> can be either "none", "untracked", "dirty" or "all", which is the default Using "none" will consider the submodule modified when it either contains untracked or modified files or its HEAD differs from the commit recorded in the superproject and can be used to override any settings of the ignore option in git-config(1) or gitmodules(5). When "untracked" is used submodules are not considered dirty when they only contain untracked content (but they are still scanned for modified content). Using "dirty" ignores all changes to the work tree of submodules, only changes to the commits stored in the superproject are shown (this was the behavior until 1.7.0). Using "all" hides all changes to submodules.

--src-prefix=<prefix>

Show the given source prefix instead of "a/".

--dst-prefix=<prefix>

Show the given destination prefix instead of "b/".

--no-prefix

Do not show any source or destination prefix.

For more detailed explanation on these common options, see also gitdiffcore(7).

<path>…

The <paths> parameters, when given, are used to limit the diff to the named paths (you can give directory names and get diff for all files under them).

Raw output format

The raw output format from "git-diff-index", "git-diff-tree", "git-diff-files" and "git diff --raw" are very similar.

These commands all compare two sets of things; what is compared differs:

git-diff-index <tree-ish>

compares the <tree-ish> and the files on the filesystem.

git-diff-index --cached <tree-ish>

compares the <tree-ish> and the index.

git-diff-tree [-r] <tree-ish-1> <tree-ish-2> [<pattern>…]

compares the trees named by the two arguments.

git-diff-files [<pattern>…]

compares the index and the files on the filesystem.

The "git-diff-tree" command begins its output by printing the hash of what is being compared. After that, all the commands print one output line per changed file.

An output line is formatted this way:

in-place edit :100644 100644 bcd1234... 0123456... M file0 copy-edit :100644 100644 abcd123... 1234567... C68 file1 file2 rename-edit :100644 100644 abcd123... 1234567... R86 file1 file3 create :000000 100644 0000000... 1234567... A file4 delete :100644 000000 1234567... 0000000... D file5 unmerged :000000 000000 0000000... 0000000... U file6

That is, from the left to the right:

  1. a colon.

  2. mode for "src"; 000000 if creation or unmerged.

  3. a space.

  4. mode for "dst"; 000000 if deletion or unmerged.

  5. a space.

  6. sha1 for "src"; 0{40} if creation or unmerged.

  7. a space.

  8. sha1 for "dst"; 0{40} if creation, unmerged or "look at work tree".

  9. a space.

  10. status, followed by optional "score" number.

  11. a tab or a NUL when -z option is used.

  12. path for "src"

  13. a tab or a NUL when -z option is used; only exists for C or R.

  14. path for "dst"; only exists for C or R.

  15. an LF or a NUL when -z option is used, to terminate the record.

Possible status letters are:

  • A: addition of a file

  • C: copy of a file into a new one

  • D: deletion of a file

  • M: modification of the contents or mode of a file

  • R: renaming of a file

  • T: change in the type of the file

  • U: file is unmerged (you must complete the merge before it can be committed)

  • X: "unknown" change type (most probably a bug, please report it)

Status letters C and R are always followed by a score (denoting the percentage of similarity between the source and target of the move or copy), and are the only ones to be so.

<sha1> is shown as all 0’s if a file is new on the filesystem and it is out of sync with the index.

Example:

:100644 100644 5be4a4...... 000000...... M file.c

When -z option is not used, TAB, LF, and backslash characters in pathnames are represented as \t, \n, and \\, respectively.

diff format for merges

"git-diff-tree", "git-diff-files" and "git-diff --raw" can take -c or --cc option to generate diff output also for merge commits. The output differs from the format described above in the following way:

  1. there is a colon for each parent

  2. there are more "src" modes and "src" sha1

  3. status is concatenated status characters for each parent

  4. no optional "score" number

  5. single path, only for "dst"

Example:

::100644 100644 100644 fabadb8... cc95eb0... 4866510... MM describe.c

Note that combined diff lists only files which were modified from all parents.

Generating patches with -p

When "git-diff-index", "git-diff-tree", or "git-diff-files" are run with a -p option, "git diff" without the --raw option, or "git log" with the "-p" option, they do not produce the output described above; instead they produce a patch file. You can customize the creation of such patches via the GIT_EXTERNAL_DIFF and the GIT_DIFF_OPTS environment variables.

What the -p option produces is slightly different from the traditional diff format:

  1. It is preceded with a "git diff" header that looks like this:

    diff --git a/file1 b/file2

    The a/ and b/ filenames are the same unless rename/copy is involved. Especially, even for a creation or a deletion, /dev/null is not used in place of the a/ or b/ filenames.

    When rename/copy is involved, file1 and file2 show the name of the source file of the rename/copy and the name of the file that rename/copy produces, respectively.

  2. It is followed by one or more extended header lines:

    old mode <mode> new mode <mode> deleted file mode <mode> new file mode <mode> copy from <path> copy to <path> rename from <path> rename to <path> similarity index <number> dissimilarity index <number> index <hash>..<hash> <mode>

    File modes are printed as 6-digit octal numbers including the file type and file permission bits.

    Path names in extended headers do not include the a/ and b/ prefixes.

    The similarity index is the percentage of unchanged lines, and the dissimilarity index is the percentage of changed lines. It is a rounded down integer, followed by a percent sign. The similarity index value of 100% is thus reserved for two equal files, while 100% dissimilarity means that no line from the old file made it into the new one.

    The index line includes the SHA-1 checksum before and after the change. The <mode> is included if the file mode does not change; otherwise, separate lines indicate the old and the new mode.

  3. TAB, LF, double quote and backslash characters in pathnames are represented as \t, \n, \" and \\, respectively. If there is need for such substitution then the whole pathname is put in double quotes.

  4. All the file1 files in the output refer to files before the commit, and all the file2 files refer to files after the commit. It is incorrect to apply each change to each file sequentially. For example, this patch will swap a and b:

    diff --git a/a b/b rename from a rename to b diff --git a/b b/a rename from b rename to a

combined diff format

Any diff-generating command can take the ‘-c` or --cc option to produce a combined diff when showing a merge. This is the default format when showing merges with git-diff(1) or git-show(1). Note also that you can give the `-m’ option to any of these commands to force generation of diffs with individual parents of a merge.

A combined diff format looks like this:

diff --combined describe.c index fabadb8,cc95eb0..4866510 --- a/describe.c +++ b/describe.c @@@ -98,20 -98,12 +98,20 @@@ return (a_date > b_date) ? -1 : (a_date == b_date) ? 0 : 1; } - static void describe(char *arg) -static void describe(struct commit *cmit, int last_one) ++static void describe(char *arg, int last_one) { + unsigned char sha1[20]; + struct commit *cmit; struct commit_list *list; static int initialized = 0; struct commit_name *n; + if (get_sha1(arg, sha1) < 0) + usage(describe_usage); + cmit = lookup_commit_reference(sha1); + if (!cmit) + usage(describe_usage); + if (!initialized) { initialized = 1; for_each_ref(get_name);

  1. It is preceded with a "git diff" header, that looks like this (when -c option is used):

    diff --combined file

    or like this (when --cc option is used):

    diff --cc file

  2. It is followed by one or more extended header lines (this example shows a merge with two parents):

    index <hash>,<hash>..<hash> mode <mode>,<mode>..<mode> new file mode <mode> deleted file mode <mode>,<mode>

    The mode <mode>,<mode>..<mode> line appears only if at least one of the <mode> is different from the rest. Extended headers with information about detected contents movement (renames and copying detection) are designed to work with diff of two <tree-ish> and are not used by combined diff format.

  3. It is followed by two-line from-file/to-file header

    --- a/file +++ b/file

    Similar to two-line header for traditional unified diff format, /dev/null is used to signal created or deleted files.

  4. Chunk header format is modified to prevent people from accidentally feeding it to patch -p1. Combined diff format was created for review of merge commit changes, and was not meant for apply. The change is similar to the change in the extended index header:

    @@@ <from-file-range> <from-file-range> <to-file-range> @@@

    There are (number of parents + 1) @ characters in the chunk header for combined diff format.

Unlike the traditional unified diff format, which shows two files A and B with a single column that has - (minus — appears in A but removed in B), + (plus — missing in A but added to B), or " " (space — unchanged) prefix, this format compares two or more files file1, file2,… with one file X, and shows how X differs from each of fileN. One column for each of fileN is prepended to the output line to note how X’s line is different from it.

A - character in the column N means that the line appears in fileN but it does not appear in the result. A + character in the column N means that the line appears in the result, and fileN does not have that line (in other words, the line was added, from the point of view of that parent).

In the above example output, the function signature was changed from both files (hence two - removals from both file1 and file2, plus ++ to mean one line that was added does not appear in either file1 nor file2). Also eight other lines are the same from file1 but do not appear in file2 (hence prefixed with +).

When shown by git diff-tree -c, it compares the parents of a merge commit with the merge result (i.e. file1..fileN are the parents). When shown by git diff-files -c, it compares the two unresolved merge parents with the working tree file (i.e. file1 is stage 2 aka "our version", file2 is stage 3 aka "their version").

other diff formats

The --summary option describes newly added, deleted, renamed and copied files. The --stat option adds diffstat(1) graph to the output. These options can be combined with other options, such as -p, and are meant for human consumption.

When showing a change that involves a rename or a copy, --stat output formats the pathnames compactly by combining common prefix and suffix of the pathnames. For example, a change that moves arch/i386/Makefile to arch/x86/Makefile while modifying 4 lines will be shown like this:

arch/{i386 => x86}/Makefile | 4 +--

The --numstat option gives the diffstat(1) information but is designed for easier machine consumption. An entry in --numstat output looks like this:

1 2 README 3 1 arch/{i386 => x86}/Makefile

That is, from left to right:

  1. the number of added lines;

  2. a tab;

  3. the number of deleted lines;

  4. a tab;

  5. pathname (possibly with rename/copy information);

  6. a newline.

When -z output option is in effect, the output is formatted this way:

1 2 README NUL 3 1 NUL arch/i386/Makefile NUL arch/x86/Makefile NUL

That is:

  1. the number of added lines;

  2. a tab;

  3. the number of deleted lines;

  4. a tab;

  5. a NUL (only exists if renamed/copied);

  6. pathname in preimage;

  7. a NUL (only exists if renamed/copied);

  8. pathname in postimage (only exists if renamed/copied);

  9. a NUL.

The extra NUL before the preimage path in renamed case is to allow scripts that read the output to tell if the current record being read is a single-path record or a rename/copy record without reading ahead. After reading added and deleted lines, reading up to NUL would yield the pathname, but if that is NUL, the record will show two paths.

EXAMPLES

Various ways to check your working tree

$ git diff <1> $ git diff --cached <2> $ git diff HEAD <3>

  1. Changes in the working tree not yet staged for the next commit.

  2. Changes between the index and your last commit; what you would be committing if you run "git commit" without "-a" option.

  3. Changes in the working tree since your last commit; what you would be committing if you run "git commit -a"

Comparing with arbitrary commits

$ git diff test <1> $ git diff HEAD -- ./test <2> $ git diff HEAD^ HEAD <3>

  1. Instead of using the tip of the current branch, compare with the tip of "test" branch.

  2. Instead of comparing with the tip of "test" branch, compare with the tip of the current branch, but limit the comparison to the file "test".

  3. Compare the version before the last commit and the last commit.

Comparing branches

$ git diff topic master <1> $ git diff topic..master <2> $ git diff topic...master <3>

  1. Changes between the tips of the topic and the master branches.

  2. Same as above.

  3. Changes that occurred on the master branch since when the topic branch was started off it.

Limiting the diff output

$ git diff --diff-filter=MRC <1> $ git diff --name-status <2> $ git diff arch/i386 include/asm-i386 <3>

  1. Show only modification, rename and copy, but not addition nor deletion.

  2. Show only names and the nature of change, but not actual diff output.

  3. Limit diff output to named subtrees.

Munging the diff output

$ git diff --find-copies-harder -B -C <1> $ git diff -R <2>

  1. Spend extra cycles to find renames, copies and complete rewrites (very expensive).

  2. Output diff in reverse.

SEE ALSO

diff(1), git-difftool(1), git-log(1), gitdiffcore(7), git-format-patch(1), git-apply(1)

GIT

Part of the git(1) suite

Last updated 2011-03-15 23:30:13 UTC

'DevOps > Git' 카테고리의 다른 글

git commit 규약  (0) 2019.09.26
.gitkeep 파일로 empty folder를 git하기  (0) 2019.09.18
gitignore bestpractice  (0) 2019.09.05
git stash커맨드에 대해 알아보자  (0) 2019.08.23
git reflog  (0) 2019.07.24

- ブランチの命名規約

機能追加:

 feature/JIRAチケットID-機能の名称 or 開発内容名

バグ修正:

 fix/修正対象(内容)

クリティカルなバグ修正:

 hotfix/修正対象(内容)

リファクタリング等:

 clean/改修内容

機能削除:

  remove/機能の名称

 

■コミットコメントのフォーマット

1行目:[コミット種別] 概要 JIRAのチケットID

2行目 :空行

3行目以降:詳細(「なぜ」「何を」を記述)

 

3行目以降の内容はJIRAと連携される

 

■コミット種別

fix:バグ修正

hotfix:クリティカルなバグ修正

add:新規(ファイル)機能追加

update:機能修正(バグではない)

change:仕様変更

clean:整理(リファクタリング等)

disable:無効化(コメントアウト等)

remove:削除(ファイル)

upgrade:バージョンアップ

revert:変更取り消し

 

例)

[add] アップロード結果テーブルのページ切り替え機能の追加 #AIPOWAZZ-82

'DevOps > Git' 카테고리의 다른 글

git-diff(1) Manual Page  (0) 2020.01.10
.gitkeep 파일로 empty folder를 git하기  (0) 2019.09.18
gitignore bestpractice  (0) 2019.09.05
git stash커맨드에 대해 알아보자  (0) 2019.08.23
git reflog  (0) 2019.07.24

https://davidwalsh.name/git-empty-directory
https://medium.com/@kinduff/hey-git-please-keep-those-folders-eb0ed37621c8

There are times when you'd like to track an empty directory within git but there's a problem: git wont allow you to add a directory that doesn't have a file in it.  The easy solution is putting an empty stub file within the directory, and the industry standard for that stub file name is .gitkeep.

You can quickly create the file and commit the "empty" directory from command line:

# ignore files in folder foo
foo/*
# but keep the folder by keeping .gitkeep file
!foo/.gitkeep


touch foo/.gitkeep #add .gitkeep file

git add . git commit -m "Adding my empty directory"

The problem is simple, the solution is easy, but I wanted to highlight that .gitkeep is the industry standard.

'DevOps > Git' 카테고리의 다른 글

git-diff(1) Manual Page  (0) 2020.01.10
git commit 규약  (0) 2019.09.26
gitignore bestpractice  (0) 2019.09.05
git stash커맨드에 대해 알아보자  (0) 2019.08.23
git reflog  (0) 2019.07.24

.gitignore bestpractice

source

be cautious

  • somefolder/* : somefolder안의 모든 파일을 깃 대상에서 제외
  • !somefolder/.gitkeep : 단 somefolder는 유지
    # exclude everything
    somefolder/*
    

exception to the rule

!somefolder/.gitkeep


#### example

Byte-compiled / optimized / DLL files

pycache/ *.py[cod]

C extensions

*.so

Distribution / packaging

bin/
build/
develop-eggs/
dist/
eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg

Installer logs

pip-log.txt
pip-delete-this-directory.txt

Unit test / coverage reports

.tox/
.coverage
.cache
nosetests.xml
coverage.xml

Translations

*.mo

Mr Developer

.mr.developer.cfg
.project
.pydevproject

Rope

.ropeproject

Django stuff:

*.log
*.pot

Sphinx documentation

docs/_build/
```

'DevOps > Git' 카테고리의 다른 글

git commit 규약  (0) 2019.09.26
.gitkeep 파일로 empty folder를 git하기  (0) 2019.09.18
git stash커맨드에 대해 알아보자  (0) 2019.08.23
git reflog  (0) 2019.07.24
Rebase하기  (0) 2019.04.03

https://qiita.com/akasakas/items/768c0b563b96f8a9be9d


色々な git stash

この記事は最終更新日から1年以上が経過しています。

git stash コマンドを個人的によく使っていますが、オプションなどを忘れてしまいがちなので、備忘録として残しておきます。

:black_square_button: スタッシュする

git stash 
git stash save

:black_square_button: メッセージをつけてスタッシュする

git stash save "message"

:black_square_button: スタッシュしたリストを表示

git stash list 

:black_square_button: N番目にスタッシュしたファイルの一覧を表示

git stash show stash@{N}

:black_square_button: N番目にスタッシュしたファイルの変更差分を表示

git stash show -p stash@{N}

:black_square_button: スタッシュを適用し、適用したスタッシュを削除する

git stash pop # 最新のスタッシュを適用し、削除 
git stash pop stash@{N} # N番目のスタッシュを適用し、削除

:black_square_button: スタッシュは適用し、適用したスタッシュを残す

git stash apply # 最新のスタッシュを適用し、残す
git stash apply stash@{N} # N番目のスタッシュを適用し、残す

:black_square_button: N番目のスタッシュを削除する

git stash drop # 最新のスタッシュを削除
git stash drop stash@{N} # N番目のスタッシュを削除

:black_square_button: unstage ファイルを全てスタッシュ

git stash -k

:black_square_button: untrackファイルも含めて全てスタッシュ

git stash -u

:black_square_button: スタッシュを全削除する

git stash clear


'DevOps > Git' 카테고리의 다른 글

.gitkeep 파일로 empty folder를 git하기  (0) 2019.09.18
gitignore bestpractice  (0) 2019.09.05
git reflog  (0) 2019.07.24
Rebase하기  (0) 2019.04.03
jenkins와 gitlab연동(github아니고 gitlab임!)  (0) 2018.10.31

1. git reflog

  • git rebase 또는 git reset 등으로 커밋이 삭제될 수 있다.
  • 하지만, git 이력은 보관되고 있는데 이러한 이력을 볼 수 있는 명령어가 git reflog

12-1

2. commit 복구하기

  1. git reflog 명령어로 삭제된 commit id 확인 후
  2. git reset --hard <커밋해시id>

3. branch 복구하기

  1. git reflog 또는 git reflog |grep 브랜치명 으로 log확인
  2. git checkout -b <삭제한 브랜치명> <커밋해시id>


+ Recent posts