Skip to content

Guidance on, or automation of .gitignore directives #21

Description

@pzel

I've been running across the problem of keeping a package's "proper" code in lib/ alongside dependencies.
I'd like to check in only my own package code and not vendor deps, using smlpkg sync to pull them on-demand.

It took me quite a long time fighting with git's ignore logic, but I managed to figure out a way to autogenerate a sequence of gitignore directives that let me ignore everything in lib/ apart from the package-proper code.

This is the awk script I use:

  printf "lib/github.com/myname/mypackage | awk -F/ '
{ p=$1; for (i=2;i<NF+1;i++) {
  p=(p "/" $i);
  print "!"p"/";
  if (i==NF) print "!"p"/**"
  else  print p"/*"
}}'

which prints out this sequence of directives:

!lib/github.com/
lib/github.com/*
!lib/github.com/myname/
lib/github.com/myname/*
!lib/github.com/myname/mypackage/
!lib/github.com/myname/mypackage/**

This sequence, when preceded by lib/*, does what I want it to do. Now, even if I issue the command:

git add lib

it will only add the directory lib/github.com/myname/mypackage and all its contents, but no other children of lib/, at any level of the tree.

I figured this is probably a pattern folks are going to encounter. Would it make sense to add a generator to smlpkg itself to create this list? Perhaps something like:

$ cat sml.pkg
package github.com/myname/mypacakge

require {
  github.com/myname/otherpackage 1.2.3
  github.com/someorg/somepackage 3.2.1
}

$ smlpkg ignore-lib
lib/*
!lib/github.com/
lib/github.com/*
!lib/github.com/myname/
lib/github.com/myname/*
!lib/github.com/myname/mypackage/
!lib/github.com/myname/mypackage/**

$ smlpkg ignore-lib >> .gitignore

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions